| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package xrayconfig |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"os" |
| 9 |
"reflect" |
| 10 |
"runtime" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/aws/aws-lambda-go/lambdacontext" |
| 15 |
"github.com/stretchr/testify/assert" |
| 16 |
"go.opentelemetry.io/otel/propagation" |
| 17 |
"go.opentelemetry.io/otel/trace" |
| 18 |
v1common "go.opentelemetry.io/proto/otlp/common/v1" |
| 19 |
v1resource "go.opentelemetry.io/proto/otlp/resource/v1" |
| 20 |
v1trace "go.opentelemetry.io/proto/otlp/trace/v1" |
| 21 |
|
| 22 |
"go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda" |
| 23 |
"go.opentelemetry.io/contrib/propagators/aws/xray" |
| 24 |
) |
| 25 |
|
| 26 |
const miB = 1 << 20 |
| 27 |
|
| 28 |
func TestEventToCarrier(t *testing.T) { |
| 29 |
t.Setenv("_X_AMZN_TRACE_ID", "traceID") |
| 30 |
carrier := xrayEventToCarrier([]byte{}) |
| 31 |
|
| 32 |
assert.Equal(t, "traceID", carrier.Get("X-Amzn-Trace-Id")) |
| 33 |
} |
| 34 |
|
| 35 |
func TestEventToCarrierWithPropagator(t *testing.T) { |
| 36 |
t.Setenv("_X_AMZN_TRACE_ID", "Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1") |
| 37 |
carrier := xrayEventToCarrier([]byte{}) |
| 38 |
ctx := xray.Propagator{}.Extract(t.Context(), carrier) |
| 39 |
|
| 40 |
expectedTraceID, _ := trace.TraceIDFromHex("5759e988bd862e3fe1be46a994272793") |
| 41 |
expectedSpanID, _ := trace.SpanIDFromHex("53995c3f42cd8ad8") |
| 42 |
expectedCtx := trace.ContextWithRemoteSpanContext(t.Context(), trace.NewSpanContext(trace.SpanContextConfig{ |
| 43 |
TraceID: expectedTraceID, |
| 44 |
SpanID: expectedSpanID, |
| 45 |
TraceFlags: trace.FlagsSampled, |
| 46 |
TraceState: trace.TraceState{}, |
| 47 |
Remote: true, |
| 48 |
})) |
| 49 |
|
| 50 |
assert.Equal(t, expectedCtx, ctx) |
| 51 |
} |
| 52 |
|
| 53 |
func setEnvVars(t *testing.T) { |
| 54 |
t.Setenv("AWS_LAMBDA_FUNCTION_NAME", "testFunction") |
| 55 |
t.Setenv("AWS_REGION", "us-texas-1") |
| 56 |
t.Setenv("AWS_LAMBDA_FUNCTION_VERSION", "$LATEST") |
| 57 |
t.Setenv("AWS_LAMBDA_LOG_STREAM_NAME", "2023/01/01/[$LATEST]5d1edb9e525d486696cf01a3503487bc") |
| 58 |
t.Setenv("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "128") |
| 59 |
t.Setenv("_X_AMZN_TRACE_ID", "Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1") |
| 60 |
|
| 61 |
// fix issue: "The requested service provider could not be loaded or initialized." |
| 62 |
// Guess: The env for Windows in GitHub action is incomplete |
| 63 |
if runtime.GOOS == "windows" && os.Getenv("SYSTEMROOT") == "" { |
| 64 |
t.Setenv("SYSTEMROOT", `C:\Windows`) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
// Vars for end to end testing. |
| 69 |
var ( |
| 70 |
mockLambdaContext = lambdacontext.LambdaContext{ |
| 71 |
AwsRequestID: "123", |
| 72 |
InvokedFunctionArn: "arn:partition:service:region:account-id:resource-type:resource-id", |
| 73 |
Identity: lambdacontext.CognitoIdentity{}, |
| 74 |
ClientContext: lambdacontext.ClientContext{}, |
| 75 |
} |
| 76 |
mockContext = xray.Propagator{}.Extract(lambdacontext.NewContext(context.Background(), &mockLambdaContext), |
| 77 |
propagation.HeaderCarrier{ |
| 78 |
"X-Amzn-Trace-Id": []string{"Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1"}, |
| 79 |
}) |
| 80 |
|
| 81 |
expectedSpans = v1trace.ScopeSpans{ |
| 82 |
Scope: &v1common.InstrumentationScope{Name: "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda", Version: otellambda.Version}, |
| 83 |
Spans: []*v1trace.Span{{ |
| 84 |
TraceId: []byte{0x57, 0x59, 0xe9, 0x88, 0xbd, 0x86, 0x2e, 0x3f, 0xe1, 0xbe, 0x46, 0xa9, 0x94, 0x27, 0x27, 0x93}, |
| 85 |
SpanId: nil, |
| 86 |
TraceState: "", |
| 87 |
ParentSpanId: []byte{0x53, 0x99, 0x5c, 0x3f, 0x42, 0xcd, 0x8a, 0xd8}, |
| 88 |
Name: "testFunction", |
| 89 |
Kind: v1trace.Span_SPAN_KIND_SERVER, |
| 90 |
StartTimeUnixNano: 0, |
| 91 |
EndTimeUnixNano: 0, |
| 92 |
Attributes: []*v1common.KeyValue{ |
| 93 |
{Key: "faas.invocation_id", Value: &v1common.AnyValue{Value: &v1common.AnyValue_StringValue{StringValue: "123"}}}, |
| 94 |
{Key: "aws.lambda.invoked_arn", Value: &v1common.AnyValue{Value: &v1common.AnyValue_StringValue{StringValue: "arn:partition:service:region:account-id:resource-type:resource-id"}}}, |
| 95 |
{Key: "cloud.account.id", Value: &v1common.AnyValue{Value: &v1common.AnyValue_StringValue{StringValue: "account-id"}}}, |
| 96 |
}, |
| 97 |
DroppedAttributesCount: 0, |
| 98 |
Events: nil, |
| 99 |
DroppedEventsCount: 0, |
| 100 |
Links: nil, |
| 101 |
DroppedLinksCount: 0, |
| 102 |
Status: &v1trace.Status{Code: v1trace.Status_STATUS_CODE_UNSET}, |
| 103 |
}}, |
| 104 |
SchemaUrl: "", |
| 105 |
} |
| 106 |
|
| 107 |
expectedSpanResource = v1resource.Resource{ |
| 108 |
Attributes: []*v1common.KeyValue{ |
| 109 |
{Key: "cloud.provider", Value: &v1common.AnyValue{Value: &v1common.AnyValue_StringValue{StringValue: "aws"}}}, |
| 110 |
{Key: "cloud.region", Value: &v1common.AnyValue{Value: &v1common.AnyValue_StringValue{StringValue: "us-texas-1"}}}, |
| 111 |
{Key: "faas.instance", Value: &v1common.AnyValue{Value: &v1common.AnyValue_StringValue{StringValue: "2023/01/01/[$LATEST]5d1edb9e525d486696cf01a3503487bc"}}}, |
| 112 |
{Key: "faas.max_memory", Value: &v1common.AnyValue{Value: &v1common.AnyValue_IntValue{IntValue: 128 * miB}}}, |
| 113 |
{Key: "faas.name", Value: &v1common.AnyValue{Value: &v1common.AnyValue_StringValue{StringValue: "testFunction"}}}, |
| 114 |
{Key: "faas.version", Value: &v1common.AnyValue{Value: &v1common.AnyValue_StringValue{StringValue: "$LATEST"}}}, |
| 115 |
}, |
| 116 |
DroppedAttributesCount: 0, |
| 117 |
} |
| 118 |
|
| 119 |
expectedResourceSpans = v1trace.ResourceSpans{ |
| 120 |
Resource: &expectedSpanResource, |
| 121 |
ScopeSpans: []*v1trace.ScopeSpans{&expectedSpans}, |
| 122 |
SchemaUrl: "", |
| 123 |
} |
| 124 |
) |
| 125 |
|
| 126 |
func assertResourceEquals(t *testing.T, expected, actual *v1resource.Resource) { |
| 127 |
assert.Len(t, actual.Attributes, 6) |
| 128 |
assert.Equal(t, expected.Attributes[0].String(), actual.Attributes[0].String()) |
| 129 |
assert.Equal(t, expected.Attributes[1].String(), actual.Attributes[1].String()) |
| 130 |
assert.Equal(t, expected.Attributes[2].String(), actual.Attributes[2].String()) |
| 131 |
assert.Equal(t, expected.Attributes[3].String(), actual.Attributes[3].String()) |
| 132 |
assert.Equal(t, expected.Attributes[4].String(), actual.Attributes[4].String()) |
| 133 |
assert.Equal(t, expected.Attributes[5].String(), actual.Attributes[5].String()) |
| 134 |
assert.Equal(t, expected.DroppedAttributesCount, actual.DroppedAttributesCount) |
| 135 |
} |
| 136 |
|
| 137 |
// ignore timestamps and SpanID since time is obviously variable, |
| 138 |
// and SpanID is randomized when using xray IDGenerator. |
| 139 |
func assertSpanEqualsIgnoreTimeAndSpanID(t *testing.T, expected, actual *v1trace.ResourceSpans) { |
| 140 |
assert.Equal(t, expected.ScopeSpans[0].Scope, actual.ScopeSpans[0].Scope) |
| 141 |
|
| 142 |
actualSpan := actual.ScopeSpans[0].Spans[0] |
| 143 |
expectedSpan := expected.ScopeSpans[0].Spans[0] |
| 144 |
assert.Equal(t, expectedSpan.Name, actualSpan.Name) |
| 145 |
assert.Equal(t, expectedSpan.ParentSpanId, actualSpan.ParentSpanId) |
| 146 |
assert.Equal(t, expectedSpan.Kind, actualSpan.Kind) |
| 147 |
assert.Equal(t, expectedSpan.Attributes, actualSpan.Attributes) |
| 148 |
assert.Equal(t, expectedSpan.Events, actualSpan.Events) |
| 149 |
assert.Equal(t, expectedSpan.Links, actualSpan.Links) |
| 150 |
assert.Equal(t, expectedSpan.Status, actualSpan.Status) |
| 151 |
assert.Equal(t, expectedSpan.DroppedAttributesCount, actualSpan.DroppedAttributesCount) |
| 152 |
assert.Equal(t, expectedSpan.DroppedEventsCount, actualSpan.DroppedEventsCount) |
| 153 |
assert.Equal(t, expectedSpan.DroppedLinksCount, actualSpan.DroppedLinksCount) |
| 154 |
|
| 155 |
assertResourceEquals(t, expected.Resource, actual.Resource) |
| 156 |
} |
| 157 |
|
| 158 |
func TestWrapEndToEnd(t *testing.T) { |
| 159 |
setEnvVars(t) |
| 160 |
|
| 161 |
ctx := t.Context() |
| 162 |
tp, err := NewTracerProvider(ctx) |
| 163 |
assert.NoError(t, err) |
| 164 |
|
| 165 |
customerHandler := func() (string, error) { |
| 166 |
return "hello world", nil |
| 167 |
} |
| 168 |
mockCollector := runMockCollectorAtEndpoint(t, "localhost:4317") |
| 169 |
defer func() { |
| 170 |
_ = mockCollector.Stop() |
| 171 |
}() |
| 172 |
<-time.After(5 * time.Millisecond) |
| 173 |
|
| 174 |
wrapped := otellambda.InstrumentHandler(customerHandler, WithRecommendedOptions(tp)...) |
| 175 |
wrappedCallable := reflect.ValueOf(wrapped) |
| 176 |
resp := wrappedCallable.Call([]reflect.Value{reflect.ValueOf(mockContext)}) |
| 177 |
assert.Len(t, resp, 2) |
| 178 |
assert.Equal(t, "hello world", resp[0].Interface()) |
| 179 |
assert.Nil(t, resp[1].Interface()) |
| 180 |
|
| 181 |
resSpans := mockCollector.getResourceSpans() |
| 182 |
assert.Len(t, resSpans, 1) |
| 183 |
assertSpanEqualsIgnoreTimeAndSpanID(t, &expectedResourceSpans, resSpans[0]) |
| 184 |
} |