From 072f7da20fe1cf40602b94fbdcadb059869043a0 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Tue, 21 Dec 2021 13:17:08 -0500 Subject: [PATCH 1/5] fix: fastify and browser autoinjection failed to compile --- .../package.json | 6 ++--- .../src/instrumentation.ts | 22 +++++++++---------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/packages/opentelemetry-browser-extension-autoinjection/package.json b/packages/opentelemetry-browser-extension-autoinjection/package.json index d8129b60a4..4ceb601343 100644 --- a/packages/opentelemetry-browser-extension-autoinjection/package.json +++ b/packages/opentelemetry-browser-extension-autoinjection/package.json @@ -71,9 +71,9 @@ "@opentelemetry/exporter-otlp-http": "0.26.0", "@opentelemetry/exporter-zipkin": "1.0.1", "@opentelemetry/instrumentation": "0.27.0", - "@opentelemetry/instrumentation-document-load": "^0.27.0", - "@opentelemetry/instrumentation-fetch": "0.26.0", - "@opentelemetry/instrumentation-xml-http-request": "0.26.0", + "@opentelemetry/instrumentation-document-load": "0.27.0", + "@opentelemetry/instrumentation-fetch": "0.27.0", + "@opentelemetry/instrumentation-xml-http-request": "0.27.0", "@opentelemetry/resources": "1.0.1", "@opentelemetry/sdk-trace-base": "1.0.1", "@opentelemetry/sdk-trace-web": "1.0.1", diff --git a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts index 985238a0b4..0946068f94 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts @@ -87,7 +87,7 @@ export class FastifyInstrumentation extends InstrumentationBase { const routeName = request.routerPath; if (routeName && rpcMetadata?.type === RPCType.HTTP) { rpcMetadata.span.setAttribute(SemanticAttributes.HTTP_ROUTE, routeName); - rpcMetadata.span.updateName(`${request.method} ${routeName || '/'}`); + rpcMetadata.span.updateName(`${request.method} ${routeName}`); } done(); }; @@ -96,18 +96,17 @@ export class FastifyInstrumentation extends InstrumentationBase { private _wrapHandler( pluginName: string, hookName: string, - original: (...args: unknown[]) => Promise | void, + original: (...args: unknown[]) => Promise, syncFunctionWithDone: boolean - ): () => Promise | void { + ): () => Promise { const instrumentation = this; - return function (this: any, ...args: unknown[]): Promise | void { + return function (this: any, ...args: unknown[]): Promise { if (!instrumentation.isEnabled()) { return original.apply(this, args); } - const spanName = `${FastifyNames.MIDDLEWARE} - ${ - original.name || ANONYMOUS_NAME - }`; + const spanName = `${FastifyNames.MIDDLEWARE} - ${original.name || ANONYMOUS_NAME + }`; const reply = args[1] as PluginFastifyReply; @@ -129,7 +128,7 @@ export class FastifyInstrumentation extends InstrumentationBase { }; } - return context.with(trace.setSpan(context.active(), span), () => { + return Promise.resolve(context.with(trace.setSpan(context.active(), span), () => { return safeExecuteInTheMiddleMaybePromise( () => { return original.apply(this, args); @@ -148,7 +147,7 @@ export class FastifyInstrumentation extends InstrumentationBase { } } ); - }); + })); }; } @@ -244,9 +243,8 @@ export class FastifyInstrumentation extends InstrumentationBase { } const requestContext = (request as any).context || {}; const handlerName = (requestContext.handler?.name || '').substr(6); - const spanName = `${FastifyNames.REQUEST_HANDLER} - ${ - handlerName || ANONYMOUS_NAME - }`; + const spanName = `${FastifyNames.REQUEST_HANDLER} - ${handlerName || ANONYMOUS_NAME + }`; const spanAttributes: SpanAttributes = { [AttributeNames.PLUGIN_NAME]: this.pluginName, From 3adaf661e067b7cc72c5496ad84c74e0702d7b07 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Tue, 21 Dec 2021 13:37:46 -0500 Subject: [PATCH 2/5] chore: lint --- .../src/instrumentation.ts | 50 ++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts index 0946068f94..4d9be42f8f 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts @@ -105,8 +105,9 @@ export class FastifyInstrumentation extends InstrumentationBase { return original.apply(this, args); } - const spanName = `${FastifyNames.MIDDLEWARE} - ${original.name || ANONYMOUS_NAME - }`; + const spanName = `${FastifyNames.MIDDLEWARE} - ${ + original.name || ANONYMOUS_NAME + }`; const reply = args[1] as PluginFastifyReply; @@ -128,26 +129,28 @@ export class FastifyInstrumentation extends InstrumentationBase { }; } - return Promise.resolve(context.with(trace.setSpan(context.active(), span), () => { - return safeExecuteInTheMiddleMaybePromise( - () => { - return original.apply(this, args); - }, - err => { - if (err) { - span.setStatus({ - code: SpanStatusCode.ERROR, - message: err.message, - }); - span.recordException(err); + return Promise.resolve( + context.with(trace.setSpan(context.active(), span), () => { + return safeExecuteInTheMiddleMaybePromise( + () => { + return original.apply(this, args); + }, + err => { + if (err) { + span.setStatus({ + code: SpanStatusCode.ERROR, + message: err.message, + }); + span.recordException(err); + } + // async hooks should end the span as soon as the promise is resolved + if (!syncFunctionWithDone) { + endSpan(reply); + } } - // async hooks should end the span as soon as the promise is resolved - if (!syncFunctionWithDone) { - endSpan(reply); - } - } - ); - })); + ); + }) + ); }; } @@ -243,8 +246,9 @@ export class FastifyInstrumentation extends InstrumentationBase { } const requestContext = (request as any).context || {}; const handlerName = (requestContext.handler?.name || '').substr(6); - const spanName = `${FastifyNames.REQUEST_HANDLER} - ${handlerName || ANONYMOUS_NAME - }`; + const spanName = `${FastifyNames.REQUEST_HANDLER} - ${ + handlerName || ANONYMOUS_NAME + }`; const spanAttributes: SpanAttributes = { [AttributeNames.PLUGIN_NAME]: this.pluginName, From c45cefa85af2470a931a859457370c0c2d59b2fb Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Tue, 21 Dec 2021 17:02:59 -0500 Subject: [PATCH 3/5] chore: update exporter --- .../opentelemetry-browser-extension-autoinjection/package.json | 2 +- .../src/instrumentation/WebInstrumentation.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/opentelemetry-browser-extension-autoinjection/package.json b/packages/opentelemetry-browser-extension-autoinjection/package.json index 4ceb601343..2238d80d1e 100644 --- a/packages/opentelemetry-browser-extension-autoinjection/package.json +++ b/packages/opentelemetry-browser-extension-autoinjection/package.json @@ -68,7 +68,7 @@ "@material-ui/lab": "4.0.0-alpha.60", "@opentelemetry/context-zone": "1.0.1", "@opentelemetry/core": "1.0.1", - "@opentelemetry/exporter-otlp-http": "0.26.0", + "@opentelemetry/exporter-trace-otlp-http": "0.27.0", "@opentelemetry/exporter-zipkin": "1.0.1", "@opentelemetry/instrumentation": "0.27.0", "@opentelemetry/instrumentation-document-load": "0.27.0", diff --git a/packages/opentelemetry-browser-extension-autoinjection/src/instrumentation/WebInstrumentation.ts b/packages/opentelemetry-browser-extension-autoinjection/src/instrumentation/WebInstrumentation.ts index 997143f630..d467460e7d 100644 --- a/packages/opentelemetry-browser-extension-autoinjection/src/instrumentation/WebInstrumentation.ts +++ b/packages/opentelemetry-browser-extension-autoinjection/src/instrumentation/WebInstrumentation.ts @@ -20,7 +20,7 @@ import { XMLHttpRequestInstrumentation } from '@opentelemetry/instrumentation-xm import { WebTracerProvider } from '@opentelemetry/sdk-trace-web'; import { ZoneContextManager } from '@opentelemetry/context-zone'; import { ZipkinExporter } from '@opentelemetry/exporter-zipkin'; -import { OTLPTraceExporter } from '@opentelemetry/exporter-otlp-http'; +import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'; import { BatchSpanProcessor, ConsoleSpanExporter, From 1d13d49e1ce14709f0d20640babd3e0e2b43ec2e Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Tue, 21 Dec 2021 21:22:27 -0500 Subject: [PATCH 4/5] fix: update fastify types to match definitelytyped declarations --- .../src/instrumentation.ts | 49 ++++++++--------- .../src/utils.ts | 53 ++++++++++--------- 2 files changed, 51 insertions(+), 51 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts index 4d9be42f8f..cbcc7c0459 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts @@ -105,9 +105,7 @@ export class FastifyInstrumentation extends InstrumentationBase { return original.apply(this, args); } - const spanName = `${FastifyNames.MIDDLEWARE} - ${ - original.name || ANONYMOUS_NAME - }`; + const spanName = `${FastifyNames.MIDDLEWARE} - ${original.name || ANONYMOUS_NAME}`; const reply = args[1] as PluginFastifyReply; @@ -129,28 +127,26 @@ export class FastifyInstrumentation extends InstrumentationBase { }; } - return Promise.resolve( - context.with(trace.setSpan(context.active(), span), () => { - return safeExecuteInTheMiddleMaybePromise( - () => { - return original.apply(this, args); - }, - err => { - if (err) { - span.setStatus({ - code: SpanStatusCode.ERROR, - message: err.message, - }); - span.recordException(err); - } - // async hooks should end the span as soon as the promise is resolved - if (!syncFunctionWithDone) { - endSpan(reply); - } + return context.with(trace.setSpan(context.active(), span), () => { + return safeExecuteInTheMiddleMaybePromise( + () => { + return original.apply(this, args); + }, + err => { + if (err instanceof Error) { + span.setStatus({ + code: SpanStatusCode.ERROR, + message: err.message, + }); + span.recordException(err); } - ); - }) - ); + // async hooks should end the span as soon as the promise is resolved + if (!syncFunctionWithDone) { + endSpan(reply); + } + } + ); + }); }; } @@ -246,9 +242,8 @@ export class FastifyInstrumentation extends InstrumentationBase { } const requestContext = (request as any).context || {}; const handlerName = (requestContext.handler?.name || '').substr(6); - const spanName = `${FastifyNames.REQUEST_HANDLER} - ${ - handlerName || ANONYMOUS_NAME - }`; + const spanName = `${FastifyNames.REQUEST_HANDLER} - ${handlerName || ANONYMOUS_NAME + }`; const spanAttributes: SpanAttributes = { [AttributeNames.PLUGIN_NAME]: this.pluginName, diff --git a/plugins/node/opentelemetry-instrumentation-fastify/src/utils.ts b/plugins/node/opentelemetry-instrumentation-fastify/src/utils.ts index 7b328fea3c..c5cc7ef074 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/src/utils.ts @@ -88,43 +88,48 @@ export function endSpan(reply: PluginFastifyReply, err?: any) { * function fails */ export function safeExecuteInTheMiddleMaybePromise( - execute: () => Promise | T, - onFinish: (e: Error | undefined, result?: T) => void, + execute: () => Promise, + onFinish: (e: unknown, result?: T) => void, + preventThrowingError?: boolean, +): Promise; +export function safeExecuteInTheMiddleMaybePromise( + execute: () => T, + onFinish: (e: unknown, result?: T) => void, + preventThrowingError?: boolean, +): T; +export function safeExecuteInTheMiddleMaybePromise( + execute: () => (T | Promise), + onFinish: (e: unknown, result?: T) => void, preventThrowingError?: boolean -): Promise | T | void { - let error: Error | undefined; - let executeResult: Promise | T | void; - let isPromise = false; - let result: T | undefined = undefined; +): T | Promise | undefined { + let error: unknown; + let result: T | Promise | undefined = undefined; try { - executeResult = execute(); - const promiseResult = executeResult as Promise; - - isPromise = promiseResult && typeof promiseResult.then === 'function'; + result = execute(); - if (isPromise) { - promiseResult.then( - res => { - onFinish(undefined, res); - }, - (err: Error) => { - onFinish(err); - } + if (isPromise(result)) { + result.then( + res => onFinish(undefined, res), + err => onFinish(err), ); - } else { - result = executeResult as T | undefined; } } catch (e) { error = e; } finally { - if (!isPromise) { + if (!isPromise(result)) { onFinish(error, result); if (error && !preventThrowingError) { // eslint-disable-next-line no-unsafe-finally throw error; } } - // eslint-disable-next-line no-unsafe-finally - return executeResult; + return result; } } + +function isPromise(val: T | Promise): val is Promise { + return typeof val === 'object' && + val && + typeof Object.getOwnPropertyDescriptor(val, "then")?.value === 'function' || + false; +} From 8593f58fe4d2c2b395aa36e31c41f1eb66a769c6 Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Tue, 21 Dec 2021 21:40:51 -0500 Subject: [PATCH 5/5] chore: lint --- .../src/instrumentation.ts | 9 ++++++--- .../src/utils.ts | 20 +++++++++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts index cbcc7c0459..cfe666c559 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts @@ -105,7 +105,9 @@ export class FastifyInstrumentation extends InstrumentationBase { return original.apply(this, args); } - const spanName = `${FastifyNames.MIDDLEWARE} - ${original.name || ANONYMOUS_NAME}`; + const spanName = `${FastifyNames.MIDDLEWARE} - ${ + original.name || ANONYMOUS_NAME + }`; const reply = args[1] as PluginFastifyReply; @@ -242,8 +244,9 @@ export class FastifyInstrumentation extends InstrumentationBase { } const requestContext = (request as any).context || {}; const handlerName = (requestContext.handler?.name || '').substr(6); - const spanName = `${FastifyNames.REQUEST_HANDLER} - ${handlerName || ANONYMOUS_NAME - }`; + const spanName = `${FastifyNames.REQUEST_HANDLER} - ${ + handlerName || ANONYMOUS_NAME + }`; const spanAttributes: SpanAttributes = { [AttributeNames.PLUGIN_NAME]: this.pluginName, diff --git a/plugins/node/opentelemetry-instrumentation-fastify/src/utils.ts b/plugins/node/opentelemetry-instrumentation-fastify/src/utils.ts index c5cc7ef074..45efc3ab75 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/src/utils.ts @@ -90,15 +90,15 @@ export function endSpan(reply: PluginFastifyReply, err?: any) { export function safeExecuteInTheMiddleMaybePromise( execute: () => Promise, onFinish: (e: unknown, result?: T) => void, - preventThrowingError?: boolean, + preventThrowingError?: boolean ): Promise; export function safeExecuteInTheMiddleMaybePromise( execute: () => T, onFinish: (e: unknown, result?: T) => void, - preventThrowingError?: boolean, + preventThrowingError?: boolean ): T; export function safeExecuteInTheMiddleMaybePromise( - execute: () => (T | Promise), + execute: () => T | Promise, onFinish: (e: unknown, result?: T) => void, preventThrowingError?: boolean ): T | Promise | undefined { @@ -110,7 +110,7 @@ export function safeExecuteInTheMiddleMaybePromise( if (isPromise(result)) { result.then( res => onFinish(undefined, res), - err => onFinish(err), + err => onFinish(err) ); } } catch (e) { @@ -123,13 +123,17 @@ export function safeExecuteInTheMiddleMaybePromise( throw error; } } + // eslint-disable-next-line no-unsafe-finally return result; } } function isPromise(val: T | Promise): val is Promise { - return typeof val === 'object' && - val && - typeof Object.getOwnPropertyDescriptor(val, "then")?.value === 'function' || - false; + return ( + (typeof val === 'object' && + val && + typeof Object.getOwnPropertyDescriptor(val, 'then')?.value === + 'function') || + false + ); }