8000 feat(otel): Updates to otel span processor (#6113) · yongdamsh/sentry-javascript@0595ee8 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 0595ee8

Browse files
authored
feat(otel): Updates to otel span processor (getsentry#6113)
* feat(otel): Improve otel span description extraction * fix(otel): ensure otel timestamps are correctly converted * feat(otel): Also parse otel span descriptions for Sentry transactions
1 parent a1a6450 commit 0595ee8

File tree

3 files changed

+214
-123
lines changed

3 files changed

+214
-123
lines changed

packages/opentelemetry-node/src/spanprocessor.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class SentrySpanProcessor implements OtelSpanProcessor {
4545
const sentryChildSpan = sentryParentSpan.startChild({
4646
description: otelSpan.name,
4747
// instrumentor: 'otel',
48-
startTimestamp: otelSpan.startTime[0],
48+
startTimestamp: convertOtelTimeToSeconds(otelSpan.startTime),
4949
spanId: otelSpanId,
5050
});
5151

@@ -56,7 +56,7 @@ export class SentrySpanProcessor implements OtelSpanProcessor {
5656
name: otelSpan.name,
5757
...traceCtx,
5858
// instrumentor: 'otel',
59-
startTimestamp: otelSpan.startTime[0],
59+
startTimestamp: convertOtelTimeToSeconds(otelSpan.startTime),
6060
spanId: otelSpanId,
6161
});
6262

@@ -82,7 +82,7 @@ export class SentrySpanProcessor implements OtelSpanProcessor {
8282
finishTransactionWithContextFromOtelData(sentrySpan, otelSpan);
8383
} else {
8484
updateSpanWithOtelData(sentrySpan, otelSpan);
85-
sentrySpan.finish(otelSpan.endTime[0]);
85+
sentrySpan.finish(convertOtelTimeToSeconds(otelSpan.endTime));
8686
}
8787

8888
this._map.delete(otelSpanId);
@@ -123,7 +123,7 @@ function finishTransactionWithContextFromOtelData(transaction: Transaction, otel
123123
resource: otelSpan.resource.attributes,
124124
});
125125

126-
transaction.finish(otelSpan.endTime[0]);
126+
transaction.finish(convertOtelTimeToSeconds(otelSpan.endTime));
127127
});
128128
}
129129

@@ -145,4 +145,12 @@ function updateSpanWithOtelData(sentrySpan: SentrySpan, otelSpan: OtelSpan): voi
145145

146146
function updateTransactionWithOtelData(transaction: Transaction, otelSpan: OtelSpan): void {
147147
transaction.setStatus(mapOtelStatus(otelSpan));
148+
149+
const { op, description } = parseSpanDescription(otelSpan);
150+
transaction.op = op;
151+
transaction.name = description;
152+
}
153+
154+
function convertOtelTimeToSeconds([seconds, nano]: [number, number]): number {
155+
return seconds + nano / 1_000_000_000;
148156
}

packages/opentelemetry-node/src/utils/parse-otel-span-description.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function descriptionForDbSystem(otelSpan: OtelSpan, _dbSystem: AttributeValue):
6969
}
7070

7171
function descriptionForHttpMethod(otelSpan: OtelSpan, httpMethod: AttributeValue): SpanDescription {
72-
const { name, kind } = otelSpan;
72+
const { name, kind, attributes } = otelSpan;
7373

7474
const opParts = ['http'];
7575

@@ -82,8 +82,15 @@ function descriptionForHttpMethod(otelSpan: OtelSpan, httpMethod: AttributeValue
8282
break;
8383
}
8484

85-
// Ex. description="GET /api/users/{user_id}".
86-
const description = `${httpMethod} ${name}`;
85+
// Ex. /api/users
86+
const httpPath = attributes[SemanticAttributes.HTTP_ROUTE] || attributes[SemanticAttributes.HTTP_TARGET];
87+
88+
if (!httpPath) {
89+
return { op: opParts.join('.'), description: name };
90+
}
91+
92+
// Ex. description="GET /api/users".
93+
const description = `${httpMethod} ${httpPath}`;
8794

8895
return { op: opParts.join('.'), description };
8996
}

0 commit comments

Comments
 (0)
0