Skip to content

Commit

Permalink
fix aws sdk span name (#3582)
Browse files Browse the repository at this point in the history
* fix aws sdk span name

* fix aws sdk span name

* Update CHANGELOG.md

Co-authored-by: Robert Pająk <[email protected]>

* add empty operation handling

* Update CHANGELOG.md

Co-authored-by: Tyler Yahn <[email protected]>

---------

Co-authored-by: Chester Cheung <[email protected]>
Co-authored-by: Robert Pająk <[email protected]>
Co-authored-by: Tyler Yahn <[email protected]>
  • Loading branch information
4 people authored Mar 20, 2023
1 parent aead737 commit b784471
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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...),
Expand Down Expand Up @@ -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/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit b784471

Please sign in to comment.