Skip to content

Commit

Permalink
Handle cls context bind errors (#923)
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorhdzg authored Mar 11, 2022
1 parent cc20002 commit 85066a5
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions AutoCollection/CorrelationContextManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Tracestate = require("../Library/Tracestate");
import HttpRequestParser = require("./HttpRequestParser");
import { SpanContext } from "@opentelemetry/api";
import { Span } from "@opentelemetry/sdk-trace-base";
import Util = require("../Library/Util");

export interface CustomProperties {
/**
Expand Down Expand Up @@ -110,18 +111,27 @@ export class CorrelationContextManager {
*/
public static runWithContext(context: CorrelationContext, fn: () => any): any {
if (CorrelationContextManager.enabled) {
return CorrelationContextManager.session.bind(fn, { [CorrelationContextManager.CONTEXT_NAME]: context })();
} else {
return fn();
try {
return CorrelationContextManager.session.bind(fn, { [CorrelationContextManager.CONTEXT_NAME]: context })();
}
catch (error) {
Logging.warn("Error binding to session context", Util.dumpObj(error));
}
}
return fn();
}

/**
* Wrapper for cls-hooked bindEmitter method
*/
public static wrapEmitter(emitter: events.EventEmitter): void {
if (CorrelationContextManager.enabled) {
CorrelationContextManager.session.bindEmitter(emitter);
try {
CorrelationContextManager.session.bindEmitter(emitter);
}
catch (error) {
Logging.warn("Error binding to session context", Util.dumpObj(error));
}
}
}

Expand All @@ -134,9 +144,14 @@ export class CorrelationContextManager {
* the call to wrapCallback. */
public static wrapCallback<T extends Function>(fn: T, context?: CorrelationContext): T {
if (CorrelationContextManager.enabled) {
return CorrelationContextManager.session.bind(fn, context ? {
[CorrelationContextManager.CONTEXT_NAME]: context
} : undefined);
try {
return CorrelationContextManager.session.bind(fn, context ? {
[CorrelationContextManager.CONTEXT_NAME]: context
} : undefined);
}
catch (error) {
Logging.warn("Error binding to session context", Util.dumpObj(error));
}
}
return fn;
}
Expand Down Expand Up @@ -168,7 +183,12 @@ export class CorrelationContextManager {
CorrelationContextManager.session = this.cls.createNamespace("AI-CLS-Session");

DiagChannel.registerContextPreservation((cb) => {
return CorrelationContextManager.session.bind(cb);
try {
return CorrelationContextManager.session.bind(cb);
}
catch (error) {
Logging.warn("Error binding to session context", Util.dumpObj(error));
}
});
}

Expand Down

0 comments on commit 85066a5

Please sign in to comment.