| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otelaws // import "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
|
| 9 |
v2Middleware "github.com/aws/aws-sdk-go-v2/aws/middleware" |
| 10 |
"github.com/aws/aws-sdk-go-v2/service/dynamodb" |
| 11 |
"github.com/aws/aws-sdk-go-v2/service/sns" |
| 12 |
"github.com/aws/aws-sdk-go-v2/service/sqs" |
| 13 |
"github.com/aws/smithy-go/middleware" |
| 14 |
"go.opentelemetry.io/otel/attribute" |
| 15 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 16 |
) |
| 17 |
|
| 18 |
// AWS attributes. |
| 19 |
const ( |
| 20 |
RegionKey attribute.Key = "aws.region" |
| 21 |
RequestIDKey attribute.Key = "aws.request_id" |
| 22 |
AWSSystemVal string = "aws-api" |
| 23 |
) |
| 24 |
|
| 25 |
var servicemap = map[string]AttributeBuilder{ |
| 26 |
dynamodb.ServiceID: DynamoDBAttributeBuilder, |
| 27 |
sqs.ServiceID: SQSAttributeBuilder, |
| 28 |
sns.ServiceID: SNSAttributeBuilder, |
| 29 |
} |
| 30 |
|
| 31 |
// SystemAttr return the AWS RPC system attribute. |
| 32 |
func SystemAttr() attribute.KeyValue { |
| 33 |
return semconv.RPCSystemNameKey.String(AWSSystemVal) |
| 34 |
} |
| 35 |
|
| 36 |
// MethodAttr returns the RPC method attribute for the AWS service and operation. |
| 37 |
func MethodAttr(service, operation string) attribute.KeyValue { |
| 38 |
if service == "" { |
| 39 |
return semconv.RPCMethod(operation) |
| 40 |
} |
| 41 |
if operation == "" { |
| 42 |
return semconv.RPCMethod(service) |
| 43 |
} |
| 44 |
return semconv.RPCMethod(service + "/" + operation) |
| 45 |
} |
| 46 |
|
| 47 |
// OperationAttr returns the AWS operation attribute. |
| 48 |
// |
| 49 |
// Deprecated: use [MethodAttr] instead. |
| 50 |
func OperationAttr(operation string) attribute.KeyValue { |
| 51 |
// rpc.service has been merged into rpc.method in semconv v1.39.0 |
| 52 |
return MethodAttr("", operation) |
| 53 |
} |
| 54 |
|
| 55 |
// RegionAttr returns the AWS region attribute. |
| 56 |
func RegionAttr(region string) attribute.KeyValue { |
| 57 |
return RegionKey.String(region) |
| 58 |
} |
| 59 |
|
| 60 |
// ServiceAttr returns the AWS service attribute. |
| 61 |
// |
| 62 |
// Deprecated: use [MethodAttr] instead. |
| 63 |
func ServiceAttr(service string) attribute.KeyValue { |
| 64 |
// rpc.service has been merged into rpc.method in semconv v1.39.0 |
| 65 |
return MethodAttr(service, "") |
| 66 |
} |
| 67 |
|
| 68 |
// RequestIDAttr returns the AWS request ID attribute. |
| 69 |
func RequestIDAttr(requestID string) attribute.KeyValue { |
| 70 |
return RequestIDKey.String(requestID) |
| 71 |
} |
| 72 |
|
| 73 |
// DefaultAttributeBuilder checks to see if there are service specific attributes available to set for the AWS service. |
| 74 |
// If there are service specific attributes available then they will be included. |
| 75 |
func DefaultAttributeBuilder(ctx context.Context, in middleware.InitializeInput, out middleware.InitializeOutput) []attribute.KeyValue { |
| 76 |
serviceID := v2Middleware.GetServiceID(ctx) |
| 77 |
|
| 78 |
if fn, ok := servicemap[serviceID]; ok { |
| 79 |
return fn(ctx, in, out) |
| 80 |
} |
| 81 |
|
| 82 |
return []attribute.KeyValue{} |
| 83 |
} |