Skip to content

Commit

Permalink
feat(otel): Updates to otel span processor (#6113)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mydea committed Nov 2, 2022
1 parent a1a6450 commit 0595ee8
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 123 deletions.
16 changes: 12 additions & 4 deletions packages/opentelemetry-node/src/spanprocessor.ts
Expand Up @@ -45,7 +45,7 @@ export class SentrySpanProcessor implements OtelSpanProcessor {
const sentryChildSpan = sentryParentSpan.startChild({
description: otelSpan.name,
// instrumentor: 'otel',
startTimestamp: otelSpan.startTime[0],
startTimestamp: convertOtelTimeToSeconds(otelSpan.startTime),
spanId: otelSpanId,
});

Expand All @@ -56,7 +56,7 @@ export class SentrySpanProcessor implements OtelSpanProcessor {
name: otelSpan.name,
...traceCtx,
// instrumentor: 'otel',
startTimestamp: otelSpan.startTime[0],
startTimestamp: convertOtelTimeToSeconds(otelSpan.startTime),
spanId: otelSpanId,
});

Expand All @@ -82,7 +82,7 @@ export class SentrySpanProcessor implements OtelSpanProcessor {
finishTransactionWithContextFromOtelData(sentrySpan, otelSpan);
} else {
updateSpanWithOtelData(sentrySpan, otelSpan);
sentrySpan.finish(otelSpan.endTime[0]);
sentrySpan.finish(convertOtelTimeToSeconds(otelSpan.endTime));
}

this._map.delete(otelSpanId);
Expand Down Expand Up @@ -123,7 +123,7 @@ function finishTransactionWithContextFromOtelData(transaction: Transaction, otel
resource: otelSpan.resource.attributes,
});

transaction.finish(otelSpan.endTime[0]);
transaction.finish(convertOtelTimeToSeconds(otelSpan.endTime));
});
}

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

function updateTransactionWithOtelData(transaction: Transaction, otelSpan: OtelSpan): void {
transaction.setStatus(mapOtelStatus(otelSpan));

const { op, description } = parseSpanDescription(otelSpan);
transaction.op = op;
transaction.name = description;
}

function convertOtelTimeToSeconds([seconds, nano]: [number, number]): number {
return seconds + nano / 1_000_000_000;
}
Expand Up @@ -69,7 +69,7 @@ function descriptionForDbSystem(otelSpan: OtelSpan, _dbSystem: AttributeValue):
}

function descriptionForHttpMethod(otelSpan: OtelSpan, httpMethod: AttributeValue): SpanDescription {
const { name, kind } = otelSpan;
const { name, kind, attributes } = otelSpan;

const opParts = ['http'];

Expand All @@ -82,8 +82,15 @@ function descriptionForHttpMethod(otelSpan: OtelSpan, httpMethod: AttributeValue
break;
}

// Ex. description="GET /api/users/{user_id}".
const description = `${httpMethod} ${name}`;
// Ex. /api/users
const httpPath = attributes[SemanticAttributes.HTTP_ROUTE] || attributes[SemanticAttributes.HTTP_TARGET];

if (!httpPath) {
return { op: opParts.join('.'), description: name };
}

// Ex. description="GET /api/users".
const description = `${httpMethod} ${httpPath}`;

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

0 comments on commit 0595ee8

Please sign in to comment.