From ff18891beee63e53c7c3b830017e7e3bdc3e4eba Mon Sep 17 00:00:00 2001 From: Jenny Nilsen Date: Wed, 12 Jan 2022 17:17:52 +0000 Subject: [PATCH 01/11] adding dynamodb attributes --- CHANGELOG.md | 5 +- .../aws/aws-sdk-go-v2/otelaws/attributes.go | 24 +- .../aws/aws-sdk-go-v2/otelaws/aws.go | 29 +- .../aws/aws-sdk-go-v2/otelaws/config.go | 14 +- .../otelaws/dynamodbattributes.go | 188 ++++++++ .../aws/aws-sdk-go-v2/otelaws/go.mod | 1 + .../aws/aws-sdk-go-v2/otelaws/go.sum | 15 + .../otelaws/test/dynamodbattributes_test.go | 409 ++++++++++++++++++ .../aws/aws-sdk-go-v2/otelaws/test/go.mod | 2 + .../aws/aws-sdk-go-v2/otelaws/test/go.sum | 10 + 10 files changed, 688 insertions(+), 9 deletions(-) create mode 100644 instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go create mode 100644 instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/dynamodbattributes_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d5da8a5163..180326c5eb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] ### Added +- Dynamodb spans will now have the appropriate attributes added for the operation being performed, this is detected automatically but it is also now possible to provide a custom function to set attributes using `WithAttributeSetter` - Add `WithClientTrace` option to `otelhttp.Transport` (#875) @@ -35,12 +36,12 @@ We have updated the project minimum supported Go version to 1.16 - `otelhttptrace.NewClientTrace` now uses `TracerProvider` from the parent context if one exists and none was set with `WithTracerProvider` (#874) ### Fixed - - The `"go.opentelemetry.io/contrib/detector/aws/ecs".Detector` no longer errors if not running in ECS. (#1428) - `go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux` does not require instrumented HTTP handlers to call `Write` nor `WriteHeader` anymore. (#1443) + ## [1.2.0/0.27.0] - 2021-11-15 ### Changed @@ -73,6 +74,8 @@ Update dependency on the `go.opentelemetry.io/otel` project to `v1.1.0`. - Add `WithTracerProvider` option for `otelhttptrace.NewClientTrace`. (#1128) - Add optional AWS X-Ray configuration module for AWS Lambda Instrumentation. (#984) + + ### Fixed - The `go.opentelemetry.io/contrib/propagators/ot` propagator returns the words `true` or `false` for the `ot-tracer-sampled` header instead of numerical `0` and `1`. (#1358) diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/attributes.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/attributes.go index 34290232fcf..8e166116e35 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/attributes.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/attributes.go @@ -14,7 +14,13 @@ package otelaws -import "go.opentelemetry.io/otel/attribute" +import ( + "context" + + v2Middleware "github.com/aws/aws-sdk-go-v2/aws/middleware" + "github.com/aws/smithy-go/middleware" + "go.opentelemetry.io/otel/attribute" +) // AWS attributes. const ( @@ -39,3 +45,19 @@ func ServiceAttr(service string) attribute.KeyValue { func RequestIDAttr(requestID string) attribute.KeyValue { return RequestIDKey.String(requestID) } + +func Defaultattributesetter(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue { + servicemap := map[string]attributesetter{ + "dynamodb": DynamodbAttributeSetter, + } + + serviceID := v2Middleware.GetServiceID(ctx) + + if val, ok := servicemap[serviceID]; ok { + function := val + attributes := function(ctx, in) + return attributes + } + + return []attribute.KeyValue{} +} 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 100936781ee..bce8338ab78 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 @@ -23,6 +23,7 @@ import ( smithyhttp "github.com/aws/smithy-go/transport/http" "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" semconv "go.opentelemetry.io/otel/semconv/v1.7.0" "go.opentelemetry.io/otel/trace" @@ -34,8 +35,11 @@ const ( type spanTimestampKey struct{} +type attributesetter func(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue + type otelMiddlewares struct { - tracer trace.Tracer + tracer trace.Tracer + attributesetter attributesetter } func (m otelMiddlewares) initializeMiddlewareBefore(stack *middleware.Stack) error { @@ -55,12 +59,22 @@ func (m otelMiddlewares) initializeMiddlewareAfter(stack *middleware.Stack) erro out middleware.InitializeOutput, metadata middleware.Metadata, err error) { serviceID := v2Middleware.GetServiceID(ctx) + + attributes := []attribute.KeyValue{ServiceAttr(serviceID), + RegionAttr(v2Middleware.GetRegion(ctx)), + OperationAttr(v2Middleware.GetOperationName(ctx)), + } + if m.attributesetter != nil { + attributes = append( + attributes, + m.attributesetter(ctx, in)..., + ) + } + ctx, span := m.tracer.Start(ctx, serviceID, trace.WithTimestamp(ctx.Value(spanTimestampKey{}).(time.Time)), trace.WithSpanKind(trace.SpanKindClient), - trace.WithAttributes(ServiceAttr(serviceID), - RegionAttr(v2Middleware.GetRegion(ctx)), - OperationAttr(v2Middleware.GetOperationName(ctx))), + trace.WithAttributes(attributes...), ) defer span.End() @@ -104,13 +118,16 @@ func (m otelMiddlewares) deserializeMiddleware(stack *middleware.Stack) error { // Please see more details in https://aws.github.io/aws-sdk-go-v2/docs/middleware/ func AppendMiddlewares(apiOptions *[]func(*middleware.Stack) error, opts ...Option) { cfg := config{ - TracerProvider: otel.GetTracerProvider(), + TracerProvider: otel.GetTracerProvider(), + AttributeSetter: Defaultattributesetter, } for _, opt := range opts { opt.apply(&cfg) } m := otelMiddlewares{tracer: cfg.TracerProvider.Tracer(tracerName, - trace.WithInstrumentationVersion(SemVersion()))} + trace.WithInstrumentationVersion(SemVersion())), + attributesetter: cfg.AttributeSetter} *apiOptions = append(*apiOptions, m.initializeMiddlewareBefore, m.initializeMiddlewareAfter, m.deserializeMiddleware) + } diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go index 3d027763ab3..5c9e19589bd 100755 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go @@ -19,7 +19,8 @@ import ( ) type config struct { - TracerProvider trace.TracerProvider + TracerProvider trace.TracerProvider + AttributeSetter attributesetter } // Option applies an option value. @@ -44,3 +45,14 @@ func WithTracerProvider(provider trace.TracerProvider) Option { } }) } + +// WithAttributeSetter specifies an attribute setter function for setting service specific attributes. +// If none is specified, the service will be determined by the Defaultattributesetter function and the corresponding attributes will be included. +func WithAttributeSetter(attributesetter attributesetter) Option { + + return optionFunc(func(cfg *config) { + if attributesetter != nil { + cfg.AttributeSetter = attributesetter + } + }) +} diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go new file mode 100644 index 00000000000..35dabd203c4 --- /dev/null +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go @@ -0,0 +1,188 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package otelaws + +import ( + "context" + "encoding/json" + + "github.com/aws/aws-sdk-go-v2/service/dynamodb" + "github.com/aws/smithy-go/middleware" + + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/semconv/v1.7.0" +) + +func DynamodbAttributeSetter(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue { + + dynamodbAttributes := []attribute.KeyValue{ + { + Key: semconv.DBSystemKey, + Value: attribute.StringValue("dynamodb"), + }, + } + + switch v := in.Parameters.(type) { + case *dynamodb.GetItemInput: + + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) + + if v.ConsistentRead != nil { + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBConsistentReadKey.Bool(*v.ConsistentRead)) + } + + if v.ProjectionExpression != nil { + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProjectionKey.String(*v.ProjectionExpression)) + } + + case *dynamodb.BatchGetItemInput: + var table_names []string + for k, _ := range v.RequestItems { + table_names = append(table_names, k) + } + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.StringSlice(table_names)) + + case *dynamodb.BatchWriteItemInput: + var table_names []string + for k, _ := range v.RequestItems { + table_names = append(table_names, k) + } + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.StringSlice(table_names)) + + case *dynamodb.CreateTableInput: + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) + + if v.GlobalSecondaryIndexes != nil { + globalindexes, _ := json.Marshal(v.GlobalSecondaryIndexes) + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBGlobalSecondaryIndexesKey.String(string(globalindexes))) + } + + if v.LocalSecondaryIndexes != nil { + localindexes, _ := json.Marshal(v.LocalSecondaryIndexes) + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBLocalSecondaryIndexesKey.String(string(localindexes))) + } + + if v.ProvisionedThroughput != nil { + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProvisionedReadCapacityKey.Int64(*v.ProvisionedThroughput.ReadCapacityUnits)) + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProvisionedWriteCapacityKey.Int64(*v.ProvisionedThroughput.WriteCapacityUnits)) + } + + case *dynamodb.DeleteItemInput: + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) + + case *dynamodb.DeleteTableInput: + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) + + case *dynamodb.DescribeTableInput: + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) + + case *dynamodb.ListTablesInput: + + if v.ExclusiveStartTableName != nil { + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBExclusiveStartTableKey.String(*v.ExclusiveStartTableName)) + } + + if v.Limit != nil { + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBLimitKey.Int(int(*v.Limit))) + } + + case *dynamodb.PutItemInput: + + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) + + case *dynamodb.QueryInput: + + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) + + if v.ConsistentRead != nil { + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBConsistentReadKey.Bool(*v.ConsistentRead)) + } + + if v.IndexName != nil { + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBIndexNameKey.String(*v.IndexName)) + } + + if v.Limit != nil { + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBLimitKey.Int(int(*v.Limit))) + } + + if v.ScanIndexForward != nil { + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBScanForwardKey.Bool(*v.ScanIndexForward)) + } + + if v.ProjectionExpression != nil { + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProjectionKey.String(*v.ProjectionExpression)) + } + + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBSelectKey.String(string(v.Select))) + + case *dynamodb.ScanInput: + + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) + + if v.ConsistentRead != nil { + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBConsistentReadKey.Bool(*v.ConsistentRead)) + } + + if v.IndexName != nil { + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBIndexNameKey.String(*v.IndexName)) + } + + if v.Limit != nil { + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBLimitKey.Int(int(*v.Limit))) + } + + if v.ProjectionExpression != nil { + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProjectionKey.String(*v.ProjectionExpression)) + } + + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBSelectKey.String(string(v.Select))) + + if v.Segment != nil { + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBSegmentKey.Int(int(*v.Segment))) + } + + if v.TotalSegments != nil { + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTotalSegmentsKey.Int(int(*v.TotalSegments))) + } + + case *dynamodb.UpdateItemInput: + + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) + + case *dynamodb.UpdateTableInput: + + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) + + if v.AttributeDefinitions != nil { + attributedefinitions, _ := json.Marshal(v.AttributeDefinitions) + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBAttributeDefinitionsKey.String(string(attributedefinitions))) + } + + if v.GlobalSecondaryIndexUpdates != nil { + globalsecondaryindexupdates, _ := json.Marshal(v.GlobalSecondaryIndexUpdates) + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBGlobalSecondaryIndexUpdatesKey.String(string(globalsecondaryindexupdates))) + } + + if v.ProvisionedThroughput != nil { + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProvisionedReadCapacityKey.Int64(*v.ProvisionedThroughput.ReadCapacityUnits)) + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProvisionedWriteCapacityKey.Int64(*v.ProvisionedThroughput.WriteCapacityUnits)) + } + + } + + return dynamodbAttributes + +} diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/go.mod b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/go.mod index 20c5ce247ef..e1f6d111b0a 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/go.mod +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/go.mod @@ -6,6 +6,7 @@ replace go.opentelemetry.io/contrib => ../../../../../ require ( github.com/aws/aws-sdk-go-v2 v1.12.0 + github.com/aws/aws-sdk-go-v2/service/dynamodb v1.11.0 github.com/aws/smithy-go v1.9.1 go.opentelemetry.io/otel v1.3.0 go.opentelemetry.io/otel/trace v1.3.0 diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/go.sum b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/go.sum index 77c8a65860c..f9fe10543e5 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/go.sum +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/go.sum @@ -1,5 +1,17 @@ +github.com/aws/aws-sdk-go-v2 v1.11.2/go.mod h1:SQfA+m2ltnu1cA0soUkj4dRSsmITiVQUJvBIZjzfPyQ= github.com/aws/aws-sdk-go-v2 v1.12.0 h1:z5bijqy+eXLK/QqF6eQcwCN2qw1k+m9OUDicqCZygu0= github.com/aws/aws-sdk-go-v2 v1.12.0/go.mod h1:tWhQI5N5SiMawto3uMAQJU5OUN/1ivhDDHq7HTsJvZ0= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.2 h1:XJLnluKuUxQG255zPNe+04izXl7GSyUVafIsgfv9aw4= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.2/go.mod h1:SgKKNBIoDC/E1ZCDhhMW3yalWjwuLjMcpLzsM/QQnWo= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.0.2 h1:EauRoYZVNPlidZSZJDscjJBQ22JhVF2+tdteatax2Ak= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.0.2/go.mod h1:xT4XX6w5Sa3dhg50JrYyy3e4WPYo/+WjY/BXtqXVunU= +github.com/aws/aws-sdk-go-v2/service/dynamodb v1.11.0 h1:te+nIFwPf5Bi/cZvd9g/+EF0gkJT3c0J/5+NMx0NBZg= +github.com/aws/aws-sdk-go-v2/service/dynamodb v1.11.0/go.mod h1:ELltfl9ri0n4sZ/VjPZBgemNMd9mYIpCAuZhc7NP7l4= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.5.0 h1:lPLbw4Gn59uoKqvOfSnkJr54XWk5Ak1NK20ZEiSWb3U= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.5.0/go.mod h1:80NaCIH9YU3rzTTs/J/ECATjXuRqzo/wB6ukO6MZ0XY= +github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.3.3 h1:ru9+IpkVIuDvIkm9Q0DEjtWHnh6ITDoZo8fH2dIjlqQ= +github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.3.3/go.mod h1:zOyLMYyg60yyZpOCniAUuibWVqTU4TuLmMa/Wh4P+HA= +github.com/aws/smithy-go v1.9.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= github.com/aws/smithy-go v1.9.1 h1:5vetTooLk4hPWV8q6ym6+lXKAT1Urnm49YkrRKo2J8o= github.com/aws/smithy-go v1.9.1/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= @@ -12,7 +24,9 @@ github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jT github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -26,6 +40,7 @@ go.opentelemetry.io/otel/trace v1.3.0/go.mod h1:c/VDhno8888bvQYmbYLqe41/Ldmr/KKu golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 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 new file mode 100644 index 00000000000..9fa782892a8 --- /dev/null +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/dynamodbattributes_test.go @@ -0,0 +1,409 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package test + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/dynamodb" + dtypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/codes" + sdktrace "go.opentelemetry.io/otel/sdk/trace" + "go.opentelemetry.io/otel/sdk/trace/tracetest" + "go.opentelemetry.io/otel/trace" + + "github.com/aws/smithy-go/middleware" +) + +func TestDynamodbTags(t *testing.T) { + cases := struct { + responseStatus int + expectedRegion string + expectedStatusCode int + expectedError codes.Code + }{ + responseStatus: 200, + expectedRegion: "us-west-2", + expectedStatusCode: 200, + } + + server := httptest.NewServer(http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(cases.responseStatus) + })) + defer server.Close() + + t.Run("dynamodb tags", func(t *testing.T) { + sr := tracetest.NewSpanRecorder() + provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) + + svc := dynamodb.NewFromConfig(aws.Config{ + Region: cases.expectedRegion, + EndpointResolverWithOptions: aws.EndpointResolverWithOptionsFunc( + func(service, region string, _ ...interface{}) (aws.Endpoint, error) { + return aws.Endpoint{ + URL: server.URL, + SigningName: "dynamodb", + }, nil + }, + ), + Retryer: func() aws.Retryer { + return aws.NopRetryer{} + }, + }) + _, err := svc.GetItem(context.Background(), &dynamodb.GetItemInput{ + TableName: aws.String("table1"), + ConsistentRead: aws.Bool(false), + ProjectionExpression: aws.String("test"), + Key: map[string]dtypes.AttributeValue{ + "id": &dtypes.AttributeValueMemberS{Value: "test"}, + }, + }, func(options *dynamodb.Options) { + otelaws.AppendMiddlewares( + &options.APIOptions, otelaws.WithAttributeSetter(otelaws.DynamodbAttributeSetter), otelaws.WithTracerProvider(provider)) + }) + + if cases.expectedError == codes.Unset { + assert.NoError(t, err) + } else { + assert.NotNil(t, err) + } + + spans := sr.Ended() + require.Len(t, spans, 1) + span := spans[0] + + assert.Equal(t, "DynamoDB", span.Name()) + assert.Equal(t, trace.SpanKindClient, span.SpanKind()) + attrs := span.Attributes() + assert.Contains(t, attrs, attribute.Int("http.status_code", cases.expectedStatusCode)) + assert.Contains(t, attrs, attribute.String("aws.service", "DynamoDB")) + assert.Contains(t, attrs, attribute.String("aws.region", cases.expectedRegion)) + assert.Contains(t, attrs, attribute.String("aws.operation", "GetItem")) + assert.Contains(t, attrs, attribute.String("aws.dynamodb.table_names", "table1")) + assert.Contains(t, attrs, attribute.String("aws.dynamodb.projection", "test")) + assert.Contains(t, attrs, attribute.Bool("aws.dynamodb.consistent_read", false)) + + }) + + +} + +func TestDynamodbTagsBatchGetItemInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.BatchGetItemInput{ + RequestItems: map[string]dtypes.KeysAndAttributes{ + "table1": { + Keys: []map[string]dtypes.AttributeValue{ + { + "id": &dtypes.AttributeValueMemberS{Value: "123"}, + }, + }, + }, + }, + }, + } + + attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.StringSlice("aws.dynamodb.table_names", []string{"table1"})) +} + +func TestDynamodbTagsBatchWriteItemInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.BatchWriteItemInput{ + RequestItems: map[string][]dtypes.WriteRequest{ + "table1": { + { + DeleteRequest: &dtypes.DeleteRequest{ + Key: map[string]dtypes.AttributeValue{ + "id": &dtypes.AttributeValueMemberS{Value: "123"}, + }, + }, + }, + { + PutRequest: &dtypes.PutRequest{ + Item: map[string]dtypes.AttributeValue{ + "id": &dtypes.AttributeValueMemberS{Value: "234"}, + }, + }, + }, + }, + }, + }, + } + + attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.StringSlice("aws.dynamodb.table_names", []string{"table1"})) +} + +func TestDynamodbTagsCreateTableInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.CreateTableInput{ + AttributeDefinitions: []dtypes.AttributeDefinition{ + { + AttributeName: aws.String("id"), + AttributeType: dtypes.ScalarAttributeTypeS, + }, + }, + KeySchema: []dtypes.KeySchemaElement{ + { + AttributeName: aws.String("id"), + KeyType: dtypes.KeyTypeHash, + }, + }, + TableName: aws.String("table1"), + BillingMode: dtypes.BillingModePayPerRequest, + ProvisionedThroughput: &dtypes.ProvisionedThroughput{ + ReadCapacityUnits: aws.Int64(123), + WriteCapacityUnits: aws.Int64(456), + }, + GlobalSecondaryIndexes: []dtypes.GlobalSecondaryIndex{ + { + IndexName: aws.String("index1"), + KeySchema: []dtypes.KeySchemaElement{ + { + AttributeName: aws.String("attributename"), + KeyType: dtypes.KeyTypeHash, + }, + }, + Projection: &dtypes.Projection{ + NonKeyAttributes: []string{"non-key-attributes"}, + }, + }, + }, + LocalSecondaryIndexes: []dtypes.LocalSecondaryIndex{ + { + IndexName: aws.String("index2"), + KeySchema: []dtypes.KeySchemaElement{ + { + AttributeName: aws.String("attributename"), + KeyType: dtypes.KeyTypeHash, + }, + }, + }, + }, + }, + } + + attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.global_secondary_indexes", "[{\"IndexName\":\"index1\",\"KeySchema\":[{\"AttributeName\":\"attributename\",\"KeyType\":\"HASH\"}],\"Projection\":{\"NonKeyAttributes\":[\"non-key-attributes\"],\"ProjectionType\":\"\"},\"ProvisionedThroughput\":null}]")) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.local_secondary_indexes", "[{\"IndexName\":\"index2\",\"KeySchema\":[{\"AttributeName\":\"attributename\",\"KeyType\":\"HASH\"}],\"Projection\":null}]")) + assert.Contains(t, attributes, attribute.Int("aws.dynamodb.provisioned_read_capacity", 123)) + assert.Contains(t, attributes, attribute.Int("aws.dynamodb.provisioned_write_capacity", 456)) +} + +func TestDynamodbTagsDeleteItemInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.DeleteItemInput{ + Key: map[string]dtypes.AttributeValue{ + "id": &dtypes.AttributeValueMemberS{Value: "123"}, + }, + TableName: aws.String("table1"), + }, + } + attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) +} + +func TestDynamodbTagsDeleteTableInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.DeleteTableInput{ + TableName: aws.String("table1"), + }, + } + attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) + +} + +func TestDynamodbTagsDescribeTableInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.DescribeTableInput{ + TableName: aws.String("table1"), + }, + } + attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) +} + +func TestDynamodbTagsListTablesInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.ListTablesInput{ + ExclusiveStartTableName: aws.String("table1"), + Limit: aws.Int32(10), + }, + } + attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.exclusive_start_table", "table1")) + assert.Contains(t, attributes, attribute.Int("aws.dynamodb.limit", 10)) + +} + +func TestDynamodbTagsPutItemInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.PutItemInput{ + TableName: aws.String("table1"), + Item: map[string]dtypes.AttributeValue{ + "id": &dtypes.AttributeValueMemberS{Value: "12346"}, + "name": &dtypes.AttributeValueMemberS{Value: "John Doe"}, + "email": &dtypes.AttributeValueMemberS{Value: "john@doe.io"}, + }, + }, + } + + attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) +} + +func TestDynamodbTagsQueryInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.QueryInput{ + TableName: aws.String("table1"), + IndexName: aws.String("index1"), + ConsistentRead: aws.Bool(true), + Limit: aws.Int32(10), + ScanIndexForward: aws.Bool(true), + ProjectionExpression: aws.String("projectionexpression"), + Select: dtypes.SelectAllAttributes, + KeyConditionExpression: aws.String("id = :hashKey and #date > :rangeKey"), + ExpressionAttributeNames: map[string]string{ + "#date": "date", + }, + ExpressionAttributeValues: map[string]dtypes.AttributeValue{ + ":hashKey": &dtypes.AttributeValueMemberS{Value: "123"}, + ":rangeKey": &dtypes.AttributeValueMemberN{Value: "20150101"}, + }, + }, + } + + attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) + assert.Contains(t, attributes, attribute.Bool("aws.dynamodb.consistent_read", true)) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.index_name", "index1")) + assert.Contains(t, attributes, attribute.Int("aws.dynamodb.limit", 10)) + assert.Contains(t, attributes, attribute.Bool("aws.dynamodb.scan_forward", true)) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.projection", "projectionexpression")) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.select", "ALL_ATTRIBUTES")) + +} + +func TestDynamodbTagsScanInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.ScanInput{ + TableName: aws.String("my-table"), + ConsistentRead: aws.Bool(true), + IndexName: aws.String("index1"), + Limit: aws.Int32(10), + ProjectionExpression: aws.String("Artist, Genre"), + Segment: aws.Int32(10), + TotalSegments: aws.Int32(100), + Select: dtypes.SelectAllAttributes, + }, + } + + attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "my-table")) + assert.Contains(t, attributes, attribute.Bool("aws.dynamodb.consistent_read", true)) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.index_name", "index1")) + assert.Contains(t, attributes, attribute.Int("aws.dynamodb.limit", 10)) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.select", "ALL_ATTRIBUTES")) + assert.Contains(t, attributes, attribute.Int("aws.dynamodb.total_segments", 100)) + assert.Contains(t, attributes, attribute.Int("aws.dynamodb.segment", 10)) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.projection", "Artist, Genre")) + +} + +func TestDynamodbTagsUpdateItemInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.UpdateItemInput{ + TableName: aws.String("my-table"), + Key: map[string]dtypes.AttributeValue{ + "id": &dtypes.AttributeValueMemberS{Value: "123"}, + }, + UpdateExpression: aws.String("set firstName = :firstName"), + ExpressionAttributeValues: map[string]dtypes.AttributeValue{ + ":firstName": &dtypes.AttributeValueMemberS{Value: "John McNewname"}, + }, + }, + } + + attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "my-table")) + +} + +func TestDynamodbTagsUpdateTableInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.UpdateTableInput{ + TableName: aws.String("my-table"), + AttributeDefinitions: []dtypes.AttributeDefinition{ + { + AttributeName: aws.String("id"), + AttributeType: dtypes.ScalarAttributeTypeS, + }, + }, + GlobalSecondaryIndexUpdates: []dtypes.GlobalSecondaryIndexUpdate{ + { + Create: &dtypes.CreateGlobalSecondaryIndexAction{ + IndexName: aws.String("index1"), + KeySchema: []dtypes.KeySchemaElement{ + { + AttributeName: aws.String("attribute"), + KeyType: dtypes.KeyTypeHash, + }, + }, + Projection: &dtypes.Projection{ + NonKeyAttributes: []string{"attribute1", "attribute2"}, + ProjectionType: dtypes.ProjectionTypeAll, + }, + }, + }, + }, + ProvisionedThroughput: &dtypes.ProvisionedThroughput{ + ReadCapacityUnits: aws.Int64(123), + WriteCapacityUnits: aws.Int64(456), + }, + }, + } + + attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "my-table")) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.attribute_definitions", "[{\"AttributeName\":\"id\",\"AttributeType\":\"S\"}]")) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.global_secondary_index_updates", "[{\"Create\":{\"IndexName\":\"index1\",\"KeySchema\":[{\"AttributeName\":\"attribute\",\"KeyType\":\"HASH\"}],\"Projection\":{\"NonKeyAttributes\":[\"attribute1\",\"attribute2\"],\"ProjectionType\":\"ALL\"},\"ProvisionedThroughput\":null},\"Delete\":null,\"Update\":null}]")) + assert.Contains(t, attributes, attribute.Int("aws.dynamodb.provisioned_read_capacity", 123)) + assert.Contains(t, attributes, attribute.Int("aws.dynamodb.provisioned_write_capacity", 456)) +} diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.mod b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.mod index 832e4825649..d4390df8946 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.mod +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.mod @@ -4,7 +4,9 @@ go 1.16 require ( github.com/aws/aws-sdk-go-v2 v1.12.0 + github.com/aws/aws-sdk-go-v2/service/dynamodb v1.11.0 github.com/aws/aws-sdk-go-v2/service/route53 v1.16.0 + github.com/aws/smithy-go v1.9.1 github.com/stretchr/testify v1.7.0 go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws v0.27.0 go.opentelemetry.io/otel v1.3.0 diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.sum b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.sum index 80f845fe2bd..1f34c576e57 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.sum +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.sum @@ -1,11 +1,21 @@ +github.com/aws/aws-sdk-go-v2 v1.11.2/go.mod h1:SQfA+m2ltnu1cA0soUkj4dRSsmITiVQUJvBIZjzfPyQ= github.com/aws/aws-sdk-go-v2 v1.12.0 h1:z5bijqy+eXLK/QqF6eQcwCN2qw1k+m9OUDicqCZygu0= github.com/aws/aws-sdk-go-v2 v1.12.0/go.mod h1:tWhQI5N5SiMawto3uMAQJU5OUN/1ivhDDHq7HTsJvZ0= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.2/go.mod h1:SgKKNBIoDC/E1ZCDhhMW3yalWjwuLjMcpLzsM/QQnWo= github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.3 h1:YPNiEXnuWdkpNOwBFHhcLwkSmewwQRcPFO9dHmxU0qg= github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.3/go.mod h1:L72JSFj9OwHwyukeuKFFyTj6uFWE4AjB0IQp97bd9Lc= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.0.2/go.mod h1:xT4XX6w5Sa3dhg50JrYyy3e4WPYo/+WjY/BXtqXVunU= github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.1.0 h1:ArRd27pSm66f7cCBDPS77wvxiS4IRjFatpzVBD7Aojc= github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.1.0/go.mod h1:KdVvdk4gb7iatuHZgIkIqvJlWHBtjCJLUtD/uO/FkWw= +github.com/aws/aws-sdk-go-v2/service/dynamodb v1.11.0 h1:te+nIFwPf5Bi/cZvd9g/+EF0gkJT3c0J/5+NMx0NBZg= +github.com/aws/aws-sdk-go-v2/service/dynamodb v1.11.0/go.mod h1:ELltfl9ri0n4sZ/VjPZBgemNMd9mYIpCAuZhc7NP7l4= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.5.0 h1:lPLbw4Gn59uoKqvOfSnkJr54XWk5Ak1NK20ZEiSWb3U= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.5.0/go.mod h1:80NaCIH9YU3rzTTs/J/ECATjXuRqzo/wB6ukO6MZ0XY= +github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.3.3 h1:ru9+IpkVIuDvIkm9Q0DEjtWHnh6ITDoZo8fH2dIjlqQ= +github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.3.3/go.mod h1:zOyLMYyg60yyZpOCniAUuibWVqTU4TuLmMa/Wh4P+HA= github.com/aws/aws-sdk-go-v2/service/route53 v1.16.0 h1:xWoSNH+tQ0x0yHqCanSN9l3hAmc6nb+UaTabp720GvU= github.com/aws/aws-sdk-go-v2/service/route53 v1.16.0/go.mod h1:WrCwjYVD8F60mRhhq2dec95l3IJKMXT+DAhTvBPfGJo= +github.com/aws/smithy-go v1.9.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= github.com/aws/smithy-go v1.9.1 h1:5vetTooLk4hPWV8q6ym6+lXKAT1Urnm49YkrRKo2J8o= github.com/aws/smithy-go v1.9.1/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= From 4fc923094871c89042631b53da92ce3fa7942899 Mon Sep 17 00:00:00 2001 From: Jenny Nilsen Date: Fri, 14 Jan 2022 12:16:17 +0000 Subject: [PATCH 02/11] applying suggestions --- CHANGELOG.md | 4 +- .../aws/aws-sdk-go-v2/otelaws/attributes.go | 16 +- .../aws/aws-sdk-go-v2/otelaws/aws.go | 6 +- .../aws/aws-sdk-go-v2/otelaws/config.go | 6 +- .../otelaws/dynamodbattributes.go | 14 +- .../otelaws/dynamodbattributes_test.go | 326 ++++++++++++++++++ .../aws/aws-sdk-go-v2/otelaws/go.mod | 3 +- .../aws/aws-sdk-go-v2/otelaws/go.sum | 23 +- .../otelaws/test/dynamodbattributes_test.go | 303 +--------------- .../aws/aws-sdk-go-v2/otelaws/test/go.mod | 3 +- .../aws/aws-sdk-go-v2/otelaws/test/go.sum | 16 +- 11 files changed, 369 insertions(+), 351 deletions(-) create mode 100644 instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 180326c5eb2..851609c4f04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,12 +36,12 @@ We have updated the project minimum supported Go version to 1.16 - `otelhttptrace.NewClientTrace` now uses `TracerProvider` from the parent context if one exists and none was set with `WithTracerProvider` (#874) ### Fixed + - The `"go.opentelemetry.io/contrib/detector/aws/ecs".Detector` no longer errors if not running in ECS. (#1428) - `go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux` does not require instrumented HTTP handlers to call `Write` nor `WriteHeader` anymore. (#1443) - ## [1.2.0/0.27.0] - 2021-11-15 ### Changed @@ -74,8 +74,6 @@ Update dependency on the `go.opentelemetry.io/otel` project to `v1.1.0`. - Add `WithTracerProvider` option for `otelhttptrace.NewClientTrace`. (#1128) - Add optional AWS X-Ray configuration module for AWS Lambda Instrumentation. (#984) - - ### Fixed - The `go.opentelemetry.io/contrib/propagators/ot` propagator returns the words `true` or `false` for the `ot-tracer-sampled` header instead of numerical `0` and `1`. (#1358) diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/attributes.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/attributes.go index 8e166116e35..c71d680582d 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/attributes.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/attributes.go @@ -19,6 +19,7 @@ import ( v2Middleware "github.com/aws/aws-sdk-go-v2/aws/middleware" "github.com/aws/smithy-go/middleware" + "go.opentelemetry.io/otel/attribute" ) @@ -30,6 +31,10 @@ const ( RequestIDKey attribute.Key = "aws.request_id" ) +var servicemap map[string]AttributeSetter = map[string]AttributeSetter{ + "dynamodb": DynamodbAttributeSetter, +} + func OperationAttr(operation string) attribute.KeyValue { return OperationKey.String(operation) } @@ -46,17 +51,12 @@ func RequestIDAttr(requestID string) attribute.KeyValue { return RequestIDKey.String(requestID) } -func Defaultattributesetter(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue { - servicemap := map[string]attributesetter{ - "dynamodb": DynamodbAttributeSetter, - } +func DefaultAttributeSetter(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue { serviceID := v2Middleware.GetServiceID(ctx) - if val, ok := servicemap[serviceID]; ok { - function := val - attributes := function(ctx, in) - return attributes + if fn, ok := servicemap[serviceID]; ok { + return fn(ctx, in) } return []attribute.KeyValue{} 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 bce8338ab78..8232df5fa5d 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 @@ -35,11 +35,11 @@ const ( type spanTimestampKey struct{} -type attributesetter func(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue +type AttributeSetter func(context.Context, middleware.InitializeInput) []attribute.KeyValue type otelMiddlewares struct { tracer trace.Tracer - attributesetter attributesetter + attributesetter AttributeSetter } func (m otelMiddlewares) initializeMiddlewareBefore(stack *middleware.Stack) error { @@ -119,7 +119,7 @@ func (m otelMiddlewares) deserializeMiddleware(stack *middleware.Stack) error { func AppendMiddlewares(apiOptions *[]func(*middleware.Stack) error, opts ...Option) { cfg := config{ TracerProvider: otel.GetTracerProvider(), - AttributeSetter: Defaultattributesetter, + AttributeSetter: DefaultAttributeSetter, } for _, opt := range opts { opt.apply(&cfg) diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go index 5c9e19589bd..19232c63aa1 100755 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go @@ -20,7 +20,7 @@ import ( type config struct { TracerProvider trace.TracerProvider - AttributeSetter attributesetter + AttributeSetter AttributeSetter } // Option applies an option value. @@ -47,8 +47,8 @@ func WithTracerProvider(provider trace.TracerProvider) Option { } // WithAttributeSetter specifies an attribute setter function for setting service specific attributes. -// If none is specified, the service will be determined by the Defaultattributesetter function and the corresponding attributes will be included. -func WithAttributeSetter(attributesetter attributesetter) Option { +// If none is specified, the service will be determined by the DefaultAttributeSetter function and the corresponding attributes will be included. +func WithAttributeSetter(attributesetter AttributeSetter) Option { return optionFunc(func(cfg *config) { if attributesetter != nil { diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go index 35dabd203c4..1172253a9e5 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go @@ -22,7 +22,7 @@ import ( "github.com/aws/smithy-go/middleware" "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/semconv/v1.7.0" + semconv "go.opentelemetry.io/otel/semconv/v1.7.0" ) func DynamodbAttributeSetter(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue { @@ -48,18 +48,18 @@ func DynamodbAttributeSetter(ctx context.Context, in middleware.InitializeInput) } case *dynamodb.BatchGetItemInput: - var table_names []string + var tableNames []string for k, _ := range v.RequestItems { - table_names = append(table_names, k) + tableNames = append(tableNames, k) } - dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.StringSlice(table_names)) + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.StringSlice(tableNames)) case *dynamodb.BatchWriteItemInput: - var table_names []string + var tableNames []string for k, _ := range v.RequestItems { - table_names = append(table_names, k) + tableNames = append(tableNames, k) } - dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.StringSlice(table_names)) + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.StringSlice(tableNames)) case *dynamodb.CreateTableInput: dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go new file mode 100644 index 00000000000..e81b96f8a26 --- /dev/null +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go @@ -0,0 +1,326 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package otelaws + +import ( + "context" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/dynamodb" + "github.com/stretchr/testify/assert" + dtypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" + "github.com/aws/smithy-go/middleware" + "go.opentelemetry.io/otel/attribute" + +) + +func TestDynamodbTagsBatchGetItemInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.BatchGetItemInput{ + RequestItems: map[string]dtypes.KeysAndAttributes{ + "table1": { + Keys: []map[string]dtypes.AttributeValue{ + { + "id": &dtypes.AttributeValueMemberS{Value: "123"}, + }, + }, + }, + }, + }, + } + + attributes := DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.StringSlice("aws.dynamodb.table_names", []string{"table1"})) +} + +func TestDynamodbTagsBatchWriteItemInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.BatchWriteItemInput{ + RequestItems: map[string][]dtypes.WriteRequest{ + "table1": { + { + DeleteRequest: &dtypes.DeleteRequest{ + Key: map[string]dtypes.AttributeValue{ + "id": &dtypes.AttributeValueMemberS{Value: "123"}, + }, + }, + }, + { + PutRequest: &dtypes.PutRequest{ + Item: map[string]dtypes.AttributeValue{ + "id": &dtypes.AttributeValueMemberS{Value: "234"}, + }, + }, + }, + }, + }, + }, + } + + attributes := DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.StringSlice("aws.dynamodb.table_names", []string{"table1"})) +} + +func TestDynamodbTagsCreateTableInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.CreateTableInput{ + AttributeDefinitions: []dtypes.AttributeDefinition{ + { + AttributeName: aws.String("id"), + AttributeType: dtypes.ScalarAttributeTypeS, + }, + }, + KeySchema: []dtypes.KeySchemaElement{ + { + AttributeName: aws.String("id"), + KeyType: dtypes.KeyTypeHash, + }, + }, + TableName: aws.String("table1"), + BillingMode: dtypes.BillingModePayPerRequest, + ProvisionedThroughput: &dtypes.ProvisionedThroughput{ + ReadCapacityUnits: aws.Int64(123), + WriteCapacityUnits: aws.Int64(456), + }, + GlobalSecondaryIndexes: []dtypes.GlobalSecondaryIndex{ + { + IndexName: aws.String("index1"), + KeySchema: []dtypes.KeySchemaElement{ + { + AttributeName: aws.String("attributename"), + KeyType: dtypes.KeyTypeHash, + }, + }, + Projection: &dtypes.Projection{ + NonKeyAttributes: []string{"non-key-attributes"}, + }, + }, + }, + LocalSecondaryIndexes: []dtypes.LocalSecondaryIndex{ + { + IndexName: aws.String("index2"), + KeySchema: []dtypes.KeySchemaElement{ + { + AttributeName: aws.String("attributename"), + KeyType: dtypes.KeyTypeHash, + }, + }, + }, + }, + }, + } + + attributes := DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.global_secondary_indexes", "[{\"IndexName\":\"index1\",\"KeySchema\":[{\"AttributeName\":\"attributename\",\"KeyType\":\"HASH\"}],\"Projection\":{\"NonKeyAttributes\":[\"non-key-attributes\"],\"ProjectionType\":\"\"},\"ProvisionedThroughput\":null}]")) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.local_secondary_indexes", "[{\"IndexName\":\"index2\",\"KeySchema\":[{\"AttributeName\":\"attributename\",\"KeyType\":\"HASH\"}],\"Projection\":null}]")) + assert.Contains(t, attributes, attribute.Int("aws.dynamodb.provisioned_read_capacity", 123)) + assert.Contains(t, attributes, attribute.Int("aws.dynamodb.provisioned_write_capacity", 456)) +} + +func TestDynamodbTagsDeleteItemInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.DeleteItemInput{ + Key: map[string]dtypes.AttributeValue{ + "id": &dtypes.AttributeValueMemberS{Value: "123"}, + }, + TableName: aws.String("table1"), + }, + } + attributes := DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) +} + +func TestDynamodbTagsDeleteTableInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.DeleteTableInput{ + TableName: aws.String("table1"), + }, + } + attributes := DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) + +} + +func TestDynamodbTagsDescribeTableInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.DescribeTableInput{ + TableName: aws.String("table1"), + }, + } + attributes := DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) +} + +func TestDynamodbTagsListTablesInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.ListTablesInput{ + ExclusiveStartTableName: aws.String("table1"), + Limit: aws.Int32(10), + }, + } + attributes := DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.exclusive_start_table", "table1")) + assert.Contains(t, attributes, attribute.Int("aws.dynamodb.limit", 10)) + +} + +func TestDynamodbTagsPutItemInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.PutItemInput{ + TableName: aws.String("table1"), + Item: map[string]dtypes.AttributeValue{ + "id": &dtypes.AttributeValueMemberS{Value: "12346"}, + "name": &dtypes.AttributeValueMemberS{Value: "John Doe"}, + "email": &dtypes.AttributeValueMemberS{Value: "john@doe.io"}, + }, + }, + } + + attributes := DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) +} + +func TestDynamodbTagsQueryInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.QueryInput{ + TableName: aws.String("table1"), + IndexName: aws.String("index1"), + ConsistentRead: aws.Bool(true), + Limit: aws.Int32(10), + ScanIndexForward: aws.Bool(true), + ProjectionExpression: aws.String("projectionexpression"), + Select: dtypes.SelectAllAttributes, + KeyConditionExpression: aws.String("id = :hashKey and #date > :rangeKey"), + ExpressionAttributeNames: map[string]string{ + "#date": "date", + }, + ExpressionAttributeValues: map[string]dtypes.AttributeValue{ + ":hashKey": &dtypes.AttributeValueMemberS{Value: "123"}, + ":rangeKey": &dtypes.AttributeValueMemberN{Value: "20150101"}, + }, + }, + } + + attributes := DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) + assert.Contains(t, attributes, attribute.Bool("aws.dynamodb.consistent_read", true)) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.index_name", "index1")) + assert.Contains(t, attributes, attribute.Int("aws.dynamodb.limit", 10)) + assert.Contains(t, attributes, attribute.Bool("aws.dynamodb.scan_forward", true)) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.projection", "projectionexpression")) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.select", "ALL_ATTRIBUTES")) + +} + +func TestDynamodbTagsScanInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.ScanInput{ + TableName: aws.String("my-table"), + ConsistentRead: aws.Bool(true), + IndexName: aws.String("index1"), + Limit: aws.Int32(10), + ProjectionExpression: aws.String("Artist, Genre"), + Segment: aws.Int32(10), + TotalSegments: aws.Int32(100), + Select: dtypes.SelectAllAttributes, + }, + } + + attributes := DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "my-table")) + assert.Contains(t, attributes, attribute.Bool("aws.dynamodb.consistent_read", true)) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.index_name", "index1")) + assert.Contains(t, attributes, attribute.Int("aws.dynamodb.limit", 10)) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.select", "ALL_ATTRIBUTES")) + assert.Contains(t, attributes, attribute.Int("aws.dynamodb.total_segments", 100)) + assert.Contains(t, attributes, attribute.Int("aws.dynamodb.segment", 10)) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.projection", "Artist, Genre")) + +} + +func TestDynamodbTagsUpdateItemInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.UpdateItemInput{ + TableName: aws.String("my-table"), + Key: map[string]dtypes.AttributeValue{ + "id": &dtypes.AttributeValueMemberS{Value: "123"}, + }, + UpdateExpression: aws.String("set firstName = :firstName"), + ExpressionAttributeValues: map[string]dtypes.AttributeValue{ + ":firstName": &dtypes.AttributeValueMemberS{Value: "John McNewname"}, + }, + }, + } + + attributes := DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "my-table")) + +} + +func TestDynamodbTagsUpdateTableInput(t *testing.T) { + input := middleware.InitializeInput{ + Parameters: &dynamodb.UpdateTableInput{ + TableName: aws.String("my-table"), + AttributeDefinitions: []dtypes.AttributeDefinition{ + { + AttributeName: aws.String("id"), + AttributeType: dtypes.ScalarAttributeTypeS, + }, + }, + GlobalSecondaryIndexUpdates: []dtypes.GlobalSecondaryIndexUpdate{ + { + Create: &dtypes.CreateGlobalSecondaryIndexAction{ + IndexName: aws.String("index1"), + KeySchema: []dtypes.KeySchemaElement{ + { + AttributeName: aws.String("attribute"), + KeyType: dtypes.KeyTypeHash, + }, + }, + Projection: &dtypes.Projection{ + NonKeyAttributes: []string{"attribute1", "attribute2"}, + ProjectionType: dtypes.ProjectionTypeAll, + }, + }, + }, + }, + ProvisionedThroughput: &dtypes.ProvisionedThroughput{ + ReadCapacityUnits: aws.Int64(123), + WriteCapacityUnits: aws.Int64(456), + }, + }, + } + + attributes := DynamodbAttributeSetter(context.TODO(), input) + + assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "my-table")) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.attribute_definitions", "[{\"AttributeName\":\"id\",\"AttributeType\":\"S\"}]")) + assert.Contains(t, attributes, attribute.String("aws.dynamodb.global_secondary_index_updates", "[{\"Create\":{\"IndexName\":\"index1\",\"KeySchema\":[{\"AttributeName\":\"attribute\",\"KeyType\":\"HASH\"}],\"Projection\":{\"NonKeyAttributes\":[\"attribute1\",\"attribute2\"],\"ProjectionType\":\"ALL\"},\"ProvisionedThroughput\":null},\"Delete\":null,\"Update\":null}]")) + assert.Contains(t, attributes, attribute.Int("aws.dynamodb.provisioned_read_capacity", 123)) + assert.Contains(t, attributes, attribute.Int("aws.dynamodb.provisioned_write_capacity", 456)) +} \ No newline at end of file diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/go.mod b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/go.mod index e1f6d111b0a..cd5217fb09a 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/go.mod +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/go.mod @@ -6,8 +6,9 @@ replace go.opentelemetry.io/contrib => ../../../../../ require ( github.com/aws/aws-sdk-go-v2 v1.12.0 - github.com/aws/aws-sdk-go-v2/service/dynamodb v1.11.0 + github.com/aws/aws-sdk-go-v2/service/dynamodb v1.12.0 github.com/aws/smithy-go v1.9.1 + github.com/stretchr/testify v1.7.0 go.opentelemetry.io/otel v1.3.0 go.opentelemetry.io/otel/trace v1.3.0 ) diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/go.sum b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/go.sum index f9fe10543e5..e89a677e741 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/go.sum +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/go.sum @@ -1,17 +1,15 @@ -github.com/aws/aws-sdk-go-v2 v1.11.2/go.mod h1:SQfA+m2ltnu1cA0soUkj4dRSsmITiVQUJvBIZjzfPyQ= github.com/aws/aws-sdk-go-v2 v1.12.0 h1:z5bijqy+eXLK/QqF6eQcwCN2qw1k+m9OUDicqCZygu0= github.com/aws/aws-sdk-go-v2 v1.12.0/go.mod h1:tWhQI5N5SiMawto3uMAQJU5OUN/1ivhDDHq7HTsJvZ0= -github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.2 h1:XJLnluKuUxQG255zPNe+04izXl7GSyUVafIsgfv9aw4= -github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.2/go.mod h1:SgKKNBIoDC/E1ZCDhhMW3yalWjwuLjMcpLzsM/QQnWo= -github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.0.2 h1:EauRoYZVNPlidZSZJDscjJBQ22JhVF2+tdteatax2Ak= -github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.0.2/go.mod h1:xT4XX6w5Sa3dhg50JrYyy3e4WPYo/+WjY/BXtqXVunU= -github.com/aws/aws-sdk-go-v2/service/dynamodb v1.11.0 h1:te+nIFwPf5Bi/cZvd9g/+EF0gkJT3c0J/5+NMx0NBZg= -github.com/aws/aws-sdk-go-v2/service/dynamodb v1.11.0/go.mod h1:ELltfl9ri0n4sZ/VjPZBgemNMd9mYIpCAuZhc7NP7l4= -github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.5.0 h1:lPLbw4Gn59uoKqvOfSnkJr54XWk5Ak1NK20ZEiSWb3U= -github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.5.0/go.mod h1:80NaCIH9YU3rzTTs/J/ECATjXuRqzo/wB6ukO6MZ0XY= -github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.3.3 h1:ru9+IpkVIuDvIkm9Q0DEjtWHnh6ITDoZo8fH2dIjlqQ= -github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.3.3/go.mod h1:zOyLMYyg60yyZpOCniAUuibWVqTU4TuLmMa/Wh4P+HA= -github.com/aws/smithy-go v1.9.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.3 h1:YPNiEXnuWdkpNOwBFHhcLwkSmewwQRcPFO9dHmxU0qg= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.3/go.mod h1:L72JSFj9OwHwyukeuKFFyTj6uFWE4AjB0IQp97bd9Lc= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.1.0 h1:ArRd27pSm66f7cCBDPS77wvxiS4IRjFatpzVBD7Aojc= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.1.0/go.mod h1:KdVvdk4gb7iatuHZgIkIqvJlWHBtjCJLUtD/uO/FkWw= +github.com/aws/aws-sdk-go-v2/service/dynamodb v1.12.0 h1:D8rIbaDiA6PoRU+ojAjnftqmy75VopL2bTJPIo2G9Ko= +github.com/aws/aws-sdk-go-v2/service/dynamodb v1.12.0/go.mod h1:tzN6ge+2WmkKBjlA6vfLxTQFb9VSQCPSVhpYz+gwYNc= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.6.0 h1:zQlcDaAP0sk7jVSkBnBd4fc07M8bSAi6k1WjL48tB9M= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.6.0/go.mod h1:lzucjNKa47J5dstwdXwRrDLMEeWwOYK2+BgUKR3xthI= +github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.4.0 h1:LVQAt97u5GvWNuzQbF/N2awTtLVwHUBDTMDypVC+xiw= +github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.4.0/go.mod h1:XTo3HdhcCDMl/syHC+mGJyP3Qmm1BD3MNtuLlZCqiP8= github.com/aws/smithy-go v1.9.1 h1:5vetTooLk4hPWV8q6ym6+lXKAT1Urnm49YkrRKo2J8o= github.com/aws/smithy-go v1.9.1/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= @@ -39,6 +37,7 @@ go.opentelemetry.io/otel/trace v1.3.0 h1:doy8Hzb1RJ+I3yFhtDmwNc7tIyw1tNMOIsyPzp1 go.opentelemetry.io/otel/trace v1.3.0/go.mod h1:c/VDhno8888bvQYmbYLqe41/Ldmr/KKunbvWM4/fEjk= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 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 9fa782892a8..ec39ae55897 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 @@ -33,7 +33,6 @@ import ( "go.opentelemetry.io/otel/sdk/trace/tracetest" "go.opentelemetry.io/otel/trace" - "github.com/aws/smithy-go/middleware" ) func TestDynamodbTags(t *testing.T) { @@ -106,304 +105,4 @@ func TestDynamodbTags(t *testing.T) { assert.Contains(t, attrs, attribute.Bool("aws.dynamodb.consistent_read", false)) }) - - -} - -func TestDynamodbTagsBatchGetItemInput(t *testing.T) { - input := middleware.InitializeInput{ - Parameters: &dynamodb.BatchGetItemInput{ - RequestItems: map[string]dtypes.KeysAndAttributes{ - "table1": { - Keys: []map[string]dtypes.AttributeValue{ - { - "id": &dtypes.AttributeValueMemberS{Value: "123"}, - }, - }, - }, - }, - }, - } - - attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) - - assert.Contains(t, attributes, attribute.StringSlice("aws.dynamodb.table_names", []string{"table1"})) -} - -func TestDynamodbTagsBatchWriteItemInput(t *testing.T) { - input := middleware.InitializeInput{ - Parameters: &dynamodb.BatchWriteItemInput{ - RequestItems: map[string][]dtypes.WriteRequest{ - "table1": { - { - DeleteRequest: &dtypes.DeleteRequest{ - Key: map[string]dtypes.AttributeValue{ - "id": &dtypes.AttributeValueMemberS{Value: "123"}, - }, - }, - }, - { - PutRequest: &dtypes.PutRequest{ - Item: map[string]dtypes.AttributeValue{ - "id": &dtypes.AttributeValueMemberS{Value: "234"}, - }, - }, - }, - }, - }, - }, - } - - attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) - - assert.Contains(t, attributes, attribute.StringSlice("aws.dynamodb.table_names", []string{"table1"})) -} - -func TestDynamodbTagsCreateTableInput(t *testing.T) { - input := middleware.InitializeInput{ - Parameters: &dynamodb.CreateTableInput{ - AttributeDefinitions: []dtypes.AttributeDefinition{ - { - AttributeName: aws.String("id"), - AttributeType: dtypes.ScalarAttributeTypeS, - }, - }, - KeySchema: []dtypes.KeySchemaElement{ - { - AttributeName: aws.String("id"), - KeyType: dtypes.KeyTypeHash, - }, - }, - TableName: aws.String("table1"), - BillingMode: dtypes.BillingModePayPerRequest, - ProvisionedThroughput: &dtypes.ProvisionedThroughput{ - ReadCapacityUnits: aws.Int64(123), - WriteCapacityUnits: aws.Int64(456), - }, - GlobalSecondaryIndexes: []dtypes.GlobalSecondaryIndex{ - { - IndexName: aws.String("index1"), - KeySchema: []dtypes.KeySchemaElement{ - { - AttributeName: aws.String("attributename"), - KeyType: dtypes.KeyTypeHash, - }, - }, - Projection: &dtypes.Projection{ - NonKeyAttributes: []string{"non-key-attributes"}, - }, - }, - }, - LocalSecondaryIndexes: []dtypes.LocalSecondaryIndex{ - { - IndexName: aws.String("index2"), - KeySchema: []dtypes.KeySchemaElement{ - { - AttributeName: aws.String("attributename"), - KeyType: dtypes.KeyTypeHash, - }, - }, - }, - }, - }, - } - - attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) - - assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) - assert.Contains(t, attributes, attribute.String("aws.dynamodb.global_secondary_indexes", "[{\"IndexName\":\"index1\",\"KeySchema\":[{\"AttributeName\":\"attributename\",\"KeyType\":\"HASH\"}],\"Projection\":{\"NonKeyAttributes\":[\"non-key-attributes\"],\"ProjectionType\":\"\"},\"ProvisionedThroughput\":null}]")) - assert.Contains(t, attributes, attribute.String("aws.dynamodb.local_secondary_indexes", "[{\"IndexName\":\"index2\",\"KeySchema\":[{\"AttributeName\":\"attributename\",\"KeyType\":\"HASH\"}],\"Projection\":null}]")) - assert.Contains(t, attributes, attribute.Int("aws.dynamodb.provisioned_read_capacity", 123)) - assert.Contains(t, attributes, attribute.Int("aws.dynamodb.provisioned_write_capacity", 456)) -} - -func TestDynamodbTagsDeleteItemInput(t *testing.T) { - input := middleware.InitializeInput{ - Parameters: &dynamodb.DeleteItemInput{ - Key: map[string]dtypes.AttributeValue{ - "id": &dtypes.AttributeValueMemberS{Value: "123"}, - }, - TableName: aws.String("table1"), - }, - } - attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) - - assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) -} - -func TestDynamodbTagsDeleteTableInput(t *testing.T) { - input := middleware.InitializeInput{ - Parameters: &dynamodb.DeleteTableInput{ - TableName: aws.String("table1"), - }, - } - attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) - - assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) - -} - -func TestDynamodbTagsDescribeTableInput(t *testing.T) { - input := middleware.InitializeInput{ - Parameters: &dynamodb.DescribeTableInput{ - TableName: aws.String("table1"), - }, - } - attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) - - assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) -} - -func TestDynamodbTagsListTablesInput(t *testing.T) { - input := middleware.InitializeInput{ - Parameters: &dynamodb.ListTablesInput{ - ExclusiveStartTableName: aws.String("table1"), - Limit: aws.Int32(10), - }, - } - attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) - - assert.Contains(t, attributes, attribute.String("aws.dynamodb.exclusive_start_table", "table1")) - assert.Contains(t, attributes, attribute.Int("aws.dynamodb.limit", 10)) - -} - -func TestDynamodbTagsPutItemInput(t *testing.T) { - input := middleware.InitializeInput{ - Parameters: &dynamodb.PutItemInput{ - TableName: aws.String("table1"), - Item: map[string]dtypes.AttributeValue{ - "id": &dtypes.AttributeValueMemberS{Value: "12346"}, - "name": &dtypes.AttributeValueMemberS{Value: "John Doe"}, - "email": &dtypes.AttributeValueMemberS{Value: "john@doe.io"}, - }, - }, - } - - attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) - - assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) -} - -func TestDynamodbTagsQueryInput(t *testing.T) { - input := middleware.InitializeInput{ - Parameters: &dynamodb.QueryInput{ - TableName: aws.String("table1"), - IndexName: aws.String("index1"), - ConsistentRead: aws.Bool(true), - Limit: aws.Int32(10), - ScanIndexForward: aws.Bool(true), - ProjectionExpression: aws.String("projectionexpression"), - Select: dtypes.SelectAllAttributes, - KeyConditionExpression: aws.String("id = :hashKey and #date > :rangeKey"), - ExpressionAttributeNames: map[string]string{ - "#date": "date", - }, - ExpressionAttributeValues: map[string]dtypes.AttributeValue{ - ":hashKey": &dtypes.AttributeValueMemberS{Value: "123"}, - ":rangeKey": &dtypes.AttributeValueMemberN{Value: "20150101"}, - }, - }, - } - - attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) - - assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) - assert.Contains(t, attributes, attribute.Bool("aws.dynamodb.consistent_read", true)) - assert.Contains(t, attributes, attribute.String("aws.dynamodb.index_name", "index1")) - assert.Contains(t, attributes, attribute.Int("aws.dynamodb.limit", 10)) - assert.Contains(t, attributes, attribute.Bool("aws.dynamodb.scan_forward", true)) - assert.Contains(t, attributes, attribute.String("aws.dynamodb.projection", "projectionexpression")) - assert.Contains(t, attributes, attribute.String("aws.dynamodb.select", "ALL_ATTRIBUTES")) - -} - -func TestDynamodbTagsScanInput(t *testing.T) { - input := middleware.InitializeInput{ - Parameters: &dynamodb.ScanInput{ - TableName: aws.String("my-table"), - ConsistentRead: aws.Bool(true), - IndexName: aws.String("index1"), - Limit: aws.Int32(10), - ProjectionExpression: aws.String("Artist, Genre"), - Segment: aws.Int32(10), - TotalSegments: aws.Int32(100), - Select: dtypes.SelectAllAttributes, - }, - } - - attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) - - assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "my-table")) - assert.Contains(t, attributes, attribute.Bool("aws.dynamodb.consistent_read", true)) - assert.Contains(t, attributes, attribute.String("aws.dynamodb.index_name", "index1")) - assert.Contains(t, attributes, attribute.Int("aws.dynamodb.limit", 10)) - assert.Contains(t, attributes, attribute.String("aws.dynamodb.select", "ALL_ATTRIBUTES")) - assert.Contains(t, attributes, attribute.Int("aws.dynamodb.total_segments", 100)) - assert.Contains(t, attributes, attribute.Int("aws.dynamodb.segment", 10)) - assert.Contains(t, attributes, attribute.String("aws.dynamodb.projection", "Artist, Genre")) - -} - -func TestDynamodbTagsUpdateItemInput(t *testing.T) { - input := middleware.InitializeInput{ - Parameters: &dynamodb.UpdateItemInput{ - TableName: aws.String("my-table"), - Key: map[string]dtypes.AttributeValue{ - "id": &dtypes.AttributeValueMemberS{Value: "123"}, - }, - UpdateExpression: aws.String("set firstName = :firstName"), - ExpressionAttributeValues: map[string]dtypes.AttributeValue{ - ":firstName": &dtypes.AttributeValueMemberS{Value: "John McNewname"}, - }, - }, - } - - attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) - - assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "my-table")) - -} - -func TestDynamodbTagsUpdateTableInput(t *testing.T) { - input := middleware.InitializeInput{ - Parameters: &dynamodb.UpdateTableInput{ - TableName: aws.String("my-table"), - AttributeDefinitions: []dtypes.AttributeDefinition{ - { - AttributeName: aws.String("id"), - AttributeType: dtypes.ScalarAttributeTypeS, - }, - }, - GlobalSecondaryIndexUpdates: []dtypes.GlobalSecondaryIndexUpdate{ - { - Create: &dtypes.CreateGlobalSecondaryIndexAction{ - IndexName: aws.String("index1"), - KeySchema: []dtypes.KeySchemaElement{ - { - AttributeName: aws.String("attribute"), - KeyType: dtypes.KeyTypeHash, - }, - }, - Projection: &dtypes.Projection{ - NonKeyAttributes: []string{"attribute1", "attribute2"}, - ProjectionType: dtypes.ProjectionTypeAll, - }, - }, - }, - }, - ProvisionedThroughput: &dtypes.ProvisionedThroughput{ - ReadCapacityUnits: aws.Int64(123), - WriteCapacityUnits: aws.Int64(456), - }, - }, - } - - attributes := otelaws.DynamodbAttributeSetter(context.TODO(), input) - - assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "my-table")) - assert.Contains(t, attributes, attribute.String("aws.dynamodb.attribute_definitions", "[{\"AttributeName\":\"id\",\"AttributeType\":\"S\"}]")) - assert.Contains(t, attributes, attribute.String("aws.dynamodb.global_secondary_index_updates", "[{\"Create\":{\"IndexName\":\"index1\",\"KeySchema\":[{\"AttributeName\":\"attribute\",\"KeyType\":\"HASH\"}],\"Projection\":{\"NonKeyAttributes\":[\"attribute1\",\"attribute2\"],\"ProjectionType\":\"ALL\"},\"ProvisionedThroughput\":null},\"Delete\":null,\"Update\":null}]")) - assert.Contains(t, attributes, attribute.Int("aws.dynamodb.provisioned_read_capacity", 123)) - assert.Contains(t, attributes, attribute.Int("aws.dynamodb.provisioned_write_capacity", 456)) -} +} \ No newline at end of file diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.mod b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.mod index d4390df8946..c6af50bcad1 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.mod +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.mod @@ -4,9 +4,8 @@ go 1.16 require ( github.com/aws/aws-sdk-go-v2 v1.12.0 - github.com/aws/aws-sdk-go-v2/service/dynamodb v1.11.0 + github.com/aws/aws-sdk-go-v2/service/dynamodb v1.12.0 github.com/aws/aws-sdk-go-v2/service/route53 v1.16.0 - github.com/aws/smithy-go v1.9.1 github.com/stretchr/testify v1.7.0 go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws v0.27.0 go.opentelemetry.io/otel v1.3.0 diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.sum b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.sum index 1f34c576e57..539d763f2b5 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.sum +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.sum @@ -1,21 +1,17 @@ -github.com/aws/aws-sdk-go-v2 v1.11.2/go.mod h1:SQfA+m2ltnu1cA0soUkj4dRSsmITiVQUJvBIZjzfPyQ= github.com/aws/aws-sdk-go-v2 v1.12.0 h1:z5bijqy+eXLK/QqF6eQcwCN2qw1k+m9OUDicqCZygu0= github.com/aws/aws-sdk-go-v2 v1.12.0/go.mod h1:tWhQI5N5SiMawto3uMAQJU5OUN/1ivhDDHq7HTsJvZ0= -github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.2/go.mod h1:SgKKNBIoDC/E1ZCDhhMW3yalWjwuLjMcpLzsM/QQnWo= github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.3 h1:YPNiEXnuWdkpNOwBFHhcLwkSmewwQRcPFO9dHmxU0qg= github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.3/go.mod h1:L72JSFj9OwHwyukeuKFFyTj6uFWE4AjB0IQp97bd9Lc= -github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.0.2/go.mod h1:xT4XX6w5Sa3dhg50JrYyy3e4WPYo/+WjY/BXtqXVunU= github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.1.0 h1:ArRd27pSm66f7cCBDPS77wvxiS4IRjFatpzVBD7Aojc= github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.1.0/go.mod h1:KdVvdk4gb7iatuHZgIkIqvJlWHBtjCJLUtD/uO/FkWw= -github.com/aws/aws-sdk-go-v2/service/dynamodb v1.11.0 h1:te+nIFwPf5Bi/cZvd9g/+EF0gkJT3c0J/5+NMx0NBZg= -github.com/aws/aws-sdk-go-v2/service/dynamodb v1.11.0/go.mod h1:ELltfl9ri0n4sZ/VjPZBgemNMd9mYIpCAuZhc7NP7l4= -github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.5.0 h1:lPLbw4Gn59uoKqvOfSnkJr54XWk5Ak1NK20ZEiSWb3U= -github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.5.0/go.mod h1:80NaCIH9YU3rzTTs/J/ECATjXuRqzo/wB6ukO6MZ0XY= -github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.3.3 h1:ru9+IpkVIuDvIkm9Q0DEjtWHnh6ITDoZo8fH2dIjlqQ= -github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.3.3/go.mod h1:zOyLMYyg60yyZpOCniAUuibWVqTU4TuLmMa/Wh4P+HA= +github.com/aws/aws-sdk-go-v2/service/dynamodb v1.12.0 h1:D8rIbaDiA6PoRU+ojAjnftqmy75VopL2bTJPIo2G9Ko= +github.com/aws/aws-sdk-go-v2/service/dynamodb v1.12.0/go.mod h1:tzN6ge+2WmkKBjlA6vfLxTQFb9VSQCPSVhpYz+gwYNc= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.6.0 h1:zQlcDaAP0sk7jVSkBnBd4fc07M8bSAi6k1WjL48tB9M= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.6.0/go.mod h1:lzucjNKa47J5dstwdXwRrDLMEeWwOYK2+BgUKR3xthI= +github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.4.0 h1:LVQAt97u5GvWNuzQbF/N2awTtLVwHUBDTMDypVC+xiw= +github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.4.0/go.mod h1:XTo3HdhcCDMl/syHC+mGJyP3Qmm1BD3MNtuLlZCqiP8= github.com/aws/aws-sdk-go-v2/service/route53 v1.16.0 h1:xWoSNH+tQ0x0yHqCanSN9l3hAmc6nb+UaTabp720GvU= github.com/aws/aws-sdk-go-v2/service/route53 v1.16.0/go.mod h1:WrCwjYVD8F60mRhhq2dec95l3IJKMXT+DAhTvBPfGJo= -github.com/aws/smithy-go v1.9.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= github.com/aws/smithy-go v1.9.1 h1:5vetTooLk4hPWV8q6ym6+lXKAT1Urnm49YkrRKo2J8o= github.com/aws/smithy-go v1.9.1/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= From 4300ac383261d2f90caea0c52a5da7578558cf2e Mon Sep 17 00:00:00 2001 From: Jenny Nilsen Date: Mon, 17 Jan 2022 18:31:29 +0000 Subject: [PATCH 03/11] allowing users to use the default attribute setter in addition to their own --- .../aws/aws-sdk-go-v2/otelaws/aws.go | 16 +-- .../aws/aws-sdk-go-v2/otelaws/config.go | 4 +- .../otelaws/dynamodbattributes_test.go | 5 +- .../otelaws/test/dynamodbattributes_test.go | 105 +++++++++--------- 4 files changed, 64 insertions(+), 66 deletions(-) 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 8232df5fa5d..09bbe3781e6 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 @@ -39,7 +39,7 @@ type AttributeSetter func(context.Context, middleware.InitializeInput) []attribu type otelMiddlewares struct { tracer trace.Tracer - attributesetter AttributeSetter + attributesetter []AttributeSetter } func (m otelMiddlewares) initializeMiddlewareBefore(stack *middleware.Stack) error { @@ -64,11 +64,8 @@ func (m otelMiddlewares) initializeMiddlewareAfter(stack *middleware.Stack) erro RegionAttr(v2Middleware.GetRegion(ctx)), OperationAttr(v2Middleware.GetOperationName(ctx)), } - if m.attributesetter != nil { - attributes = append( - attributes, - m.attributesetter(ctx, in)..., - ) + for _, setter := range m.attributesetter { + attributes = append(attributes, setter(ctx, in)...) } ctx, span := m.tracer.Start(ctx, serviceID, @@ -118,13 +115,16 @@ func (m otelMiddlewares) deserializeMiddleware(stack *middleware.Stack) error { // Please see more details in https://aws.github.io/aws-sdk-go-v2/docs/middleware/ func AppendMiddlewares(apiOptions *[]func(*middleware.Stack) error, opts ...Option) { cfg := config{ - TracerProvider: otel.GetTracerProvider(), - AttributeSetter: DefaultAttributeSetter, + TracerProvider: otel.GetTracerProvider(), } for _, opt := range opts { opt.apply(&cfg) } + if cfg.AttributeSetter == nil { + cfg.AttributeSetter = []AttributeSetter{DefaultAttributeSetter} + } + m := otelMiddlewares{tracer: cfg.TracerProvider.Tracer(tracerName, trace.WithInstrumentationVersion(SemVersion())), attributesetter: cfg.AttributeSetter} diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go index 19232c63aa1..7025e3d5ba1 100755 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go @@ -20,7 +20,7 @@ import ( type config struct { TracerProvider trace.TracerProvider - AttributeSetter AttributeSetter + AttributeSetter []AttributeSetter } // Option applies an option value. @@ -48,7 +48,7 @@ func WithTracerProvider(provider trace.TracerProvider) Option { // WithAttributeSetter specifies an attribute setter function for setting service specific attributes. // If none is specified, the service will be determined by the DefaultAttributeSetter function and the corresponding attributes will be included. -func WithAttributeSetter(attributesetter AttributeSetter) Option { +func WithAttributeSetter(attributesetter []AttributeSetter) Option { return optionFunc(func(cfg *config) { if attributesetter != nil { diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go index e81b96f8a26..26c266b123f 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go @@ -20,11 +20,10 @@ import ( "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/dynamodb" - "github.com/stretchr/testify/assert" dtypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" "github.com/aws/smithy-go/middleware" + "github.com/stretchr/testify/assert" "go.opentelemetry.io/otel/attribute" - ) func TestDynamodbTagsBatchGetItemInput(t *testing.T) { @@ -323,4 +322,4 @@ func TestDynamodbTagsUpdateTableInput(t *testing.T) { assert.Contains(t, attributes, attribute.String("aws.dynamodb.global_secondary_index_updates", "[{\"Create\":{\"IndexName\":\"index1\",\"KeySchema\":[{\"AttributeName\":\"attribute\",\"KeyType\":\"HASH\"}],\"Projection\":{\"NonKeyAttributes\":[\"attribute1\",\"attribute2\"],\"ProjectionType\":\"ALL\"},\"ProvisionedThroughput\":null},\"Delete\":null,\"Update\":null}]")) assert.Contains(t, attributes, attribute.Int("aws.dynamodb.provisioned_read_capacity", 123)) assert.Contains(t, attributes, attribute.Int("aws.dynamodb.provisioned_write_capacity", 456)) -} \ No newline at end of file +} 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 ec39ae55897..ea34da54b76 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 @@ -32,7 +32,6 @@ import ( sdktrace "go.opentelemetry.io/otel/sdk/trace" "go.opentelemetry.io/otel/sdk/trace/tracetest" "go.opentelemetry.io/otel/trace" - ) func TestDynamodbTags(t *testing.T) { @@ -47,62 +46,62 @@ func TestDynamodbTags(t *testing.T) { expectedStatusCode: 200, } - server := httptest.NewServer(http.HandlerFunc( - func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(cases.responseStatus) - })) - defer server.Close() + server := httptest.NewServer(http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(cases.responseStatus) + })) + defer server.Close() - t.Run("dynamodb tags", func(t *testing.T) { - sr := tracetest.NewSpanRecorder() - provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) + t.Run("dynamodb tags", func(t *testing.T) { + sr := tracetest.NewSpanRecorder() + provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) - svc := dynamodb.NewFromConfig(aws.Config{ - Region: cases.expectedRegion, - EndpointResolverWithOptions: aws.EndpointResolverWithOptionsFunc( - func(service, region string, _ ...interface{}) (aws.Endpoint, error) { - return aws.Endpoint{ - URL: server.URL, - SigningName: "dynamodb", - }, nil - }, - ), - Retryer: func() aws.Retryer { - return aws.NopRetryer{} - }, - }) - _, err := svc.GetItem(context.Background(), &dynamodb.GetItemInput{ - TableName: aws.String("table1"), - ConsistentRead: aws.Bool(false), - ProjectionExpression: aws.String("test"), - Key: map[string]dtypes.AttributeValue{ - "id": &dtypes.AttributeValueMemberS{Value: "test"}, + svc := dynamodb.NewFromConfig(aws.Config{ + Region: cases.expectedRegion, + EndpointResolverWithOptions: aws.EndpointResolverWithOptionsFunc( + func(service, region string, _ ...interface{}) (aws.Endpoint, error) { + return aws.Endpoint{ + URL: server.URL, + SigningName: "dynamodb", + }, nil }, - }, func(options *dynamodb.Options) { - otelaws.AppendMiddlewares( - &options.APIOptions, otelaws.WithAttributeSetter(otelaws.DynamodbAttributeSetter), otelaws.WithTracerProvider(provider)) - }) + ), + Retryer: func() aws.Retryer { + return aws.NopRetryer{} + }, + }) + _, err := svc.GetItem(context.Background(), &dynamodb.GetItemInput{ + TableName: aws.String("table1"), + ConsistentRead: aws.Bool(false), + ProjectionExpression: aws.String("test"), + Key: map[string]dtypes.AttributeValue{ + "id": &dtypes.AttributeValueMemberS{Value: "test"}, + }, + }, func(options *dynamodb.Options) { + otelaws.AppendMiddlewares( + &options.APIOptions, otelaws.WithAttributeSetter([]otelaws.AttributeSetter{otelaws.DynamodbAttributeSetter}), otelaws.WithTracerProvider(provider)) + }) - if cases.expectedError == codes.Unset { - assert.NoError(t, err) - } else { - assert.NotNil(t, err) - } + if cases.expectedError == codes.Unset { + assert.NoError(t, err) + } else { + assert.NotNil(t, err) + } - spans := sr.Ended() - require.Len(t, spans, 1) - span := spans[0] + spans := sr.Ended() + require.Len(t, spans, 1) + span := spans[0] - assert.Equal(t, "DynamoDB", span.Name()) - assert.Equal(t, trace.SpanKindClient, span.SpanKind()) - attrs := span.Attributes() - assert.Contains(t, attrs, attribute.Int("http.status_code", cases.expectedStatusCode)) - assert.Contains(t, attrs, attribute.String("aws.service", "DynamoDB")) - assert.Contains(t, attrs, attribute.String("aws.region", cases.expectedRegion)) - assert.Contains(t, attrs, attribute.String("aws.operation", "GetItem")) - assert.Contains(t, attrs, attribute.String("aws.dynamodb.table_names", "table1")) - assert.Contains(t, attrs, attribute.String("aws.dynamodb.projection", "test")) - assert.Contains(t, attrs, attribute.Bool("aws.dynamodb.consistent_read", false)) + assert.Equal(t, "DynamoDB", span.Name()) + assert.Equal(t, trace.SpanKindClient, span.SpanKind()) + attrs := span.Attributes() + assert.Contains(t, attrs, attribute.Int("http.status_code", cases.expectedStatusCode)) + assert.Contains(t, attrs, attribute.String("aws.service", "DynamoDB")) + assert.Contains(t, attrs, attribute.String("aws.region", cases.expectedRegion)) + assert.Contains(t, attrs, attribute.String("aws.operation", "GetItem")) + assert.Contains(t, attrs, attribute.String("aws.dynamodb.table_names", "table1")) + assert.Contains(t, attrs, attribute.String("aws.dynamodb.projection", "test")) + assert.Contains(t, attrs, attribute.Bool("aws.dynamodb.consistent_read", false)) - }) -} \ No newline at end of file + }) +} From c74eee925f88c543ea10d69378508282ad560a5f Mon Sep 17 00:00:00 2001 From: Jenny Nilsen Date: Thu, 20 Jan 2022 16:44:06 +0000 Subject: [PATCH 04/11] making setting attributesetters more flexible --- .../aws/aws-sdk-go-v2/otelaws/config.go | 6 +- .../otelaws/test/dynamodbattributes_test.go | 93 ++++++++++++++++++- .../aws/aws-sdk-go-v2/otelaws/test/go.mod | 1 + .../aws/aws-sdk-go-v2/otelaws/test/go.sum | 3 +- 4 files changed, 98 insertions(+), 5 deletions(-) diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go index 7025e3d5ba1..fa40055909d 100755 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go @@ -48,11 +48,11 @@ func WithTracerProvider(provider trace.TracerProvider) Option { // WithAttributeSetter specifies an attribute setter function for setting service specific attributes. // If none is specified, the service will be determined by the DefaultAttributeSetter function and the corresponding attributes will be included. -func WithAttributeSetter(attributesetter []AttributeSetter) Option { +func WithAttributeSetter(attributesetters ...AttributeSetter) Option { return optionFunc(func(cfg *config) { - if attributesetter != nil { - cfg.AttributeSetter = attributesetter + if len(attributesetters) > 0 { + cfg.AttributeSetter = append(cfg.AttributeSetter, attributesetters...) } }) } 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 ea34da54b76..8eaaf9b3836 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 @@ -23,6 +23,7 @@ import ( "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/dynamodb" dtypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" + "github.com/aws/smithy-go/middleware" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -79,7 +80,7 @@ func TestDynamodbTags(t *testing.T) { }, }, func(options *dynamodb.Options) { otelaws.AppendMiddlewares( - &options.APIOptions, otelaws.WithAttributeSetter([]otelaws.AttributeSetter{otelaws.DynamodbAttributeSetter}), otelaws.WithTracerProvider(provider)) + &options.APIOptions, otelaws.WithAttributeSetter(otelaws.DynamodbAttributeSetter), otelaws.WithTracerProvider(provider)) }) if cases.expectedError == codes.Unset { @@ -105,3 +106,93 @@ func TestDynamodbTags(t *testing.T) { }) } + +func TestDynamodbTagsCustomSetter(t *testing.T) { + cases := struct { + responseStatus int + expectedRegion string + expectedStatusCode int + expectedError codes.Code + }{ + responseStatus: 200, + expectedRegion: "us-west-2", + expectedStatusCode: 200, + } + + server := httptest.NewServer(http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(cases.responseStatus) + })) + defer server.Close() + + t.Run("dynamodb tags", func(t *testing.T) { + sr := tracetest.NewSpanRecorder() + provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) + + svc := dynamodb.NewFromConfig(aws.Config{ + Region: cases.expectedRegion, + EndpointResolverWithOptions: aws.EndpointResolverWithOptionsFunc( + func(service, region string, _ ...interface{}) (aws.Endpoint, error) { + return aws.Endpoint{ + URL: server.URL, + SigningName: "dynamodb", + }, nil + }, + ), + Retryer: func() aws.Retryer { + return aws.NopRetryer{} + }, + }) + + mycustomsetter := otelaws.AttributeSetter(func(context.Context, middleware.InitializeInput) []attribute.KeyValue { + customAttributes := []attribute.KeyValue{ + { + Key: "customattribute2key", + Value: attribute.StringValue("customattribute2value"), + }, + { + Key: "customattribute1key", + Value: attribute.StringValue("customattribute1value"), + }, + } + + return customAttributes + }) + + _, err := svc.GetItem(context.Background(), &dynamodb.GetItemInput{ + TableName: aws.String("table1"), + ConsistentRead: aws.Bool(false), + ProjectionExpression: aws.String("test"), + Key: map[string]dtypes.AttributeValue{ + "id": &dtypes.AttributeValueMemberS{Value: "test"}, + }, + }, func(options *dynamodb.Options) { + otelaws.AppendMiddlewares( + &options.APIOptions, otelaws.WithAttributeSetter(otelaws.DynamodbAttributeSetter, mycustomsetter), otelaws.WithTracerProvider(provider)) + }) + + if cases.expectedError == codes.Unset { + assert.NoError(t, err) + } else { + assert.NotNil(t, err) + } + + spans := sr.Ended() + require.Len(t, spans, 1) + span := spans[0] + + assert.Equal(t, "DynamoDB", span.Name()) + assert.Equal(t, trace.SpanKindClient, span.SpanKind()) + attrs := span.Attributes() + assert.Contains(t, attrs, attribute.Int("http.status_code", cases.expectedStatusCode)) + assert.Contains(t, attrs, attribute.String("aws.service", "DynamoDB")) + assert.Contains(t, attrs, attribute.String("aws.region", cases.expectedRegion)) + assert.Contains(t, attrs, attribute.String("aws.operation", "GetItem")) + assert.Contains(t, attrs, attribute.String("aws.dynamodb.table_names", "table1")) + assert.Contains(t, attrs, attribute.String("aws.dynamodb.projection", "test")) + assert.Contains(t, attrs, attribute.Bool("aws.dynamodb.consistent_read", false)) + assert.Contains(t, attrs, attribute.String("customattribute2key", "customattribute2value")) + assert.Contains(t, attrs, attribute.String("customattribute1key", "customattribute1value")) + + }) +} diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.mod b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.mod index c6af50bcad1..145d159b5d1 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.mod +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.mod @@ -6,6 +6,7 @@ require ( github.com/aws/aws-sdk-go-v2 v1.12.0 github.com/aws/aws-sdk-go-v2/service/dynamodb v1.12.0 github.com/aws/aws-sdk-go-v2/service/route53 v1.16.0 + github.com/aws/smithy-go v1.10.0 github.com/stretchr/testify v1.7.0 go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws v0.27.0 go.opentelemetry.io/otel v1.3.0 diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.sum b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.sum index 539d763f2b5..4480323270e 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.sum +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test/go.sum @@ -12,8 +12,9 @@ github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.4.0 h1:LVQAt github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.4.0/go.mod h1:XTo3HdhcCDMl/syHC+mGJyP3Qmm1BD3MNtuLlZCqiP8= github.com/aws/aws-sdk-go-v2/service/route53 v1.16.0 h1:xWoSNH+tQ0x0yHqCanSN9l3hAmc6nb+UaTabp720GvU= github.com/aws/aws-sdk-go-v2/service/route53 v1.16.0/go.mod h1:WrCwjYVD8F60mRhhq2dec95l3IJKMXT+DAhTvBPfGJo= -github.com/aws/smithy-go v1.9.1 h1:5vetTooLk4hPWV8q6ym6+lXKAT1Urnm49YkrRKo2J8o= github.com/aws/smithy-go v1.9.1/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= +github.com/aws/smithy-go v1.10.0 h1:gsoZQMNHnX+PaghNw4ynPsyGP7aUCqx5sY2dlPQsZ0w= +github.com/aws/smithy-go v1.10.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= From 05c38472bac444a5cedc8d80ab47f5b09eccd4b5 Mon Sep 17 00:00:00 2001 From: Jenny Nilsen <44649100+jennynilsen@users.noreply.github.com> Date: Fri, 28 Jan 2022 15:52:43 +0000 Subject: [PATCH 05/11] Apply suggestions from code review Co-authored-by: Tyler Yahn --- CHANGELOG.md | 4 +++- .../aws/aws-sdk-go-v2/otelaws/aws.go | 5 ++-- .../aws/aws-sdk-go-v2/otelaws/config.go | 1 - .../otelaws/dynamodbattributes.go | 23 +++---------------- .../otelaws/dynamodbattributes_test.go | 2 +- 5 files changed, 10 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 851609c4f04..a0bc5aeb048 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] ### Added -- Dynamodb spans will now have the appropriate attributes added for the operation being performed, this is detected automatically but it is also now possible to provide a custom function to set attributes using `WithAttributeSetter` + +- Dynamodb spans created with the `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/` package will now have the appropriate database attributes added for the operation being performed. + These attributes are detected automatically, but it is also now possible to provide a custom function to set attributes using `WithAttributeSetter`. (#1582) - Add `WithClientTrace` option to `otelhttp.Transport` (#875) 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 09bbe3781e6..f72ffffac87 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 @@ -39,7 +39,7 @@ type AttributeSetter func(context.Context, middleware.InitializeInput) []attribu type otelMiddlewares struct { tracer trace.Tracer - attributesetter []AttributeSetter + attributeSetter []AttributeSetter } func (m otelMiddlewares) initializeMiddlewareBefore(stack *middleware.Stack) error { @@ -60,7 +60,8 @@ func (m otelMiddlewares) initializeMiddlewareAfter(stack *middleware.Stack) erro serviceID := v2Middleware.GetServiceID(ctx) - attributes := []attribute.KeyValue{ServiceAttr(serviceID), + attributes := []attribute.KeyValue{ + ServiceAttr(serviceID), RegionAttr(v2Middleware.GetRegion(ctx)), OperationAttr(v2Middleware.GetOperationName(ctx)), } diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go index fa40055909d..f79d6ebc70f 100755 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go @@ -49,7 +49,6 @@ func WithTracerProvider(provider trace.TracerProvider) Option { // WithAttributeSetter specifies an attribute setter function for setting service specific attributes. // If none is specified, the service will be determined by the DefaultAttributeSetter function and the corresponding attributes will be included. func WithAttributeSetter(attributesetters ...AttributeSetter) Option { - return optionFunc(func(cfg *config) { if len(attributesetters) > 0 { cfg.AttributeSetter = append(cfg.AttributeSetter, attributesetters...) diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go index 1172253a9e5..8144913669a 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go @@ -25,7 +25,7 @@ import ( semconv "go.opentelemetry.io/otel/semconv/v1.7.0" ) -func DynamodbAttributeSetter(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue { +func DynamoDBAttributeSetter(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue { dynamodbAttributes := []attribute.KeyValue{ { @@ -47,14 +47,7 @@ func DynamodbAttributeSetter(ctx context.Context, in middleware.InitializeInput) dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProjectionKey.String(*v.ProjectionExpression)) } - case *dynamodb.BatchGetItemInput: - var tableNames []string - for k, _ := range v.RequestItems { - tableNames = append(tableNames, k) - } - dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.StringSlice(tableNames)) - - case *dynamodb.BatchWriteItemInput: + case *dynamodb.BatchGetItemInput, *dynamodb.BatchWriteItemInput: var tableNames []string for k, _ := range v.RequestItems { tableNames = append(tableNames, k) @@ -79,13 +72,7 @@ func DynamodbAttributeSetter(ctx context.Context, in middleware.InitializeInput) dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProvisionedWriteCapacityKey.Int64(*v.ProvisionedThroughput.WriteCapacityUnits)) } - case *dynamodb.DeleteItemInput: - dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) - - case *dynamodb.DeleteTableInput: - dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) - - case *dynamodb.DescribeTableInput: + case *dynamodb.DeleteItemInput, *dynamodb.DeleteTableInput, *dynamodb.DescribeTableInput, *dynamodb.PutItemInput: dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) case *dynamodb.ListTablesInput: @@ -98,10 +85,6 @@ func DynamodbAttributeSetter(ctx context.Context, in middleware.InitializeInput) dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBLimitKey.Int(int(*v.Limit))) } - case *dynamodb.PutItemInput: - - dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) - case *dynamodb.QueryInput: dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go index 26c266b123f..7ec1614a342 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go @@ -26,7 +26,7 @@ import ( "go.opentelemetry.io/otel/attribute" ) -func TestDynamodbTagsBatchGetItemInput(t *testing.T) { +func TestDynamoDBTagsBatchGetItemInput(t *testing.T) { input := middleware.InitializeInput{ Parameters: &dynamodb.BatchGetItemInput{ RequestItems: map[string]dtypes.KeysAndAttributes{ From b4851a5741d1b24858ef7ff42ed6caf6bcf5352c Mon Sep 17 00:00:00 2001 From: Jenny Nilsen Date: Fri, 28 Jan 2022 17:24:39 +0000 Subject: [PATCH 06/11] Revert "Apply suggestions from code review" This reverts commit 05c38472bac444a5cedc8d80ab47f5b09eccd4b5. --- CHANGELOG.md | 4 +--- .../aws/aws-sdk-go-v2/otelaws/aws.go | 5 ++-- .../aws/aws-sdk-go-v2/otelaws/config.go | 1 + .../otelaws/dynamodbattributes.go | 23 ++++++++++++++++--- .../otelaws/dynamodbattributes_test.go | 2 +- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0bc5aeb048..851609c4f04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,9 +9,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] ### Added - -- Dynamodb spans created with the `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/` package will now have the appropriate database attributes added for the operation being performed. - These attributes are detected automatically, but it is also now possible to provide a custom function to set attributes using `WithAttributeSetter`. (#1582) +- Dynamodb spans will now have the appropriate attributes added for the operation being performed, this is detected automatically but it is also now possible to provide a custom function to set attributes using `WithAttributeSetter` - Add `WithClientTrace` option to `otelhttp.Transport` (#875) 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 f72ffffac87..09bbe3781e6 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 @@ -39,7 +39,7 @@ type AttributeSetter func(context.Context, middleware.InitializeInput) []attribu type otelMiddlewares struct { tracer trace.Tracer - attributeSetter []AttributeSetter + attributesetter []AttributeSetter } func (m otelMiddlewares) initializeMiddlewareBefore(stack *middleware.Stack) error { @@ -60,8 +60,7 @@ func (m otelMiddlewares) initializeMiddlewareAfter(stack *middleware.Stack) erro serviceID := v2Middleware.GetServiceID(ctx) - attributes := []attribute.KeyValue{ - ServiceAttr(serviceID), + attributes := []attribute.KeyValue{ServiceAttr(serviceID), RegionAttr(v2Middleware.GetRegion(ctx)), OperationAttr(v2Middleware.GetOperationName(ctx)), } diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go index f79d6ebc70f..fa40055909d 100755 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go @@ -49,6 +49,7 @@ func WithTracerProvider(provider trace.TracerProvider) Option { // WithAttributeSetter specifies an attribute setter function for setting service specific attributes. // If none is specified, the service will be determined by the DefaultAttributeSetter function and the corresponding attributes will be included. func WithAttributeSetter(attributesetters ...AttributeSetter) Option { + return optionFunc(func(cfg *config) { if len(attributesetters) > 0 { cfg.AttributeSetter = append(cfg.AttributeSetter, attributesetters...) diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go index 8144913669a..1172253a9e5 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go @@ -25,7 +25,7 @@ import ( semconv "go.opentelemetry.io/otel/semconv/v1.7.0" ) -func DynamoDBAttributeSetter(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue { +func DynamodbAttributeSetter(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue { dynamodbAttributes := []attribute.KeyValue{ { @@ -47,7 +47,14 @@ func DynamoDBAttributeSetter(ctx context.Context, in middleware.InitializeInput) dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProjectionKey.String(*v.ProjectionExpression)) } - case *dynamodb.BatchGetItemInput, *dynamodb.BatchWriteItemInput: + case *dynamodb.BatchGetItemInput: + var tableNames []string + for k, _ := range v.RequestItems { + tableNames = append(tableNames, k) + } + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.StringSlice(tableNames)) + + case *dynamodb.BatchWriteItemInput: var tableNames []string for k, _ := range v.RequestItems { tableNames = append(tableNames, k) @@ -72,7 +79,13 @@ func DynamoDBAttributeSetter(ctx context.Context, in middleware.InitializeInput) dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProvisionedWriteCapacityKey.Int64(*v.ProvisionedThroughput.WriteCapacityUnits)) } - case *dynamodb.DeleteItemInput, *dynamodb.DeleteTableInput, *dynamodb.DescribeTableInput, *dynamodb.PutItemInput: + case *dynamodb.DeleteItemInput: + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) + + case *dynamodb.DeleteTableInput: + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) + + case *dynamodb.DescribeTableInput: dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) case *dynamodb.ListTablesInput: @@ -85,6 +98,10 @@ func DynamoDBAttributeSetter(ctx context.Context, in middleware.InitializeInput) dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBLimitKey.Int(int(*v.Limit))) } + case *dynamodb.PutItemInput: + + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) + case *dynamodb.QueryInput: dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go index 7ec1614a342..26c266b123f 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go @@ -26,7 +26,7 @@ import ( "go.opentelemetry.io/otel/attribute" ) -func TestDynamoDBTagsBatchGetItemInput(t *testing.T) { +func TestDynamodbTagsBatchGetItemInput(t *testing.T) { input := middleware.InitializeInput{ Parameters: &dynamodb.BatchGetItemInput{ RequestItems: map[string]dtypes.KeysAndAttributes{ From 1f3aa4c79f9c2e768558dec9197f5b397fb508b9 Mon Sep 17 00:00:00 2001 From: Jenny Nilsen <44649100+jennynilsen@users.noreply.github.com> Date: Fri, 28 Jan 2022 17:43:49 +0000 Subject: [PATCH 07/11] Apply suggestions from code review Co-authored-by: Tyler Yahn --- CHANGELOG.md | 4 +++- instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/aws.go | 5 +++-- .../github.com/aws/aws-sdk-go-v2/otelaws/config.go | 1 - .../aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 851609c4f04..a0bc5aeb048 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] ### Added -- Dynamodb spans will now have the appropriate attributes added for the operation being performed, this is detected automatically but it is also now possible to provide a custom function to set attributes using `WithAttributeSetter` + +- Dynamodb spans created with the `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/` package will now have the appropriate database attributes added for the operation being performed. + These attributes are detected automatically, but it is also now possible to provide a custom function to set attributes using `WithAttributeSetter`. (#1582) - Add `WithClientTrace` option to `otelhttp.Transport` (#875) 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 09bbe3781e6..f72ffffac87 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 @@ -39,7 +39,7 @@ type AttributeSetter func(context.Context, middleware.InitializeInput) []attribu type otelMiddlewares struct { tracer trace.Tracer - attributesetter []AttributeSetter + attributeSetter []AttributeSetter } func (m otelMiddlewares) initializeMiddlewareBefore(stack *middleware.Stack) error { @@ -60,7 +60,8 @@ func (m otelMiddlewares) initializeMiddlewareAfter(stack *middleware.Stack) erro serviceID := v2Middleware.GetServiceID(ctx) - attributes := []attribute.KeyValue{ServiceAttr(serviceID), + attributes := []attribute.KeyValue{ + ServiceAttr(serviceID), RegionAttr(v2Middleware.GetRegion(ctx)), OperationAttr(v2Middleware.GetOperationName(ctx)), } diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go index fa40055909d..f79d6ebc70f 100755 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go @@ -49,7 +49,6 @@ func WithTracerProvider(provider trace.TracerProvider) Option { // WithAttributeSetter specifies an attribute setter function for setting service specific attributes. // If none is specified, the service will be determined by the DefaultAttributeSetter function and the corresponding attributes will be included. func WithAttributeSetter(attributesetters ...AttributeSetter) Option { - return optionFunc(func(cfg *config) { if len(attributesetters) > 0 { cfg.AttributeSetter = append(cfg.AttributeSetter, attributesetters...) diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go index 1172253a9e5..e63a2d63fd4 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go @@ -25,7 +25,7 @@ import ( semconv "go.opentelemetry.io/otel/semconv/v1.7.0" ) -func DynamodbAttributeSetter(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue { +func DynamoDBAttributeSetter(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue { dynamodbAttributes := []attribute.KeyValue{ { From e553bd477bbfb03f2f103b9b497502004830f46d Mon Sep 17 00:00:00 2001 From: Jenny Nilsen Date: Mon, 31 Jan 2022 16:47:35 +0000 Subject: [PATCH 08/11] adding comments for exported functions and types --- .../aws/aws-sdk-go-v2/otelaws/attributes.go | 4 +++- .../aws/aws-sdk-go-v2/otelaws/aws.go | 5 ++-- .../otelaws/dynamodbattributes.go | 8 ++----- .../otelaws/dynamodbattributes_test.go | 24 +++++++++---------- .../otelaws/test/dynamodbattributes_test.go | 4 ++-- 5 files changed, 22 insertions(+), 23 deletions(-) diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/attributes.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/attributes.go index c71d680582d..d115c663009 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/attributes.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/attributes.go @@ -32,7 +32,7 @@ const ( ) var servicemap map[string]AttributeSetter = map[string]AttributeSetter{ - "dynamodb": DynamodbAttributeSetter, + "dynamodb": DynamoDBAttributeSetter, } func OperationAttr(operation string) attribute.KeyValue { @@ -51,6 +51,8 @@ func RequestIDAttr(requestID string) attribute.KeyValue { return RequestIDKey.String(requestID) } +// DefaultAttributeSetter checks to see if there are service specific attributes available to set for the aws service. +// If there are service specific attributes available then they will be included. func DefaultAttributeSetter(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue { serviceID := v2Middleware.GetServiceID(ctx) 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 f72ffffac87..19f79ebd028 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 @@ -35,6 +35,7 @@ const ( type spanTimestampKey struct{} +// AttributeSetter returns an array of KeyValue pairs, it can be used to set custom attributes. type AttributeSetter func(context.Context, middleware.InitializeInput) []attribute.KeyValue type otelMiddlewares struct { @@ -65,7 +66,7 @@ func (m otelMiddlewares) initializeMiddlewareAfter(stack *middleware.Stack) erro RegionAttr(v2Middleware.GetRegion(ctx)), OperationAttr(v2Middleware.GetOperationName(ctx)), } - for _, setter := range m.attributesetter { + for _, setter := range m.attributeSetter { attributes = append(attributes, setter(ctx, in)...) } @@ -128,7 +129,7 @@ func AppendMiddlewares(apiOptions *[]func(*middleware.Stack) error, opts ...Opti m := otelMiddlewares{tracer: cfg.TracerProvider.Tracer(tracerName, trace.WithInstrumentationVersion(SemVersion())), - attributesetter: cfg.AttributeSetter} + attributeSetter: cfg.AttributeSetter} *apiOptions = append(*apiOptions, m.initializeMiddlewareBefore, m.initializeMiddlewareAfter, m.deserializeMiddleware) } diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go index e63a2d63fd4..cd781915cb1 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go @@ -25,14 +25,10 @@ import ( semconv "go.opentelemetry.io/otel/semconv/v1.7.0" ) +// DynamoDBAttributeSetter sets DynamoDB specific attributes depending on the the DynamoDB operation being performed. func DynamoDBAttributeSetter(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue { - dynamodbAttributes := []attribute.KeyValue{ - { - Key: semconv.DBSystemKey, - Value: attribute.StringValue("dynamodb"), - }, - } + dynamodbAttributes := []attribute.KeyValue{semconv.DBSystemKey.String("dynamodb")} switch v := in.Parameters.(type) { case *dynamodb.GetItemInput: diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go index 26c266b123f..66950100a09 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go @@ -41,7 +41,7 @@ func TestDynamodbTagsBatchGetItemInput(t *testing.T) { }, } - attributes := DynamodbAttributeSetter(context.TODO(), input) + attributes := DynamoDBAttributeSetter(context.TODO(), input) assert.Contains(t, attributes, attribute.StringSlice("aws.dynamodb.table_names", []string{"table1"})) } @@ -70,7 +70,7 @@ func TestDynamodbTagsBatchWriteItemInput(t *testing.T) { }, } - attributes := DynamodbAttributeSetter(context.TODO(), input) + attributes := DynamoDBAttributeSetter(context.TODO(), input) assert.Contains(t, attributes, attribute.StringSlice("aws.dynamodb.table_names", []string{"table1"})) } @@ -124,7 +124,7 @@ func TestDynamodbTagsCreateTableInput(t *testing.T) { }, } - attributes := DynamodbAttributeSetter(context.TODO(), input) + attributes := DynamoDBAttributeSetter(context.TODO(), input) assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) assert.Contains(t, attributes, attribute.String("aws.dynamodb.global_secondary_indexes", "[{\"IndexName\":\"index1\",\"KeySchema\":[{\"AttributeName\":\"attributename\",\"KeyType\":\"HASH\"}],\"Projection\":{\"NonKeyAttributes\":[\"non-key-attributes\"],\"ProjectionType\":\"\"},\"ProvisionedThroughput\":null}]")) @@ -142,7 +142,7 @@ func TestDynamodbTagsDeleteItemInput(t *testing.T) { TableName: aws.String("table1"), }, } - attributes := DynamodbAttributeSetter(context.TODO(), input) + attributes := DynamoDBAttributeSetter(context.TODO(), input) assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) } @@ -153,7 +153,7 @@ func TestDynamodbTagsDeleteTableInput(t *testing.T) { TableName: aws.String("table1"), }, } - attributes := DynamodbAttributeSetter(context.TODO(), input) + attributes := DynamoDBAttributeSetter(context.TODO(), input) assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) @@ -165,7 +165,7 @@ func TestDynamodbTagsDescribeTableInput(t *testing.T) { TableName: aws.String("table1"), }, } - attributes := DynamodbAttributeSetter(context.TODO(), input) + attributes := DynamoDBAttributeSetter(context.TODO(), input) assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) } @@ -177,7 +177,7 @@ func TestDynamodbTagsListTablesInput(t *testing.T) { Limit: aws.Int32(10), }, } - attributes := DynamodbAttributeSetter(context.TODO(), input) + attributes := DynamoDBAttributeSetter(context.TODO(), input) assert.Contains(t, attributes, attribute.String("aws.dynamodb.exclusive_start_table", "table1")) assert.Contains(t, attributes, attribute.Int("aws.dynamodb.limit", 10)) @@ -196,7 +196,7 @@ func TestDynamodbTagsPutItemInput(t *testing.T) { }, } - attributes := DynamodbAttributeSetter(context.TODO(), input) + attributes := DynamoDBAttributeSetter(context.TODO(), input) assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) } @@ -222,7 +222,7 @@ func TestDynamodbTagsQueryInput(t *testing.T) { }, } - attributes := DynamodbAttributeSetter(context.TODO(), input) + attributes := DynamoDBAttributeSetter(context.TODO(), input) assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "table1")) assert.Contains(t, attributes, attribute.Bool("aws.dynamodb.consistent_read", true)) @@ -248,7 +248,7 @@ func TestDynamodbTagsScanInput(t *testing.T) { }, } - attributes := DynamodbAttributeSetter(context.TODO(), input) + attributes := DynamoDBAttributeSetter(context.TODO(), input) assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "my-table")) assert.Contains(t, attributes, attribute.Bool("aws.dynamodb.consistent_read", true)) @@ -275,7 +275,7 @@ func TestDynamodbTagsUpdateItemInput(t *testing.T) { }, } - attributes := DynamodbAttributeSetter(context.TODO(), input) + attributes := DynamoDBAttributeSetter(context.TODO(), input) assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "my-table")) @@ -315,7 +315,7 @@ func TestDynamodbTagsUpdateTableInput(t *testing.T) { }, } - attributes := DynamodbAttributeSetter(context.TODO(), input) + attributes := DynamoDBAttributeSetter(context.TODO(), input) assert.Contains(t, attributes, attribute.String("aws.dynamodb.table_names", "my-table")) assert.Contains(t, attributes, attribute.String("aws.dynamodb.attribute_definitions", "[{\"AttributeName\":\"id\",\"AttributeType\":\"S\"}]")) 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 8eaaf9b3836..5ca35ec9d4e 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 @@ -80,7 +80,7 @@ func TestDynamodbTags(t *testing.T) { }, }, func(options *dynamodb.Options) { otelaws.AppendMiddlewares( - &options.APIOptions, otelaws.WithAttributeSetter(otelaws.DynamodbAttributeSetter), otelaws.WithTracerProvider(provider)) + &options.APIOptions, otelaws.WithAttributeSetter(otelaws.DynamoDBAttributeSetter), otelaws.WithTracerProvider(provider)) }) if cases.expectedError == codes.Unset { @@ -168,7 +168,7 @@ func TestDynamodbTagsCustomSetter(t *testing.T) { }, }, func(options *dynamodb.Options) { otelaws.AppendMiddlewares( - &options.APIOptions, otelaws.WithAttributeSetter(otelaws.DynamodbAttributeSetter, mycustomsetter), otelaws.WithTracerProvider(provider)) + &options.APIOptions, otelaws.WithAttributeSetter(otelaws.DynamoDBAttributeSetter, mycustomsetter), otelaws.WithTracerProvider(provider)) }) if cases.expectedError == codes.Unset { From 007cad8e4229d1841cb102c8a724a9df7b0307cc Mon Sep 17 00:00:00 2001 From: Jenny Nilsen <44649100+jennynilsen@users.noreply.github.com> Date: Wed, 9 Feb 2022 15:23:39 +0000 Subject: [PATCH 09/11] Apply suggestions from code review Co-authored-by: Tyler Yahn --- CHANGELOG.md | 2 +- .../github.com/aws/aws-sdk-go-v2/otelaws/attributes.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0bc5aeb048..c9ea906c397 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Added -- Dynamodb spans created with the `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/` package will now have the appropriate database attributes added for the operation being performed. +- DynamoDB spans created with the `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws` package will now have the appropriate database attributes added for the operation being performed. These attributes are detected automatically, but it is also now possible to provide a custom function to set attributes using `WithAttributeSetter`. (#1582) - Add `WithClientTrace` option to `otelhttp.Transport` (#875) diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/attributes.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/attributes.go index d115c663009..40063887dc9 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/attributes.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/attributes.go @@ -51,7 +51,7 @@ func RequestIDAttr(requestID string) attribute.KeyValue { return RequestIDKey.String(requestID) } -// DefaultAttributeSetter checks to see if there are service specific attributes available to set for the aws service. +// DefaultAttributeSetter checks to see if there are service specific attributes available to set for the AWS service. // If there are service specific attributes available then they will be included. func DefaultAttributeSetter(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue { From f888275d081729adbb8f380cb5698574619f827b Mon Sep 17 00:00:00 2001 From: Jenny Nilsen Date: Wed, 9 Feb 2022 15:42:45 +0000 Subject: [PATCH 10/11] fixing test and applying suggestions --- .../github.com/aws/aws-sdk-go-v2/otelaws/config.go | 4 +--- .../aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go index f79d6ebc70f..4dc07f404fb 100755 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go @@ -50,8 +50,6 @@ func WithTracerProvider(provider trace.TracerProvider) Option { // If none is specified, the service will be determined by the DefaultAttributeSetter function and the corresponding attributes will be included. func WithAttributeSetter(attributesetters ...AttributeSetter) Option { return optionFunc(func(cfg *config) { - if len(attributesetters) > 0 { - cfg.AttributeSetter = append(cfg.AttributeSetter, attributesetters...) - } + cfg.AttributeSetter = append(cfg.AttributeSetter, attributesetters...) }) } diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go index cd781915cb1..84f0ef2a4aa 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes.go @@ -45,14 +45,14 @@ func DynamoDBAttributeSetter(ctx context.Context, in middleware.InitializeInput) case *dynamodb.BatchGetItemInput: var tableNames []string - for k, _ := range v.RequestItems { + for k := range v.RequestItems { tableNames = append(tableNames, k) } dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.StringSlice(tableNames)) case *dynamodb.BatchWriteItemInput: var tableNames []string - for k, _ := range v.RequestItems { + for k := range v.RequestItems { tableNames = append(tableNames, k) } dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.StringSlice(tableNames)) From 7d86f7729086f0306638793ee2f4c19b42cb30de Mon Sep 17 00:00:00 2001 From: Jenny Nilsen Date: Fri, 25 Feb 2022 17:42:07 +0000 Subject: [PATCH 11/11] fixing checks --- .../github.com/aws/aws-lambda-go/otellambda/example/go.sum | 7 +++++++ .../aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go | 1 + 2 files changed, 8 insertions(+) diff --git a/instrumentation/github.com/aws/aws-lambda-go/otellambda/example/go.sum b/instrumentation/github.com/aws/aws-lambda-go/otellambda/example/go.sum index c2c3d57e407..8dce652945e 100644 --- a/instrumentation/github.com/aws/aws-lambda-go/otellambda/example/go.sum +++ b/instrumentation/github.com/aws/aws-lambda-go/otellambda/example/go.sum @@ -17,8 +17,12 @@ github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.2.0 h1:3ADoioDMOtF4uiK59vC github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.2.0/go.mod h1:BsCSJHx5DnDXIrOcqB8KN1/B+hXLG/bi4Y6Vjcx/x9E= github.com/aws/aws-sdk-go-v2/internal/ini v1.3.5 h1:ixotxbfTCFpqbuwFv/RcZwyzhkxPSYDYEMcj4niB5Uk= github.com/aws/aws-sdk-go-v2/internal/ini v1.3.5/go.mod h1:R3sWUqPcfXSiF/LSFJhjyJmpg9uV6yP2yv3YZZjldVI= +github.com/aws/aws-sdk-go-v2/service/dynamodb v1.13.0 h1:Xlmdkxi8WcIwX5Cy9BS+scWcmvARw8pg0bi7kaeERUY= +github.com/aws/aws-sdk-go-v2/service/dynamodb v1.13.0/go.mod h1:eNvoR4P1XQN7xElmYA8cWeFENLY3pfsj/5nFRItzXnA= github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.7.0 h1:F1diQIOkNn8jcez4173r+PLPdkWK7chy74r3fKpDrLI= github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.7.0/go.mod h1:8ctElVINyp+SjhoZZceUAZw78glZH6R8ox5MVNu5j2s= +github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.5.0 h1:tzVhIPr/psp8Gb2Blst9mq6HklkhAGPqv2eaiSq6yoU= +github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.5.0/go.mod h1:u0rI/Mm45zCJe86J5kvPfG7pYzkVZzNjEkoTVbfOYE8= github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.7.0 h1:4QAOB3KrvI1ApJK14sliGr3Ie2pjyvNypn/lfzDHfUw= github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.7.0/go.mod h1:K/qPe6AP2TGYv4l6n7c88zh9jWBDf6nHhvg1fx/EWfU= github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.11.0 h1:XAe+PDnaBELHr25qaJKfB415V4CKFWE8H+prUreql8k= @@ -46,7 +50,9 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= +github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -78,6 +84,7 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1N golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ= diff --git a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go index 66950100a09..2a104a7a881 100644 --- a/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go +++ b/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/dynamodbattributes_test.go @@ -23,6 +23,7 @@ import ( dtypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" "github.com/aws/smithy-go/middleware" "github.com/stretchr/testify/assert" + "go.opentelemetry.io/otel/attribute" )