Debian Codegraph @

instrumentation/github.com/aws/aws-lambda-go/otellambda/lambda.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otellambda // import "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda"
5
6 import (
7 "context"
8 "log"
9 "os"
10 "strings"
11
12 "github.com/aws/aws-lambda-go/lambdacontext"
13 "go.opentelemetry.io/otel"
14 "go.opentelemetry.io/otel/attribute"
15 semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
16 "go.opentelemetry.io/otel/trace"
17 )
18
19 const (
20 // ScopeName is the instrumentation scope name.
21 ScopeName = "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda"
22 )
23
24 var errorLogger = log.New(log.Writer(), "OTel Lambda Error: ", 0)
25
26 type instrumentor struct {
27 configuration config
28 resAttrs []attribute.KeyValue
29 tracer trace.Tracer
30 }
31
32 func newInstrumentor(opts ...Option) instrumentor {
33 cfg := config{
34 TracerProvider: otel.GetTracerProvider(),
35 Flusher: &noopFlusher{},
36 EventToCarrier: emptyEventToCarrier,
37 Propagator: otel.GetTextMapPropagator(),
38 TraceAttributeFn: emptyTraceAttributeFn,
39 }
40 for _, opt := range opts {
41 opt.apply(&cfg)
42 }
43
44 return instrumentor{
45 configuration: cfg,
46 tracer: cfg.TracerProvider.Tracer(ScopeName, trace.WithInstrumentationVersion(Version)),
47 resAttrs: []attribute.KeyValue{},
48 }
49 }
50
51 // Logic to start OTel Tracing.
52 func (i *instrumentor) tracingBegin(ctx context.Context, eventJSON []byte) (context.Context, trace.Span) {
53 // Add trace id to context
54 mc := i.configuration.EventToCarrier(eventJSON)
55 ctx = i.configuration.Propagator.Extract(ctx, mc)
56
57 var span trace.Span
58 spanName := os.Getenv("AWS_LAMBDA_FUNCTION_NAME")
59
60 var attributes []attribute.KeyValue
61 customAttrs := i.configuration.TraceAttributeFn(eventJSON)
62 attributes = append(attributes, customAttrs...)
63 lc, ok := lambdacontext.FromContext(ctx)
64 if !ok {
65 errorLogger.Println("failed to load lambda context from context, ensure tracing enabled in Lambda")
66 }
67 if lc != nil {
68 ctxRequestID := lc.AwsRequestID
69 attributes = append(attributes, semconv.FaaSInvocationID(ctxRequestID))
70
71 // Some resource attrs added as span attrs because lambda
72 // resource detectors are created before a lambda
73 // invocation and therefore lack lambdacontext.
74 // Create these attrs upon first invocation
75 if len(i.resAttrs) == 0 {
76 ctxFunctionArn := lc.InvokedFunctionArn
77 attributes = append(attributes, semconv.AWSLambdaInvokedARN(ctxFunctionArn))
78 arnParts := strings.Split(ctxFunctionArn, ":")
79 if len(arnParts) >= 5 {
80 attributes = append(attributes, semconv.CloudAccountID(arnParts[4]))
81 }
82 }
83 attributes = append(attributes, i.resAttrs...)
84 }
85
86 ctx, span = i.tracer.Start(ctx, spanName, trace.WithSpanKind(trace.SpanKindServer), trace.WithAttributes(attributes...))
87
88 return ctx, span
89 }
90
91 // Logic to wrap up OTel Tracing.
92 func (i *instrumentor) tracingEnd(ctx context.Context, span trace.Span) {
93 span.End()
94
95 // force flush any tracing data since lambda may freeze
96 err := i.configuration.Flusher.ForceFlush(ctx)
97 if err != nil {
98 errorLogger.Println("failed to force a flush, lambda may freeze before instrumentation exported: ", err)
99 }
100 }