| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Package lambda provides a resource detector for AWS Lambda. |
| 5 |
package lambda // import "go.opentelemetry.io/contrib/detectors/aws/lambda" |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"os" |
| 11 |
"strconv" |
| 12 |
|
| 13 |
"go.opentelemetry.io/otel/attribute" |
| 14 |
"go.opentelemetry.io/otel/sdk/resource" |
| 15 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 16 |
) |
| 17 |
|
| 18 |
// For a complete list of reserved environment variables in Lambda, see: |
| 19 |
// https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html |
| 20 |
const ( |
| 21 |
lambdaFunctionNameEnvVar = "AWS_LAMBDA_FUNCTION_NAME" |
| 22 |
awsRegionEnvVar = "AWS_REGION" |
| 23 |
lambdaFunctionVersionEnvVar = "AWS_LAMBDA_FUNCTION_VERSION" |
| 24 |
lambdaLogStreamNameEnvVar = "AWS_LAMBDA_LOG_STREAM_NAME" |
| 25 |
lambdaMemoryLimitEnvVar = "AWS_LAMBDA_FUNCTION_MEMORY_SIZE" |
| 26 |
miB = 1 << 20 |
| 27 |
) |
| 28 |
|
| 29 |
var ( |
| 30 |
empty = resource.Empty() |
| 31 |
errNotOnLambda = errors.New("process is not on Lambda, cannot detect environment variables from Lambda") |
| 32 |
) |
| 33 |
|
| 34 |
// resource detector collects resource information from Lambda environment. |
| 35 |
type resourceDetector struct{} |
| 36 |
|
| 37 |
// compile time assertion that resource detector implements the resource.Detector interface. |
| 38 |
var _ resource.Detector = (*resourceDetector)(nil) |
| 39 |
|
| 40 |
// NewResourceDetector returns a resource detector that will detect AWS Lambda resources. |
| 41 |
func NewResourceDetector() resource.Detector { |
| 42 |
return &resourceDetector{} |
| 43 |
} |
| 44 |
|
| 45 |
// Detect collects resource attributes available when running on lambda. |
| 46 |
func (*resourceDetector) Detect(context.Context) (*resource.Resource, error) { |
| 47 |
// Lambda resources come from ENV |
| 48 |
lambdaName := os.Getenv(lambdaFunctionNameEnvVar) |
| 49 |
if lambdaName == "" { |
| 50 |
return empty, errNotOnLambda |
| 51 |
} |
| 52 |
awsRegion := os.Getenv(awsRegionEnvVar) |
| 53 |
functionVersion := os.Getenv(lambdaFunctionVersionEnvVar) |
| 54 |
// The instance attributes corresponds to the log stream name for AWS lambda, |
| 55 |
// see the FaaS resource specification for more details. |
| 56 |
instance := os.Getenv(lambdaLogStreamNameEnvVar) |
| 57 |
|
| 58 |
attrs := []attribute.KeyValue{ |
| 59 |
semconv.CloudProviderAWS, |
| 60 |
semconv.CloudRegion(awsRegion), |
| 61 |
semconv.FaaSInstance(instance), |
| 62 |
semconv.FaaSName(lambdaName), |
| 63 |
semconv.FaaSVersion(functionVersion), |
| 64 |
} |
| 65 |
|
| 66 |
maxMemoryStr := os.Getenv(lambdaMemoryLimitEnvVar) |
| 67 |
maxMemory, err := strconv.Atoi(maxMemoryStr) |
| 68 |
if err == nil { |
| 69 |
attrs = append(attrs, semconv.FaaSMaxMemory(maxMemory*miB)) |
| 70 |
} |
| 71 |
|
| 72 |
return resource.NewWithAttributes(semconv.SchemaURL, attrs...), nil |
| 73 |
} |