UNPKG

applicationinsights

Version:

Microsoft Application Insights module for Node.js

1 lines 21.2 kB
{"version":3,"file":"correlationContextManager.js","sourceRoot":"","sources":["../../../src/shim/correlationContextManager.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;AAIlC,4CAAgF;AAChF,8CAAiD;AAGjD,yCAAsC;AAKtC,MAAM,YAAY,GAAG,6BAA6B,CAAC;AAEnD,MAAa,yBAAyB;IAIlC;;;;;;;OAOG;IACI,MAAM,CAAC,mBAAmB,CAAC,WAAwB,EAAE,QAAiB,EAAE,IAAa,EAAE,UAAuB;;QACjH,6EAA6E;QAC7E,MAAM,YAAY,GAAiB;YAC/B,YAAY,EAAE,EAAE;YAChB,OAAO,EAAE,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,OAAO;YAC7B,MAAM,EAAE,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,MAAM;YAC3B,SAAS,EAAE,MAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,UAAU,0CAAE,QAAQ,EAAE;YAC9C,QAAQ,EAAE,QAAQ;YAClB,OAAO,EAAE,IAAI;SAChB,CAAC;QAEF,OAAO,IAAI,CAAC,qBAAqB,CAAC,YAAY,CAAC,OAAO,EAAE,YAAY,CAAC,QAAQ,EAAE,IAAI,EAAE,YAAY,EAAE,UAAU,CAAC,CAAC;IACnH,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,iBAAiB;;QAC3B,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE;YACnB,sGAAsG;YACtG,IAAI,UAAU,GAAS,WAAK,CAAC,OAAO,CAAC,aAAO,CAAC,MAAM,EAAE,CAAS,CAAC;YAE/D,oHAAoH;YACpH,IAAI,CAAC,UAAU,EAAE;gBACb,UAAU,GAAG,WAAK,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC,SAAS,CAAC,YAAY,CAAS,CAAC;aAC9E;YACD,MAAM,aAAa,GAAe,IAAI,iBAAU,CAAC,MAAA,MA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IAED;;OAEG;IACI,MAAM,CAAC,OAAO;QACjB,UAAI,CAAC,IAAI,CAAC,sFAAsF,CAAC,CAAC;QAClG,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;QACxB,aAAO,CAAC,OAAO,EAAE,CAAC;IACtB,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,KAAK;QACf,UAAI,CAAC,IAAI,CAAC,4DAA4D,CAAC,CAAC;IAC5E,CAAC;IAED;;;;OAIG;IACK,MAAM,CAAC,2BAA2B,CAAC,GAAwB;;QAC/D,OAAO;YACH,OAAO,EAAE,GAAG,CAAC,SAAS,CAAC,EAAE;YACzB,MAAM,EAAE,MAAA,MAAA,GAAG,CAAC,SAAS,CAAC,WAAW,0CAAE,MAAM,mCAAI,EAAE;YAC/C,UAAU,EAAE,CAAA,MAAA,GAAG,CAAC,SAAS,CAAC,WAAW,0CAAE,SAAS,EAAC,CAAC,CAAC,MAAM,CAAC,MAAA,GAAG,CAAC,SAAS,CAAC,WAAW,0CAAE,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS;SAC9G,CAAC;IACN,CAAC;;AAtRL,8DAuRC;AAtRG,0GAA0G;AAC3F,qCAAW,GAAG,KAAK,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\r\n// Licensed under the MIT license.\r\n\r\nimport * as events from \"events\";\r\nimport * as http from \"http\";\r\nimport { context, SpanContext, trace, Context, diag } from \"@opentelemetry/api\";\r\nimport { TraceState } from \"@opentelemetry/core\";\r\nimport { Span } from \"@opentelemetry/sdk-trace-base\";\r\nimport { ICorrelationContext, ITraceparent, ITracestate, ICustomProperties, AzureFnContext, AzureFnRequest, AzureFnTraceContext, HttpRequest } from \"./types\";\r\nimport { Util } from \"../shared/util\";\r\nimport { HttpRequestHeaders } from \"@azure/functions-old\";\r\nimport { HttpRequest as AzureFnHttpRequest } from \"@azure/functions\";\r\n\r\n\r\nconst CONTEXT_NAME = \"ApplicationInsights-Context\";\r\n\r\nexport class CorrelationContextManager {\r\n // Context is taken from the trace API and not context API so we need a flag to disable this functionality\r\n private static _isDisabled = false;\r\n\r\n /**\r\n * Converts an OpenTelemetry SpanContext object to an ICorrelationContext object for backwards compatibility with ApplicationInsights\r\n * @param spanContext OpenTelmetry SpanContext object\r\n * @param parentId spanId of the parent span\r\n * @param name OpenTelemetry human readable name of the span\r\n * @param traceState String of key value pairs for additional trace context\r\n * @returns ICorrelationContext object\r\n */\r\n public static spanToContextObject(spanContext: SpanContext, parentId?: string, name?: string, traceState?: TraceState): ICorrelationContext {\r\n // Generate a basic ITraceparent to satisfy the ICorrelationContext interface\r\n const traceContext: ITraceparent = {\r\n legacyRootId: \"\",\r\n traceId: spanContext?.traceId,\r\n spanId: spanContext?.spanId,\r\n traceFlag: spanContext?.traceFlags?.toString(),\r\n parentId: parentId,\r\n version: \"00\"\r\n };\r\n\r\n return this.generateContextObject(traceContext.traceId, traceContext.parentId, name, traceContext, traceState);\r\n }\r\n\r\n /**\r\n * Provides the current Context.\r\n * The context is the most recent one entered into for the current\r\n * logical chain of execution, including across asynchronous calls.\r\n * @returns ICorrelationContext object\r\n */\r\n public static getCurrentContext(): ICorrelationContext | null {\r\n if (!this._isDisabled) {\r\n // Gets the active span and extracts the context to populate and return the ICorrelationContext object\r\n let activeSpan: Span = trace.getSpan(context.active()) as Span;\r\n\r\n // If no active span exists, create a new one. This is needed if runWithContext() is executed without an active span\r\n if (!activeSpan) {\r\n activeSpan = trace.getTracer(CONTEXT_NAME).startSpan(CONTEXT_NAME) as Span;\r\n }\r\n const traceStateObj: TraceState = new TraceState(activeSpan?.spanContext()?.traceState?.serialize());\r\n\r\n return this.spanToContextObject(activeSpan?.spanContext(), activeSpan?.parentSpanId, activeSpan?.name, traceStateObj);\r\n }\r\n return null;\r\n }\r\n\r\n /**\r\n * Helper to generate objects conforming to the CorrelationContext interface\r\n * @param operationId String assigned to a series of related telemetry items - equivalent to OpenTelemetry traceId\r\n * @param parentId spanId of the parent span\r\n * @param operationName Human readable name of the span\r\n * @param traceparent Context conveying string in the format version-traceId-spanId-traceFlag\r\n * @param tracestate String of key value pairs for additional trace context\r\n * @returns ICorrelationContext object\r\n */\r\n public static generateContextObject(\r\n operationId: string,\r\n parentId?: string,\r\n operationName?: string,\r\n traceparent?: ITraceparent,\r\n tracestate?: TraceState\r\n ): ICorrelationContext {\r\n // Cast OpenTelemetry TraceState object to ITracestate object\r\n const ITraceState: ITracestate = {\r\n fieldmap: tracestate?.serialize()?.split(\",\")\r\n };\r\n\r\n return {\r\n operation: {\r\n name: operationName,\r\n id: operationId,\r\n parentId: parentId,\r\n traceparent: traceparent,\r\n tracestate: ITraceState,\r\n },\r\n // Headers are not being used so custom properties will always be stubbed out\r\n customProperties: {\r\n getProperty(prop: string) { return \"\" },\r\n setProperty(prop: string) { return \"\" },\r\n } as ICustomProperties,\r\n }\r\n }\r\n\r\n /**\r\n * Runs a function inside a given Context.\r\n * All logical children of the execution path that entered this Context\r\n * will receive this Context object on calls to GetCurrentContext.\r\n * @param ctx Context to run the function within\r\n * @param fn Function to run within the stated context\r\n * @returns any\r\n */\r\n public static runWithContext(ctx: ICorrelationContext, fn: () => any): any {\r\n // Creates a new Context object containing the values from the ICorrelationContext object, then sets the active context to the new Context\r\n try {\r\n const newContext: Context = trace.setSpanContext(context.active(), this._contextObjectToSpanContext(ctx));\r\n return context.with(newContext, fn);\r\n } catch (error) {\r\n diag.warn(\"Error binding to session context\", Util.getInstance().dumpObj(error));\r\n }\r\n return fn();\r\n }\r\n\r\n /**\r\n * Wrapper for cls-hooked bindEmitter method\r\n * @param emitter emitter to bind to the current context\r\n */\r\n public static wrapEmitter(emitter: events.EventEmitter): void {\r\n try {\r\n context.bind(context.active(), emitter);\r\n } catch (error) {\r\n diag.warn(\"Error binding to session context\", Util.getInstance().dumpObj(error));\r\n }\r\n }\r\n\r\n /**\r\n * Patches a callback to restore the correct Context when getCurrentContext\r\n * is run within it. This is necessary if automatic correlation fails to work\r\n * with user-included libraries.\r\n * The supplied callback will be given the same context that was present for\r\n * the call to wrapCallback\r\n * @param fn Function to be wrapped in the provided context\r\n * @param ctx Context to bind the function to\r\n * @returns Generic type T\r\n */\r\n public static wrapCallback<T>(fn: T, ctx?: ICorrelationContext): T {\r\n try {\r\n if (ctx) {\r\n // Create the new context and bind it if context is passed\r\n const newContext: Context = trace.setSpanContext(context.active(), this._contextObjectToSpanContext(ctx));\r\n return context.bind(newContext, fn);\r\n }\r\n // If no context is passed, bind to the current context\r\n return context.bind(context.active(), fn);\r\n } catch (error) {\r\n diag.error(\"Error binding to session context\", Util.getInstance().dumpObj(error));\r\n return fn;\r\n }\r\n }\r\n\r\n /**\r\n * Enables the CorrelationContextManager\r\n * @param forceClsHooked unused parameter used to satisfy backward compatibility\r\n */\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n public static enable(forceClsHooked?: boolean) {\r\n diag.info(\"Enabling the context manager is no longer necessary and this method is a no-op.\");\r\n }\r\n\r\n /**\r\n * Creates a new correlation context\r\n * @param input Any kind of object we can extract context information from\r\n * @param request HTTP request we can pull context information from in the form of the request's headers\r\n * @returns IcorrelationContext object\r\n */\r\n public static startOperation(\r\n input: AzureFnContext | (http.IncomingMessage | AzureFnRequest) | SpanContext | Span,\r\n request?: HttpRequest | string | AzureFnHttpRequest\r\n ): ICorrelationContext {\r\n const traceContext = (input && (input as AzureFnContext).traceContext || null) as AzureFnTraceContext;\r\n const span = input && (input as Span).spanContext ? input as Span : null;\r\n const spanContext = input && (input as SpanContext).traceId ? input as SpanContext : null;\r\n const headers = input && (input as any).headers;\r\n\r\n if (span) {\r\n trace.setSpanContext(context.active(), span.spanContext());\r\n return this.spanToContextObject(\r\n span.spanContext(),\r\n span.parentSpanId,\r\n );\r\n }\r\n\r\n if (spanContext) {\r\n trace.setSpanContext(context.active(), spanContext);\r\n return this.spanToContextObject(\r\n spanContext,\r\n );\r\n }\r\n\r\n if (traceContext || headers) {\r\n let traceparent = null;\r\n let tracestate = null;\r\n if (traceContext) {\r\n // Use the headers on the request from Azure Functions to set the active context\r\n const azureFnRequest = request as AzureFnRequest;\r\n // New programming model\r\n if (azureFnRequest?.headers && azureFnRequest?.headers.get) {\r\n traceparent = (azureFnRequest.headers as any).get(\"traceparent\") || (azureFnRequest.headers as any).get(\"request-id\");\r\n tracestate = (azureFnRequest.headers as any).get(\"tracestate\");\r\n }\r\n // Old programming model \r\n else if (azureFnRequest?.headers) {\r\n // request-id is a GUID-based unique identifier for the request\r\n traceparent = (azureFnRequest.headers as HttpRequestHeaders).traceparent ? (azureFnRequest.headers as HttpRequestHeaders).traceparent : (azureFnRequest.headers as HttpRequestHeaders)[\"request-id\"];\r\n tracestate = (azureFnRequest.headers as HttpRequestHeaders).tracestate;\r\n }\r\n\r\n if (!traceparent && traceContext.traceparent) {\r\n traceparent = traceContext.traceparent;\r\n } else if (!traceparent && traceContext.traceParent) {\r\n traceparent = traceContext.traceParent;\r\n }\r\n\r\n if (!tracestate && traceContext.tracestate) {\r\n tracestate = traceContext.tracestate;\r\n } else if (!tracestate && traceContext.traceState) {\r\n tracestate = traceContext.traceState;\r\n }\r\n }\r\n\r\n // If headers is defined instead of traceContext, use the headers to set the traceparent and tracestate\r\n // If headers is not an instance of Headers, we use the old programming model, otherwise use the old v3 values\r\n if (headers && (headers as HttpRequestHeaders).traceparent) {\r\n traceparent = (headers as HttpRequestHeaders).traceparent ? (headers as HttpRequestHeaders).traceparent.toString() : null;\r\n tracestate = (headers as HttpRequestHeaders).tracestate ? (headers as HttpRequestHeaders).tracestate.toString() : tracestate;\r\n } else if (headers && headers.get) {\r\n traceparent = (headers as any).get(\"traceparent\") || (headers as any).get(\"request-id\");\r\n tracestate = (headers as any).get(\"tracestate\");\r\n }\r\n\r\n const traceArray: string[] = traceparent?.split(\"-\");\r\n\r\n const tracestateObj: TraceState = new TraceState();\r\n tracestate?.split(\",\").forEach((pair: string) => {\r\n const kv = pair.split(\"=\");\r\n tracestateObj.set(kv[0], kv[1]);\r\n });\r\n try {\r\n return this.generateContextObject(\r\n traceArray[1],\r\n traceArray[2],\r\n null,\r\n {\r\n legacyRootId: \"\",\r\n parentId: \"\",\r\n spanId: traceArray[2],\r\n traceFlag: \"\",\r\n traceId: traceArray[1],\r\n version: \"00\",\r\n },\r\n tracestateObj\r\n );\r\n } catch (error) {\r\n diag.warn(\"Error creating context object\", Util.getInstance().dumpObj(error));\r\n }\r\n }\r\n diag.warn(\"startOperation was called with invalid arguments\");\r\n return null;\r\n }\r\n\r\n /**\r\n * Disables the CorrelationContextManager\r\n */\r\n public static disable() {\r\n diag.warn(\"It will not be possible to re-enable the current context manager after disabling it!\");\r\n this._isDisabled = true;\r\n context.disable();\r\n }\r\n\r\n /**\r\n * Resets the namespace\r\n */\r\n public static reset() {\r\n diag.info(\"This is a no-op and exists only for compatibility reasons.\");\r\n }\r\n\r\n /**\r\n * Converts ApplicationInsights' ICorrelationContext to an OpenTelemetry SpanContext\r\n * @param ctx ICorrelationContext object to convert to a SpanContext\r\n * @returns OpenTelemetry SpanContext\r\n */\r\n private static _contextObjectToSpanContext(ctx: ICorrelationContext): SpanContext {\r\n return {\r\n traceId: ctx.operation.id,\r\n spanId: ctx.operation.traceparent?.spanId ?? \"\",\r\n traceFlags: ctx.operation.traceparent?.traceFlag ? Number(ctx.operation.traceparent?.traceFlag) : undefined,\r\n };\r\n }\r\n}\r\n"]}