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: Add recordException helper function #431

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,14 +1,75 @@
import { InstrumentationConfig } from '@opentelemetry/instrumentation';
import { InstrumentationAbstract } from './web-vitals-autoinstrumentation';
import { VERSION } from './version';
import { context, SpanStatusCode } from '@opentelemetry/api';
import {
SEMATTRS_EXCEPTION_MESSAGE,
SEMATTRS_EXCEPTION_STACKTRACE,
SEMATTRS_EXCEPTION_TYPE,
Attributes,
context,
SpanStatusCode,
trace,
Tracer,
} from '@opentelemetry/api';
import {
ATTR_EXCEPTION_MESSAGE,
Copy link
Contributor

Choose a reason for hiding this comment

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

just checking, but the attribute keys would remain the same right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes they're the same! just a different variable name!

ATTR_EXCEPTION_STACKTRACE,
ATTR_EXCEPTION_TYPE,
} from '@opentelemetry/semantic-conventions';
import { computeStackTrace, StackFrame } from 'tracekit';

const LIBRARY_NAME = '@honeycombio/instrumentation-global-errors';

export function getStructuredStackTrace(error: Error | undefined) {
if (!error) {
return {};
}

// OTLP does not accept arrays of objects
// breaking down the stack into arrays of strings/numbers
const structuredStack: StackFrame[] = computeStackTrace(error).stack;
const lines: number[] = [];
const columns: number[] = [];
const functions: string[] = [];
const urls: string[] = [];

for (const stackFrame of structuredStack) {
lines.push(stackFrame.line);
columns.push(stackFrame.column);
functions.push(stackFrame.func);
urls.push(stackFrame.url);
}

return {
'exception.structured_stacktrace.columns': columns,
'exception.structured_stacktrace.lines': lines,
'exception.structured_stacktrace.functions': functions,
'exception.structured_stacktrace.urls': urls,
};
}

export function recordException(
error: Error,
attributes: Attributes,
tracer: Tracer = trace.getTracer(LIBRARY_NAME),
) {
const message = error.message;
const type = error.name;
const errorAttributes = {
[ATTR_EXCEPTION_TYPE]: type,
[ATTR_EXCEPTION_MESSAGE]: message,
[ATTR_EXCEPTION_STACKTRACE]: error.stack,
...getStructuredStackTrace(error),
...attributes,
};

const errorSpan = tracer.startSpan(
'exception',
{ attributes: errorAttributes },
context.active(),
);

errorSpan.setStatus({ code: SpanStatusCode.ERROR, message });
errorSpan.end();
}

export interface GlobalErrorsInstrumentationConfig
extends InstrumentationConfig {}

Expand All @@ -20,61 +81,19 @@ export class GlobalErrorsInstrumentation extends InstrumentationAbstract {
private _isEnabled: boolean;
constructor({ enabled = true }: GlobalErrorsInstrumentationConfig = {}) {
const config: GlobalErrorsInstrumentationConfig = { enabled };
super('@honeycombio/instrumentation-global-errors', VERSION, config);
super(LIBRARY_NAME, VERSION, config);
if (enabled) {
this.enable();
}
this._isEnabled = enabled;
}

_computeStackTrace = (error: Error | undefined) => {
if (!error) {
return {};
}

// OTLP does not accept arrays of objects
// breaking down the stack into arrays of strings/numbers
const structuredStack: StackFrame[] = computeStackTrace(error).stack;
const lines: number[] = [];
const columns: number[] = [];
const functions: string[] = [];
const urls: string[] = [];

for (const stackFrame of structuredStack) {
lines.push(stackFrame.line);
columns.push(stackFrame.column);
functions.push(stackFrame.func);
urls.push(stackFrame.url);
}

return {
'exception.structured_stacktrace.columns': columns,
'exception.structured_stacktrace.lines': lines,
'exception.structured_stacktrace.functions': functions,
'exception.structured_stacktrace.urls': urls,
};
};

onError = (event: ErrorEvent | PromiseRejectionEvent) => {
const error: Error | undefined =
'reason' in event ? event.reason : event.error;
const message = error?.message;
const type = error?.name;
const attributes = {
[SEMATTRS_EXCEPTION_TYPE]: type,
[SEMATTRS_EXCEPTION_MESSAGE]: message,
[SEMATTRS_EXCEPTION_STACKTRACE]: error?.stack,
...this._computeStackTrace(error),
};
// otel spec requires at minimum these two
if (!message || !type) return;
const errorSpan = this.tracer.startSpan(
'exception',
{ attributes },
context.active(),
);
errorSpan.setStatus({ code: SpanStatusCode.ERROR, message });
errorSpan.end();
if (error) {
recordException(error, {}, this.tracer);
}
};

init() {}
Expand Down
1 change: 1 addition & 0 deletions packages/honeycomb-opentelemetry-web/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './base-otel-sdk';
export * from './honeycomb-otel-sdk';
export * from './web-vitals-autoinstrumentation';
export * from './global-errors-autoinstrumentation';
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import {
InMemorySpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import { GlobalErrorsInstrumentation } from '../src/global-errors-autoinstrumentation';
import {
getStructuredStackTrace,
GlobalErrorsInstrumentation,
} from '../src/global-errors-autoinstrumentation';
import timers from 'node:timers/promises';

describe('Global Errors Instrumentation Tests', () => {
Expand Down Expand Up @@ -81,7 +84,7 @@ describe('Global Errors Instrumentation Tests', () => {

describe('_computeStackTrace', () => {
it('should return an empty object if error is undefined', () => {
expect(instr._computeStackTrace(undefined)).toEqual({});
expect(getStructuredStackTrace(undefined)).toEqual({});
});

it('should return an object with structured stack trace information', () => {
Expand All @@ -97,7 +100,7 @@ describe('Global Errors Instrumentation Tests', () => {
' at http://example.com/js/test.js:67:5\n' + // stack frame 5
' at namedFunc4 (http://example.com/js/script.js:100001:10002)'; // stack frame 6

const structuredStack = instr._computeStackTrace(err);
const structuredStack = getStructuredStackTrace(err);

expect(structuredStack).toEqual({
'exception.structured_stacktrace.columns': [
Expand Down
Loading