forked from DataDog/datadog-lambda-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddlambda_example_test.go
54 lines (42 loc) · 1.24 KB
/
ddlambda_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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)
}