Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

otelaws: adding dynamodb attributes #1582

Merged
merged 16 commits into from Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Expand Up @@ -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`
jennynilsen marked this conversation as resolved.
Show resolved Hide resolved

- Add `WithClientTrace` option to `otelhttp.Transport` (#875)

Expand All @@ -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

jennynilsen marked this conversation as resolved.
Show resolved Hide resolved
- 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
Expand Down Expand Up @@ -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)
Expand Down
Expand Up @@ -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 (
Expand All @@ -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 {
jennynilsen marked this conversation as resolved.
Show resolved Hide resolved
servicemap := map[string]attributesetter{
"dynamodb": DynamodbAttributeSetter,
}
jennynilsen marked this conversation as resolved.
Show resolved Hide resolved

serviceID := v2Middleware.GetServiceID(ctx)

if val, ok := servicemap[serviceID]; ok {
function := val
attributes := function(ctx, in)
return attributes
}
jennynilsen marked this conversation as resolved.
Show resolved Hide resolved

return []attribute.KeyValue{}
}
29 changes: 23 additions & 6 deletions instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/aws.go
Expand Up @@ -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"
Expand All @@ -34,8 +35,11 @@ const (

type spanTimestampKey struct{}

type attributesetter func(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue
jennynilsen marked this conversation as resolved.
Show resolved Hide resolved

type otelMiddlewares struct {
tracer trace.Tracer
tracer trace.Tracer
attributesetter attributesetter
}

func (m otelMiddlewares) initializeMiddlewareBefore(stack *middleware.Stack) error {
Expand All @@ -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),
jennynilsen marked this conversation as resolved.
Show resolved Hide resolved
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()

Expand Down Expand Up @@ -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)

}
14 changes: 13 additions & 1 deletion instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config.go
Expand Up @@ -19,7 +19,8 @@ import (
)

type config struct {
TracerProvider trace.TracerProvider
TracerProvider trace.TracerProvider
AttributeSetter attributesetter
}

// Option applies an option value.
Expand All @@ -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 {

jennynilsen marked this conversation as resolved.
Show resolved Hide resolved
return optionFunc(func(cfg *config) {
if attributesetter != nil {
cfg.AttributeSetter = attributesetter
}
})
}
@@ -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 {
jennynilsen marked this conversation as resolved.
Show resolved Hide resolved
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

dynamodbAttributes := []attribute.KeyValue{
{
Key: semconv.DBSystemKey,
Value: attribute.StringValue("dynamodb"),
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
},
}

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))
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

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

}
Expand Up @@ -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
jennynilsen marked this conversation as resolved.
Show resolved Hide resolved
github.com/aws/smithy-go v1.9.1
go.opentelemetry.io/otel v1.3.0
go.opentelemetry.io/otel/trace v1.3.0
Expand Down
15 changes: 15 additions & 0 deletions 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=
Expand All @@ -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=
Expand All @@ -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=