Skip to content

Commit 7ec128c

Browse files
authored
style: revisit recordException method (#156)
* style: move tracer, span and attributes to hint arg * fix: one more * one more * docs: add changeset * fix: move spread operator to try-catch block
1 parent 742f1cf commit 7ec128c

5 files changed

Lines changed: 54 additions & 24 deletions

File tree

.changeset/early-toes-try.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@hyperdx/instrumentation-exception': patch
3+
'@hyperdx/otel-web': patch
4+
'@hyperdx/browser': patch
5+
'@hyperdx/node-opentelemetry': patch
6+
---
7+
8+
style: move 'tracer', 'span', 'attributes' args to hint (recordException method)

packages/instrumentation-exception/src/browser/index.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,23 @@ export const buildEventFromException = async (
7878

7979
export const recordException = async (
8080
e: any,
81-
hint?: EventHint,
82-
tracer?: Tracer,
83-
span?: Span,
84-
attributes?: Attributes,
81+
hint?: EventHint & {
82+
tracer?: Tracer;
83+
span?: Span;
84+
attributes?: Attributes;
85+
},
8586
) => {
86-
const _hint = hint ?? {
87-
mechanism: {
88-
type: 'generic',
89-
handled: true,
90-
},
91-
};
9287
try {
88+
const { tracer, span, attributes, ...eventHint } = hint ?? {};
89+
const _hint =
90+
Object.keys(eventHint).length > 0
91+
? eventHint
92+
: {
93+
mechanism: {
94+
type: 'generic',
95+
handled: true,
96+
},
97+
};
9398
const _eventProcessor = getEventProcessor(tracer ?? defaultTracer);
9499
const event = await buildEventFromException(e, {
95100
data: _hint,

packages/instrumentation-exception/src/browser/instrumentations/HyperDXErrorInstrumentation.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ export class HyperDXErrorInstrumentation extends InstrumentationBase {
114114
);
115115
span.setAttribute('error.message', limitLen(msg, MESSAGE_LIMIT));
116116
addStackIfUseful(span, err);
117-
recordException(err, {}, this.tracer, span).finally(() => {
117+
recordException(err, {
118+
tracer: this.tracer,
119+
span,
120+
}).finally(() => {
118121
span.end(now);
119122
});
120123
}
@@ -137,7 +140,10 @@ export class HyperDXErrorInstrumentation extends InstrumentationBase {
137140
if (firstError) {
138141
addStackIfUseful(span, firstError);
139142
// FIXME: record only the first error?
140-
recordException(firstError, {}, this.tracer, span).finally(() => {
143+
recordException(firstError, {
144+
tracer: this.tracer,
145+
span,
146+
}).finally(() => {
141147
span.end(now);
142148
});
143149
} else {
@@ -169,7 +175,10 @@ export class HyperDXErrorInstrumentation extends InstrumentationBase {
169175
span.setAttribute('target_xpath', getElementXPath(ev.target, true));
170176
span.setAttribute('target_src', (ev.target as any).src);
171177
}
172-
recordException(ev, {}, this.tracer, span).finally(() => {
178+
recordException(ev, {
179+
tracer: this.tracer,
180+
span,
181+
}).finally(() => {
173182
span.end(now);
174183
});
175184
}

packages/instrumentation-exception/src/node/index.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,23 @@ export const buildEventFromException = async (
8585

8686
export const recordException = async (
8787
e: any,
88-
hint?: EventHint,
89-
tracer?: Tracer,
90-
span?: Span,
91-
attributes?: Attributes,
88+
hint?: EventHint & {
89+
tracer?: Tracer;
90+
span?: Span;
91+
attributes?: Attributes;
92+
},
9293
) => {
93-
const _hint = hint ?? {
94-
mechanism: {
95-
type: 'generic',
96-
handled: true,
97-
},
98-
};
9994
try {
95+
const { tracer, span, attributes, ...eventHint } = hint ?? {};
96+
const _hint =
97+
Object.keys(eventHint).length > 0
98+
? eventHint
99+
: {
100+
mechanism: {
101+
type: 'generic',
102+
handled: true,
103+
},
104+
};
100105
const _eventProcessor = getEventProcessor(tracer ?? defaultTracer);
101106
const event = await buildEventFromException(e, {
102107
data: _hint,

packages/otel-web/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,10 @@ export const Rum: RumOtelWebType = {
710710
return;
711711
}
712712
const tracer = this.provider.getTracer('record-exception');
713-
_recordException(error, undefined, tracer, undefined, attributes);
713+
return _recordException(error, {
714+
tracer,
715+
attributes,
716+
});
714717
},
715718

716719
error(...args) {

0 commit comments

Comments
 (0)