Debian Codegraph @

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

raw
1 # OpenTelemetry AWS Lambda Instrumentation for Golang
2
3 [![Go Reference][goref-image]][goref-url]
4 [![Apache License][license-image]][license-url]
5
6 This module provides instrumentation for [`AWS Lambda`](https://docs.aws.amazon.com/lambda/latest/dg/golang-handler.html).
7
8 ## Installation
9
10 ```bash
11 go get -u go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda
12 ```
13
14 ## example
15
16 See [./example](https://github.com/open-telemetry/opentelemetry-go-contrib/tree/main/instrumentation/github.com/aws/aws-lambda-go/otellambda/example)
17
18 ## Usage
19
20 Create a sample Lambda Go application such as below.
21
22 ```go
23 package main
24
25 import (
26 "context"
27 "fmt"
28 "github.com/aws/aws-lambda-go/lambda"
29 )
30
31 type MyEvent struct {
32 Name string `json:"name"`
33 }
34
35 func HandleRequest(ctx context.Context, name MyEvent) (string, error) {
36 return fmt.Sprintf("Hello %s!", name.Name ), nil
37 }
38
39 func main() {
40 lambda.Start(HandleRequest)
41 }
42 ```
43
44 Now use the provided wrapper to instrument your basic Lambda function:
45
46 ```go
47 // Add import
48 import "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda"
49
50 // wrap lambda handler function
51 func main() {
52 lambda.Start(otellambda.InstrumentHandler(HandleRequest))
53 }
54 ```
55
56 ## AWS Lambda Instrumentation Options
57
58 | Options | Input Type | Description | Default |
59 | --- | --- | --- | --- |
60 | `WithTracerProvider` | `trace.TracerProvider` | Provide a custom `TracerProvider` for creating spans. Consider using the [AWS Lambda Resource Detector][lambda-detector-url] with your tracer provider to improve tracing information. | `otel.GetTracerProvider()`
61 | `WithFlusher` | `otellambda.Flusher` | This instrumentation will call the `ForceFlush` method of its `Flusher` at the end of each invocation. Should you be using asynchronous logic (such as `sddktrace's BatchSpanProcessor`) it is very import for spans to be `ForceFlush`'ed before [Lambda freezes](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html) to avoid data delays. | `Flusher` with noop `ForceFlush`
62 | `WithEventToCarrier` | `func(eventJSON []byte) propagation.TextMapCarrier{}` | Function for providing custom logic to support retrieving trace header from different event types that are handled by AWS Lambda (e.g., SQS, CloudWatch, Kinesis, API Gateway) and returning them in a `propagation.TextMapCarrier` which a Propagator can use to extract the trace header into the context. | Function which returns an empty `TextMapCarrier` - new spans will be part of a new Trace and have no parent past Lambda instrumentation span
63 | `WithPropagator` | `propagation.Propagator` | The `Propagator` the instrumentation will use to extract trace information into the context. | `otel.GetTextMapPropagator()` |
64 | `WithTraceAttributeFn` | `func(eventJSON []byte) []attribute.KeyValue` | Function to extract custom attributes from different event types (e.g., SQS, CloudWatch, Kinesis, API Gateway, custom event) and return them as a slice of `attribute.KeyValue` to be added to the span. | Function which returns an empty `[]]attribute.KeyValue` (no custom attributes) |
65
66 ### Usage With Options Example
67
68 ```go
69 var someHeaderKey = "Key" // used by propagator and EventToCarrier function to identify trace header
70
71 type mockHTTPRequest struct {
72 Headers map[string][]string
73 Body string
74 }
75
76 func mockEventToCarrier(eventJSON []byte) propagation.TextMapCarrier{
77 var request mockHTTPRequest
78 _ = json.unmarshal(eventJSON, &request)
79 return propagation.HeaderCarrier{someHeaderKey: []string{request.Headers[someHeaderKey]}}
80 }
81
82 func mockTraceAttributeFn(eventJSON []byte) []attribute.KeyValue {
83 var request mockHTTPRequest
84 _ = json.unmarshal(eventJSON, &request)
85 return []attribute.KeyValue{attribute.String("mock.request.type", reflect.TypeOf(request).String())}
86 }
87
88 type mockPropagator struct{}
89 // Extract - read from `someHeaderKey`
90 // Inject
91 // Fields
92
93 func HandleRequest(ctx context.Context, request mockHTTPRequest) error {
94 return fmt.Sprintf("Hello %s!", request.Body ), nil
95 }
96
97 func main() {
98 exp, _ := stdouttrace.New()
99
100 tp := sdktrace.NewTracerProvider(
101 sdktrace.WithBatcher(exp))
102
103 lambda.Start(otellambda.InstrumentHandler(HandleRequest,
104 otellambda.WithTracerProvider(tp),
105 otellambda.WithFlusher(tp),
106 otellambda.WithTraceAttributeFn(mockTraceAttributeFn),
107 otellambda.WithEventToCarrier(mockEventToCarrier),
108 otellambda.WithPropagator(mockPropagator{})))
109 }
110 ```
111
112 ## Useful links
113
114 - For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
115 - For more about OpenTelemetry Go: <https://github.com/open-telemetry/opentelemetry-go>
116 - For help or feedback on this project, join us in [GitHub Discussions][discussions-url]
117
118 ## License
119
120 Apache 2.0 - See [LICENSE][license-url] for more information.
121
122 [license-url]: https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/LICENSE
123 [license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
124 [goref-image]: https://pkg.go.dev/badge/go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda.svg
125 [goref-url]: https://pkg.go.dev/go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda
126 [discussions-url]: https://github.com/open-telemetry/opentelemetry-go/discussions
127 [lambda-detector-url]: https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/detectors/aws/lambda