| 1 |
# Recommended Configurations for OpenTelemetry AWS Lambda Instrumentation with AWS X-Ray |
| 2 |
|
| 3 |
[![Go Reference][goref-image]][goref-url] |
| 4 |
[![Apache License][license-image]][license-url] |
| 5 |
|
| 6 |
This module provides recommended configuration options for [`AWS Lambda Instrumentation`](https://github.com/open-telemetry/opentelemetry-go-contrib/tree/main/instrumentation/github.com/aws/aws-lambda-go/otellambda) when using [AWS X-Ray](https://aws.amazon.com/xray/). By using this configuration, trace context will automatically be extracted from incoming requests with the `X-Amzn-Trace-Id` header if present. Trace context will also always be injected using the `X-Amzn-Trace-Id` format into downstream requests from the Lambda function. |
| 7 |
|
| 8 |
## Installation |
| 9 |
|
| 10 |
```bash |
| 11 |
go get -u go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda/xrayconfig |
| 12 |
``` |
| 13 |
|
| 14 |
## Usage |
| 15 |
|
| 16 |
Create a sample Lambda Go application instrumented by the `otellambda` package such as below. |
| 17 |
|
| 18 |
```go |
| 19 |
package main |
| 20 |
|
| 21 |
import ( |
| 22 |
"context" |
| 23 |
"fmt" |
| 24 |
"github.com/aws/aws-lambda-go/lambda" |
| 25 |
"go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda" |
| 26 |
) |
| 27 |
|
| 28 |
type MyEvent struct { |
| 29 |
Name string `json:"name"` |
| 30 |
} |
| 31 |
|
| 32 |
func HandleRequest(ctx context.Context, name MyEvent) (string, error) { |
| 33 |
return fmt.Sprintf("Hello %s!", name.Name ), nil |
| 34 |
} |
| 35 |
|
| 36 |
func main() { |
| 37 |
lambda.Start(otellambda.InstrumentHandler(HandleRequest)) |
| 38 |
} |
| 39 |
``` |
| 40 |
|
| 41 |
Now configure the instrumentation with the provided options to export traces to AWS X-Ray via [the OpenTelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector) running as a Lambda Extension. Instructions for running the OTel Collector as a Lambda Extension can be found in the [AWS OpenTelemetry Documentation](https://aws-otel.github.io/docs/getting-started/lambda). |
| 42 |
|
| 43 |
```go |
| 44 |
// Add import |
| 45 |
import "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda/xrayconfig" |
| 46 |
|
| 47 |
// add options to InstrumentHandler call |
| 48 |
func main() { |
| 49 |
lambda.Start(otellambda.InstrumentHandler(HandleRequest, xrayconfig.WithRecommendedOptions()...)) |
| 50 |
} |
| 51 |
``` |
| 52 |
|
| 53 |
## Recommended AWS Lambda Instrumentation Options |
| 54 |
|
| 55 |
| Instrumentation Option | Recommended Value | Exported As | |
| 56 |
| --- | --- | --- | |
| 57 |
| `WithTracerProvider` | An `sdktrace.TracerProvider` configured to export in batches to an OTel Collector running locally in Lambda | Not individually exported. Can only be used via `WithRecommendedOptions()` |
| 58 |
| `WithFlusher` | An `otellambda.Flusher` which yields before calling ForceFlush on the configured `sdktrace.TracerProvider`. Yielding mitigates data delays caused by asynchronous nature of batching TracerProvider when in Lambda | Not individually exported. Can only be used via `WithRecommendedOptions()` |
| 59 |
| `WithEventToCarrier` | Function which reads X-Ray TraceID from Lambda environment and inserts it into a `propagtation.TextMapCarrier` | Individually exported as `WithEventToCarrier()`, also included in `WithRecommendedOptions()` |
| 60 |
| `WithPropagator` | An `xray.propagator` | Individually exported as `WithPropagator()`, also included in `WithRecommendedOptions()` |
| 61 |
|
| 62 |
## Useful links |
| 63 |
|
| 64 |
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/> |
| 65 |
- For more about OpenTelemetry Go: <https://github.com/open-telemetry/opentelemetry-go> |
| 66 |
- For help or feedback on this project, join us in [GitHub Discussions][discussions-url] |
| 67 |
|
| 68 |
## License |
| 69 |
|
| 70 |
Apache 2.0 - See [LICENSE][license-url] for more information. |
| 71 |
|
| 72 |
[license-url]: https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/LICENSE |
| 73 |
[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat |
| 74 |
[goref-image]: https://pkg.go.dev/badge/go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda/xrayconfig.svg |
| 75 |
[goref-url]: https://pkg.go.dev/go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda/xrayconfig |
| 76 |
[discussions-url]: https://github.com/open-telemetry/opentelemetry-go/discussions |