@opentelemetry/propagation-utils 0.31.9 → 0.31.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -59,11 +59,13 @@ const patchArrayFunction = (messages, functionName, tracer, loopContext) => {
59
59
  });
60
60
  if (typeof res === 'object') {
61
61
  const startSpanFunction = Object.getOwnPropertyDescriptor(message, START_SPAN_FUNCTION);
62
- startSpanFunction &&
62
+ if (startSpanFunction) {
63
63
  Object.defineProperty(res, START_SPAN_FUNCTION, startSpanFunction);
64
+ }
64
65
  const endSpanFunction = Object.getOwnPropertyDescriptor(message, END_SPAN_FUNCTION);
65
- endSpanFunction &&
66
+ if (endSpanFunction) {
66
67
  Object.defineProperty(res, END_SPAN_FUNCTION, endSpanFunction);
68
+ }
67
69
  }
68
70
  return res;
69
71
  };
@@ -1 +1 @@
1
- {"version":3,"file":"pubsub-propagation.js","sourceRoot":"","sources":["../../src/pubsub-propagation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAEL,QAAQ,EAIR,OAAO,EACP,KAAK,EACL,IAAI,GAEL,MAAM,oBAAoB,CAAC;AAE5B,MAAM,mBAAmB,GAAG,MAAM,CAChC,6CAA6C,CAC9C,CAAC;AACF,MAAM,iBAAiB,GAAG,MAAM,CAAC,2CAA2C,CAAC,CAAC;AAO9E,MAAM,gBAAgB,GAAG,CACvB,QAAmB,EACnB,MAAc,EACd,WAAoB,EACpB,EAAE;IACF,MAAM,QAAQ,GAAG,QAAQ,CAAC,MAAM,CAAC;IACjC,MAAM,WAAW,GAAG,UAElB,GAAG,IAAiC;QAEpC,MAAM,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAC5C,yBAAyB,CAAC,QAAQ,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QACzD,OAAO,QAAQ,CAAC;IAClB,CAAC,CAAC;IAEF,MAAM,CAAC,cAAc,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AAE,OAAO,CAAC,CAAC;KACrC;IAAC,OAAO,GAAG,EAAE;QACZ,IAAI,CAAC,KAAK,CAAC,sDAAsD,EAAE,GAAG,CAAC,CAAC;KACzE;IAED,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAkBF,MAAM,qCAAqC,GAAG,CAAI,EAChD,QAAQ,EACR,MAAM,EACN,aAAa,EACb,oBAAoB,EACpB,WAAW,GACkB,EAAE,EAAE;IACjC,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;QACzB,MAAM,EACJ,UAAU,EACV,IAAI,EACJ,aAAa,EAAE,iBAAiB,GACjC,GAAG,oBAAoB,CAAC,OAAO,CAAC,CAAC;QAElC,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,mBAAmB,EAAE;YAClD,UAAU,EAAE,KAAK;YACjB,QAAQ,EAAE,IAAI;YACd,KAAK,EAAE,GAAG,EAAE,CACV,yBAAyB,CACvB,OAAO,EACP,IAAI,EACJ,UAAU,EACV,aAAa,EACb,iBAAiB,EACjB,MAAM,EACN,WAAW,CACZ;SACJ,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,eAAe;IACb,qCAAqC;IACrC,yBAAyB;CAC1B,CAAC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport {\n Tracer,\n SpanKind,\n Span,\n Context,\n Link,\n context,\n trace,\n diag,\n SpanAttributes,\n} from '@opentelemetry/api';\n\nconst START_SPAN_FUNCTION = Symbol(\n 'opentelemetry.pubsub-propagation.start_span'\n);\nconst END_SPAN_FUNCTION = Symbol('opentelemetry.pubsub-propagation.end_span');\n\ninterface OtelProcessedMessage {\n [START_SPAN_FUNCTION]?: () => Span;\n [END_SPAN_FUNCTION]?: () => void;\n}\n\nconst patchArrayFilter = (\n messages: unknown[],\n tracer: Tracer,\n loopContext: Context\n) => {\n const origFunc = messages.filter;\n const patchedFunc = function (\n this: unknown,\n ...args: Parameters<typeof origFunc>\n ) {\n const newArray = origFunc.apply(this, args);\n patchArrayForProcessSpans(newArray, tracer, loopContext);\n return newArray;\n };\n\n Object.defineProperty(messages, 'filter', {\n enumerable: false,\n value: patchedFunc,\n });\n};\n\nfunction isPromise(value: unknown): value is Promise<unknown> {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return typeof (value as any)?.then === 'function';\n}\n\nconst patchArrayFunction = (\n messages: OtelProcessedMessage[],\n functionName: 'forEach' | 'map',\n tracer: Tracer,\n loopContext: Context\n) => {\n const origFunc = messages[functionName] as typeof messages.map;\n const patchedFunc = function (\n this: unknown,\n ...arrFuncArgs: Parameters<typeof origFunc>\n ) {\n const callback = arrFuncArgs[0];\n const wrappedCallback = function (\n this: unknown,\n ...callbackArgs: Parameters<typeof callback>\n ) {\n const message = callbackArgs[0];\n const messageSpan = message?.[START_SPAN_FUNCTION]?.();\n if (!messageSpan) return callback.apply(this, callbackArgs);\n\n const res = context.with(trace.setSpan(loopContext, messageSpan), () => {\n let result: Promise<unknown> | unknown;\n try {\n result = callback.apply(this, callbackArgs);\n if (isPromise(result)) {\n const endSpan = () => message[END_SPAN_FUNCTION]?.();\n result.then(endSpan, endSpan);\n }\n return result;\n } finally {\n if (!isPromise(result)) {\n message[END_SPAN_FUNCTION]?.();\n }\n }\n });\n\n if (typeof res === 'object') {\n const startSpanFunction = Object.getOwnPropertyDescriptor(\n message,\n START_SPAN_FUNCTION\n );\n startSpanFunction &&\n Object.defineProperty(res, START_SPAN_FUNCTION, startSpanFunction);\n\n const endSpanFunction = Object.getOwnPropertyDescriptor(\n message,\n END_SPAN_FUNCTION\n );\n endSpanFunction &&\n Object.defineProperty(res, END_SPAN_FUNCTION, endSpanFunction);\n }\n return res;\n };\n arrFuncArgs[0] = wrappedCallback;\n const funcResult = origFunc.apply(this, arrFuncArgs);\n if (Array.isArray(funcResult))\n patchArrayForProcessSpans(funcResult, tracer, loopContext);\n return funcResult;\n };\n\n Object.defineProperty(messages, functionName, {\n enumerable: false,\n value: patchedFunc,\n });\n};\n\nconst patchArrayForProcessSpans = (\n messages: unknown[],\n tracer: Tracer,\n loopContext: Context = context.active()\n) => {\n patchArrayFunction(\n messages as OtelProcessedMessage[],\n 'forEach',\n tracer,\n loopContext\n );\n patchArrayFunction(\n messages as OtelProcessedMessage[],\n 'map',\n tracer,\n loopContext\n );\n patchArrayFilter(messages, tracer, loopContext);\n};\n\nconst startMessagingProcessSpan = <T>(\n message: T,\n name: string,\n attributes: SpanAttributes,\n parentContext: Context,\n propagatedContext: Context,\n tracer: Tracer,\n processHook?: ProcessHook<T>\n): Span => {\n const links: Link[] = [];\n const spanContext = trace.getSpanContext(propagatedContext);\n if (spanContext) {\n links.push({\n context: spanContext,\n } as Link);\n }\n\n const spanName = `${name} process`;\n const processSpan = tracer.startSpan(\n spanName,\n {\n kind: SpanKind.CONSUMER,\n attributes: {\n ...attributes,\n ['messaging.operation']: 'process',\n },\n links,\n },\n parentContext\n );\n\n Object.defineProperty(message, START_SPAN_FUNCTION, {\n enumerable: false,\n writable: true,\n value: () => processSpan,\n });\n\n Object.defineProperty(message, END_SPAN_FUNCTION, {\n enumerable: false,\n writable: true,\n value: () => {\n processSpan.end();\n Object.defineProperty(message, END_SPAN_FUNCTION, {\n enumerable: false,\n writable: true,\n value: () => {},\n });\n },\n });\n\n try {\n processHook?.(processSpan, message);\n } catch (err) {\n diag.error('opentelemetry-pubsub-propagation: process hook error', err);\n }\n\n return processSpan;\n};\n\ninterface SpanDetails {\n attributes: SpanAttributes;\n parentContext: Context;\n name: string;\n}\n\ntype ProcessHook<T> = (processSpan: Span, message: T) => void;\n\ninterface PatchForProcessingPayload<T> {\n messages: T[];\n tracer: Tracer;\n parentContext: Context;\n messageToSpanDetails: (message: T) => SpanDetails;\n processHook?: ProcessHook<T>;\n}\n\nconst patchMessagesArrayToStartProcessSpans = <T>({\n messages,\n tracer,\n parentContext,\n messageToSpanDetails,\n processHook,\n}: PatchForProcessingPayload<T>) => {\n messages.forEach(message => {\n const {\n attributes,\n name,\n parentContext: propagatedContext,\n } = messageToSpanDetails(message);\n\n Object.defineProperty(message, START_SPAN_FUNCTION, {\n enumerable: false,\n writable: true,\n value: () =>\n startMessagingProcessSpan<T>(\n message,\n name,\n attributes,\n parentContext,\n propagatedContext,\n tracer,\n processHook\n ),\n });\n });\n};\n\nexport default {\n patchMessagesArrayToStartProcessSpans,\n patchArrayForProcessSpans,\n};\n"]}
1
+ {"version":3,"file":"pubsub-propagation.js","sourceRoot":"","sources":["../../src/pubsub-propagation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAEL,QAAQ,EAIR,OAAO,EACP,KAAK,EACL,IAAI,GAEL,MAAM,oBAAoB,CAAC;AAE5B,MAAM,mBAAmB,GAAG,MAAM,CAChC,6CAA6C,CAC9C,CAAC;AACF,MAAM,iBAAiB,GAAG,MAAM,CAAC,2CAA2C,CAAC,CAAC;AAO9E,MAAM,gBAAgB,GAAG,CACvB,QAAmB,EACnB,MAAc,EACd,WAAoB,EACpB,EAAE;IACF,MAAM,QAAQ,GAAG,QAAQ,CAAC,MAAM,CAAC;IACjC,MAAM,WAAW,GAAG,UAElB,GAAG,IAAiC;QAEpC,MAAM,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAC5C,yBAAyB,CAAC,QAAQ,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QACzD,OAAO,QAAQ,CAAC;IAClB,CAAC,CAAC;IAEF,MAAM,CAAC,cAAc,CAAC,QAAQ,EAAE,QAAQ,EAA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,WAAW,EAAE,OAAO,CAAC,CAAC;KACrC;IAAC,OAAO,GAAG,EAAE;QACZ,IAAI,CAAC,KAAK,CAAC,sDAAsD,EAAE,GAAG,CAAC,CAAC;KACzE;IAED,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAkBF,MAAM,qCAAqC,GAAG,CAAI,EAChD,QAAQ,EACR,MAAM,EACN,aAAa,EACb,oBAAoB,EACpB,WAAW,GACkB,EAAE,EAAE;IACjC,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;QACzB,MAAM,EACJ,UAAU,EACV,IAAI,EACJ,aAAa,EAAE,iBAAiB,GACjC,GAAG,oBAAoB,CAAC,OAAO,CAAC,CAAC;QAElC,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,mBAAmB,EAAE;YAClD,UAAU,EAAE,KAAK;YACjB,QAAQ,EAAE,IAAI;YACd,KAAK,EAAE,GAAG,EAAE,CACV,yBAAyB,CACvB,OAAO,EACP,IAAI,EACJ,UAAU,EACV,aAAa,EACb,iBAAiB,EACjB,MAAM,EACN,WAAW,CACZ;SACJ,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,eAAe;IACb,qCAAqC;IACrC,yBAAyB;CAC1B,CAAC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport {\n Tracer,\n SpanKind,\n Span,\n Context,\n Link,\n context,\n trace,\n diag,\n SpanAttributes,\n} from '@opentelemetry/api';\n\nconst START_SPAN_FUNCTION = Symbol(\n 'opentelemetry.pubsub-propagation.start_span'\n);\nconst END_SPAN_FUNCTION = Symbol('opentelemetry.pubsub-propagation.end_span');\n\ninterface OtelProcessedMessage {\n [START_SPAN_FUNCTION]?: () => Span;\n [END_SPAN_FUNCTION]?: () => void;\n}\n\nconst patchArrayFilter = (\n messages: unknown[],\n tracer: Tracer,\n loopContext: Context\n) => {\n const origFunc = messages.filter;\n const patchedFunc = function (\n this: unknown,\n ...args: Parameters<typeof origFunc>\n ) {\n const newArray = origFunc.apply(this, args);\n patchArrayForProcessSpans(newArray, tracer, loopContext);\n return newArray;\n };\n\n Object.defineProperty(messages, 'filter', {\n enumerable: false,\n value: patchedFunc,\n });\n};\n\nfunction isPromise(value: unknown): value is Promise<unknown> {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return typeof (value as any)?.then === 'function';\n}\n\nconst patchArrayFunction = (\n messages: OtelProcessedMessage[],\n functionName: 'forEach' | 'map',\n tracer: Tracer,\n loopContext: Context\n) => {\n const origFunc = messages[functionName] as typeof messages.map;\n const patchedFunc = function (\n this: unknown,\n ...arrFuncArgs: Parameters<typeof origFunc>\n ) {\n const callback = arrFuncArgs[0];\n const wrappedCallback = function (\n this: unknown,\n ...callbackArgs: Parameters<typeof callback>\n ) {\n const message = callbackArgs[0];\n const messageSpan = message?.[START_SPAN_FUNCTION]?.();\n if (!messageSpan) return callback.apply(this, callbackArgs);\n\n const res = context.with(trace.setSpan(loopContext, messageSpan), () => {\n let result: Promise<unknown> | unknown;\n try {\n result = callback.apply(this, callbackArgs);\n if (isPromise(result)) {\n const endSpan = () => message[END_SPAN_FUNCTION]?.();\n result.then(endSpan, endSpan);\n }\n return result;\n } finally {\n if (!isPromise(result)) {\n message[END_SPAN_FUNCTION]?.();\n }\n }\n });\n\n if (typeof res === 'object') {\n const startSpanFunction = Object.getOwnPropertyDescriptor(\n message,\n START_SPAN_FUNCTION\n );\n if (startSpanFunction) {\n Object.defineProperty(res, START_SPAN_FUNCTION, startSpanFunction);\n }\n\n const endSpanFunction = Object.getOwnPropertyDescriptor(\n message,\n END_SPAN_FUNCTION\n );\n if (endSpanFunction) {\n Object.defineProperty(res, END_SPAN_FUNCTION, endSpanFunction);\n }\n }\n return res;\n };\n arrFuncArgs[0] = wrappedCallback;\n const funcResult = origFunc.apply(this, arrFuncArgs);\n if (Array.isArray(funcResult))\n patchArrayForProcessSpans(funcResult, tracer, loopContext);\n return funcResult;\n };\n\n Object.defineProperty(messages, functionName, {\n enumerable: false,\n value: patchedFunc,\n });\n};\n\nconst patchArrayForProcessSpans = (\n messages: unknown[],\n tracer: Tracer,\n loopContext: Context = context.active()\n) => {\n patchArrayFunction(\n messages as OtelProcessedMessage[],\n 'forEach',\n tracer,\n loopContext\n );\n patchArrayFunction(\n messages as OtelProcessedMessage[],\n 'map',\n tracer,\n loopContext\n );\n patchArrayFilter(messages, tracer, loopContext);\n};\n\nconst startMessagingProcessSpan = <T>(\n message: T,\n name: string,\n attributes: SpanAttributes,\n parentContext: Context,\n propagatedContext: Context,\n tracer: Tracer,\n processHook?: ProcessHook<T>\n): Span => {\n const links: Link[] = [];\n const spanContext = trace.getSpanContext(propagatedContext);\n if (spanContext) {\n links.push({\n context: spanContext,\n } as Link);\n }\n\n const spanName = `${name} process`;\n const processSpan = tracer.startSpan(\n spanName,\n {\n kind: SpanKind.CONSUMER,\n attributes: {\n ...attributes,\n ['messaging.operation']: 'process',\n },\n links,\n },\n parentContext\n );\n\n Object.defineProperty(message, START_SPAN_FUNCTION, {\n enumerable: false,\n writable: true,\n value: () => processSpan,\n });\n\n Object.defineProperty(message, END_SPAN_FUNCTION, {\n enumerable: false,\n writable: true,\n value: () => {\n processSpan.end();\n Object.defineProperty(message, END_SPAN_FUNCTION, {\n enumerable: false,\n writable: true,\n value: () => {},\n });\n },\n });\n\n try {\n processHook?.(processSpan, message);\n } catch (err) {\n diag.error('opentelemetry-pubsub-propagation: process hook error', err);\n }\n\n return processSpan;\n};\n\ninterface SpanDetails {\n attributes: SpanAttributes;\n parentContext: Context;\n name: string;\n}\n\ntype ProcessHook<T> = (processSpan: Span, message: T) => void;\n\ninterface PatchForProcessingPayload<T> {\n messages: T[];\n tracer: Tracer;\n parentContext: Context;\n messageToSpanDetails: (message: T) => SpanDetails;\n processHook?: ProcessHook<T>;\n}\n\nconst patchMessagesArrayToStartProcessSpans = <T>({\n messages,\n tracer,\n parentContext,\n messageToSpanDetails,\n processHook,\n}: PatchForProcessingPayload<T>) => {\n messages.forEach(message => {\n const {\n attributes,\n name,\n parentContext: propagatedContext,\n } = messageToSpanDetails(message);\n\n Object.defineProperty(message, START_SPAN_FUNCTION, {\n enumerable: false,\n writable: true,\n value: () =>\n startMessagingProcessSpan<T>(\n message,\n name,\n attributes,\n parentContext,\n propagatedContext,\n tracer,\n processHook\n ),\n });\n });\n};\n\nexport default {\n patchMessagesArrayToStartProcessSpans,\n patchArrayForProcessSpans,\n};\n"]}
@@ -61,11 +61,13 @@ const patchArrayFunction = (messages, functionName, tracer, loopContext) => {
61
61
  });
