Skip to content

Commit

Permalink
fix(tracing): fix utils to handle undefined span (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
rannyeli authored Jan 2, 2024
1 parent 982900e commit 7c3a69c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/metrics/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { Metrics } from './metrics';
export { defaultMetricsMiddleware, collectMetricsExpressMiddleware } from './middleware/metrics';
export { metricsMiddleware, defaultMetricsMiddleware, collectMetricsExpressMiddleware } from './middleware/metrics';
12 changes: 10 additions & 2 deletions src/tracing/utils/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ export const callWithSpan = <T>(fn: (span?: Span) => T, tracer: Tracer, spanName
* Ends the given span with status OK
* @param span span to be ended
*/
export const handleSpanOnSuccess = (span: Span): void => {
export const handleSpanOnSuccess = (span: Span | undefined): void => {
if (!span) {
return;
}

span.setStatus({ code: SpanStatusCode.OK });
span.end();
};
Expand All @@ -80,7 +84,11 @@ export const handleSpanOnSuccess = (span: Span): void => {
* @param span span to be ended
* @param error error to be recorded
*/
export const handleSpanOnError = (span: Span, error?: unknown): void => {
export const handleSpanOnError = (span: Span | undefined, error?: unknown): void => {
if (!span) {
return;
}

span.setStatus({ code: SpanStatusCode.ERROR });

if (error instanceof Error) {
Expand Down

0 comments on commit 7c3a69c

Please sign in to comment.