| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Package otelaws provides instrumentation for the AWS SDK. |
| 5 |
package otelaws // import "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws" |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"time" |
| 10 |
|
| 11 |
v2Middleware "github.com/aws/aws-sdk-go-v2/aws/middleware" |
| 12 |
"github.com/aws/smithy-go/middleware" |
| 13 |
smithyhttp "github.com/aws/smithy-go/transport/http" |
| 14 |
"go.opentelemetry.io/otel" |
| 15 |
"go.opentelemetry.io/otel/attribute" |
| 16 |
"go.opentelemetry.io/otel/codes" |
| 17 |
"go.opentelemetry.io/otel/propagation" |
| 18 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 19 |
"go.opentelemetry.io/otel/trace" |
| 20 |
) |
| 21 |
|
| 22 |
const ( |
| 23 |
// ScopeName is the instrumentation scope name. |
| 24 |
ScopeName = "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws" |
| 25 |
) |
| 26 |
|
| 27 |
type spanTimestampKey struct{} |
| 28 |
|
| 29 |
// AttributeBuilder returns an array of KeyValue pairs, it can be used to set custom attributes. |
| 30 |
type AttributeBuilder func(ctx context.Context, in middleware.InitializeInput, out middleware.InitializeOutput) []attribute.KeyValue |
| 31 |
|
| 32 |
type otelMiddlewares struct { |
| 33 |
tracer trace.Tracer |
| 34 |
propagator propagation.TextMapPropagator |
| 35 |
attributeBuilders []AttributeBuilder |
| 36 |
} |
| 37 |
|
| 38 |
func (otelMiddlewares) initializeMiddlewareBefore(stack *middleware.Stack) error { |
| 39 |
return stack.Initialize.Add(middleware.InitializeMiddlewareFunc("OTelInitializeMiddlewareBefore", func( |
| 40 |
ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler) ( |
| 41 |
out middleware.InitializeOutput, metadata middleware.Metadata, err error, |
| 42 |
) { |
| 43 |
ctx = context.WithValue(ctx, spanTimestampKey{}, time.Now()) |
| 44 |
return next.HandleInitialize(ctx, in) |
| 45 |
}), |
| 46 |
middleware.Before) |
| 47 |
} |
| 48 |
|
| 49 |
func (m otelMiddlewares) initializeMiddlewareAfter(stack *middleware.Stack) error { |
| 50 |
return stack.Initialize.Add(middleware.InitializeMiddlewareFunc("OTelInitializeMiddlewareAfter", func( |
| 51 |
ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler) ( |
| 52 |
out middleware.InitializeOutput, metadata middleware.Metadata, err error, |
| 53 |
) { |
| 54 |
serviceID := v2Middleware.GetServiceID(ctx) |
| 55 |
operation := v2Middleware.GetOperationName(ctx) |
| 56 |
region := v2Middleware.GetRegion(ctx) |
| 57 |
|
| 58 |
attributes := []attribute.KeyValue{ |
| 59 |
SystemAttr(), |
| 60 |
MethodAttr(serviceID, operation), |
| 61 |
RegionAttr(region), |
| 62 |
} |
| 63 |
|
| 64 |
ctx, span := m.tracer.Start(ctx, spanName(serviceID, operation), |
| 65 |
trace.WithTimestamp(ctx.Value(spanTimestampKey{}).(time.Time)), |
| 66 |
trace.WithSpanKind(trace.SpanKindClient), |
| 67 |
trace.WithAttributes(attributes...), |
| 68 |
) |
| 69 |
defer span.End() |
| 70 |
|
| 71 |
out, metadata, err = next.HandleInitialize(ctx, in) |
| 72 |
span.SetAttributes(m.buildAttributes(ctx, in, out)...) |
| 73 |
if err != nil { |
| 74 |
span.SetAttributes(semconv.ErrorType(err)) |
| 75 |
span.SetStatus(codes.Error, err.Error()) |
| 76 |
} |
| 77 |
|
| 78 |
return out, metadata, err |
| 79 |
}), |
| 80 |
middleware.After) |
| 81 |
} |
| 82 |
|
| 83 |
func (m otelMiddlewares) finalizeMiddlewareAfter(stack *middleware.Stack) error { |
| 84 |
return stack.Finalize.Add(middleware.FinalizeMiddlewareFunc("OTelFinalizeMiddleware", func( |
| 85 |
ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) ( |
| 86 |
out middleware.FinalizeOutput, metadata middleware.Metadata, err error, |
| 87 |
) { |
| 88 |
// Propagate the Trace information by injecting it into the HTTP request. |
| 89 |
switch req := in.Request.(type) { |
| 90 |
case *smithyhttp.Request: |
| 91 |
m.propagator.Inject(ctx, propagation.HeaderCarrier(req.Header)) |
| 92 |
default: |
| 93 |
} |
| 94 |
|
| 95 |
return next.HandleFinalize(ctx, in) |
| 96 |
}), |
| 97 |
middleware.After) |
| 98 |
} |
| 99 |
|
| 100 |
func (otelMiddlewares) deserializeMiddleware(stack *middleware.Stack) error { |
| 101 |
return stack.Deserialize.Add(middleware.DeserializeMiddlewareFunc("OTelDeserializeMiddleware", func( |
| 102 |
ctx context.Context, in middleware.DeserializeInput, next middleware.DeserializeHandler) ( |
| 103 |
out middleware.DeserializeOutput, metadata middleware.Metadata, err error, |
| 104 |
) { |
| 105 |
out, metadata, err = next.HandleDeserialize(ctx, in) |
| 106 |
resp, ok := out.RawResponse.(*smithyhttp.Response) |
| 107 |
if !ok { |
| 108 |
// No raw response to wrap with. |
| 109 |
return out, metadata, err |
| 110 |
} |
| 111 |
|
| 112 |
span := trace.SpanFromContext(ctx) |
| 113 |
span.SetAttributes(semconv.HTTPResponseStatusCode(resp.StatusCode)) |
| 114 |
|
| 115 |
requestID, ok := v2Middleware.GetRequestIDMetadata(metadata) |
| 116 |
if ok { |
| 117 |
span.SetAttributes(RequestIDAttr(requestID)) |
| 118 |
} |
| 119 |
|
| 120 |
return out, metadata, err |
| 121 |
}), |
| 122 |
middleware.Before) |
| 123 |
} |
| 124 |
|
| 125 |
func (m otelMiddlewares) buildAttributes(ctx context.Context, in middleware.InitializeInput, out middleware.InitializeOutput) (attributes []attribute.KeyValue) { |
| 126 |
for _, builder := range m.attributeBuilders { |
| 127 |
attributes = append(attributes, builder(ctx, in, out)...) |
| 128 |
} |
| 129 |
|
| 130 |
return attributes |
| 131 |
} |
| 132 |
|
| 133 |
func spanName(serviceID, operation string) string { |
| 134 |
spanName := serviceID |
| 135 |
if operation != "" { |
| 136 |
spanName += "." + operation |
| 137 |
} |
| 138 |
return spanName |
| 139 |
} |
| 140 |
|
| 141 |
// AppendMiddlewares attaches OTel middlewares to the AWS Go SDK V2 for instrumentation. |
| 142 |
// OTel middlewares can be appended to either all aws clients or a specific operation. |
| 143 |
// Please see more details in https://aws.github.io/aws-sdk-go-v2/docs/middleware/ |
| 144 |
func AppendMiddlewares(apiOptions *[]func(*middleware.Stack) error, opts ...Option) { |
| 145 |
cfg := config{ |
| 146 |
TracerProvider: otel.GetTracerProvider(), |
| 147 |
TextMapPropagator: otel.GetTextMapPropagator(), |
| 148 |
} |
| 149 |
for _, opt := range opts { |
| 150 |
opt.apply(&cfg) |
| 151 |
} |
| 152 |
|
| 153 |
if cfg.AttributeBuilders == nil { |
| 154 |
cfg.AttributeBuilders = []AttributeBuilder{DefaultAttributeBuilder} |
| 155 |
} |
| 156 |
|
| 157 |
m := otelMiddlewares{ |
| 158 |
tracer: cfg.TracerProvider.Tracer(ScopeName, |
| 159 |
trace.WithInstrumentationVersion(Version)), |
| 160 |
propagator: cfg.TextMapPropagator, |
| 161 |
attributeBuilders: cfg.AttributeBuilders, |
| 162 |
} |
| 163 |
*apiOptions = append(*apiOptions, m.initializeMiddlewareBefore, m.initializeMiddlewareAfter, m.finalizeMiddlewareAfter, m.deserializeMiddleware) |
| 164 |
} |