-
Notifications
You must be signed in to change notification settings - Fork 444
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
[serverless] Add S3 span pointers #3083
Open
nhulston
wants to merge
9
commits into
main
Choose a base branch
from
nicholas.hulston/s3-span-pointers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+314
−4
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
61a3728
finish span after aws request is done, not when the request is made
nhulston f4cd35d
create add AddSpanPointers and hash calcation logic
nhulston f74fcae
implement `handleS3Operation`
nhulston 74b3b21
add tests for `generatePointerHash`
nhulston 81f4054
add tests for `handleS3Operation`
nhulston 94588ff
add copyright
nhulston b59b212
fix package name (no underscore)
nhulston 5b18226
lint
nhulston dee04b6
move `WithErrorCheck` to before `span.Finish()`
nhulston File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016 Datadog, Inc. | ||
|
||
package spanpointers | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"github.com/aws/smithy-go/middleware" | ||
smithyhttp "github.com/aws/smithy-go/transport/http" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
"gopkg.in/DataDog/dd-trace-go.v1/internal/log" | ||
"strings" | ||
) | ||
|
||
const ( | ||
// SpanPointerHashLengthBytes 16 bytes = 32 chars. | ||
// See https://github.com/DataDog/dd-span-pointer-rules/blob/main/README.md#general-hashing-rules | ||
SpanPointerHashLengthBytes = 16 | ||
PointerDownDirection = "d" | ||
LinkKind = "span-pointer" | ||
S3PointerKind = "aws.s3.object" | ||
) | ||
|
||
func AddSpanPointers(serviceID string, in middleware.DeserializeInput, out middleware.DeserializeOutput, span tracer.Span) { | ||
switch serviceID { | ||
case "S3": | ||
handleS3Operation(in, out, span) | ||
} | ||
} | ||
|
||
func handleS3Operation(in middleware.DeserializeInput, out middleware.DeserializeOutput, span tracer.Span) { | ||
spanWithLinks, ok := span.(tracer.SpanWithLinks) | ||
if !ok { | ||
return | ||
} | ||
|
||
req, ok := in.Request.(*smithyhttp.Request) | ||
if !ok { | ||
return | ||
} | ||
res, ok := out.RawResponse.(*smithyhttp.Response) | ||
if !ok { | ||
return | ||
} | ||
|
||
// URL format: https://BUCKETNAME.s3.REGION.amazonaws.com/KEYNAME?x-id=OPERATIONNAME | ||
key := strings.TrimPrefix(req.URL.Path, "/") | ||
bucket := strings.Split(req.URL.Host, ".")[0] | ||
// the AWS SDK sometimes wraps the eTag in quotes | ||
etag := strings.Trim(res.Header.Get("ETag"), "\"") | ||
if key == "" || bucket == "" || etag == "" { | ||
log.Debug("Unable to create S3 span pointer because required fields could not be found.") | ||
return | ||
} | ||
|
||
// Hash calculation rules: https://github.com/DataDog/dd-span-pointer-rules/blob/main/AWS/S3/Object/README.md | ||
components := []string{bucket, key, etag} | ||
hash := generatePointerHash(components) | ||
|
||
link := ddtrace.SpanLink{ | ||
// We leave trace_id, span_id, trade_id_high, tracestate, and flags as 0 or empty. | ||
// The Datadog frontend will use `ptr.hash` to find the linked span. | ||
TraceID: 0, | ||
SpanID: 0, | ||
TraceIDHigh: 0, | ||
Flags: 0, | ||
Tracestate: "", | ||
Attributes: map[string]string{ | ||
"ptr.kind": S3PointerKind, | ||
"ptr.dir": PointerDownDirection, | ||
"ptr.hash": hash, | ||
"link.kind": LinkKind, | ||
}, | ||
} | ||
|
||
spanWithLinks.AddSpanLink(link) | ||
} | ||
|
||
// generatePointerHash generates a unique hash from an array of strings by joining them with | before hashing. | ||
// Used to uniquely identify AWS requests for span pointers. | ||
// Returns a 32-character hash uniquely identifying the components. | ||
func generatePointerHash(components []string) string { | ||
h := sha256.New() | ||
for i, component := range components { | ||
if i > 0 { | ||
h.Write([]byte("|")) | ||
} | ||
h.Write([]byte(component)) | ||
} | ||
|
||
fullHash := h.Sum(nil) | ||
return hex.EncodeToString(fullHash[:SpanPointerHashLengthBytes]) | ||
} |
190 changes: 190 additions & 0 deletions
190
contrib/aws/internal/span_pointers/span_pointers_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016 Datadog, Inc. | ||
|
||
package spanpointers | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"github.com/aws/smithy-go/middleware" | ||
smithyhttp "github.com/aws/smithy-go/transport/http" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/mocktracer" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
func TestGeneratePointerHash(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
components []string | ||
expectedHash string | ||
}{ | ||
{ | ||
name: "basic values", | ||
components: []string{ | ||
"some-bucket", | ||
"some-key.data", | ||
"ab12ef34", | ||
}, | ||
expectedHash: "e721375466d4116ab551213fdea08413", | ||
}, | ||
{ | ||
name: "non-ascii key", | ||
components: []string{ | ||
"some-bucket", | ||
"some-key.你好", | ||
"ab12ef34", | ||
}, | ||
expectedHash: "d1333a04b9928ab462b5c6cadfa401f4", | ||
}, | ||
{ | ||
name: "multipart-upload", | ||
components: []string{ | ||
"some-bucket", | ||
"some-key.data", | ||
"ab12ef34-5", | ||
}, | ||
expectedHash: "2b90dffc37ebc7bc610152c3dc72af9f", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := generatePointerHash(tt.components) | ||
if got != tt.expectedHash { | ||
t.Errorf("GeneratePointerHash() = %v, want %v", got, tt.expectedHash) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestHandleS3Operation(t *testing.T) { | ||
mt := mocktracer.Start() | ||
defer mt.Stop() | ||
|
||
tests := []struct { | ||
name string | ||
bucket string | ||
key string | ||
etag string | ||
expectedHash string | ||
expectSuccess bool | ||
}{ | ||
{ | ||
name: "basic operation", | ||
bucket: "some-bucket", | ||
key: "some-key.data", | ||
etag: "ab12ef34", | ||
expectedHash: "e721375466d4116ab551213fdea08413", | ||
expectSuccess: true, | ||
}, | ||
{ | ||
name: "quoted etag", | ||
bucket: "some-bucket", | ||
key: "some-key.data", | ||
etag: "\"ab12ef34\"", | ||
expectedHash: "e721375466d4116ab551213fdea08413", | ||
expectSuccess: true, | ||
}, | ||
{ | ||
name: "non-ascii key", | ||
bucket: "some-bucket", | ||
key: "some-key.你好", | ||
etag: "ab12ef34", | ||
expectedHash: "d1333a04b9928ab462b5c6cadfa401f4", | ||
expectSuccess: true, | ||
}, | ||
{ | ||
name: "empty bucket", | ||
bucket: "", | ||
key: "some_key", | ||
etag: "some_etag", | ||
expectSuccess: false, | ||
}, | ||
{ | ||
name: "empty key", | ||
bucket: "some_bucket", | ||
key: "", | ||
etag: "some_etag", | ||
expectSuccess: false, | ||
}, | ||
{ | ||
name: "empty etag", | ||
bucket: "some_bucket", | ||
key: "some_key", | ||
etag: "", | ||
expectSuccess: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctx := context.Background() | ||
span, ctx := tracer.StartSpanFromContext(ctx, "test.s3.operation") | ||
|
||
// Create request | ||
reqURL, _ := url.Parse("https://" + tt.bucket + ".s3.region.amazonaws.com/" + tt.key) | ||
req := &smithyhttp.Request{ | ||
Request: &http.Request{ | ||
URL: reqURL, | ||
}, | ||
} | ||
|
||
// Create response | ||
header := http.Header{} | ||
header.Set("ETag", tt.etag) | ||
res := &smithyhttp.Response{ | ||
Response: &http.Response{ | ||
Header: header, | ||
}, | ||
} | ||
|
||
// Create input/output | ||
in := middleware.DeserializeInput{ | ||
Request: req, | ||
} | ||
out := middleware.DeserializeOutput{ | ||
RawResponse: res, | ||
} | ||
|
||
AddSpanPointers("S3", in, out, span) | ||
span.Finish() | ||
spans := mt.FinishedSpans() | ||
if tt.expectSuccess { | ||
require.Len(t, spans, 1) | ||
meta := spans[0].Tags() | ||
|
||
spanLinks, exists := meta["_dd.span_links"] | ||
assert.True(t, exists, "Expected span links to be set") | ||
assert.NotEmpty(t, spanLinks, "Expected span links to not be empty") | ||
|
||
spanLinksStr, ok := spanLinks.(string) | ||
assert.True(t, ok, "Expected span links to be a string") | ||
|
||
var links []ddtrace.SpanLink | ||
err := json.Unmarshal([]byte(spanLinksStr), &links) | ||
require.NoError(t, err) | ||
require.Len(t, links, 1) | ||
|
||
attributes := links[0].Attributes | ||
assert.Equal(t, S3PointerKind, attributes["ptr.kind"]) | ||
assert.Equal(t, PointerDownDirection, attributes["ptr.dir"]) | ||
assert.Equal(t, LinkKind, attributes["link.kind"]) | ||
assert.Equal(t, tt.expectedHash, attributes["ptr.hash"]) | ||
} else { | ||
require.Len(t, spans, 1) | ||
tags := spans[0].Tags() | ||
_, exists := tags["_dd.span_links"] | ||
assert.False(t, exists, "Expected no span links to be set") | ||
} | ||
mt.Reset() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the aws span should live until we receive a response, not when the request is sent. (this is how AWS spans work in other tracers)
(from my testing this has barely any actual impact on the span end time)
This has to be done because we can't add links to a span that has already finished.