Skip to content

Commit

Permalink
Fix extraction of trace context from SQS messageAttributes (open-tele…
Browse files Browse the repository at this point in the history
  • Loading branch information
RafalSumislawski authored Nov 16, 2023
1 parent 4c7ad4c commit 3a84a4b
Showing 1 changed file with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ import {
AWSXRAY_TRACE_ID_HEADER,
AWSXRayPropagator,
} from '@opentelemetry/propagator-aws-xray';
import { APIGatewayProxyEventHeaders, SQSEvent, SQSRecord } from 'aws-lambda';
import { APIGatewayProxyEventHeaders, SQSEvent, SQSMessageAttributes, SQSRecord } from 'aws-lambda';
import { isDefined } from '../utils';
import {
LambdaTrigger,
TriggerSpanInitializerResult,
validateRecordsEvent,
} from './common';
import { TriggerOrigin } from './index';
import { defaultTextMapGetter } from '@opentelemetry/api/build/src/propagation/TextMapPropagator';

const sqsAttributes: Attributes = {
[SemanticAttributes.FAAS_TRIGGER]: 'pubsub',
Expand Down Expand Up @@ -75,13 +74,29 @@ function getSqsLinkFromMessageAttributes(record: SQSRecord): Link | undefined {
const extractedContext = propagation.extract(
otelContext.active(),
messageAttributes,
defaultTextMapGetter
messageAttributesTextMapGetter
);
const context = trace.getSpan(extractedContext)?.spanContext();
if (!context) return undefined;
return { context };
}

const messageAttributesTextMapGetter: TextMapGetter<SQSMessageAttributes> = {
get(carrier, key) {
if (carrier == null) {
return undefined;
}
return carrier[key]?.stringValue;
},

keys(carrier) {
if (carrier == null) {
return [];
}
return Object.keys(carrier);
},
};

/*
as defined here
https://opentelemetry.io/docs/specs/otel/trace/semantic_conventions/instrumentation/aws-lambda/#sqs-event
Expand Down

0 comments on commit 3a84a4b

Please sign in to comment.