From b784471d7e7e58ab6663472efbc4953e122fa2a7 Mon Sep 17 00:00:00 2001 From: Nimrod Shlagman Date: Mon, 20 Mar 2023 16:39:24 +0200 Subject: [PATCH] fix aws sdk span name (#3582) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix aws sdk span name * fix aws sdk span name * Update CHANGELOG.md Co-authored-by: Robert Pająk * add empty operation handling * Update CHANGELOG.md Co-authored-by: Tyler Yahn --------- Co-authored-by: Chester Cheung Co-authored-by: Robert Pająk Co-authored-by: Tyler Yahn --- CHANGELOG.md | 4 ++++ .../github.com/aws/aws-sdk-go-v2/otelaws/aws.go | 16 +++++++++++++--- .../aws/aws-sdk-go-v2/otelaws/aws_test.go | 12 ++++++++++++ .../aws/aws-sdk-go-v2/otelaws/test/aws_test.go | 2 +- .../otelaws/test/dynamodbattributes_test.go | 4 ++-- 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70b5097705a..4361ef53fa5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,10 @@ 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) - The `WithPublicEndpoint` and `WithPublicEndpointFn` options in `go.opentelemetry.io/contrib/instrumentation/github.com/emicklei/go-restful/otelrestful`. (#3563) +### Fixed + +- - AWS SDK span name to be of the format `Service.Operation` in `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws`. (#3582, #3521) + ## [1.16.0-rc.1/0.41.0-rc.1/0.9.0-rc.1] - 2023-03-02 ### Changed diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/aws.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/aws.go index ff8e51b4b93..4bb01253b87 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/aws.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/aws.go @@ -60,17 +60,19 @@ func (m otelMiddlewares) initializeMiddlewareAfter(stack *middleware.Stack) erro ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler) ( out middleware.InitializeOutput, metadata middleware.Metadata, err error) { serviceID := v2Middleware.GetServiceID(ctx) + operation := v2Middleware.GetOperationName(ctx) + region := v2Middleware.GetRegion(ctx) attributes := []attribute.KeyValue{ ServiceAttr(serviceID), - RegionAttr(v2Middleware.GetRegion(ctx)), - OperationAttr(v2Middleware.GetOperationName(ctx)), + RegionAttr(region), + OperationAttr(operation), } for _, setter := range m.attributeSetter { attributes = append(attributes, setter(ctx, in)...) } - ctx, span := m.tracer.Start(ctx, serviceID, + ctx, span := m.tracer.Start(ctx, spanName(serviceID, operation), trace.WithTimestamp(ctx.Value(spanTimestampKey{}).(time.Time)), trace.WithSpanKind(trace.SpanKindClient), trace.WithAttributes(attributes...), @@ -128,6 +130,14 @@ func (m otelMiddlewares) finalizeMiddleware(stack *middleware.Stack) error { middleware.Before) } +func spanName(serviceID, operation string) string { + spanName := serviceID + if operation != "" { + spanName += "." + operation + } + return spanName +} + // AppendMiddlewares attaches OTel middlewares to the AWS Go SDK V2 for instrumentation. // OTel middlewares can be appended to either all aws clients or a specific operation. // Please see more details in https://aws.github.io/aws-sdk-go-v2/docs/middleware/ diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/aws_test.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/aws_test.go index c4df6c6cd5b..0d83dde6f58 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/aws_test.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/aws_test.go @@ -78,3 +78,15 @@ func Test_otelMiddlewares_finalizeMiddleware(t *testing.T) { assert.Contains(t, input.Header, key) assert.Contains(t, input.Header[key], value) } + +func Test_Span_name(t *testing.T) { + serviceID1 := "" + serviceID2 := "ServiceID" + operation1 := "" + operation2 := "Operation" + + assert.Equal(t, spanName(serviceID1, operation1), "") + assert.Equal(t, spanName(serviceID1, operation2), "."+operation2) + assert.Equal(t, spanName(serviceID2, operation1), serviceID2) + assert.Equal(t, spanName(serviceID2, operation2), serviceID2+"."+operation2) +} diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/aws_test.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/aws_test.go index aaa6ec64db3..382e607e6a5 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/aws_test.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/aws_test.go @@ -138,7 +138,7 @@ func TestAppendMiddlewares(t *testing.T) { require.Len(t, spans, 1) span := spans[0] - assert.Equal(t, "Route 53", span.Name()) + assert.Equal(t, "Route 53.ChangeResourceRecordSets", span.Name()) assert.Equal(t, trace.SpanKindClient, span.SpanKind()) assert.Equal(t, c.expectedError, span.Status().Code) attrs := span.Attributes() diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/dynamodbattributes_test.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/dynamodbattributes_test.go index 4a578455477..7660499e733 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/dynamodbattributes_test.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/dynamodbattributes_test.go @@ -93,7 +93,7 @@ func TestDynamodbTags(t *testing.T) { require.Len(t, spans, 1) span := spans[0] - assert.Equal(t, "DynamoDB", span.Name()) + assert.Equal(t, "DynamoDB.GetItem", span.Name()) assert.Equal(t, trace.SpanKindClient, span.SpanKind()) attrs := span.Attributes() assert.Contains(t, attrs, attribute.Int("http.status_code", cases.expectedStatusCode)) @@ -182,7 +182,7 @@ func TestDynamodbTagsCustomSetter(t *testing.T) { require.Len(t, spans, 1) span := spans[0] - assert.Equal(t, "DynamoDB", span.Name()) + assert.Equal(t, "DynamoDB.GetItem", span.Name()) assert.Equal(t, trace.SpanKindClient, span.SpanKind()) attrs := span.Attributes() assert.Contains(t, attrs, attribute.Int("http.status_code", cases.expectedStatusCode))