@envelop/opentelemetry 6.2.0 → 6.2.1-rc-20240118120948-a98c61be
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/cjs/index.js +37 -11
- package/esm/index.js +34 -10
- package/package.json +1 -1
- package/typings/index.d.cts +3 -0
- package/typings/index.d.ts +3 -0
package/cjs/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useOpenTelemetry = exports.AttributeName = void 0;
|
|
3
|
+
exports.useOpenTelemetry = exports.setCurrentOtelContext = exports.getCurrentOtelContext = exports.otelContextMap = exports.AttributeName = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const graphql_1 = require("graphql");
|
|
6
6
|
const core_1 = require("@envelop/core");
|
|
@@ -24,6 +24,21 @@ var AttributeName;
|
|
|
24
24
|
AttributeName["EXECUTION_VARIABLES"] = "graphql.execute.variables";
|
|
25
25
|
})(AttributeName || (exports.AttributeName = AttributeName = {}));
|
|
26
26
|
const tracingSpanSymbol = Symbol('OPEN_TELEMETRY_GRAPHQL');
|
|
27
|
+
exports.otelContextMap = new WeakMap();
|
|
28
|
+
function getCurrentOtelContext(graphqlContext) {
|
|
29
|
+
let otelContext = exports.otelContextMap.get(graphqlContext);
|
|
30
|
+
if (!otelContext) {
|
|
31
|
+
otelContext = opentelemetry.context.active();
|
|
32
|
+
exports.otelContextMap.set(graphqlContext, otelContext);
|
|
33
|
+
}
|
|
34
|
+
return otelContext;
|
|
35
|
+
}
|
|
36
|
+
exports.getCurrentOtelContext = getCurrentOtelContext;
|
|
37
|
+
function setCurrentOtelContext(graphqlContext, otelContext) {
|
|
38
|
+
exports.otelContextMap.set(graphqlContext, otelContext);
|
|
39
|
+
return otelContext;
|
|
40
|
+
}
|
|
41
|
+
exports.setCurrentOtelContext = setCurrentOtelContext;
|
|
27
42
|
const useOpenTelemetry = (options, tracingProvider, spanKind = api_1.SpanKind.SERVER, spanAdditionalAttributes = {}, serviceName = 'graphql', spanPrefix = '') => {
|
|
28
43
|
if (!tracingProvider) {
|
|
29
44
|
const basicTraceProvider = new sdk_trace_base_1.BasicTracerProvider();
|
|
@@ -39,7 +54,7 @@ const useOpenTelemetry = (options, tracingProvider, spanKind = api_1.SpanKind.SE
|
|
|
39
54
|
addPlugin((0, on_resolve_1.useOnResolve)(({ info, context, args }) => {
|
|
40
55
|
const parentSpan = spanByContext.get(context);
|
|
41
56
|
if (parentSpan) {
|
|
42
|
-
const ctx = opentelemetry.trace.setSpan(
|
|
57
|
+
const ctx = opentelemetry.trace.setSpan(getCurrentOtelContext(context), parentSpan);
|
|
43
58
|
const { fieldName, returnType, parentType } = info;
|
|
44
59
|
const resolverSpan = tracer.startSpan(`${spanPrefix}${parentType.name}.${fieldName}`, {
|
|
45
60
|
attributes: {
|
|
@@ -65,7 +80,10 @@ const useOpenTelemetry = (options, tracingProvider, spanKind = api_1.SpanKind.SE
|
|
|
65
80
|
}));
|
|
66
81
|
}
|
|
67
82
|
},
|
|
68
|
-
onExecute({ args }) {
|
|
83
|
+
onExecute({ args, executeFn, setExecuteFn }) {
|
|
84
|
+
setExecuteFn(function wrappedExecuteFnWithOtelCtx(args) {
|
|
85
|
+
return opentelemetry.context.with(getCurrentOtelContext(args.contextValue), () => executeFn(args));
|
|
86
|
+
});
|
|
69
87
|
const operationAst = (0, graphql_1.getOperationAST)(args.document, args.operationName);
|
|
70
88
|
if (!operationAst) {
|
|
71
89
|
return;
|
|
@@ -87,6 +105,7 @@ const useOpenTelemetry = (options, tracingProvider, spanKind = api_1.SpanKind.SE
|
|
|
87
105
|
isDocumentLoggable = false;
|
|
88
106
|
}
|
|
89
107
|
const operationName = operationAst.name?.value || 'anonymous';
|
|
108
|
+
const currOtelContext = getCurrentOtelContext(args.contextValue);
|
|
90
109
|
const executionSpan = tracer.startSpan(`${spanPrefix}${operationType}.${operationName}`, {
|
|
91
110
|
kind: spanKind,
|
|
92
111
|
attributes: {
|
|
@@ -100,8 +119,8 @@ const useOpenTelemetry = (options, tracingProvider, spanKind = api_1.SpanKind.SE
|
|
|
100
119
|
? { [AttributeName.EXECUTION_VARIABLES]: JSON.stringify(args.variableValues ?? {}) }
|
|
101
120
|
: {}),
|
|
102
121
|
},
|
|
103
|
-
});
|
|
104
|
-
|
|
122
|
+
}, currOtelContext);
|
|
123
|
+
setCurrentOtelContext(args.contextValue, opentelemetry.trace.setSpan(currOtelContext, executionSpan));
|
|
105
124
|
const resultCbs = {
|
|
106
125
|
onExecuteDone({ result, setResult }) {
|
|
107
126
|
if (!(0, core_1.isAsyncIterable)(result)) {
|
|
@@ -109,7 +128,8 @@ const useOpenTelemetry = (options, tracingProvider, spanKind = api_1.SpanKind.SE
|
|
|
109
128
|
executionSpan.setAttribute(AttributeName.EXECUTION_RESULT, JSON.stringify(result));
|
|
110
129
|
}
|
|
111
130
|
if (options.traceIdInResult) {
|
|
112
|
-
|
|
131
|
+
const currOtelContext = getCurrentOtelContext(args.contextValue);
|
|
132
|
+
setResult(addTraceIdToResult(currOtelContext, result, options.traceIdInResult));
|
|
113
133
|
}
|
|
114
134
|
markError(executionSpan, result);
|
|
115
135
|
executionSpan.end();
|
|
@@ -118,7 +138,8 @@ const useOpenTelemetry = (options, tracingProvider, spanKind = api_1.SpanKind.SE
|
|
|
118
138
|
// handles async iterator
|
|
119
139
|
onNext: ({ result, setResult }) => {
|
|
120
140
|
if (options.traceIdInResult) {
|
|
121
|
-
|
|
141
|
+
const currOtelContext = getCurrentOtelContext(args.contextValue);
|
|
142
|
+
setResult(addTraceIdToResult(currOtelContext, result, options.traceIdInResult));
|
|
122
143
|
}
|
|
123
144
|
markError(executionSpan, result);
|
|
124
145
|
},
|
|
@@ -133,7 +154,10 @@ const useOpenTelemetry = (options, tracingProvider, spanKind = api_1.SpanKind.SE
|
|
|
133
154
|
}
|
|
134
155
|
return resultCbs;
|
|
135
156
|
},
|
|
136
|
-
onSubscribe({ args }) {
|
|
157
|
+
onSubscribe({ args, subscribeFn, setSubscribeFn }) {
|
|
158
|
+
setSubscribeFn(function wrappedSubscribeFnWithOtelCtx(args) {
|
|
159
|
+
return opentelemetry.context.with(getCurrentOtelContext(args.contextValue), () => subscribeFn(args));
|
|
160
|
+
});
|
|
137
161
|
const operationAst = (0, graphql_1.getOperationAST)(args.document, args.operationName);
|
|
138
162
|
if (!operationAst) {
|
|
139
163
|
return;
|
|
@@ -149,6 +173,7 @@ const useOpenTelemetry = (options, tracingProvider, spanKind = api_1.SpanKind.SE
|
|
|
149
173
|
else {
|
|
150
174
|
isDocumentLoggable = false;
|
|
151
175
|
}
|
|
176
|
+
const currOtelContext = getCurrentOtelContext(args.contextValue);
|
|
152
177
|
const operationName = operationAst.name?.value || 'anonymous';
|
|
153
178
|
const subscriptionSpan = tracer.startSpan(`${operationType}.${operationName}`, {
|
|
154
179
|
kind: spanKind,
|
|
@@ -163,8 +188,8 @@ const useOpenTelemetry = (options, tracingProvider, spanKind = api_1.SpanKind.SE
|
|
|
163
188
|
? { [AttributeName.EXECUTION_VARIABLES]: JSON.stringify(args.variableValues ?? {}) }
|
|
164
189
|
: {}),
|
|
165
190
|
},
|
|
166
|
-
});
|
|
167
|
-
|
|
191
|
+
}, currOtelContext);
|
|
192
|
+
setCurrentOtelContext(args.contextValue, opentelemetry.trace.setSpan(currOtelContext, subscriptionSpan));
|
|
168
193
|
const resultCbs = {
|
|
169
194
|
onSubscribeError: ({ error }) => {
|
|
170
195
|
if (error)
|
|
@@ -175,7 +200,8 @@ const useOpenTelemetry = (options, tracingProvider, spanKind = api_1.SpanKind.SE
|
|
|
175
200
|
// handles async iterator
|
|
176
201
|
onNext: ({ result, setResult }) => {
|
|
177
202
|
if (options.traceIdInResult) {
|
|
178
|
-
|
|
203
|
+
const currOtelContext = getCurrentOtelContext(args.contextValue);
|
|
204
|
+
setResult(addTraceIdToResult(currOtelContext, result, options.traceIdInResult));
|
|
179
205
|
}
|
|
180
206
|
markError(subscriptionSpan, result);
|
|
181
207
|
},
|
package/esm/index.js
CHANGED
|
@@ -20,6 +20,19 @@ export var AttributeName;
|
|
|
20
20
|
AttributeName["EXECUTION_VARIABLES"] = "graphql.execute.variables";
|
|
21
21
|
})(AttributeName || (AttributeName = {}));
|
|
22
22
|
const tracingSpanSymbol = Symbol('OPEN_TELEMETRY_GRAPHQL');
|
|
23
|
+
export const otelContextMap = new WeakMap();
|
|
24
|
+
export function getCurrentOtelContext(graphqlContext) {
|
|
25
|
+
let otelContext = otelContextMap.get(graphqlContext);
|
|
26
|
+
if (!otelContext) {
|
|
27
|
+
otelContext = opentelemetry.context.active();
|
|
28
|
+
otelContextMap.set(graphqlContext, otelContext);
|
|
29
|
+
}
|
|
30
|
+
return otelContext;
|
|
31
|
+
}
|
|
32
|
+
export function setCurrentOtelContext(graphqlContext, otelContext) {
|
|
33
|
+
otelContextMap.set(graphqlContext, otelContext);
|
|
34
|
+
return otelContext;
|
|
35
|
+
}
|
|
23
36
|
export const useOpenTelemetry = (options, tracingProvider, spanKind = SpanKind.SERVER, spanAdditionalAttributes = {}, serviceName = 'graphql', spanPrefix = '') => {
|
|
24
37
|
if (!tracingProvider) {
|
|
25
38
|
const basicTraceProvider = new BasicTracerProvider();
|
|
@@ -35,7 +48,7 @@ export const useOpenTelemetry = (options, tracingProvider, spanKind = SpanKind.S
|
|
|
35
48
|
addPlugin(useOnResolve(({ info, context, args }) => {
|
|
36
49
|
const parentSpan = spanByContext.get(context);
|
|
37
50
|
if (parentSpan) {
|
|
38
|
-
const ctx = opentelemetry.trace.setSpan(
|
|
51
|
+
const ctx = opentelemetry.trace.setSpan(getCurrentOtelContext(context), parentSpan);
|
|
39
52
|
const { fieldName, returnType, parentType } = info;
|
|
40
53
|
const resolverSpan = tracer.startSpan(`${spanPrefix}${parentType.name}.${fieldName}`, {
|
|
41
54
|
attributes: {
|
|
@@ -61,7 +74,10 @@ export const useOpenTelemetry = (options, tracingProvider, spanKind = SpanKind.S
|
|
|
61
74
|
}));
|
|
62
75
|
}
|
|
63
76
|
},
|
|
64
|
-
onExecute({ args }) {
|
|
77
|
+
onExecute({ args, executeFn, setExecuteFn }) {
|
|
78
|
+
setExecuteFn(function wrappedExecuteFnWithOtelCtx(args) {
|
|
79
|
+
return opentelemetry.context.with(getCurrentOtelContext(args.contextValue), () => executeFn(args));
|
|
80
|
+
});
|
|
65
81
|
const operationAst = getOperationAST(args.document, args.operationName);
|
|
66
82
|
if (!operationAst) {
|
|
67
83
|
return;
|
|
@@ -83,6 +99,7 @@ export const useOpenTelemetry = (options, tracingProvider, spanKind = SpanKind.S
|
|
|
83
99
|
isDocumentLoggable = false;
|
|
84
100
|
}
|
|
85
101
|
const operationName = operationAst.name?.value || 'anonymous';
|
|
102
|
+
const currOtelContext = getCurrentOtelContext(args.contextValue);
|
|
86
103
|
const executionSpan = tracer.startSpan(`${spanPrefix}${operationType}.${operationName}`, {
|
|
87
104
|
kind: spanKind,
|
|
88
105
|
attributes: {
|
|
@@ -96,8 +113,8 @@ export const useOpenTelemetry = (options, tracingProvider, spanKind = SpanKind.S
|
|
|
96
113
|
? { [AttributeName.EXECUTION_VARIABLES]: JSON.stringify(args.variableValues ?? {}) }
|
|
97
114
|
: {}),
|
|
98
115
|
},
|
|
99
|
-
});
|
|
100
|
-
|
|
116
|
+
}, currOtelContext);
|
|
117
|
+
setCurrentOtelContext(args.contextValue, opentelemetry.trace.setSpan(currOtelContext, executionSpan));
|
|
101
118
|
const resultCbs = {
|
|
102
119
|
onExecuteDone({ result, setResult }) {
|
|
103
120
|
if (!isAsyncIterable(result)) {
|
|
@@ -105,7 +122,8 @@ export const useOpenTelemetry = (options, tracingProvider, spanKind = SpanKind.S
|
|
|
105
122
|
executionSpan.setAttribute(AttributeName.EXECUTION_RESULT, JSON.stringify(result));
|
|
106
123
|
}
|
|
107
124
|
if (options.traceIdInResult) {
|
|
108
|
-
|
|
125
|
+
const currOtelContext = getCurrentOtelContext(args.contextValue);
|
|
126
|
+
setResult(addTraceIdToResult(currOtelContext, result, options.traceIdInResult));
|
|
109
127
|
}
|
|
110
128
|
markError(executionSpan, result);
|
|
111
129
|
executionSpan.end();
|
|
@@ -114,7 +132,8 @@ export const useOpenTelemetry = (options, tracingProvider, spanKind = SpanKind.S
|
|
|
114
132
|
// handles async iterator
|
|
115
133
|
onNext: ({ result, setResult }) => {
|
|
116
134
|
if (options.traceIdInResult) {
|
|
117
|
-
|
|
135
|
+
const currOtelContext = getCurrentOtelContext(args.contextValue);
|
|
136
|
+
setResult(addTraceIdToResult(currOtelContext, result, options.traceIdInResult));
|
|
118
137
|
}
|
|
119
138
|
markError(executionSpan, result);
|
|
120
139
|
},
|
|
@@ -129,7 +148,10 @@ export const useOpenTelemetry = (options, tracingProvider, spanKind = SpanKind.S
|
|
|
129
148
|
}
|
|
130
149
|
return resultCbs;
|
|
131
150
|
},
|
|
132
|
-
onSubscribe({ args }) {
|
|
151
|
+
onSubscribe({ args, subscribeFn, setSubscribeFn }) {
|
|
152
|
+
setSubscribeFn(function wrappedSubscribeFnWithOtelCtx(args) {
|
|
153
|
+
return opentelemetry.context.with(getCurrentOtelContext(args.contextValue), () => subscribeFn(args));
|
|
154
|
+
});
|
|
133
155
|
const operationAst = getOperationAST(args.document, args.operationName);
|
|
134
156
|
if (!operationAst) {
|
|
135
157
|
return;
|
|
@@ -145,6 +167,7 @@ export const useOpenTelemetry = (options, tracingProvider, spanKind = SpanKind.S
|
|
|
145
167
|
else {
|
|
146
168
|
isDocumentLoggable = false;
|
|
147
169
|
}
|
|
170
|
+
const currOtelContext = getCurrentOtelContext(args.contextValue);
|
|
148
171
|
const operationName = operationAst.name?.value || 'anonymous';
|
|
149
172
|
const subscriptionSpan = tracer.startSpan(`${operationType}.${operationName}`, {
|
|
150
173
|
kind: spanKind,
|
|
@@ -159,8 +182,8 @@ export const useOpenTelemetry = (options, tracingProvider, spanKind = SpanKind.S
|
|
|
159
182
|
? { [AttributeName.EXECUTION_VARIABLES]: JSON.stringify(args.variableValues ?? {}) }
|
|
160
183
|
: {}),
|
|
161
184
|
},
|
|
162
|
-
});
|
|
163
|
-
|
|
185
|
+
}, currOtelContext);
|
|
186
|
+
setCurrentOtelContext(args.contextValue, opentelemetry.trace.setSpan(currOtelContext, subscriptionSpan));
|
|
164
187
|
const resultCbs = {
|
|
165
188
|
onSubscribeError: ({ error }) => {
|
|
166
189
|
if (error)
|
|
@@ -171,7 +194,8 @@ export const useOpenTelemetry = (options, tracingProvider, spanKind = SpanKind.S
|
|
|
171
194
|
// handles async iterator
|
|
172
195
|
onNext: ({ result, setResult }) => {
|
|
173
196
|
if (options.traceIdInResult) {
|
|
174
|
-
|
|
197
|
+
const currOtelContext = getCurrentOtelContext(args.contextValue);
|
|
198
|
+
setResult(addTraceIdToResult(currOtelContext, result, options.traceIdInResult));
|
|
175
199
|
}
|
|
176
200
|
markError(subscriptionSpan, result);
|
|
177
201
|
},
|
package/package.json
CHANGED
package/typings/index.d.cts
CHANGED
|
@@ -25,5 +25,8 @@ export type TracingOptions = {
|
|
|
25
25
|
type PluginContext = {
|
|
26
26
|
[tracingSpanSymbol]: opentelemetry.Span;
|
|
27
27
|
};
|
|
28
|
+
export declare const otelContextMap: WeakMap<any, opentelemetry.Context>;
|
|
29
|
+
export declare function getCurrentOtelContext(graphqlContext: any): opentelemetry.Context;
|
|
30
|
+
export declare function setCurrentOtelContext(graphqlContext: any, otelContext: opentelemetry.Context): opentelemetry.Context;
|
|
28
31
|
export declare const useOpenTelemetry: (options: TracingOptions, tracingProvider?: TracerProvider, spanKind?: SpanKind, spanAdditionalAttributes?: SpanAttributes, serviceName?: string, spanPrefix?: string) => Plugin<PluginContext>;
|
|
29
32
|
export {};
|
package/typings/index.d.ts
CHANGED
|
@@ -25,5 +25,8 @@ export type TracingOptions = {
|
|
|
25
25
|
type PluginContext = {
|
|
26
26
|
[tracingSpanSymbol]: opentelemetry.Span;
|
|
27
27
|
};
|
|
28
|
+
export declare const otelContextMap: WeakMap<any, opentelemetry.Context>;
|
|
29
|
+
export declare function getCurrentOtelContext(graphqlContext: any): opentelemetry.Context;
|
|
30
|
+
export declare function setCurrentOtelContext(graphqlContext: any, otelContext: opentelemetry.Context): opentelemetry.Context;
|
|
28
31
|
export declare const useOpenTelemetry: (options: TracingOptions, tracingProvider?: TracerProvider, spanKind?: SpanKind, spanAdditionalAttributes?: SpanAttributes, serviceName?: string, spanPrefix?: string) => Plugin<PluginContext>;
|
|
29
32
|
export {};
|