Skip to content
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

feat(otel): Add basic SentrySpanProcessor #6023

Merged
merged 4 commits into from Oct 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/opentelemetry-node/package.json
Expand Up @@ -28,7 +28,8 @@
},
"devDependencies": {
"@opentelemetry/api": "^1.2.0",
"@opentelemetry/sdk-trace-base": "^1.7.0"
"@opentelemetry/sdk-trace-base": "^1.7.0",
"@opentelemetry/sdk-trace-node": "^1.7.0"
},
"scripts": {
"build": "run-p build:rollup build:types",
Expand All @@ -54,5 +55,12 @@
},
"volta": {
"extends": "../../package.json"
}
},
"sideEffects": [
"./cjs/index.js",
"./esm/index.js",
"./build/npm/cjs/index.js",
"./build/npm/esm/index.js",
"./src/index.ts"
]
}
9 changes: 3 additions & 6 deletions packages/opentelemetry-node/src/index.ts
@@ -1,6 +1,3 @@
/**
* Test function
*/
export function test(): void {
// no-op
}
import '@sentry/tracing';

export { SentrySpanProcessor } from './spanprocessor';
115 changes: 115 additions & 0 deletions packages/opentelemetry-node/src/spanprocessor.ts
@@ -0,0 +1,115 @@
import { Context } from '@opentelemetry/api';
import { Span as OtelSpan, SpanProcessor as OtelSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { getCurrentHub } from '@sentry/core';
import { Span as SentrySpan, TransactionContext } from '@sentry/types';
import { logger } from '@sentry/utils';

/**
* Converts OpenTelemetry Spans to Sentry Spans and sends them to Sentry via
* the Sentry SDK.
*/
export class SentrySpanProcessor implements OtelSpanProcessor {
// public only for testing
public readonly _map: Map<SentrySpan['spanId'], SentrySpan> = new Map<SentrySpan['spanId'], SentrySpan>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not super nice to make this public just so we can use it in tests, so if there are other ideas for this, let me know. It works, at least 😅

Also FYI I changed this to a Map instead of {}, as that makes delete() nicer imho. I think there shouldn't really be a perf difference, but let me know if we should rather leave this a POJO.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a map seems fine to me - cleaner pattern overall.


/**
* @inheritDoc
*/
public onStart(otelSpan: OtelSpan, _parentContext: Context): void {
const hub = getCurrentHub();
if (!hub) {
__DEBUG_BUILD__ && logger.error('SentrySpanProcessor has triggered onStart before a hub has been setup.');
return;
}
const scope = hub.getScope();
if (!scope) {
__DEBUG_BUILD__ && logger.error('SentrySpanProcessor has triggered onStart before a scope has been setup.');
return;
}

// TODO: handle sentry requests
// if isSentryRequest(otelSpan) return;

const otelSpanId = otelSpan.spanContext().spanId;
const otelParentSpanId = otelSpan.parentSpanId;

// Otel supports having multiple non-nested spans at the same time
// so we cannot use hub.getSpan(), as we cannot rely on this being on the current span
const sentryParentSpan = otelParentSpanId && this._map.get(otelParentSpanId);

if (sentryParentSpan) {
const sentryChildSpan = sentryParentSpan.startChild({
description: otelSpan.name,
// instrumentor: 'otel',
startTimestamp: otelSpan.startTime[0],
spanId: otelSpanId,
});

this._map.set(otelSpanId, sentryChildSpan);
} else {
const traceCtx = getTraceData(otelSpan);
const transaction = hub.startTransaction({
name: otelSpan.name,
...traceCtx,
// instrumentor: 'otel',
startTimestamp: otelSpan.startTime[0],
spanId: otelSpanId,
});

this._map.set(otelSpanId, transaction);
}
}

/**
* @inheritDoc
*/
public onEnd(otelSpan: OtelSpan): void {
const otelSpanId = otelSpan.spanContext().spanId;
const mapVal = this._map.get(otelSpanId);

if (!mapVal) {
__DEBUG_BUILD__ &&
logger.error(`SentrySpanProcessor could not find span with OTEL-spanId ${otelSpanId} to finish.`);
return;
}

const sentrySpan = mapVal;

// TODO: actually add context etc. to span
// updateSpanWithOtelData(sentrySpan, otelSpan);

sentrySpan.finish(otelSpan.endTime[0]);

this._map.delete(otelSpanId);
}

/**
* @inheritDoc
*/
public shutdown(): Promise<void> {
return Promise.resolve();
}

/**
* @inheritDoc
*/
public async forceFlush(): Promise<void> {
const client = getCurrentHub().getClient();
if (client) {
return client.flush().then();
}
return Promise.resolve();
}
}

function getTraceData(otelSpan: OtelSpan): Partial<TransactionContext> {
const spanContext = otelSpan.spanContext();
const traceId = spanContext.traceId;
const spanId = spanContext.spanId;

const parentSpanId = otelSpan.parentSpanId;
return { spanId, traceId, parentSpanId };
}

// function updateSpanWithOtelData(sentrySpan: SentrySpan, otelSpan: OtelSpan): void {
// }
7 changes: 0 additions & 7 deletions packages/opentelemetry-node/test/index.test.ts

This file was deleted.

141 changes: 141 additions & 0 deletions packages/opentelemetry-node/test/spanprocessor.test.ts
@@ -0,0 +1,141 @@
import * as OpenTelemetry from '@opentelemetry/api';
import { Span as OtelSpan } from '@opentelemetry/sdk-trace-base';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { Hub, makeMain } from '@sentry/core';
import { addExtensionMethods, Span as SentrySpan, Transaction } from '@sentry/tracing';

import { SentrySpanProcessor } from '../src/spanprocessor';

// Integration Test of SentrySpanProcessor

beforeAll(() => {
addExtensionMethods();
});

describe('SentrySpanProcessor', () => {
let hub: Hub;
let provider: NodeTracerProvider;
let spanProcessor: SentrySpanProcessor;

beforeEach(() => {
hub = new Hub();
makeMain(hub);

spanProcessor = new SentrySpanProcessor();
provider = new NodeTracerProvider();
provider.addSpanProcessor(spanProcessor);
provider.register();
});

afterEach(async () => {
await provider.forceFlush();
await provider.shutdown();
});

function getSpanForOtelSpan(otelSpan: OtelSpan | OpenTelemetry.Span) {
return spanProcessor._map.get(otelSpan.spanContext().spanId);
}

it('creates a transaction', async () => {
const startTime = otelNumberToHrtime(new Date().valueOf());

const otelSpan = provider.getTracer('default').startSpan('GET /users', { startTime }) as OtelSpan;

const sentrySpanTransaction = getSpanForOtelSpan(otelSpan) as Transaction | undefined;
expect(sentrySpanTransaction).toBeInstanceOf(Transaction);

expect(sentrySpanTransaction?.name).toBe('GET /users');
expect(sentrySpanTransaction?.startTimestamp).toEqual(otelSpan.startTime[0]);
expect(sentrySpanTransaction?.startTimestamp).toEqual(startTime[0]);
expect(sentrySpanTransaction?.traceId).toEqual(otelSpan.spanContext().traceId);
expect(sentrySpanTransaction?.parentSpanId).toEqual(otelSpan.parentSpanId);
expect(sentrySpanTransaction?.spanId).toEqual(otelSpan.spanContext().spanId);

expect(hub.getScope()?.getSpan()).toBeUndefined();

const endTime = otelNumberToHrtime(new Date().valueOf());
otelSpan.end(endTime);

expect(sentrySpanTransaction?.endTimestamp).toBe(endTime[0]);
expect(sentrySpanTransaction?.endTimestamp).toBe(otelSpan.endTime[0]);

expect(hub.getScope()?.getSpan()).toBeUndefined();
});

it('creates a child span if there is a running transaction', () => {
const tracer = provider.getTracer('default');

tracer.startActiveSpan('GET /users', parentOtelSpan => {
tracer.startActiveSpan('SELECT * FROM users;', child => {
const childOtelSpan = child as OtelSpan;

const sentrySpanTransaction = getSpanForOtelSpan(parentOtelSpan) as Transaction | undefined;
expect(sentrySpanTransaction).toBeInstanceOf(Transaction);

const sentrySpan = getSpanForOtelSpan(childOtelSpan);
expect(sentrySpan).toBeInstanceOf(SentrySpan);
expect(sentrySpan?.description).toBe('SELECT * FROM users;');
expect(sentrySpan?.startTimestamp).toEqual(childOtelSpan.startTime[0]);
expect(sentrySpan?.spanId).toEqual(childOtelSpan.spanContext().spanId);
expect(sentrySpan?.parentSpanId).toEqual(sentrySpanTransaction?.spanId);

expect(hub.getScope()?.getSpan()).toBeUndefined();

const endTime = otelNumberToHrtime(new Date().valueOf());
child.end(endTime);

expect(sentrySpan?.endTimestamp).toEqual(childOtelSpan.endTime[0]);
expect(sentrySpan?.endTimestamp).toEqual(endTime[0]);
});

parentOtelSpan.end();
});
});

it('allows to create multiple child spans on same level', () => {
const tracer = provider.getTracer('default');

tracer.startActiveSpan('GET /users', parentOtelSpan => {
const sentrySpanTransaction = getSpanForOtelSpan(parentOtelSpan) as Transaction | undefined;

expect(sentrySpanTransaction).toBeInstanceOf(SentrySpan);
expect(sentrySpanTransaction?.name).toBe('GET /users');

// Create some parallel, independent spans
const span1 = tracer.startSpan('SELECT * FROM users;') as OtelSpan;
const span2 = tracer.startSpan('SELECT * FROM companies;') as OtelSpan;
const span3 = tracer.startSpan('SELECT * FROM locations;') as OtelSpan;

const sentrySpan1 = getSpanForOtelSpan(span1);
const sentrySpan2 = getSpanForOtelSpan(span2);
const sentrySpan3 = getSpanForOtelSpan(span3);

expect(sentrySpan1?.parentSpanId).toEqual(sentrySpanTransaction?.spanId);
expect(sentrySpan2?.parentSpanId).toEqual(sentrySpanTransaction?.spanId);
expect(sentrySpan3?.parentSpanId).toEqual(sentrySpanTransaction?.spanId);

expect(sentrySpan1?.description).toEqual('SELECT * FROM users;');
expect(sentrySpan2?.description).toEqual('SELECT * FROM companies;');
expect(sentrySpan3?.description).toEqual('SELECT * FROM locations;');

span1.end();
span2.end();
span3.end();

parentOtelSpan.end();
});
});
});

// OTEL expects a custom date format
const NANOSECOND_DIGITS = 9;
const SECOND_TO_NANOSECONDS = Math.pow(10, NANOSECOND_DIGITS);

function otelNumberToHrtime(epochMillis: number): OpenTelemetry.HrTime {
const epochSeconds = epochMillis / 1000;
// Decimals only.
const seconds = Math.trunc(epochSeconds);
// Round sub-nanosecond accuracy to nanosecond.
const nanos = Number((epochSeconds - seconds).toFixed(NANOSECOND_DIGITS)) * SECOND_TO_NANOSECONDS;
return [seconds, nanos];
}
4 changes: 1 addition & 3 deletions packages/types/src/span.ts
Expand Up @@ -132,9 +132,7 @@ export interface Span extends SpanContext {
* Creates a new `Span` while setting the current `Span.id` as `parentSpanId`.
* Also the `sampled` decision will be inherited.
*/
startChild(
spanContext?: Pick<SpanContext, Exclude<keyof SpanContext, 'spanId' | 'sampled' | 'traceId' | 'parentSpanId'>>,
): Span;
startChild(spanContext?: Pick<SpanContext, Exclude<keyof SpanContext, 'sampled' | 'traceId' | 'parentSpanId'>>): Span;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was missed in #6028?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes 🤦


/**
* Determines whether span was successful (HTTP200)
Expand Down
33 changes: 32 additions & 1 deletion yarn.lock
Expand Up @@ -3898,6 +3898,11 @@
resolved "https://registry.yarnpkg.com/@opentelemetry/api/-/api-1.2.0.tgz#89ef99401cde6208cff98760b67663726ef26686"
integrity sha512-0nBr+VZNKm9tvNDZFstI3Pq1fCTEDK5OZTnVKNvBNAKgd0yIvmwsP4m61rEv7ZP+tOUjWJhROpxK5MsnlF911g==

"@opentelemetry/context-async-hooks@1.7.0":
version "1.7.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/context-async-hooks/-/context-async-hooks-1.7.0.tgz#b78d1f4f30b484d92d7926dc9d29ec1ccd489cf5"
integrity sha512-g4bMzyVW5dVBeMkyadaf3NRFpmNrdD4Pp9OJsrP29HwIam/zVMNfIWQpT5IBzjtTSMhl/ED5YQYR+UOSjVq3sQ==

"@opentelemetry/context-base@^0.12.0":
version "0.12.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/context-base/-/context-base-0.12.0.tgz#4906ae27359d3311e3dea1b63770a16f60848550"
Expand All @@ -3924,6 +3929,20 @@
"@opentelemetry/context-base" "^0.12.0"
semver "^7.1.3"

"@opentelemetry/propagator-b3@1.7.0":
version "1.7.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/propagator-b3/-/propagator-b3-1.7.0.tgz#8c089c2bab733ea7122cb4a5f7ffaaa355127555"
integrity sha512-8kKGS1KwArvkThdhubMZlomuREE9FaBcn9L4JrYHh2jly1FZpqOtFNO2byHymVRjH59d43Pa+eJuFpD0Fp7kSw==
dependencies:
"@opentelemetry/core" "1.7.0"

"@opentelemetry/propagator-jaeger@1.7.0":
version "1.7.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/propagator-jaeger/-/propagator-jaeger-1.7.0.tgz#1c1439866e05ba81da303ad28286aa25d129bf03"
integrity sha512-V7i/L1bx+R/ve4z6dTdn2jtvFxGThRsXS2wNb/tWZVfV8gqnePQp+HfoLrqB/Yz2iRPUcMWrcjx6vV78umvJFA==
dependencies:
"@opentelemetry/core" "1.7.0"

"@opentelemetry/resources@1.7.0":
version "1.7.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/resources/-/resources-1.7.0.tgz#90ccd3a6a86b4dfba4e833e73944bd64958d78c5"
Expand All @@ -3940,7 +3959,7 @@
"@opentelemetry/api" "^0.12.0"
"@opentelemetry/core" "^0.12.0"

"@opentelemetry/sdk-trace-base@^1.7.0":
"@opentelemetry/sdk-trace-base@1.7.0", "@opentelemetry/sdk-trace-base@^1.7.0":
version "1.7.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/sdk-trace-base/-/sdk-trace-base-1.7.0.tgz#b498424e0c6340a9d80de63fd408c5c2130a60a5"
integrity sha512-Iz84C+FVOskmauh9FNnj4+VrA+hG5o+tkMzXuoesvSfunVSioXib0syVFeNXwOm4+M5GdWCuW632LVjqEXStIg==
Expand All @@ -3949,6 +3968,18 @@
"@opentelemetry/resources" "1.7.0"
"@opentelemetry/semantic-conventions" "1.7.0"

"@opentelemetry/sdk-trace-node@^1.7.0":
version "1.7.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/sdk-trace-node/-/sdk-trace-node-1.7.0.tgz#83bf458c33db930144cebed72b524034135fce7b"
integrity sha512-DCAAbi0Zbb1pIofQcKzoAVy9/6bz24asFYeLb4fW/8QYAaawDnxumA++5Huw/RcYdJs8q8AIRBykwjYWWCm/5A==
dependencies:
"@opentelemetry/context-async-hooks" "1.7.0"
"@opentelemetry/core" "1.7.0"
"@opentelemetry/propagator-b3" "1.7.0"
"@opentelemetry/propagator-jaeger" "1.7.0"
"@opentelemetry/sdk-trace-base" "1.7.0"
semver "^7.3.5"

"@opentelemetry/semantic-conventions@1.7.0":
version "1.7.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/semantic-conventions/-/semantic-conventions-1.7.0.tgz#af80a1ef7cf110ea3a68242acd95648991bcd763"
Expand Down