Skip to content

Commit

Permalink
fix(instrumentation/otel): only use strings for span status and recor…
Browse files Browse the repository at this point in the history
…d exception (#31481)
  • Loading branch information
secustor committed Sep 19, 2024
1 parent 4ee3374 commit 8e267a6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lib/instrumentation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ import {
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import {
ATTR_SERVICE_NAME,
ATTR_SERVICE_VERSION,
} from '@opentelemetry/semantic-conventions';
import { ATTR_SERVICE_NAMESPACE } from '@opentelemetry/semantic-conventions/incubating';
import { pkg } from '../expose.cjs';
import {
isTraceDebuggingEnabled,
isTraceSendingEnabled,
isTracingEnabled,
massageThrowable,
} from './utils';

let instrumentations: Instrumentation[] = [];
Expand All @@ -41,12 +46,10 @@ export function init(): void {
const traceProvider = new NodeTracerProvider({
resource: new Resource({
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/README.md#semantic-attributes-with-sdk-provided-default-value
[SemanticResourceAttributes.SERVICE_NAME]:
process.env.OTEL_SERVICE_NAME ?? 'renovate',
[SemanticResourceAttributes.SERVICE_NAMESPACE]:
[ATTR_SERVICE_NAME]: process.env.OTEL_SERVICE_NAME ?? 'renovate',
[ATTR_SERVICE_NAMESPACE]:
process.env.OTEL_SERVICE_NAMESPACE ?? 'renovatebot.com',
[SemanticResourceAttributes.SERVICE_VERSION]:
process.env.OTEL_SERVICE_VERSION ?? pkg.version,
[ATTR_SERVICE_VERSION]: process.env.OTEL_SERVICE_VERSION ?? pkg.version,
}),
});

Expand Down Expand Up @@ -144,9 +147,10 @@ export function instrument<F extends () => ReturnType<F>>(
if (ret instanceof Promise) {
return ret
.catch((e) => {
span.recordException(e);
span.setStatus({
code: SpanStatusCode.ERROR,
message: e,
message: massageThrowable(e),
});
throw e;
})
Expand All @@ -155,9 +159,10 @@ export function instrument<F extends () => ReturnType<F>>(
span.end();
return ret;
} catch (e) {
span.recordException(e);
span.setStatus({
code: SpanStatusCode.ERROR,
message: e,
message: massageThrowable(e),
});
span.end();
throw e;
Expand Down
16 changes: 16 additions & 0 deletions lib/instrumentation/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { massageThrowable } from './utils';

describe('instrumentation/utils', () => {
describe('massageThrowable', () => {
it.each`
input | expected
${null} | ${undefined}
${undefined} | ${undefined}
${new Error('test')} | ${'test'}
${'test'} | ${'test'}
${123} | ${'123'}
`('should return $expected for $input', ({ input, expected }) => {
expect(massageThrowable(input)).toEqual(expected);
});
});
});
12 changes: 12 additions & 0 deletions lib/instrumentation/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import is from '@sindresorhus/is';

export function isTracingEnabled(): boolean {
return isTraceDebuggingEnabled() || isTraceSendingEnabled();
}
Expand All @@ -9,3 +11,13 @@ export function isTraceDebuggingEnabled(): boolean {
export function isTraceSendingEnabled(): boolean {
return !!process.env.OTEL_EXPORTER_OTLP_ENDPOINT;
}

export function massageThrowable(e: unknown): string | undefined {
if (is.nullOrUndefined(e)) {
return undefined;
}
if (e instanceof Error) {
return e.message;
}
return String(e);
}

0 comments on commit 8e267a6

Please sign in to comment.