Skip to content

Commit

Permalink
Add missing recommended resource attributes to AWS Lambda detector (#…
Browse files Browse the repository at this point in the history
…3148)

* Add missing recommended attributes

- faas.instance (corresponds to log stream name)
- faas.max_memory

Signed-off-by: Matej Gera <[email protected]>

* Adjust Lambda tests

Signed-off-by: Matej Gera <[email protected]>

* Adjust Xrayconfig test

Signed-off-by: Matej Gera <[email protected]>

* Adjust documentation

Signed-off-by: Matej Gera <[email protected]>

* Update CHANGELOG

Signed-off-by: Matej Gera <[email protected]>

Signed-off-by: Matej Gera <[email protected]>
  • Loading branch information
matej-g authored Jan 20, 2023
1 parent 4b49f06 commit 4dbdb21
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`. (#3148)
- Improve documentation for `samplers/jaegerremote` by providing examples of sampling endpoints. (#3147)

## [1.12.0/0.37.0/0.6.0]
Expand Down
2 changes: 2 additions & 0 deletions detectors/aws/lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
13 changes: 13 additions & 0 deletions detectors/aws/lambda/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"os"
"strconv"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
Expand All @@ -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 (
Expand Down Expand Up @@ -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 resource specification 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
}
4 changes: 4 additions & 0 deletions detectors/aws/lambda/detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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()},
}
)
Expand Down Expand Up @@ -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()},
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}

Expand Down

0 comments on commit 4dbdb21

Please sign in to comment.