Skip to content

Commit

Permalink
fix: fastify and browser autoinjection failed to compile (open-teleme…
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan authored Dec 22, 2021
1 parent bf7a199 commit c08efa8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@
"@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",
"@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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
Expand All @@ -96,11 +96,11 @@ export class FastifyInstrumentation extends InstrumentationBase {
private _wrapHandler(
pluginName: string,
hookName: string,
original: (...args: unknown[]) => Promise<any> | void,
original: (...args: unknown[]) => Promise<unknown>,
syncFunctionWithDone: boolean
): () => Promise<any> | void {
): () => Promise<unknown> {
const instrumentation = this;
return function (this: any, ...args: unknown[]): Promise<any> | void {
return function (this: any, ...args: unknown[]): Promise<unknown> {
if (!instrumentation.isEnabled()) {
return original.apply(this, args);
}
Expand Down Expand Up @@ -135,7 +135,7 @@ export class FastifyInstrumentation extends InstrumentationBase {
return original.apply(this, args);
},
err => {
if (err) {
if (err instanceof Error) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: err.message,
Expand Down
55 changes: 32 additions & 23 deletions plugins/node/opentelemetry-instrumentation-fastify/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,43 +88,52 @@ export function endSpan(reply: PluginFastifyReply, err?: any) {
* function fails
*/
export function safeExecuteInTheMiddleMaybePromise<T>(
execute: () => Promise<T> | T,
onFinish: (e: Error | undefined, result?: T) => void,
execute: () => Promise<T>,
onFinish: (e: unknown, result?: T) => void,
preventThrowingError?: boolean
): Promise<T> | T | void {
let error: Error | undefined;
let executeResult: Promise<T> | T | void;
let isPromise = false;
let result: T | undefined = undefined;
): Promise<T>;
export function safeExecuteInTheMiddleMaybePromise<T>(
execute: () => T,
onFinish: (e: unknown, result?: T) => void,
preventThrowingError?: boolean
): T;
export function safeExecuteInTheMiddleMaybePromise<T>(
execute: () => T | Promise<T>,
onFinish: (e: unknown, result?: T) => void,
preventThrowingError?: boolean
): T | Promise<T> | undefined {
let error: unknown;
let result: T | Promise<T> | undefined = undefined;
try {
executeResult = execute();
const promiseResult = executeResult as Promise<T>;
result = execute();

isPromise = promiseResult && typeof promiseResult.then === 'function';

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<T>(val: T | Promise<T>): val is Promise<T> {
return (
(typeof val === 'object' &&
val &&
typeof Object.getOwnPropertyDescriptor(val, 'then')?.value ===
'function') ||
false
);
}

0 comments on commit c08efa8

Please sign in to comment.