62
62
  if (typeof res === 'object') {
63
63
  const startSpanFunction = Object.getOwnPropertyDescriptor(message, START_SPAN_FUNCTION);
64
- startSpanFunction &&
64
+ if (startSpanFunction) {
65
65
  Object.defineProperty(res, START_SPAN_FUNCTION, startSpanFunction);
66
+ }
66
67
  const endSpanFunction = Object.getOwnPropertyDescriptor(message, END_SPAN_FUNCTION);
67
- endSpanFunction &&
68
+ if (endSpanFunction) {
68
69
  Object.defineProperty(res, END_SPAN_FUNCTION, endSpanFunction);
70
+ }
69
71
  }
70
72
  return res;
71
73
  };
@@ -1 +1 @@
1
- {"version":3,"file":"pubsub-propagation.js","sourceRoot":"","sources":["../../src/pubsub-propagation.ts"],"names":[],"mappings":";;AAAA;;;;;;;;;;;;;;GAcG;AACH,4CAU4B;AAE5B,MAAM,mBAAmB,GAAG,MAAM,CAChC,6CAA6C,CAC9C,CAAC;AACF,MAAM,iBAAiB,GAAG,MAAM,CAAC,2CAA2C,CAAC,CAAC;AAO9E,MAAM,gBAAgB,GAAG,CACvB,QAAmB,EAC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,EAAE,KAAK;QACjB,QAAQ,EAAE,IAAI;QACd,KAAK,EAAE,GAAG,EAAE;YACV,WAAW,CAAC,GAAG,EAAE,CAAC;YAClB,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,iBAAiB,EAAE;gBAChD,UAAU,EAAE,KAAK;gBACjB,QAAQ,EAAE,IAAI;gBACd,KAAK,EAAE,GAAG,EAAE,GAAE,CAAC;aAChB,CAAC,CAAC;QACL,CAAC;KACF,CAAC,CAAC;IAEH,IAAI;QACF,WAAW,EAAE,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;KACrC;IAAC,OAAO,GAAG,EAAE;QACZ,UAAI,CAAC,KAAK,CAAC,sDAAsD,EAAE,GAAG,CAAC,CAAC;KACzE;IAED,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAkBF,MAAM,qCAAqC,GAAG,CAAI,EAChD,QAAQ,EACR,MAAM,EACN,aAAa,EACb,oBAAoB,EACpB,WAAW,GACkB,EAAE,EAAE;IACjC,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;QACzB,MAAM,EACJ,UAAU,EACV,IAAI,EACJ,aAAa,EAAE,iBAAiB,GACjC,GAAG,oBAAoB,CAAC,OAAO,CAAC,CAAC;QAElC,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,mBAAmB,EAAE;YAClD,UAAU,EAAE,KAAK;YACjB,QAAQ,EAAE,IAAI;YACd,KAAK,EAAE,GAAG,EAAE,CACV,yBAAyB,CACvB,OAAO,EACP,IAAI,EACJ,UAAU,EACV,aAAa,EACb,iBAAiB,EACjB,MAAM,EACN,WAAW,CACZ;SACJ,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,kBAAe;IACb,qCAAqC;IACrC,yBAAyB;CAC1B,CAAC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport {\n Tracer,\n SpanKind,\n Span,\n Context,\n Link,\n context,\n trace,\n diag,\n SpanAttributes,\n} from '@opentelemetry/api';\n\nconst START_SPAN_FUNCTION = Symbol(\n 'opentelemetry.pubsub-propagation.start_span'\n);\nconst END_SPAN_FUNCTION = Symbol('opentelemetry.pubsub-propagation.end_span');\n\ninterface OtelProcessedMessage {\n [START_SPAN_FUNCTION]?: () => Span;\n [END_SPAN_FUNCTION]?: () => void;\n}\n\nconst patchArrayFilter = (\n messages: unknown[],\n tracer: Tracer,\n loopContext: Context\n) => {\n const origFunc = messages.filter;\n const patchedFunc = function (\n this: unknown,\n ...args: Parameters<typeof origFunc>\n ) {\n const newArray = origFunc.apply(this, args);\n patchArrayForProcessSpans(newArray, tracer, loopContext);\n return newArray;\n };\n\n Object.defineProperty(messages, 'filter', {\n enumerable: false,\n value: patchedFunc,\n });\n};\n\nfunction isPromise(value: unknown): value is Promise<unknown> {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return typeof (value as any)?.then === 'function';\n}\n\nconst patchArrayFunction = (\n messages: OtelProcessedMessage[],\n functionName: 'forEach' | 'map',\n tracer: Tracer,\n loopContext: Context\n) => {\n const origFunc = messages[functionName] as typeof messages.map;\n const patchedFunc = function (\n this: unknown,\n ...arrFuncArgs: Parameters<typeof origFunc>\n ) {\n const callback = arrFuncArgs[0];\n const wrappedCallback = function (\n this: unknown,\n ...callbackArgs: Parameters<typeof callback>\n ) {\n const message = callbackArgs[0];\n const messageSpan = message?.[START_SPAN_FUNCTION]?.();\n if (!messageSpan) return callback.apply(this, callbackArgs);\n\n const res = context.with(trace.setSpan(loopContext, messageSpan), () => {\n let result: Promise<unknown> | unknown;\n try {\n result = callback.apply(this, callbackArgs);\n if (isPromise(result)) {\n const endSpan = () => message[END_SPAN_FUNCTION]?.();\n result.then(endSpan, endSpan);\n }\n return result;\n } finally {\n if (!isPromise(result)) {\n message[END_SPAN_FUNCTION]?.();\n }\n }\n });\n\n if (typeof res === 'object') {\n const startSpanFunction = Object.getOwnPropertyDescriptor(\n message,\n START_SPAN_FUNCTION\n );\n startSpanFunction &&\n Object.defineProperty(res, START_SPAN_FUNCTION, startSpanFunction);\n\n const endSpanFunction = Object.getOwnPropertyDescriptor(\n message,\n END_SPAN_FUNCTION\n );\n endSpanFunction &&\n Object.defineProperty(res, END_SPAN_FUNCTION, endSpanFunction);\n }\n return res;\n };\n arrFuncArgs[0] = wrappedCallback;\n const funcResult = origFunc.apply(this, arrFuncArgs);\n if (Array.isArray(funcResult))\n patchArrayForProcessSpans(funcResult, tracer, loopContext);\n return funcResult;\n };\n\n Object.defineProperty(messages, functionName, {\n enumerable: false,\n value: patchedFunc,\n });\n};\n\nconst patchArrayForProcessSpans = (\n messages: unknown[],\n tracer: Tracer,\n loopContext: Context = context.active()\n) => {\n patchArrayFunction(\n messages as OtelProcessedMessage[],\n 'forEach',\n tracer,\n loopContext\n );\n patchArrayFunction(\n messages as OtelProcessedMessage[],\n 'map',\n tracer,\n loopContext\n );\n patchArrayFilter(messages, tracer, loopContext);\n};\n\nconst startMessagingProcessSpan = <T>(\n message: T,\n name: string,\n attributes: SpanAttributes,\n parentContext: Context,\n propagatedContext: Context,\n tracer: Tracer,\n processHook?: ProcessHook<T>\n): Span => {\n const links: Link[] = [];\n const spanContext = trace.getSpanContext(propagatedContext);\n if (spanContext) {\n links.push({\n context: spanContext,\n } as Link);\n }\n\n const spanName = `${name} process`;\n const processSpan = tracer.startSpan(\n spanName,\n {\n kind: SpanKind.CONSUMER,\n attributes: {\n ...attributes,\n ['messaging.operation']: 'process',\n },\n links,\n },\n parentContext\n );\n\n Object.defineProperty(message, START_SPAN_FUNCTION, {\n enumerable: false,\n writable: true,\n value: () => processSpan,\n });\n\n Object.defineProperty(message, END_SPAN_FUNCTION, {\n enumerable: false,\n writable: true,\n value: () => {\n processSpan.end();\n Object.defineProperty(message, END_SPAN_FUNCTION, {\n enumerable: false,\n writable: true,\n value: () => {},\n });\n },\n });\n\n try {\n processHook?.(processSpan, message);\n } catch (err) {\n diag.error('opentelemetry-pubsub-propagation: process hook error', err);\n }\n\n return processSpan;\n};\n\ninterface SpanDetails {\n attributes: SpanAttributes;\n parentContext: Context;\n name: string;\n}\n\ntype ProcessHook<T> = (processSpan: Span, message: T) => void;\n\ninterface PatchForProcessingPayload<T> {\n messages: T[];\n tracer: Tracer;\n parentContext: Context;\n messageToSpanDetails: (message: T) => SpanDetails;\n processHook?: ProcessHook<T>;\n}\n\nconst patchMessagesArrayToStartProcessSpans = <T>({\n messages,\n tracer,\n parentContext,\n messageToSpanDetails,\n processHook,\n}: PatchForProcessingPayload<T>) => {\n messages.forEach(message => {\n const {\n attributes,\n name,\n parentContext: propagatedContext,\n } = messageToSpanDetails(message);\n\n Object.defineProperty(message, START_SPAN_FUNCTION, {\n enumerable: false,\n writable: true,\n value: () =>\n startMessagingProcessSpan<T>(\n message,\n name,\n attributes,\n parentContext,\n propagatedContext,\n tracer,\n processHook\n ),\n });\n });\n};\n\nexport default {\n patchMessagesArrayToStartProcessSpans,\n patchArrayForProcessSpans,\n};\n"]}
1
+ {"version":3,"file":"pubsub-propagation.js","sourceRoot":"","sources":["../../src/pubsub-propagation.ts"],"names":[],"mappings":";;AAAA;;;;;;;;;;;;;;GAcG;AACH,4CAU4B;AAE5B,MAAM,mBAAmB,GAAG,MAAM,CAChC,6CAA6C,CAC9C,CAAC;AACF,MAAM,iBAAiB,GAAG,MAAM,CAAC,2CAA2C,CAAC,CAAC;AAO9E,MAAM,gBAAgB,GAAG,CACvB,QAAmB,EACnB,MAAc,EACd,WAAoB,EACpB,EAAE;IACF,MAAM,QAAQ,GAAG,QAAQ,CAAC,MAAM,CAAC;IACjC,MAAM,WAA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,EAAE;YACV,WAAW,CAAC,GAAG,EAAE,CAAC;YAClB,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,iBAAiB,EAAE;gBAChD,UAAU,EAAE,KAAK;gBACjB,QAAQ,EAAE,IAAI;gBACd,KAAK,EAAE,GAAG,EAAE,GAAE,CAAC;aAChB,CAAC,CAAC;QACL,CAAC;KACF,CAAC,CAAC;IAEH,IAAI;QACF,WAAW,EAAE,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;KACrC;IAAC,OAAO,GAAG,EAAE;QACZ,UAAI,CAAC,KAAK,CAAC,sDAAsD,EAAE,GAAG,CAAC,CAAC;KACzE;IAED,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAkBF,MAAM,qCAAqC,GAAG,CAAI,EAChD,QAAQ,EACR,MAAM,EACN,aAAa,EACb,oBAAoB,EACpB,WAAW,GACkB,EAAE,EAAE;IACjC,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;QACzB,MAAM,EACJ,UAAU,EACV,IAAI,EACJ,aAAa,EAAE,iBAAiB,GACjC,GAAG,oBAAoB,CAAC,OAAO,CAAC,CAAC;QAElC,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,mBAAmB,EAAE;YAClD,UAAU,EAAE,KAAK;YACjB,QAAQ,EAAE,IAAI;YACd,KAAK,EAAE,GAAG,EAAE,CACV,yBAAyB,CACvB,OAAO,EACP,IAAI,EACJ,UAAU,EACV,aAAa,EACb,iBAAiB,EACjB,MAAM,EACN,WAAW,CACZ;SACJ,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,kBAAe;IACb,qCAAqC;IACrC,yBAAyB;CAC1B,CAAC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport {\n Tracer,\n SpanKind,\n Span,\n Context,\n Link,\n context,\n trace,\n diag,\n SpanAttributes,\n} from '@opentelemetry/api';\n\nconst START_SPAN_FUNCTION = Symbol(\n 'opentelemetry.pubsub-propagation.start_span'\n);\nconst END_SPAN_FUNCTION = Symbol('opentelemetry.pubsub-propagation.end_span');\n\ninterface OtelProcessedMessage {\n [START_SPAN_FUNCTION]?: () => Span;\n [END_SPAN_FUNCTION]?: () => void;\n}\n\nconst patchArrayFilter = (\n messages: unknown[],\n tracer: Tracer,\n loopContext: Context\n) => {\n const origFunc = messages.filter;\n const patchedFunc = function (\n this: unknown,\n ...args: Parameters<typeof origFunc>\n ) {\n const newArray = origFunc.apply(this, args);\n patchArrayForProcessSpans(newArray, tracer, loopContext);\n return newArray;\n };\n\n Object.defineProperty(messages, 'filter', {\n enumerable: false,\n value: patchedFunc,\n });\n};\n\nfunction isPromise(value: unknown): value is Promise<unknown> {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return typeof (value as any)?.then === 'function';\n}\n\nconst patchArrayFunction = (\n messages: OtelProcessedMessage[],\n functionName: 'forEach' | 'map',\n tracer: Tracer,\n loopContext: Context\n) => {\n const origFunc = messages[functionName] as typeof messages.map;\n const patchedFunc = function (\n this: unknown,\n ...arrFuncArgs: Parameters<typeof origFunc>\n ) {\n const callback = arrFuncArgs[0];\n const wrappedCallback = function (\n this: unknown,\n ...callbackArgs: Parameters<typeof callback>\n ) {\n const message = callbackArgs[0];\n const messageSpan = message?.[START_SPAN_FUNCTION]?.();\n if (!messageSpan) return callback.apply(this, callbackArgs);\n\n const res = context.with(trace.setSpan(loopContext, messageSpan), () => {\n let result: Promise<unknown> | unknown;\n try {\n result = callback.apply(this, callbackArgs);\n if (isPromise(result)) {\n const endSpan = () => message[END_SPAN_FUNCTION]?.();\n result.then(endSpan, endSpan);\n }\n return result;\n } finally {\n if (!isPromise(result)) {\n message[END_SPAN_FUNCTION]?.();\n }\n }\n });\n\n if (typeof res === 'object') {\n const startSpanFunction = Object.getOwnPropertyDescriptor(\n message,\n START_SPAN_FUNCTION\n );\n if (startSpanFunction) {\n Object.defineProperty(res, START_SPAN_FUNCTION, startSpanFunction);\n }\n\n const endSpanFunction = Object.getOwnPropertyDescriptor(\n message,\n END_SPAN_FUNCTION\n );\n if (endSpanFunction) {\n Object.defineProperty(res, END_SPAN_FUNCTION, endSpanFunction);\n }\n }\n return res;\n };\n arrFuncArgs[0] = wrappedCallback;\n const funcResult = origFunc.apply(this, arrFuncArgs);\n if (Array.isArray(funcResult))\n patchArrayForProcessSpans(funcResult, tracer, loopContext);\n return funcResult;\n };\n\n Object.defineProperty(messages, functionName, {\n enumerable: false,\n value: patchedFunc,\n });\n};\n\nconst patchArrayForProcessSpans = (\n messages: unknown[],\n tracer: Tracer,\n loopContext: Context = context.active()\n) => {\n patchArrayFunction(\n messages as OtelProcessedMessage[],\n 'forEach',\n tracer,\n loopContext\n );\n patchArrayFunction(\n messages as OtelProcessedMessage[],\n 'map',\n tracer,\n loopContext\n );\n patchArrayFilter(messages, tracer, loopContext);\n};\n\nconst startMessagingProcessSpan = <T>(\n message: T,\n name: string,\n attributes: SpanAttributes,\n parentContext: Context,\n propagatedContext: Context,\n tracer: Tracer,\n processHook?: ProcessHook<T>\n): Span => {\n const links: Link[] = [];\n const spanContext = trace.getSpanContext(propagatedContext);\n if (spanContext) {\n links.push({\n context: spanContext,\n } as Link);\n }\n\n const spanName = `${name} process`;\n const processSpan = tracer.startSpan(\n spanName,\n {\n kind: SpanKind.CONSUMER,\n attributes: {\n ...attributes,\n ['messaging.operation']: 'process',\n },\n links,\n },\n parentContext\n );\n\n Object.defineProperty(message, START_SPAN_FUNCTION, {\n enumerable: false,\n writable: true,\n value: () => processSpan,\n });\n\n Object.defineProperty(message, END_SPAN_FUNCTION, {\n enumerable: false,\n writable: true,\n value: () => {\n processSpan.end();\n Object.defineProperty(message, END_SPAN_FUNCTION, {\n enumerable: false,\n writable: true,\n value: () => {},\n });\n },\n });\n\n try {\n processHook?.(processSpan, message);\n } catch (err) {\n diag.error('opentelemetry-pubsub-propagation: process hook error', err);\n }\n\n return processSpan;\n};\n\ninterface SpanDetails {\n attributes: SpanAttributes;\n parentContext: Context;\n name: string;\n}\n\ntype ProcessHook<T> = (processSpan: Span, message: T) => void;\n\ninterface PatchForProcessingPayload<T> {\n messages: T[];\n tracer: Tracer;\n parentContext: Context;\n messageToSpanDetails: (message: T) => SpanDetails;\n processHook?: ProcessHook<T>;\n}\n\nconst patchMessagesArrayToStartProcessSpans = <T>({\n messages,\n tracer,\n parentContext,\n messageToSpanDetails,\n processHook,\n}: PatchForProcessingPayload<T>) => {\n messages.forEach(message => {\n const {\n attributes,\n name,\n parentContext: propagatedContext,\n } = messageToSpanDetails(message);\n\n Object.defineProperty(message, START_SPAN_FUNCTION, {\n enumerable: false,\n writable: true,\n value: () =>\n startMessagingProcessSpan<T>(\n message,\n name,\n attributes,\n parentContext,\n propagatedContext,\n tracer,\n processHook\n ),\n });\n });\n};\n\nexport default {\n patchMessagesArrayToStartProcessSpans,\n patchArrayForProcessSpans,\n};\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentelemetry/propagation-utils",
3
- "version": "0.31.9",
3
+ "version": "0.31.10",
4
4
  "description": "Propagation utilities for opentelemetry instrumentations",
5
5
  "main": "build/src/index.js",
6
6
  "types": "build/src/index.d.ts",
@@ -11,8 +11,6 @@
11
11
  "clean": "tsc --build --clean tsconfig.json tsconfig.esm.json",
12
12
  "compile": "tsc --build tsconfig.json tsconfig.esm.json",
13
13
  "compile:with-dependencies": "nx run-many -t compile -p @opentelemetry/propagation-utils",
14
- "lint": "eslint . --ext=ts,js,mjs",
15
- "lint:fix": "eslint . --ext=ts,js,mjs --fix",
16
14
  "prepublishOnly": "npm run compile",
17
15
  "tdd": "npm run test -- --watch-extensions ts --watch",
18
16
  "test": "nyc mocha 'test/**/*.test.ts'",
@@ -46,7 +44,7 @@
46
44
  },
47
45
  "devDependencies": {
48
46
  "@opentelemetry/api": "^1.0.0",
49
- "@opentelemetry/contrib-test-utils": "^0.53.0",
47
+ "@opentelemetry/contrib-test-utils": "^0.54.0",
50
48
  "@types/mocha": "^10.0.0",
51
49
  "@types/node": "18.18.14",
52
50
  "@types/sinon": "^17.0.0",
@@ -55,5 +53,5 @@
55
53
  "sinon": "15.2.0",
56
54
  "typescript": "5.0.4"
57
55
  },
58
- "gitHead": "333e026413f082f90859bb778abf7519fbffa940"
56
+ "gitHead": "2801ab2f0f9243b154a624298dacb7228c4f70cd"
59
57
  }