From d67240ae0aa4d76b940fa75539524e1b42496e81 Mon Sep 17 00:00:00 2001 From: Matej Gera Date: Fri, 13 Jan 2023 11:02:50 +0100 Subject: [PATCH 1/5] Add missing recommended attributes - faas.instance (corresponds to log stream name) - faas.max_memory Signed-off-by: Matej Gera --- detectors/aws/lambda/detector.go | 13 +++++++++++++ detectors/aws/lambda/detector_test.go | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/detectors/aws/lambda/detector.go b/detectors/aws/lambda/detector.go index ff60db88b0f..9b21cd950b4 100644 --- a/detectors/aws/lambda/detector.go +++ b/detectors/aws/lambda/detector.go @@ -18,6 +18,7 @@ import ( "context" "errors" "os" + "strconv" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/sdk/resource" @@ -30,6 +31,8 @@ const ( lambdaFunctionNameEnvVar = "AWS_LAMBDA_FUNCTION_NAME" awsRegionEnvVar = "AWS_REGION" lambdaFunctionVersionEnvVar = "AWS_LAMBDA_FUNCTION_VERSION" + lambdaLogStreamNameEnvVar = "AWS_LAMBDA_LOG_STREAM_NAME" + lambdaMemoryLimitEnvVar = "AWS_LAMBDA_FUNCTION_MEMORY_SIZE" ) var ( @@ -57,13 +60,23 @@ func (detector *resourceDetector) Detect(context.Context) (*resource.Resource, e } awsRegion := os.Getenv(awsRegionEnvVar) functionVersion := os.Getenv(lambdaFunctionVersionEnvVar) + // The instance attributes corresponds to the log stream name for AWS lambda, + // see the FaaS convention for more details. + instance := os.Getenv(lambdaLogStreamNameEnvVar) attrs := []attribute.KeyValue{ semconv.CloudProviderAWS, semconv.CloudRegionKey.String(awsRegion), + semconv.FaaSInstanceKey.String(instance), semconv.FaaSNameKey.String(lambdaName), semconv.FaaSVersionKey.String(functionVersion), } + maxMemoryStr := os.Getenv(lambdaMemoryLimitEnvVar) + maxMemory, err := strconv.Atoi(maxMemoryStr) + if err == nil { + attrs = append(attrs, semconv.FaaSMaxMemoryKey.Int(maxMemory)) + } + return resource.NewWithAttributes(semconv.SchemaURL, attrs...), nil } diff --git a/detectors/aws/lambda/detector_test.go b/detectors/aws/lambda/detector_test.go index 9c6c65d308a..311362d8cdd 100644 --- a/detectors/aws/lambda/detector_test.go +++ b/detectors/aws/lambda/detector_test.go @@ -32,12 +32,16 @@ func TestDetectSuccess(t *testing.T) { _ = os.Setenv(lambdaFunctionNameEnvVar, "testFunction") _ = os.Setenv(awsRegionEnvVar, "us-texas-1") _ = os.Setenv(lambdaFunctionVersionEnvVar, "$LATEST") + _ = os.Setenv(lambdaLogStreamNameEnvVar, "2023/01/01/[$LATEST]5d1edb9e525d486696cf01a3503487bc") + _ = os.Setenv(lambdaMemoryLimitEnvVar, "128") attributes := []attribute.KeyValue{ semconv.CloudProviderAWS, semconv.CloudRegionKey.String("us-texas-1"), semconv.FaaSNameKey.String("testFunction"), semconv.FaaSVersionKey.String("$LATEST"), + semconv.FaaSInstanceKey.String("2023/01/01/[$LATEST]5d1edb9e525d486696cf01a3503487bc"), + semconv.FaaSMaxMemoryKey.Int(128), } expectedResource := resource.NewWithAttributes(semconv.SchemaURL, attributes...) detector := resourceDetector{} From b153c87f94d621ba3831290ea72edc7e4a46d2fe Mon Sep 17 00:00:00 2001 From: Matej Gera Date: Fri, 13 Jan 2023 11:14:17 +0100 Subject: [PATCH 2/5] Adjust Lambda tests Signed-off-by: Matej Gera --- .../aws/aws-lambda-go/otellambda/lambda_test.go | 2 ++ .../aws/aws-lambda-go/otellambda/test/lambda_test.go | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/instrumentation/github.com/aws/aws-lambda-go/otellambda/lambda_test.go b/instrumentation/github.com/aws/aws-lambda-go/otellambda/lambda_test.go index caa823ce8af..537b3d757a4 100644 --- a/instrumentation/github.com/aws/aws-lambda-go/otellambda/lambda_test.go +++ b/instrumentation/github.com/aws/aws-lambda-go/otellambda/lambda_test.go @@ -54,6 +54,8 @@ func setEnvVars() { _ = os.Setenv("AWS_LAMBDA_FUNCTION_NAME", "testFunction") _ = os.Setenv("AWS_REGION", "us-texas-1") _ = os.Setenv("AWS_LAMBDA_FUNCTION_VERSION", "$LATEST") + _ = os.Setenv("AWS_LAMBDA_LOG_STREAM_NAME", "2023/01/01/[$LATEST]5d1edb9e525d486696cf01a3503487bc") + _ = os.Setenv("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "128") _ = os.Setenv("_X_AMZN_TRACE_ID", "Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1") } diff --git a/instrumentation/github.com/aws/aws-lambda-go/otellambda/test/lambda_test.go b/instrumentation/github.com/aws/aws-lambda-go/otellambda/test/lambda_test.go index edc73b9ad71..87b246f24b6 100644 --- a/instrumentation/github.com/aws/aws-lambda-go/otellambda/test/lambda_test.go +++ b/instrumentation/github.com/aws/aws-lambda-go/otellambda/test/lambda_test.go @@ -102,6 +102,8 @@ func setEnvVars() { _ = os.Setenv("AWS_LAMBDA_FUNCTION_NAME", "testFunction") _ = os.Setenv("AWS_REGION", "us-texas-1") _ = os.Setenv("AWS_LAMBDA_FUNCTION_VERSION", "$LATEST") + _ = os.Setenv("AWS_LAMBDA_LOG_STREAM_NAME", "2023/01/01/[$LATEST]5d1edb9e525d486696cf01a3503487bc") + _ = os.Setenv("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "128") _ = os.Setenv("_X_AMZN_TRACE_ID", "Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1") } @@ -148,7 +150,9 @@ var ( attribute.String("cloud.provider", "aws"), attribute.String("cloud.region", "us-texas-1"), attribute.String("faas.name", "testFunction"), - attribute.String("faas.version", "$LATEST")), + attribute.String("faas.version", "$LATEST"), + attribute.String("faas.instance", "2023/01/01/[$LATEST]5d1edb9e525d486696cf01a3503487bc"), + attribute.Int("faas.max_memory", 128)), InstrumentationLibrary: instrumentation.Library{Name: "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda", Version: otellambda.SemVersion()}, } ) @@ -336,7 +340,9 @@ var ( attribute.String("cloud.provider", "aws"), attribute.String("cloud.region", "us-texas-1"), attribute.String("faas.name", "testFunction"), - attribute.String("faas.version", "$LATEST")), + attribute.String("faas.version", "$LATEST"), + attribute.String("faas.instance", "2023/01/01/[$LATEST]5d1edb9e525d486696cf01a3503487bc"), + attribute.Int("faas.max_memory", 128)), InstrumentationLibrary: instrumentation.Library{Name: "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda", Version: otellambda.SemVersion()}, } ) From 0c3a08e239c9d3ddc2cd2ff5002671876b19d26c Mon Sep 17 00:00:00 2001 From: Matej Gera Date: Fri, 13 Jan 2023 11:14:29 +0100 Subject: [PATCH 3/5] Adjust Xrayconfig test Signed-off-by: Matej Gera --- .../otellambda/xrayconfig/xrayconfig_test.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/instrumentation/github.com/aws/aws-lambda-go/otellambda/xrayconfig/xrayconfig_test.go b/instrumentation/github.com/aws/aws-lambda-go/otellambda/xrayconfig/xrayconfig_test.go index df27ea6516a..89c03dcea85 100644 --- a/instrumentation/github.com/aws/aws-lambda-go/otellambda/xrayconfig/xrayconfig_test.go +++ b/instrumentation/github.com/aws/aws-lambda-go/otellambda/xrayconfig/xrayconfig_test.go @@ -67,6 +67,8 @@ func setEnvVars() { _ = os.Setenv("AWS_LAMBDA_FUNCTION_NAME", "testFunction") _ = os.Setenv("AWS_REGION", "us-texas-1") _ = os.Setenv("AWS_LAMBDA_FUNCTION_VERSION", "$LATEST") + _ = os.Setenv("AWS_LAMBDA_LOG_STREAM_NAME", "2023/01/01/[$LATEST]5d1edb9e525d486696cf01a3503487bc") + _ = os.Setenv("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "128") _ = os.Setenv("_X_AMZN_TRACE_ID", "Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1") // fix issue: "The requested service provider could not be loaded or initialized." @@ -116,6 +118,8 @@ var ( expectedSpanResource = v1resource.Resource{ Attributes: []*v1common.KeyValue{{Key: "cloud.provider", Value: &v1common.AnyValue{Value: &v1common.AnyValue_StringValue{StringValue: "aws"}}}, {Key: "cloud.region", Value: &v1common.AnyValue{Value: &v1common.AnyValue_StringValue{StringValue: "us-texas-1"}}}, + {Key: "faas.instance", Value: &v1common.AnyValue{Value: &v1common.AnyValue_StringValue{StringValue: "2023/01/01/[$LATEST]5d1edb9e525d486696cf01a3503487bc"}}}, + {Key: "faas.max_memory", Value: &v1common.AnyValue{Value: &v1common.AnyValue_IntValue{IntValue: 128}}}, {Key: "faas.name", Value: &v1common.AnyValue{Value: &v1common.AnyValue_StringValue{StringValue: "testFunction"}}}, {Key: "faas.version", Value: &v1common.AnyValue{Value: &v1common.AnyValue_StringValue{StringValue: "$LATEST"}}}}, DroppedAttributesCount: 0, @@ -129,11 +133,13 @@ var ( ) func assertResourceEquals(t *testing.T, expected *v1resource.Resource, actual *v1resource.Resource) { - assert.Len(t, actual.Attributes, 4) + assert.Len(t, actual.Attributes, 6) assert.Equal(t, expected.Attributes[0].String(), actual.Attributes[0].String()) assert.Equal(t, expected.Attributes[1].String(), actual.Attributes[1].String()) assert.Equal(t, expected.Attributes[2].String(), actual.Attributes[2].String()) assert.Equal(t, expected.Attributes[3].String(), actual.Attributes[3].String()) + assert.Equal(t, expected.Attributes[4].String(), actual.Attributes[4].String()) + assert.Equal(t, expected.Attributes[5].String(), actual.Attributes[5].String()) assert.Equal(t, expected.DroppedAttributesCount, actual.DroppedAttributesCount) } From 5c51af366261f7bdc82ba8476fbc2befe9b13203 Mon Sep 17 00:00:00 2001 From: Matej Gera Date: Fri, 13 Jan 2023 12:23:10 +0100 Subject: [PATCH 4/5] Adjust documentation Signed-off-by: Matej Gera --- detectors/aws/lambda/README.md | 2 ++ detectors/aws/lambda/detector.go | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/detectors/aws/lambda/README.md b/detectors/aws/lambda/README.md index 0f8f1bbe24e..505b9f8f871 100644 --- a/detectors/aws/lambda/README.md +++ b/detectors/aws/lambda/README.md @@ -46,6 +46,8 @@ Now your `TracerProvider` will have the following resource attributes and attach |`cloud.region` | us-east-1 |`faas.name` | MyLambdaFunction |`faas.version` | $LATEST +|`faas.instance` | 2021/06/28/[$LATEST]2f399eb14537447da05ab2a2e39309de +|`faas.max_memory`| 128 Of note, `faas.id` and `cloud.account.id` are not set by the Lambda resource detector because they are not available outside a Lambda invocation. For this reason, when using the AWS Lambda Instrumentation these attributes are set as additional span attributes. diff --git a/detectors/aws/lambda/detector.go b/detectors/aws/lambda/detector.go index 9b21cd950b4..07cea8f57bd 100644 --- a/detectors/aws/lambda/detector.go +++ b/detectors/aws/lambda/detector.go @@ -61,7 +61,7 @@ func (detector *resourceDetector) Detect(context.Context) (*resource.Resource, e awsRegion := os.Getenv(awsRegionEnvVar) functionVersion := os.Getenv(lambdaFunctionVersionEnvVar) // The instance attributes corresponds to the log stream name for AWS lambda, - // see the FaaS convention for more details. + // see the FaaS resource specification for more details. instance := os.Getenv(lambdaLogStreamNameEnvVar) attrs := []attribute.KeyValue{ From 02a2709e0d8a0fe8b2fde6f87beb1ca4e40c872f Mon Sep 17 00:00:00 2001 From: Matej Gera Date: Fri, 13 Jan 2023 12:27:46 +0100 Subject: [PATCH 5/5] Update CHANGELOG Signed-off-by: Matej Gera --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 226875eba02..a1516d285fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068) - `otelmux`: Add new `WithSpanNameFormatter` option to `go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux` to allow customizing span names. (#3041) +- Add missing recommended AWS Lambda resource attributes `faas.instance` and `faas.max_memory` in `go.opentelemetry.io/contrib/detectors/aws/lambda`. (#2333) ## [1.12.0/0.37.0/0.6.0]