@opentelemetry/propagation-utils 0.30.16 → 0.31.0
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.
- package/build/esm/pubsub-propagation.d.ts +1 -1
- package/build/esm/pubsub-propagation.js +43 -72
- package/build/esm/pubsub-propagation.js.map +1 -1
- package/build/src/pubsub-propagation.d.ts +1 -1
- package/build/src/pubsub-propagation.js +9 -9
- package/build/src/pubsub-propagation.js.map +1 -1
- package/package.json +7 -7
|
@@ -4,7 +4,7 @@ interface SpanDetails {
|
|
|
4
4
|
parentContext: Context;
|
|
5
5
|
name: string;
|
|
6
6
|
}
|
|
7
|
-
|
|
7
|
+
type ProcessHook<T> = (processSpan: Span, message: T) => void;
|
|
8
8
|
interface PatchForProcessingPayload<T> {
|
|
9
9
|
messages: T[];
|
|
10
10
|
tracer: Tracer;
|
|
@@ -1,14 +1,3 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
1
|
/*
|
|
13
2
|
* Copyright The OpenTelemetry Authors
|
|
14
3
|
*
|
|
@@ -25,16 +14,12 @@ var __assign = (this && this.__assign) || function () {
|
|
|
25
14
|
* limitations under the License.
|
|
26
15
|
*/
|
|
27
16
|
import { SpanKind, context, trace, diag, } from '@opentelemetry/api';
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
35
|
-
args[_i] = arguments[_i];
|
|
36
|
-
}
|
|
37
|
-
var newArray = origFunc.apply(this, args);
|
|
17
|
+
const START_SPAN_FUNCTION = Symbol('opentelemetry.pubsub-propagation.start_span');
|
|
18
|
+
const END_SPAN_FUNCTION = Symbol('opentelemetry.pubsub-propagation.end_span');
|
|
19
|
+
const patchArrayFilter = (messages, tracer, loopContext) => {
|
|
20
|
+
const origFunc = messages.filter;
|
|
21
|
+
const patchedFunc = function (...args) {
|
|
22
|
+
const newArray = origFunc.apply(this, args);
|
|
38
23
|
patchArrayForProcessSpans(newArray, tracer, loopContext);
|
|
39
24
|
return newArray;
|
|
40
25
|
};
|
|
@@ -44,58 +29,46 @@ var patchArrayFilter = function (messages, tracer, loopContext) {
|
|
|
44
29
|
});
|
|
45
30
|
};
|
|
46
31
|
function isPromise(value) {
|
|
47
|
-
var _a;
|
|
48
32
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
49
|
-
return typeof
|
|
33
|
+
return typeof value?.then === 'function';
|
|
50
34
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
var callback = arrFuncArgs[0];
|
|
59
|
-
var wrappedCallback = function () {
|
|
60
|
-
var _this = this;
|
|
61
|
-
var _a;
|
|
62
|
-
var callbackArgs = [];
|
|
63
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
64
|
-
callbackArgs[_i] = arguments[_i];
|
|
65
|
-
}
|
|
66
|
-
var message = callbackArgs[0];
|
|
67
|
-
var messageSpan = (_a = message === null || message === void 0 ? void 0 : message[START_SPAN_FUNCTION]) === null || _a === void 0 ? void 0 : _a.call(message);
|
|
35
|
+
const patchArrayFunction = (messages, functionName, tracer, loopContext) => {
|
|
36
|
+
const origFunc = messages[functionName];
|
|
37
|
+
const patchedFunc = function (...arrFuncArgs) {
|
|
38
|
+
const callback = arrFuncArgs[0];
|
|
39
|
+
const wrappedCallback = function (...callbackArgs) {
|
|
40
|
+
const message = callbackArgs[0];
|
|
41
|
+
const messageSpan = message?.[START_SPAN_FUNCTION]?.();
|
|
68
42
|
if (!messageSpan)
|
|
69
43
|
return callback.apply(this, callbackArgs);
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
var result;
|
|
44
|
+
const res = context.with(trace.setSpan(loopContext, messageSpan), () => {
|
|
45
|
+
let result;
|
|
73
46
|
try {
|
|
74
|
-
result = callback.apply(
|
|
47
|
+
result = callback.apply(this, callbackArgs);
|
|
75
48
|
if (isPromise(result)) {
|
|
76
|
-
|
|
49
|
+
const endSpan = () => message[END_SPAN_FUNCTION]?.();
|
|
77
50
|
result.then(endSpan, endSpan);
|
|
78
51
|
}
|
|
79
52
|
return result;
|
|
80
53
|
}
|
|
81
54
|
finally {
|
|
82
55
|
if (!isPromise(result)) {
|
|
83
|
-
|
|
56
|
+
message[END_SPAN_FUNCTION]?.();
|
|
84
57
|
}
|
|
85
58
|
}
|
|
86
59
|
});
|
|
87
60
|
if (typeof res === 'object') {
|
|
88
|
-
|
|
61
|
+
const startSpanFunction = Object.getOwnPropertyDescriptor(message, START_SPAN_FUNCTION);
|
|
89
62
|
startSpanFunction &&
|
|
90
63
|
Object.defineProperty(res, START_SPAN_FUNCTION, startSpanFunction);
|
|
91
|
-
|
|
64
|
+
const endSpanFunction = Object.getOwnPropertyDescriptor(message, END_SPAN_FUNCTION);
|
|
92
65
|
endSpanFunction &&
|
|
93
66
|
Object.defineProperty(res, END_SPAN_FUNCTION, endSpanFunction);
|
|
94
67
|
}
|
|
95
68
|
return res;
|
|
96
69
|
};
|
|
97
70
|
arrFuncArgs[0] = wrappedCallback;
|
|
98
|
-
|
|
71
|
+
const funcResult = origFunc.apply(this, arrFuncArgs);
|
|
99
72
|
if (Array.isArray(funcResult))
|
|
100
73
|
patchArrayForProcessSpans(funcResult, tracer, loopContext);
|
|
101
74
|
return funcResult;
|
|
@@ -105,67 +78,65 @@ var patchArrayFunction = function (messages, functionName, tracer, loopContext)
|
|
|
105
78
|
value: patchedFunc,
|
|
106
79
|
});
|
|
107
80
|
};
|
|
108
|
-
|
|
109
|
-
if (loopContext === void 0) { loopContext = context.active(); }
|
|
81
|
+
const patchArrayForProcessSpans = (messages, tracer, loopContext = context.active()) => {
|
|
110
82
|
patchArrayFunction(messages, 'forEach', tracer, loopContext);
|
|
111
83
|
patchArrayFunction(messages, 'map', tracer, loopContext);
|
|
112
84
|
patchArrayFilter(messages, tracer, loopContext);
|
|
113
85
|
};
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
var spanContext = trace.getSpanContext(propagatedContext);
|
|
86
|
+
const startMessagingProcessSpan = (message, name, attributes, parentContext, propagatedContext, tracer, processHook) => {
|
|
87
|
+
const links = [];
|
|
88
|
+
const spanContext = trace.getSpanContext(propagatedContext);
|
|
118
89
|
if (spanContext) {
|
|
119
90
|
links.push({
|
|
120
91
|
context: spanContext,
|
|
121
92
|
});
|
|
122
93
|
}
|
|
123
|
-
|
|
124
|
-
|
|
94
|
+
const spanName = `${name} process`;
|
|
95
|
+
const processSpan = tracer.startSpan(spanName, {
|
|
125
96
|
kind: SpanKind.CONSUMER,
|
|
126
|
-
attributes:
|
|
127
|
-
|
|
97
|
+
attributes: {
|
|
98
|
+
...attributes,
|
|
99
|
+
['messaging.operation']: 'process',
|
|
100
|
+
},
|
|
101
|
+
links,
|
|
128
102
|
}, parentContext);
|
|
129
103
|
Object.defineProperty(message, START_SPAN_FUNCTION, {
|
|
130
104
|
enumerable: false,
|
|
131
105
|
writable: true,
|
|
132
|
-
value:
|
|
106
|
+
value: () => processSpan,
|
|
133
107
|
});
|
|
134
108
|
Object.defineProperty(message, END_SPAN_FUNCTION, {
|
|
135
109
|
enumerable: false,
|
|
136
110
|
writable: true,
|
|
137
|
-
value:
|
|
111
|
+
value: () => {
|
|
138
112
|
processSpan.end();
|
|
139
113
|
Object.defineProperty(message, END_SPAN_FUNCTION, {
|
|
140
114
|
enumerable: false,
|
|
141
115
|
writable: true,
|
|
142
|
-
value:
|
|
116
|
+
value: () => { },
|
|
143
117
|
});
|
|
144
118
|
},
|
|
145
119
|
});
|
|
146
120
|
try {
|
|
147
|
-
processHook
|
|
121
|
+
processHook?.(processSpan, message);
|
|
148
122
|
}
|
|
149
123
|
catch (err) {
|
|
150
124
|
diag.error('opentelemetry-pubsub-propagation: process hook error', err);
|
|
151
125
|
}
|
|
152
126
|
return processSpan;
|
|
153
127
|
};
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
var _a = messageToSpanDetails(message), attributes = _a.attributes, name = _a.name, propagatedContext = _a.parentContext;
|
|
128
|
+
const patchMessagesArrayToStartProcessSpans = ({ messages, tracer, parentContext, messageToSpanDetails, processHook, }) => {
|
|
129
|
+
messages.forEach(message => {
|
|
130
|
+
const { attributes, name, parentContext: propagatedContext, } = messageToSpanDetails(message);
|
|
158
131
|
Object.defineProperty(message, START_SPAN_FUNCTION, {
|
|
159
132
|
enumerable: false,
|
|
160
133
|
writable: true,
|
|
161
|
-
value:
|
|
162
|
-
return startMessagingProcessSpan(message, name, attributes, parentContext, propagatedContext, tracer, processHook);
|
|
163
|
-
},
|
|
134
|
+
value: () => startMessagingProcessSpan(message, name, attributes, parentContext, propagatedContext, tracer, processHook),
|
|
164
135
|
});
|
|
165
136
|
});
|
|
166
137
|
};
|
|
167
138
|
export default {
|
|
168
|
-
patchMessagesArrayToStartProcessSpans
|
|
169
|
-
patchArrayForProcessSpans
|
|
139
|
+
patchMessagesArrayToStartProcessSpans,
|
|
140
|
+
patchArrayForProcessSpans,
|
|
170
141
|
};
|
|
171
142
|
//# sourceMappingURL=pubsub-propagation.js.map
|
|
@@ -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;AA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mB,EAAE;YAClD,UAAU,EAAE,KAAK;YACjB,QAAQ,EAAE,IAAI;YACd,KAAK,EAAE;gBACL,OAAA,yBAAyB,CACvB,OAAO,EACP,IAAI,EACJ,UAAU,EACV,aAAa,EACb,iBAAiB,EACjB,MAAM,EACN,WAAW,CACZ;YARD,CAQC;SACJ,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,eAAe;IACb,qCAAqC,uCAAA;IACrC,yBAAyB,2BAAA;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,EAAE;QACxC,UAAU,EAAE,KAAK;QACjB,KAAK,EAAE,WAAW;KACnB,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,SAAS,SAAS,CAAC,KAAc;IAC/B,8DAA8D;IAC9D,OAAO,OAAQ,KAAa,EAAE,IAAI,KAAK,UAAU,CAAC;AACpD,CAAC;AAED,MAAM,kBAAkB,GAAG,CACzB,QAAgC,EAChC,YAA+B,EAC/B,MAAc,EACd,WAAoB,EACpB,EAAE;IACF,MAAM,QAAQ,GAAG,QAAQ,CAAC,YAAY,CAAwB,CAAC;IAC/D,MAAM,WAAW,GAAG,UAElB,GAAG,WAAwC;QAE3C,MAAM,QAAQ,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;QA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,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"]}
|
|
@@ -4,7 +4,7 @@ interface SpanDetails {
|
|
|
4
4
|
parentContext: Context;
|
|
5
5
|
name: string;
|
|
6
6
|
}
|
|
7
|
-
|
|
7
|
+
type ProcessHook<T> = (processSpan: Span, message: T) => void;
|
|
8
8
|
interface PatchForProcessingPayload<T> {
|
|
9
9
|
messages: T[];
|
|
10
10
|
tracer: Tracer;
|
|
@@ -31,34 +31,31 @@ const patchArrayFilter = (messages, tracer, loopContext) => {
|
|
|
31
31
|
});
|
|
32
32
|
};
|
|
33
33
|
function isPromise(value) {
|
|
34
|
-
var _a;
|
|
35
34
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
-
return typeof
|
|
35
|
+
return typeof value?.then === 'function';
|
|
37
36
|
}
|
|
38
37
|
const patchArrayFunction = (messages, functionName, tracer, loopContext) => {
|
|
39
38
|
const origFunc = messages[functionName];
|
|
40
39
|
const patchedFunc = function (...arrFuncArgs) {
|
|
41
40
|
const callback = arrFuncArgs[0];
|
|
42
41
|
const wrappedCallback = function (...callbackArgs) {
|
|
43
|
-
var _a;
|
|
44
42
|
const message = callbackArgs[0];
|
|
45
|
-
const messageSpan =
|
|
43
|
+
const messageSpan = message?.[START_SPAN_FUNCTION]?.();
|
|
46
44
|
if (!messageSpan)
|
|
47
45
|
return callback.apply(this, callbackArgs);
|
|
48
46
|
const res = api_1.context.with(api_1.trace.setSpan(loopContext, messageSpan), () => {
|
|
49
|
-
var _a;
|
|
50
47
|
let result;
|
|
51
48
|
try {
|
|
52
49
|
result = callback.apply(this, callbackArgs);
|
|
53
50
|
if (isPromise(result)) {
|
|
54
|
-
const endSpan = () =>
|
|
51
|
+
const endSpan = () => message[END_SPAN_FUNCTION]?.();
|
|
55
52
|
result.then(endSpan, endSpan);
|
|
56
53
|
}
|
|
57
54
|
return result;
|
|
58
55
|
}
|
|
59
56
|
finally {
|
|
60
57
|
if (!isPromise(result)) {
|
|
61
|
-
|
|
58
|
+
message[END_SPAN_FUNCTION]?.();
|
|
62
59
|
}
|
|
63
60
|
}
|
|
64
61
|
});
|
|
@@ -99,7 +96,10 @@ const startMessagingProcessSpan = (message, name, attributes, parentContext, pro
|
|
|
99
96
|
const spanName = `${name} process`;
|
|
100
97
|
const processSpan = tracer.startSpan(spanName, {
|
|
101
98
|
kind: api_1.SpanKind.CONSUMER,
|
|
102
|
-
attributes:
|
|
99
|
+
attributes: {
|
|
100
|
+
...attributes,
|
|
101
|
+
['messaging.operation']: 'process',
|
|
102
|
+
},
|
|
103
103
|
links,
|
|
104
104
|
}, parentContext);
|
|
105
105
|
Object.defineProperty(message, START_SPAN_FUNCTION, {
|
|
@@ -120,7 +120,7 @@ const startMessagingProcessSpan = (message, name, attributes, parentContext, pro
|
|
|
120
120
|
},
|
|
121
121
|
});
|
|
122
122
|
try {
|
|
123
|
-
processHook
|
|
123
|
+
processHook?.(processSpan, message);
|
|
124
124
|
}
|
|
125
125
|
catch (err) {
|
|
126
126
|
api_1.diag.error('opentelemetry-pubsub-propagation: process hook error', err);
|
|
@@ -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,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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,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,aAAX,WAAW,uBAAX,WAAW,CAAG,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,WAAW,GAAG,UAElB,GAAG,IAAiC;QAEpC,MAAM,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAC5C,yBAAyB,CAAC,QAAQ,EAAE,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AE,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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opentelemetry/propagation-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.31.0",
|
|
4
4
|
"description": "Propagation utilities for opentelemetry instrumentations",
|
|
5
5
|
"main": "build/src/index.js",
|
|
6
6
|
"types": "build/src/index.d.ts",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"author": "OpenTelemetry Authors",
|
|
32
32
|
"license": "Apache-2.0",
|
|
33
33
|
"engines": {
|
|
34
|
-
"node": ">=
|
|
34
|
+
"node": "^18.19.0 || >=20.6.0"
|
|
35
35
|
},
|
|
36
36
|
"bugs": {
|
|
37
37
|
"url": "https://github.com/open-telemetry/opentelemetry-js-contrib/issues"
|
|
@@ -42,14 +42,14 @@
|
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@opentelemetry/api": "^1.0.0",
|
|
45
|
-
"@opentelemetry/contrib-test-utils": "^0.
|
|
46
|
-
"@types/mocha": "^
|
|
45
|
+
"@opentelemetry/contrib-test-utils": "^0.46.0",
|
|
46
|
+
"@types/mocha": "^10.0.0",
|
|
47
47
|
"@types/node": "18.18.14",
|
|
48
|
-
"@types/sinon": "^
|
|
48
|
+
"@types/sinon": "^17.0.0",
|
|
49
49
|
"expect": "29.2.0",
|
|
50
50
|
"nyc": "15.1.0",
|
|
51
51
|
"sinon": "15.2.0",
|
|
52
|
-
"typescript": "
|
|
52
|
+
"typescript": "5.0.4"
|
|
53
53
|
},
|
|
54
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "32abc4c3c01d0e78e10022c74b6805b06e0e1fe7"
|
|
55
55
|
}
|