| 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 |
|
| 9 |
"go.opentelemetry.io/otel/attribute" |
| 10 |
"go.opentelemetry.io/otel/propagation" |
| 11 |
"go.opentelemetry.io/otel/trace" |
| 12 |
) |
| 13 |
|
| 14 |
// A Flusher dictates how the instrumentation will attempt to flush |
| 15 |
// unexported spans at the end of each Lambda innovation. This is |
| 16 |
// very important in asynchronous settings because the Lambda runtime |
| 17 |
// may enter a 'frozen' state any time after the invocation completes. |
| 18 |
// Should this freeze happen and spans are left unexported, there can be a |
| 19 |
// long delay before those spans are exported. |
| 20 |
type Flusher interface { |
| 21 |
ForceFlush(context.Context) error |
| 22 |
} |
| 23 |
|
| 24 |
type noopFlusher struct{} |
| 25 |
|
| 26 |
func (*noopFlusher) ForceFlush(context.Context) error { return nil } |
| 27 |
|
| 28 |
// Compile time check our noopFlusher implements Flusher. |
| 29 |
var _ Flusher = &noopFlusher{} |
| 30 |
|
| 31 |
// An EventToCarrier function defines how the instrumentation should |
| 32 |
// prepare a TextMapCarrier for the configured propagator to read from. This |
| 33 |
// extra step is necessary because Lambda does not have HTTP headers to read |
| 34 |
// from and instead stores the headers it was invoked with (including TraceID, etc.) |
| 35 |
// as part of the invocation event. If using the AWS XRay tracing then the |
| 36 |
// trace information is instead stored in the Lambda environment. |
| 37 |
type EventToCarrier func(eventJSON []byte) propagation.TextMapCarrier |
| 38 |
|
| 39 |
// TraceAttributeFn defines a function that extracts attributes |
| 40 |
// from the event JSON to be added to the span created by the instrumentation. |
| 41 |
type TraceAttributeFn func(eventJSON []byte) []attribute.KeyValue |
| 42 |
|
| 43 |
func emptyEventToCarrier([]byte) propagation.TextMapCarrier { |
| 44 |
return propagation.HeaderCarrier{} |
| 45 |
} |
| 46 |
|
| 47 |
func emptyTraceAttributeFn([]byte) []attribute.KeyValue { |
| 48 |
return []attribute.KeyValue{} |
| 49 |
} |
| 50 |
|
| 51 |
// Compile time check our emptyEventToCarrier implements EventToCarrier. |
| 52 |
var _ EventToCarrier = emptyEventToCarrier |
| 53 |
|
| 54 |
// Option applies a configuration option. |
| 55 |
type Option interface { |
| 56 |
apply(*config) |
| 57 |
} |
| 58 |
|
| 59 |
type optionFunc func(*config) |
| 60 |
|
| 61 |
func (o optionFunc) apply(c *config) { |
| 62 |
o(c) |
| 63 |
} |
| 64 |
|
| 65 |
type config struct { |
| 66 |
// TracerProvider is the TracerProvider which will be used |
| 67 |
// to create instrumentation spans |
| 68 |
// The default value of TracerProvider the global otel TracerProvider |
| 69 |
// returned by otel.GetTracerProvider() |
| 70 |
TracerProvider trace.TracerProvider |
| 71 |
|
| 72 |
// Flusher is the mechanism used to flush any unexported spans |
| 73 |
// each Lambda Invocation to avoid spans being unexported for long |
| 74 |
// when periods of time if Lambda freezes the execution environment |
| 75 |
// The default value of Flusher is a noop Flusher, using this |
| 76 |
// default can result in long data delays in asynchronous settings |
| 77 |
Flusher Flusher |
| 78 |
|
| 79 |
// EventToCarrier is the mechanism used to retrieve the TraceID |
| 80 |
// from the event or environment and generate a TextMapCarrier which |
| 81 |
// can then be used by a Propagator to extract the TraceID into our context |
| 82 |
// The default value of eventToCarrier is emptyEventToCarrier which returns |
| 83 |
// an empty HeaderCarrier, using this default will cause new spans to be part |
| 84 |
// of a new Trace and have no parent past our Lambda instrumentation span |
| 85 |
EventToCarrier EventToCarrier |
| 86 |
|
| 87 |
// Propagator is the Propagator which will be used |
| 88 |
// to extract Trace info into the context |
| 89 |
// The default value of Propagator the global otel Propagator |
| 90 |
// returned by otel.GetTextMapPropagator() |
| 91 |
Propagator propagation.TextMapPropagator |
| 92 |
|
| 93 |
// TraceAttributeFn is a function that returns custom attributes |
| 94 |
// to be added to the span created by the instrumentation. |
| 95 |
// The default value of TraceAttributeFn is nil, which means no attributes |
| 96 |
// will be added to the span. |
| 97 |
TraceAttributeFn TraceAttributeFn |
| 98 |
} |
| 99 |
|
| 100 |
// WithTracerProvider configures the TracerProvider used by the |
| 101 |
// instrumentation. |
| 102 |
// |
| 103 |
// By default, the global TracerProvider is used. |
| 104 |
func WithTracerProvider(tracerProvider trace.TracerProvider) Option { |
| 105 |
return optionFunc(func(c *config) { |
| 106 |
c.TracerProvider = tracerProvider |
| 107 |
}) |
| 108 |
} |
| 109 |
|
| 110 |
// WithFlusher sets the used flusher. |
| 111 |
func WithFlusher(flusher Flusher) Option { |
| 112 |
return optionFunc(func(c *config) { |
| 113 |
c.Flusher = flusher |
| 114 |
}) |
| 115 |
} |
| 116 |
|
| 117 |
// WithEventToCarrier sets the used EventToCarrier. |
| 118 |
func WithEventToCarrier(eventToCarrier EventToCarrier) Option { |
| 119 |
return optionFunc(func(c *config) { |
| 120 |
c.EventToCarrier = eventToCarrier |
| 121 |
}) |
| 122 |
} |
| 123 |
|
| 124 |
// WithPropagator configures the propagator used by the instrumentation. |
| 125 |
// |
| 126 |
// By default, the global TextMapPropagator will be used. |
| 127 |
func WithPropagator(propagator propagation.TextMapPropagator) Option { |
| 128 |
return optionFunc(func(c *config) { |
| 129 |
c.Propagator = propagator |
| 130 |
}) |
| 131 |
} |
| 132 |
|
| 133 |
// WithTraceAttributeFn configures a function that returns custom attributes. |
| 134 |
func WithTraceAttributeFn(fn TraceAttributeFn) Option { |
| 135 |
return optionFunc(func(c *config) { |
| 136 |
c.TraceAttributeFn = fn |
| 137 |
}) |
| 138 |
} |