New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lambda instrumentation enhancements #1390
Changes from all commits
dcc01de
1dd54d9
03f6fb4
5919be5
ebd45b4
e1ff7d0
041d9b8
e12a4b1
e906d2a
86c04d1
047f5d0
21db8fa
5204d27
c9c1bca
c956d4b
12c6c74
e042a6f
1f6b745
ada0137
9c6a430
79d6d15
e21f9a6
3470b86
fbcf1ac
f03fca4
4a3ea70
0a90830
b9fede8
38663b0
9d2d737
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -18,7 +18,6 @@ import ( | |||||||||||||
"context" | ||||||||||||||
"log" | ||||||||||||||
"os" | ||||||||||||||
"runtime" | ||||||||||||||
|
||||||||||||||
lambdadetector "go.opentelemetry.io/contrib/detectors/aws/lambda" | ||||||||||||||
"go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda" | ||||||||||||||
|
@@ -35,48 +34,37 @@ func xrayEventToCarrier([]byte) propagation.TextMapCarrier { | |||||||||||||
return propagation.HeaderCarrier{"X-Amzn-Trace-Id": []string{xrayTraceID}} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
type asyncSafeFlusher struct { | ||||||||||||||
tp *sdktrace.TracerProvider | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func (f asyncSafeFlusher) ForceFlush(ctx context.Context) error { | ||||||||||||||
// yield processor to attempt to ensure all spans have | ||||||||||||||
// been consumed and are ready to be flushed | ||||||||||||||
// - see https://github.com/open-telemetry/opentelemetry-go/issues/2080 | ||||||||||||||
// to be removed upon resolution of above issue | ||||||||||||||
runtime.Gosched() | ||||||||||||||
|
||||||||||||||
return f.tp.ForceFlush(ctx) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// tracerProviderAndFlusher returns a list of otellambda.Option(s) to | ||||||||||||||
// enable using a TracerProvider configured for AWS XRay via a collector | ||||||||||||||
// and an otellambda.Flusher to flush this TracerProvider. | ||||||||||||||
// tracerProviderAndFlusher is not exported because it should not be used | ||||||||||||||
// without the provided EventToCarrier function and XRay Propagator | ||||||||||||||
func tracerProviderAndFlusher() ([]otellambda.Option, error) { | ||||||||||||||
ctx := context.Background() | ||||||||||||||
|
||||||||||||||
// Do not need transport security in Lambda because collector | ||||||||||||||
// runs locally in Lambda execution environment | ||||||||||||||
// PrepareTracerProvider returns a TracerProvider configured with exporter, | ||||||||||||||
// id generator and lambda resource detector to send trace data to AWS X-Ray via Collector | ||||||||||||||
func PrepareTracerProvider(ctx context.Context) (*sdktrace.TracerProvider, error) { | ||||||||||||||
Comment on lines
+37
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
log.Println("creating trace exporter") | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
exp, err := otlptracegrpc.New(ctx, otlptracegrpc.WithInsecure()) | ||||||||||||||
if err != nil { | ||||||||||||||
return []otellambda.Option{}, err | ||||||||||||||
errorLogger.Println("failed to create exporter: ", err) | ||||||||||||||
return nil, err | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
detector := lambdadetector.NewResourceDetector() | ||||||||||||||
res, err := detector.Detect(ctx) | ||||||||||||||
resource, err := detector.Detect(ctx) | ||||||||||||||
if err != nil { | ||||||||||||||
return []otellambda.Option{}, err | ||||||||||||||
errorLogger.Println("failed to detect lambda resources: ", err) | ||||||||||||||
return nil, err | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
tp := sdktrace.NewTracerProvider( | ||||||||||||||
return sdktrace.NewTracerProvider( | ||||||||||||||
sdktrace.WithBatcher(exp), | ||||||||||||||
sdktrace.WithIDGenerator(xray.NewIDGenerator()), | ||||||||||||||
sdktrace.WithResource(res), | ||||||||||||||
) | ||||||||||||||
sdktrace.WithResource(resource), | ||||||||||||||
), nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return []otellambda.Option{otellambda.WithTracerProvider(tp), otellambda.WithFlusher(asyncSafeFlusher{tp: tp})}, nil | ||||||||||||||
// tracerProviderAndFlusher returns a list of otellambda.Option(s) to | ||||||||||||||
// enable using a TracerProvider configured for AWS XRay via a collector | ||||||||||||||
// and an otellambda.Flusher to flush this TracerProvider. | ||||||||||||||
// tracerProviderAndFlusher is not exported because it should not be used | ||||||||||||||
// without the provided EventToCarrier function and XRay Propagator | ||||||||||||||
Comment on lines
+64
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why should these two options not be used without the other two options? |
||||||||||||||
func tracerProviderAndFlusher(tp *sdktrace.TracerProvider) ([]otellambda.Option, error) { | ||||||||||||||
return []otellambda.Option{otellambda.WithTracerProvider(tp), otellambda.WithAsyncSafeFlusher(tp)}, nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// EventToCarrier returns an otellambda.Option to enable | ||||||||||||||
|
@@ -89,14 +77,13 @@ func EventToCarrier() otellambda.Option { | |||||||||||||
|
||||||||||||||
// Propagator returns an otellambda.Option to enable the xray.Propagator | ||||||||||||||
func Propagator() otellambda.Option { | ||||||||||||||
|
||||||||||||||
return otellambda.WithPropagator(xray.Propagator{}) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// AllRecommendedOptions returns a list of all otellambda.Option(s) | ||||||||||||||
// recommended for the otellambda package when using AWS XRay | ||||||||||||||
func AllRecommendedOptions() []otellambda.Option { | ||||||||||||||
options, err := tracerProviderAndFlusher() | ||||||||||||||
func AllRecommendedOptions(tp *sdktrace.TracerProvider) []otellambda.Option { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The |
||||||||||||||
options, err := tracerProviderAndFlusher(tp) | ||||||||||||||
if err != nil { | ||||||||||||||
// should we fail to create the TracerProvider, do not alter otellambda's default configuration | ||||||||||||||
errorLogger.Println("failed to create recommended configuration: ", err) | ||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have just landed open-telemetry/opentelemetry-go#2335 which will enable us to remove this entirely and simply call
ForceFlush
on the providedTracerProvider
. The entireFlusher
type and its implementations and configuration options can be removed.