Skip to content

Commit da9c726

Browse files
committed
feat: introduce BaseContext, pass shutdown context to f()
1 parent 8577d1e commit da9c726

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

app/app.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ func Run(f func(ctx context.Context, lg *zap.Logger, m *Telemetry) error, op ...
120120
if ctx, err = autologs.Setup(ctx, m.LoggerProvider(), opts.zapTee); err != nil {
121121
panic(fmt.Sprintf("failed to setup logs: %v", err))
122122
}
123+
123124
shutdownCtx = zctx.Base(shutdownCtx, zctx.From(ctx))
125+
m.shutdownContext = shutdownCtx
126+
m.baseContext = ctx
124127

125128
{
126129
// Automatically setting GOMAXPROCS.
@@ -155,7 +158,8 @@ func Run(f func(ctx context.Context, lg *zap.Logger, m *Telemetry) error, op ...
155158
rerr = fmt.Errorf("shutting down (panic): %v", ec)
156159
}
157160
}()
158-
if err := f(ctx, zctx.From(ctx), m); err != nil {
161+
m.baseContext = ctx
162+
if err := f(m.shutdownContext, zctx.From(ctx), m); err != nil {
159163
if errors.Is(err, ctx.Err()) {
160164
// Parent context got cancelled, error is expected.
161165
// TODO(ernado): check for shutdownCtx instead.

app/telemetry.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,28 @@ type Telemetry struct {
5353
meterProvider metric.MeterProvider
5454
loggerProvider log.LoggerProvider
5555
shutdownContext context.Context
56+
baseContext context.Context
5657

57-
resource *resource.Resource
58-
propagator propagation.TextMapPropagator
58+
resource *resource.Resource
5959

60-
shutdowns []shutdown
60+
propagator propagation.TextMapPropagator
61+
shutdowns []shutdown
6162
}
6263

6364
// ShutdownContext is context for triggering graceful shutdown.
6465
// It is cancelled on SIGINT.
6566
//
66-
// Base context can be used during shutdown to finish pending operations, it will be cancelled later
67+
// Base context [Telemetry.BaseContext] can be used during shutdown to finish pending operations, it will be cancelled later
6768
// on timeout.
6869
func (m *Telemetry) ShutdownContext() context.Context {
6970
return m.shutdownContext
7071
}
7172

73+
// BaseContext is base context for the application.
74+
func (m *Telemetry) BaseContext() context.Context {
75+
return m.baseContext
76+
}
77+
7278
func (m *Telemetry) registerShutdown(name string, fn func(ctx context.Context) error) {
7379
m.shutdowns = append(m.shutdowns, shutdown{name: name, fn: fn})
7480
}
@@ -214,6 +220,7 @@ func newTelemetry(
214220
resource: res,
215221

216222
shutdownContext: shutdownCtx,
223+
baseContext: baseCtx,
217224
}
218225
ctx := baseCtx
219226
{

0 commit comments

Comments
 (0)