-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support custom root trace extraction from Lambda body content (#80)
* feat: support custom root trace extraction from Lambda body * feat: run go mod tidy * feat: code review improvements * Rename Context -> TraceContext to remove ambiguity with Go context. * Rename the DefaultTraceExtractor func to getHeadersFromEventHeaders. * feat: fix conflict and add documentation
- Loading branch information
Showing
6 changed files
with
143 additions
and
49 deletions.
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
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,54 @@ | ||
package ddlambda_test | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/aws/aws-lambda-go/events" | ||
|
||
ddlambda "github.com/DataDog/datadog-lambda-go" | ||
) | ||
|
||
var exampleSQSExtractor = func(ctx context.Context, ev json.RawMessage) map[string]string { | ||
eh := events.SQSEvent{} | ||
|
||
headers := map[string]string{} | ||
|
||
if err := json.Unmarshal(ev, &eh); err != nil { | ||
return headers | ||
} | ||
|
||
// Using SQS as a trigger with a batchSize=1 so its important we check for this as a single SQS message | ||
// will drive the execution of the handler. | ||
if len(eh.Records) != 1 { | ||
return headers | ||
} | ||
|
||
record := eh.Records[0] | ||
|
||
lowercaseHeaders := map[string]string{} | ||
for k, v := range record.MessageAttributes { | ||
if v.StringValue != nil { | ||
lowercaseHeaders[strings.ToLower(k)] = *v.StringValue | ||
} | ||
} | ||
|
||
return lowercaseHeaders | ||
} | ||
|
||
func TestCustomExtractorExample(t *testing.T) { | ||
handler := func(ctx context.Context, event events.SQSEvent) error { | ||
// Use the parent span retrieved from the SQS Message Attributes. | ||
span, _ := tracer.SpanFromContext(ctx) | ||
span.SetTag("key", "value") | ||
return nil | ||
} | ||
|
||
cfg := &ddlambda.Config{ | ||
TraceContextExtractor: exampleSQSExtractor, | ||
} | ||
ddlambda.WrapFunction(handler, cfg) | ||
} |
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
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