跳转到主内容
跳转到主内容

AWS Lambda

本指南集成以下内容:

✅ 日志✅ 指标✅ 链路追踪

安装 OpenTelemetry Lambda 层

OpenTelemetry 项目提供了独立的 Lambda 层,用于:

  1. 使用 OpenTelemetry 自动埋点功能自动为你的 Lambda 函数代码进行监测。
  2. 将收集到的日志、指标和链路追踪数据转发到 ClickStack。

添加特定语言的自动埋点层

特定语言的自动埋点 Lambda layer 会使用适用于该语言的 OpenTelemetry 自动埋点包,自动对你的 Lambda 函数代码进行埋点。

每种语言和每个 Region 都有各自的 layer ARN。

如果你的 Lambda 已经通过 OpenTelemetry SDK 完成埋点,可以跳过此步骤。

开始操作

  1. Layers 部分点击 “Add a layer”
  2. 选择 “Specify an ARN”,并根据语言选择对应的 ARN,确保将 <region> 替换为你的 Region(例如 us-east-2):
arn:aws:lambda:<region>:184161586896:layer:opentelemetry-nodejs-0_7_0:1

The latest releases of the layers can be found in the OpenTelemetry Lambda Layers GitHub repository.

  1. Configure the following environment variables in your Lambda function under "Configuration" > "Environment variables".
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
AWS_LAMBDA_EXEC_WRAPPER=/opt/otel-handler
OTEL_PROPAGATORS=tracecontext
OTEL_TRACES_SAMPLER=always_on

安装 OpenTelemetry collector Lambda 层

collector Lambda 层允许你将 Lambda 函数中的日志、指标和追踪(traces)转发到 ClickStack,而不会因为导出器(exporter)延迟而影响响应时间。

安装 collector 层的步骤

  1. 在 Layers 区域点击 “Add a layer”
  2. 选择 “Specify an ARN”,并根据架构选择正确的 ARN,确保将 <region> 替换为你的区域(例如 us-east-2):
arn:aws:lambda:<region>:184161586896:layer:opentelemetry-collector-amd64-0_8_0:1
  1. 将以下 collector.yaml 文件添加到你的项目中,用于配置 collector 将数据发送到 ClickStack:
# collector.yaml
receivers:
  otlp:
    protocols:
      grpc:
        endpoint: 'localhost:4317'
      http:
        endpoint: 'localhost:4318'

processors:
  batch:
  decouple:

exporters:
  otlphttp:
    endpoint: "<YOU_OTEL_COLLECTOR_HTTP_ENDPOINT>"
    compression: gzip

service:
  pipelines:
    traces:
      receivers: [otlp]
      processors: [batch, decouple]
      exporters: [otlphttp]
    metrics:
      receivers: [otlp]
      processors: [batch, decouple]
      exporters: [otlphttp]
    logs:
      receivers: [otlp]
      processors: [batch, decouple]
      exporters: [otlphttp]
  1. 添加以下环境变量:
OPENTELEMETRY_COLLECTOR_CONFIG_FILE=/var/task/collector.yaml

Checking the installation

After deploying the layers, you should now see traces automatically collected from your Lambda function in HyperDX. The decouple and batching processor may introduce a delay in telemetry collection, so traces may be delayed in showing up. To emit custom logs or metrics, you'll need to instrument your code your language-specific OpenTelemetry SDKs.

Troubleshooting

Custom instrumentation not sending

If you're not seeing your manually defined traces or other telemetry, you may be using an incompatible version of the OpenTelemetry API package. Ensure your OpenTelemetry API package is at least the same or lower version than the version included in the AWS lambda.

Enabling SDK debug logs

Set the OTEL_LOG_LEVEL environment variable to DEBUG to enable debug logs from the OpenTelemetry SDK. This will help ensure that the auto-instrumentation layer is correctly instrumenting your application.

Enabling collector debug logs

To debug collector issues, you can enable debug logs by modifying your collector configuration file to add the logging exporter and setting the telemetry log level to debug to enable more verbose logging from the collector lambda layer.

# collector.yaml
receivers:
  otlp:
    protocols:
      grpc:
        endpoint: 'localhost:4317'
      http:
        endpoint: 'localhost:4318'

exporters:
  logging:
    verbosity: detailed
  otlphttp:
    endpoint: "<YOU_OTEL_COLLECTOR_HTTP_ENDPOINT>"
    compression: gzip

service:
  telemetry:
    logs:
      level: "debug"
  pipelines:
    traces:
      receivers: [otlp]
      processors: [batch, decouple]
      exporters: [otlphttp, logging]
    metrics:
      receivers: [otlp]
      processors: [batch, decouple]
      exporters: [otlphttp, logging]
    logs:
      receivers: [otlp]
      processors: [batch, decouple]
      exporters: [otlphttp, logging]