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

Handle cls context bind errors #923

Merged
merged 2 commits into from
Mar 11, 2022
Merged
Changes from all commits
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
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