Skip to content

Commit

Permalink
Add Lambda Termination Hook
Browse files Browse the repository at this point in the history
  • Loading branch information
driverpt committed Oct 21, 2024
1 parent af91db3 commit 1bf7cb8
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions frotel/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,29 @@ func InstrumentHandler(tp *trace.TracerProvider, handlerFunc interface{}) interf
otellambda.WithPropagator(propagation.TraceContext{}))
}

func Start(handlerFunc interface{}) {
ctx := context.Background()
func initOtelProviders(ctx context.Context) *OtelProviders {
otelProviders, err := NewProvider(ctx)
if err != nil {
log.Error("creating tracing provider failed", err)
otelProviders = DefaultProviders()
}

return &otelProviders
}

func Start(handlerFunc interface{}) {
ctx := context.Background()
otelProviders := initOtelProviders(ctx)
defer otelProviders.Shutdown(ctx)

lambda.Start(InstrumentHandler(otelProviders.TracerProvider, handlerFunc))
}

func StartWithTerminationHook(handlerFunc interface{}, hook func()) {
ctx := context.Background()
otelProviders := initOtelProviders(ctx)
defer otelProviders.Shutdown(ctx)

lambda.StartWithOptions(InstrumentHandler(otelProviders.TracerProvider, handlerFunc),
lambda.WithEnableSIGTERM(hook))
}

0 comments on commit 1bf7cb8

Please sign in to comment.