Skip to content

feat(opentelemetry): Update OTEL packages & relax some version ranges #11580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
"access": "public"
},
"dependencies": {
"@opentelemetry/api": "1.7.0",
"@opentelemetry/context-async-hooks": "1.21.0",
"@opentelemetry/core": "1.21.0",
"@opentelemetry/api": "^1.8.0",
"@opentelemetry/context-async-hooks": "^1.23.0",
"@opentelemetry/core": "^1.23.0",
"@opentelemetry/instrumentation": "0.48.0",
"@opentelemetry/instrumentation-express": "0.35.0",
"@opentelemetry/instrumentation-fastify": "0.33.0",
Expand All @@ -69,9 +69,9 @@
"@opentelemetry/instrumentation-mysql2": "0.35.0",
"@opentelemetry/instrumentation-nestjs-core": "0.34.0",
"@opentelemetry/instrumentation-pg": "0.38.0",
"@opentelemetry/resources": "1.21.0",
"@opentelemetry/sdk-trace-base": "1.21.0",
"@opentelemetry/semantic-conventions": "1.21.0",
"@opentelemetry/resources": "^1.23.0",
"@opentelemetry/sdk-trace-base": "^1.23.0",
"@opentelemetry/semantic-conventions": "^1.23.0",
"@prisma/instrumentation": "5.9.0",
"@sentry/core": "8.0.0-beta.1",
"@sentry/opentelemetry": "8.0.0-beta.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/integrations/tracing/koa.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { KoaInstrumentation } from '@opentelemetry/instrumentation-koa';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
import {
captureException,
defineIntegration,
Expand All @@ -26,7 +26,7 @@ const _koaIntegration = (() => {
return;
}
const attributes = spanToJSON(span).data;
const route = attributes && attributes[SemanticAttributes.HTTP_ROUTE];
const route = attributes && attributes[SEMATTRS_HTTP_ROUTE];
const method = info.context.request.method.toUpperCase() || 'GET';
if (route) {
getIsolationScope().setTransactionName(`${method} ${route}`);
Expand Down
12 changes: 8 additions & 4 deletions packages/node/src/sdk/initOtel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { DiagLogLevel, diag } from '@opentelemetry/api';
import { Resource } from '@opentelemetry/resources';
import { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMRESATTRS_SERVICE_NAME,
SEMRESATTRS_SERVICE_NAMESPACE,
SEMRESATTRS_SERVICE_VERSION,
} from '@opentelemetry/semantic-conventions';
import { SDK_VERSION } from '@sentry/core';
import { SentryPropagator, SentrySampler, SentrySpanProcessor, setupEventContextTrace } from '@sentry/opentelemetry';
import { logger } from '@sentry/utils';
Expand Down Expand Up @@ -36,9 +40,9 @@ export function setupOtel(client: NodeClient): BasicTracerProvider {
const provider = new BasicTracerProvider({
sampler: new SentrySampler(client),
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'node',
[SemanticResourceAttributes.SERVICE_NAMESPACE]: 'sentry',
[SemanticResourceAttributes.SERVICE_VERSION]: SDK_VERSION,
[SEMRESATTRS_SERVICE_NAME]: 'node',
[SEMRESATTRS_SERVICE_NAMESPACE]: 'sentry',
[SEMRESATTRS_SERVICE_VERSION]: SDK_VERSION,
}),
forceFlushTimeoutMillis: 500,
});
Expand Down
19 changes: 9 additions & 10 deletions packages/opentelemetry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,17 @@
"@sentry/utils": "8.0.0-beta.1"
},
"peerDependencies": {
"@opentelemetry/api": "^1.0.0",
"@opentelemetry/core": "^1.0.0",
"@opentelemetry/sdk-trace-base": "^1.0.0",
"@opentelemetry/semantic-conventions": "^1.0.0"
"@opentelemetry/api": "^1.8.0",
"@opentelemetry/core": "^1.23.0",
"@opentelemetry/sdk-trace-base": "^1.23.0",
"@opentelemetry/semantic-conventions": "^1.23.0"
},
"devDependencies": {
"@opentelemetry/api": "^1.6.0",
"@opentelemetry/context-async-hooks": "^1.17.1",
"@opentelemetry/core": "^1.17.1",
"@opentelemetry/sdk-trace-base": "^1.17.1",
"@opentelemetry/sdk-trace-node": "^1.17.1",
"@opentelemetry/semantic-conventions": "^1.17.1"
"@opentelemetry/api": "^1.8.0",
"@opentelemetry/context-async-hooks": "^1.23.0",
"@opentelemetry/core": "^1.23.0",
"@opentelemetry/sdk-trace-base": "^1.23.0",
"@opentelemetry/semantic-conventions": "^1.23.0"
},
"scripts": {
"build": "run-p build:transpile build:types",
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry/src/propagator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Baggage, Context, Span, SpanContext, TextMapGetter, TextMapSetter
import { context } from '@opentelemetry/api';
import { TraceFlags, propagation, trace } from '@opentelemetry/api';
import { TraceState, W3CBaggagePropagator, isTracingSuppressed } from '@opentelemetry/core';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_HTTP_URL } from '@opentelemetry/semantic-conventions';
import type { continueTrace } from '@sentry/core';
import { hasTracingEnabled } from '@sentry/core';
import { getRootSpan } from '@sentry/core';
Expand Down Expand Up @@ -329,7 +329,7 @@ function getExistingBaggage(carrier: unknown): string | undefined {
* 2. Else, if the active span has no URL attribute (e.g. it is unsampled), we check a special trace state (which we set in our sampler).
*/
function getCurrentURL(span: Span): string | undefined {
const urlAttribute = spanToJSON(span).data?.[SemanticAttributes.HTTP_URL];
const urlAttribute = spanToJSON(span).data?.[SEMATTRS_HTTP_URL];
if (urlAttribute) {
return urlAttribute;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/opentelemetry/src/sampler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { Client, SpanAttributes } from '@sentry/types';
import { logger } from '@sentry/utils';
import { SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, SENTRY_TRACE_STATE_URL } from './constants';

import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_HTTP_METHOD, SEMATTRS_HTTP_URL } from '@opentelemetry/semantic-conventions';
import { DEBUG_BUILD } from './debug-build';
import { getPropagationContextFromSpan } from './propagator';
import { getSamplingDecision } from './utils/getSamplingDecision';
Expand Down Expand Up @@ -43,7 +43,7 @@ export class SentrySampler implements Sampler {
let traceState = parentContext?.traceState || new TraceState();

// We always keep the URL on the trace state, so we can access it in the propagator
const url = spanAttributes[SemanticAttributes.HTTP_URL];
const url = spanAttributes[SEMATTRS_HTTP_URL];
if (url && typeof url === 'string') {
traceState = traceState.set(SENTRY_TRACE_STATE_URL, url);
}
Expand All @@ -56,7 +56,7 @@ export class SentrySampler implements Sampler {
// but we want to leave downstream sampling decisions up to the server
if (
spanKind === SpanKind.CLIENT &&
spanAttributes[SemanticAttributes.HTTP_METHOD] &&
spanAttributes[SEMATTRS_HTTP_METHOD] &&
(!parentSpan || parentContext?.isRemote)
) {
return { decision: SamplingDecision.NOT_RECORD, traceState };
Expand Down Expand Up @@ -86,7 +86,7 @@ export class SentrySampler implements Sampler {
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: sampleRate,
};

const method = `${spanAttributes[SemanticAttributes.HTTP_METHOD]}`.toUpperCase();
const method = `${spanAttributes[SEMATTRS_HTTP_METHOD]}`.toUpperCase();
if (method === 'OPTIONS' || method === 'HEAD') {
DEBUG_BUILD && logger.log(`[Tracing] Not sampling span because HTTP method is '${method}' for ${spanName}`);
return {
Expand Down
6 changes: 3 additions & 3 deletions packages/opentelemetry/src/spanExporter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Span } from '@opentelemetry/api';
import { SpanKind } from '@opentelemetry/api';
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_HTTP_STATUS_CODE } from '@opentelemetry/semantic-conventions';
import {
captureEvent,
getCapturedScopesOnSpan,
Expand Down Expand Up @@ -335,8 +335,8 @@ function getData(span: ReadableSpan): Record<string, unknown> {
'otel.kind': SpanKind[span.kind],
};

if (attributes[SemanticAttributes.HTTP_STATUS_CODE]) {
const statusCode = attributes[SemanticAttributes.HTTP_STATUS_CODE] as string;
if (attributes[SEMATTRS_HTTP_STATUS_CODE]) {
const statusCode = attributes[SEMATTRS_HTTP_STATUS_CODE] as string;
data['http.response.status_code'] = statusCode;
}

Expand Down
8 changes: 4 additions & 4 deletions packages/opentelemetry/src/utils/getRequestSpanData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Span } from '@opentelemetry/api';
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_HTTP_METHOD, SEMATTRS_HTTP_URL } from '@opentelemetry/semantic-conventions';
import type { SanitizedRequestData } from '@sentry/types';
import { getSanitizedUrlString, parseUrl } from '@sentry/utils';

Expand All @@ -16,8 +16,8 @@ export function getRequestSpanData(span: Span | ReadableSpan): Partial<Sanitized
}

const data: Partial<SanitizedRequestData> = {
url: span.attributes[SemanticAttributes.HTTP_URL] as string | undefined,
'http.method': span.attributes[SemanticAttributes.HTTP_METHOD] as string | undefined,
url: span.attributes[SEMATTRS_HTTP_URL] as string | undefined,
'http.method': span.attributes[SEMATTRS_HTTP_METHOD] as string | undefined,
};

// Default to GET if URL is set but method is not
Expand All @@ -26,7 +26,7 @@ export function getRequestSpanData(span: Span | ReadableSpan): Partial<Sanitized
}

try {
const urlStr = span.attributes[SemanticAttributes.HTTP_URL];
const urlStr = span.attributes[SEMATTRS_HTTP_URL];
if (typeof urlStr === 'string') {
const url = parseUrl(urlStr);

Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry/src/utils/isSentryRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_HTTP_URL } from '@opentelemetry/semantic-conventions';
import { getClient, isSentryRequestUrl } from '@sentry/core';

import type { AbstractSpan } from '../types';
Expand All @@ -16,7 +16,7 @@ export function isSentryRequestSpan(span: AbstractSpan): boolean {

const { attributes } = span;

const httpUrl = attributes[SemanticAttributes.HTTP_URL];
const httpUrl = attributes[SEMATTRS_HTTP_URL];

if (!httpUrl) {
return false;
Expand Down
6 changes: 3 additions & 3 deletions packages/opentelemetry/src/utils/mapStatus.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SpanStatusCode } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_HTTP_STATUS_CODE, SEMATTRS_RPC_GRPC_STATUS_CODE } from '@opentelemetry/semantic-conventions';
import { SPAN_STATUS_ERROR, SPAN_STATUS_OK, getSpanStatusFromHttpCode } from '@sentry/core';
import type { SpanStatus } from '@sentry/types';

Expand Down Expand Up @@ -53,8 +53,8 @@ export function mapStatus(span: AbstractSpan): SpanStatus {

// If the span status is UNSET, we try to infer it from HTTP or GRPC status codes.

const httpCodeAttribute = attributes[SemanticAttributes.HTTP_STATUS_CODE];
const grpcCodeAttribute = attributes[SemanticAttributes.RPC_GRPC_STATUS_CODE];
const httpCodeAttribute = attributes[SEMATTRS_HTTP_STATUS_CODE];
const grpcCodeAttribute = attributes[SEMATTRS_RPC_GRPC_STATUS_CODE];

const numberHttpCode =
typeof httpCodeAttribute === 'number'
Expand Down
30 changes: 20 additions & 10 deletions packages/opentelemetry/src/utils/parseSpanDescription.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import type { AttributeValue, Attributes } from '@opentelemetry/api';
import { SpanKind } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMATTRS_DB_STATEMENT,
SEMATTRS_DB_SYSTEM,
SEMATTRS_FAAS_TRIGGER,
SEMATTRS_HTTP_METHOD,
SEMATTRS_HTTP_ROUTE,
SEMATTRS_HTTP_TARGET,
SEMATTRS_HTTP_URL,
SEMATTRS_MESSAGING_SYSTEM,
SEMATTRS_RPC_SERVICE,
} from '@opentelemetry/semantic-conventions';
import type { TransactionSource } from '@sentry/types';
import { getSanitizedUrlString, parseUrl, stripUrlQueryAndFragment } from '@sentry/utils';

Expand All @@ -25,19 +35,19 @@ export function parseSpanDescription(span: AbstractSpan): SpanDescription {
const name = spanHasName(span) ? span.name : '<unknown>';

// if http.method exists, this is an http request span
const httpMethod = attributes[SemanticAttributes.HTTP_METHOD];
const httpMethod = attributes[SEMATTRS_HTTP_METHOD];
if (httpMethod) {
return descriptionForHttpMethod({ attributes, name, kind: getSpanKind(span) }, httpMethod);
}

// If db.type exists then this is a database call span.
const dbSystem = attributes[SemanticAttributes.DB_SYSTEM];
const dbSystem = attributes[SEMATTRS_DB_SYSTEM];
if (dbSystem) {
return descriptionForDbSystem({ attributes, name });
}

// If rpc.service exists then this is a rpc call span.
const rpcService = attributes[SemanticAttributes.RPC_SERVICE];
const rpcService = attributes[SEMATTRS_RPC_SERVICE];
if (rpcService) {
return {
op: 'rpc',
Expand All @@ -47,7 +57,7 @@ export function parseSpanDescription(span: AbstractSpan): SpanDescription {
}

// If messaging.system exists then this is a messaging system span.
const messagingSystem = attributes[SemanticAttributes.MESSAGING_SYSTEM];
const messagingSystem = attributes[SEMATTRS_MESSAGING_SYSTEM];
if (messagingSystem) {
return {
op: 'message',
Expand All @@ -57,7 +67,7 @@ export function parseSpanDescription(span: AbstractSpan): SpanDescription {
}

// If faas.trigger exists then this is a function as a service span.
const faasTrigger = attributes[SemanticAttributes.FAAS_TRIGGER];
const faasTrigger = attributes[SEMATTRS_FAAS_TRIGGER];
if (faasTrigger) {
return { op: faasTrigger.toString(), description: name, source: 'route' };
}
Expand All @@ -67,7 +77,7 @@ export function parseSpanDescription(span: AbstractSpan): SpanDescription {

function descriptionForDbSystem({ attributes, name }: { attributes: Attributes; name: string }): SpanDescription {
// Use DB statement (Ex "SELECT * FROM table") if possible as description.
const statement = attributes[SemanticAttributes.DB_STATEMENT];
const statement = attributes[SEMATTRS_DB_STATEMENT];

const description = statement ? statement.toString() : name;

Expand Down Expand Up @@ -134,11 +144,11 @@ export function getSanitizedUrl(
hasRoute: boolean;
} {
// This is the relative path of the URL, e.g. /sub
const httpTarget = attributes[SemanticAttributes.HTTP_TARGET];
const httpTarget = attributes[SEMATTRS_HTTP_TARGET];
// This is the full URL, including host & query params etc., e.g. https://example.com/sub?foo=bar
const httpUrl = attributes[SemanticAttributes.HTTP_URL];
const httpUrl = attributes[SEMATTRS_HTTP_URL];
// This is the normalized route name - may not always be available!
const httpRoute = attributes[SemanticAttributes.HTTP_ROUTE];
const httpRoute = attributes[SEMATTRS_HTTP_ROUTE];

const parsedUrl = typeof httpUrl === 'string' ? parseUrl(httpUrl) : undefined;
const url = parsedUrl ? getSanitizedUrlString(parsedUrl) : undefined;
Expand Down
12 changes: 8 additions & 4 deletions packages/opentelemetry/test/helpers/initOtel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { DiagLogLevel, diag } from '@opentelemetry/api';
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
import { Resource } from '@opentelemetry/resources';
import { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMRESATTRS_SERVICE_NAME,
SEMRESATTRS_SERVICE_NAMESPACE,
SEMRESATTRS_SERVICE_VERSION,
} from '@opentelemetry/semantic-conventions';
import { SDK_VERSION, getClient } from '@sentry/core';
import { logger } from '@sentry/utils';

Expand Down Expand Up @@ -51,9 +55,9 @@ export function setupOtel(client: TestClientInterface): BasicTracerProvider {
const provider = new BasicTracerProvider({
sampler: new SentrySampler(client),
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'opentelemetry-test',
[SemanticResourceAttributes.SERVICE_NAMESPACE]: 'sentry',
[SemanticResourceAttributes.SERVICE_VERSION]: SDK_VERSION,
[SEMRESATTRS_SERVICE_NAME]: 'opentelemetry-test',
[SEMRESATTRS_SERVICE_NAMESPACE]: 'sentry',
[SEMRESATTRS_SERVICE_VERSION]: SDK_VERSION,
}),
forceFlushTimeoutMillis: 500,
});
Expand Down
Loading