@uphold/opentelemetry-instrumentation-connect-node 0.1.2 → 0.1.4
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/dist/instrumentation.js
CHANGED
|
@@ -34,7 +34,7 @@ class ConnectNodeInstrumentation extends instrumentation_1.InstrumentationBase {
|
|
|
34
34
|
return (original) => {
|
|
35
35
|
this._diag.debug('patched createConnectTransport');
|
|
36
36
|
return (options) => {
|
|
37
|
-
const interceptor = (0, interceptor_1.
|
|
37
|
+
const interceptor = (0, interceptor_1.createClientInterceptor)(this.getConfig(), this._diag, this.tracer);
|
|
38
38
|
return original({
|
|
39
39
|
...options,
|
|
40
40
|
interceptors: [interceptor, ...(options.interceptors ?? [])]
|
|
@@ -46,7 +46,7 @@ class ConnectNodeInstrumentation extends instrumentation_1.InstrumentationBase {
|
|
|
46
46
|
return (original) => {
|
|
47
47
|
this._diag.debug('patched createGrpcTransport');
|
|
48
48
|
return (options) => {
|
|
49
|
-
const interceptor = (0, interceptor_1.
|
|
49
|
+
const interceptor = (0, interceptor_1.createClientInterceptor)(this.getConfig(), this._diag, this.tracer);
|
|
50
50
|
return original({
|
|
51
51
|
...options,
|
|
52
52
|
interceptors: [interceptor, ...(options.interceptors ?? [])]
|
|
@@ -58,7 +58,7 @@ class ConnectNodeInstrumentation extends instrumentation_1.InstrumentationBase {
|
|
|
58
58
|
return (original) => {
|
|
59
59
|
this._diag.debug('patched createGrpcWebTransport');
|
|
60
60
|
return (options) => {
|
|
61
|
-
const interceptor = (0, interceptor_1.
|
|
61
|
+
const interceptor = (0, interceptor_1.createClientInterceptor)(this.getConfig(), this._diag, this.tracer);
|
|
62
62
|
return original({
|
|
63
63
|
...options,
|
|
64
64
|
interceptors: [interceptor, ...(options.interceptors ?? [])]
|
|
@@ -70,7 +70,7 @@ class ConnectNodeInstrumentation extends instrumentation_1.InstrumentationBase {
|
|
|
70
70
|
return (original) => {
|
|
71
71
|
this._diag.debug('patched connectNodeAdapter');
|
|
72
72
|
return (options) => {
|
|
73
|
-
const interceptor = (0, interceptor_1.
|
|
73
|
+
const interceptor = (0, interceptor_1.createServerInterceptor)(this.getConfig(), this._diag, this.tracer);
|
|
74
74
|
return original({
|
|
75
75
|
...options,
|
|
76
76
|
interceptors: [interceptor, ...(options.interceptors ?? [])]
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"instrumentation.js","sourceRoot":"","sources":["../src/instrumentation.ts"],"names":[],"mappings":";;;;;;AAWA,oEAA0G;AAC1G,+
|
|
1
|
+
{"version":3,"file":"instrumentation.js","sourceRoot":"","sources":["../src/instrumentation.ts"],"names":[],"mappings":";;;;;;AAWA,oEAA0G;AAC1G,+CAAiF;AACjF,mEAA0C;AAE1C,MAAa,0BAA2B,SAAQ,qCAAqD;IACnG,YAAY,SAA2C,EAAE;QACvD,KAAK,CAAC,oDAAoD,EAAE,sBAAW,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAC3F,CAAC;IAED,IAAI;QACF,OAAO;YACL,IAAI,qDAAmC,CACrC,0BAA0B,EAC1B,CAAC,QAAQ,CAAC,EACV,aAAa,CAAC,EAAE;gBACd,IAAI,CAAC,KAAK,CAAC,aAAa,EAAE,wBAAwB,EAAE,IAAI,CAAC,4BAA4B,EAAE,CAAC,CAAC;gBACzF,IAAI,CAAC,KAAK,CAAC,aAAa,EAAE,qBAAqB,EAAE,IAAI,CAAC,yBAAyB,EAAE,CAAC,CAAC;gBACnF,IAAI,CAAC,KAAK,CAAC,aAAa,EAAE,wBAAwB,EAAE,IAAI,CAAC,4BAA4B,EAAE,CAAC,CAAC;gBACzF,IAAI,CAAC,KAAK,CAAC,aAAa,EAAE,oBAAoB,EAAE,IAAI,CAAC,wBAAwB,EAAE,CAAC,CAAC;gBAEjF,OAAO,aAAa,CAAC;YACvB,CAAC,EACD,aAAa,CAAC,EAAE;gBACd,IAAI,aAAa,KAAK,SAAS,EAAE,CAAC;oBAChC,OAAO;gBACT,CA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AAC,CAAC;QACJ,CAAC,CAAC;IACJ,CAAC;IAEO,4BAA4B;QAClC,OAAO,CAAC,QAAuC,EAAE,EAAE;YACjD,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,gCAAgC,CAAC,CAAC;YAEnD,OAAO,CAAC,OAAgC,EAAE,EAAE;gBAC1C,MAAM,WAAW,GAAG,IAAA,qCAAuB,EAAC,IAAI,CAAC,SAAS,EAAE,EAAE,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;gBAEvF,OAAO,QAAQ,CAAC;oBACd,GAAG,OAAO;oBACV,YAAY,EAAE,CAAC,WAAW,EAAE,GAAG,CAAC,OAAO,CAAC,YAAY,IAAI,EAAE,CAAC,CAAC;iBAC7D,CAAC,CAAC;YACL,CAAC,CAAC;QACJ,CAAC,CAAC;IACJ,CAAC;IAEO,wBAAwB;QAC9B,OAAO,CAAC,QAAmC,EAAE,EAAE;YAC7C,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAC;YAE/C,OAAO,CAAC,OAAkC,EAAE,EAAE;gBAC5C,MAAM,WAAW,GAAG,IAAA,qCAAuB,EAAC,IAAI,CAAC,SAAS,EAAE,EAAE,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;gBAEvF,OAAO,QAAQ,CAAC;oBACd,GAAG,OAAO;oBACV,YAAY,EAAE,CAAC,WAAW,EAAE,GAAG,CAAC,OAAO,CAAC,YAAY,IAAI,EAAE,CAAC,CAAC;iBAC7D,CAAC,CAAC;YACL,CAAC,CAAC;QACJ,CAAC,CAAC;IACJ,CAAC;CACF;AA3FD,gEA2FC","sourcesContent":["import type {\n ConnectNodeAdapterOptions,\n ConnectTransportOptions,\n GrpcTransportOptions,\n GrpcWebTransportOptions,\n connectNodeAdapter,\n createConnectTransport,\n createGrpcTransport,\n createGrpcWebTransport\n} from '@connectrpc/connect-node';\nimport { ConnectNodeInstrumentationConfig } from './types';\nimport { InstrumentationBase, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation';\nimport { createClientInterceptor, createServerInterceptor } from './interceptor';\nimport packageJson from '../package.json';\n\nexport class ConnectNodeInstrumentation extends InstrumentationBase<ConnectNodeInstrumentationConfig> {\n constructor(config: ConnectNodeInstrumentationConfig = {}) {\n super('@uphold/opentelemetry-instrumentation-connect-node', packageJson.version, config);\n }\n\n init() {\n return [\n new InstrumentationNodeModuleDefinition(\n '@connectrpc/connect-node',\n ['^2.0.0'],\n moduleExports => {\n this._wrap(moduleExports, 'createConnectTransport', this._patchCreateConnectTransport());\n this._wrap(moduleExports, 'createGrpcTransport', this._patchCreateGrpcTransport());\n this._wrap(moduleExports, 'createGrpcWebTransport', this._patchCreateGrpcWebTransport());\n this._wrap(moduleExports, 'connectNodeAdapter', this._patchConnectNodeAdapter());\n\n return moduleExports;\n },\n moduleExports => {\n if (moduleExports === undefined) {\n return;\n }\n\n this._unwrap(moduleExports, 'createConnectTransport');\n this._unwrap(moduleExports, 'createGrpcTransport');\n this._unwrap(moduleExports, 'createGrpcWebTransport');\n this._unwrap(moduleExports, 'connectNodeAdapter');\n }\n )\n ];\n }\n\n private _patchCreateConnectTransport() {\n return (original: typeof createConnectTransport) => {\n this._diag.debug('patched createConnectTransport');\n\n return (options: ConnectTransportOptions) => {\n const interceptor = createClientInterceptor(this.getConfig(), this._diag, this.tracer);\n\n return original({\n ...options,\n interceptors: [interceptor, ...(options.interceptors ?? [])]\n });\n };\n };\n }\n\n private _patchCreateGrpcTransport() {\n return (original: typeof createGrpcTransport) => {\n this._diag.debug('patched createGrpcTransport');\n\n return (options: GrpcTransportOptions) => {\n const interceptor = createClientInterceptor(this.getConfig(), this._diag, this.tracer);\n\n return original({\n ...options,\n interceptors: [interceptor, ...(options.interceptors ?? [])]\n });\n };\n };\n }\n\n private _patchCreateGrpcWebTransport() {\n return (original: typeof createGrpcWebTransport) => {\n this._diag.debug('patched createGrpcWebTransport');\n\n return (options: GrpcWebTransportOptions) => {\n const interceptor = createClientInterceptor(this.getConfig(), this._diag, this.tracer);\n\n return original({\n ...options,\n interceptors: [interceptor, ...(options.interceptors ?? [])]\n });\n };\n };\n }\n\n private _patchConnectNodeAdapter() {\n return (original: typeof connectNodeAdapter) => {\n this._diag.debug('patched connectNodeAdapter');\n\n return (options: ConnectNodeAdapterOptions) => {\n const interceptor = createServerInterceptor(this.getConfig(), this._diag, this.tracer);\n\n return original({\n ...options,\n interceptors: [interceptor, ...(options.interceptors ?? [])]\n });\n };\n };\n }\n}\n"]}
|
package/dist/interceptor.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ConnectNodeInstrumentationConfig } from './types';
|
|
2
2
|
import { DiagLogger, Tracer } from '@opentelemetry/api';
|
|
3
|
-
import { InterceptorAnyFn
|
|
4
|
-
export declare const
|
|
3
|
+
import { InterceptorAnyFn } from './internal-types';
|
|
4
|
+
export declare const createClientInterceptor: (config: ConnectNodeInstrumentationConfig, diag: DiagLogger, tracer: Tracer) => (next: InterceptorAnyFn) => InterceptorAnyFn;
|
|
5
|
+
export declare const createServerInterceptor: (config: ConnectNodeInstrumentationConfig, diag: DiagLogger, tracer: Tracer) => (next: InterceptorAnyFn) => InterceptorAnyFn;
|
|
5
6
|
//# sourceMappingURL=interceptor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interceptor.d.ts","sourceRoot":"","sources":["../src/interceptor.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,gCAAgC,EAAE,MAAM,SAAS,CAAC;AAC3D,OAAO,EAAE,UAAU,EAAkB,MAAM,EAA+B,MAAM,oBAAoB,CAAC;AACrG,OAAO,EAAE,gBAAgB,
|
|
1
|
+
{"version":3,"file":"interceptor.d.ts","sourceRoot":"","sources":["../src/interceptor.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,gCAAgC,EAAE,MAAM,SAAS,CAAC;AAC3D,OAAO,EAAE,UAAU,EAAkB,MAAM,EAA+B,MAAM,oBAAoB,CAAC;AACrG,OAAO,EAAE,gBAAgB,EAAqB,MAAM,kBAAkB,CAAC;AA6HvE,eAAO,MAAM,uBAAuB,GAAI,QAAQ,gCAAgC,EAAE,MAAM,UAAU,EAAE,QAAQ,MAAM,MAKxG,MAAM,gBAAgB,KAAG,gBAwClC,CAAC;AAEF,eAAO,MAAM,uBAAuB,GAAI,QAAQ,gCAAgC,EAAE,MAAM,UAAU,EAAE,QAAQ,MAAM,MAKxG,MAAM,gBAAgB,KAAG,gBAwClC,CAAC"}
|
package/dist/interceptor.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.createServerInterceptor = exports.createClientInterceptor = void 0;
|
|
4
4
|
const incubating_1 = require("@opentelemetry/semantic-conventions/incubating");
|
|
5
5
|
const api_1 = require("@opentelemetry/api");
|
|
6
6
|
const utils_1 = require("./utils");
|
|
@@ -94,10 +94,10 @@ const carrierSetterAndGetter = {
|
|
|
94
94
|
keys: (carrier) => Array.from(carrier.keys()),
|
|
95
95
|
set: (carrier, key, value) => carrier.set(key, value)
|
|
96
96
|
};
|
|
97
|
-
const
|
|
98
|
-
const startSpan = createStartSpan(config, tracer,
|
|
99
|
-
const endSpanWithSuccess = createEndSpanWithSuccess(config,
|
|
100
|
-
const endSpanWithError = createEndSpanWithError(config,
|
|
97
|
+
const createClientInterceptor = (config, diag, tracer) => {
|
|
98
|
+
const startSpan = createStartSpan(config, tracer, 'client');
|
|
99
|
+
const endSpanWithSuccess = createEndSpanWithSuccess(config, 'client');
|
|
100
|
+
const endSpanWithError = createEndSpanWithError(config, 'client');
|
|
101
101
|
return (next) => async (req) => {
|
|
102
102
|
// Only unary requests are supported due to a bug in Node.js async context propagation on generator functions.
|
|
103
103
|
// See https://github.com/open-telemetry/opentelemetry-js/issues/2951 and https://github.com/nodejs/node/issues/42237
|
|
@@ -112,17 +112,10 @@ const createInterceptor = (config, diag, tracer, kind) => {
|
|
|
112
112
|
if (shouldIgnoreRequest) {
|
|
113
113
|
return await next(req);
|
|
114
114
|
}
|
|
115
|
-
let ctx = api_1.context.active();
|
|
116
115
|
const span = startSpan(req);
|
|
117
|
-
if (kind === 'server') {
|
|
118
|
-
ctx = api_1.propagation.extract(ctx, req.header, carrierSetterAndGetter);
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
api_1.propagation.inject(ctx, req.header, carrierSetterAndGetter);
|
|
122
|
-
}
|
|
123
116
|
try {
|
|
124
|
-
return await api_1.context.with(api_1.trace.setSpan(
|
|
125
|
-
|
|
117
|
+
return await api_1.context.with(api_1.trace.setSpan(api_1.context.active(), span), async () => {
|
|
118
|
+
api_1.propagation.inject(api_1.context.active(), req.header, carrierSetterAndGetter);
|
|
126
119
|
const res = await next(req);
|
|
127
120
|
endSpanWithSuccess(span, res);
|
|
128
121
|
return res;
|
|
@@ -134,5 +127,40 @@ const createInterceptor = (config, diag, tracer, kind) => {
|
|
|
134
127
|
}
|
|
135
128
|
};
|
|
136
129
|
};
|
|
137
|
-
exports.
|
|
130
|
+
exports.createClientInterceptor = createClientInterceptor;
|
|
131
|
+
const createServerInterceptor = (config, diag, tracer) => {
|
|
132
|
+
const startSpan = createStartSpan(config, tracer, 'server');
|
|
133
|
+
const endSpanWithSuccess = createEndSpanWithSuccess(config, 'server');
|
|
134
|
+
const endSpanWithError = createEndSpanWithError(config, 'server');
|
|
135
|
+
return (next) => async (req) => {
|
|
136
|
+
// Only unary requests are supported due to a bug in Node.js async context propagation on generator functions.
|
|
137
|
+
// See https://github.com/open-telemetry/opentelemetry-js/issues/2951 and https://github.com/nodejs/node/issues/42237
|
|
138
|
+
if (req.method.methodKind !== 'unary') {
|
|
139
|
+
return await next(req);
|
|
140
|
+
}
|
|
141
|
+
const shouldIgnoreRequest = (0, instrumentation_1.safeExecuteInTheMiddle)(() => config.ignoreRequest?.(req) === true, (err) => {
|
|
142
|
+
if (err != null) {
|
|
143
|
+
diag.error('caught ignoreRequest error: ', err);
|
|
144
|
+
}
|
|
145
|
+
}, true);
|
|
146
|
+
if (shouldIgnoreRequest) {
|
|
147
|
+
return await next(req);
|
|
148
|
+
}
|
|
149
|
+
return await api_1.context.with(api_1.propagation.extract(api_1.context.active(), req.header, carrierSetterAndGetter), async () => {
|
|
150
|
+
const span = startSpan(req);
|
|
151
|
+
try {
|
|
152
|
+
return await api_1.context.with(api_1.trace.setSpan(api_1.context.active(), span), async () => {
|
|
153
|
+
const res = await next(req);
|
|
154
|
+
endSpanWithSuccess(span, res);
|
|
155
|
+
return res;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
catch (err) {
|
|
159
|
+
endSpanWithError(span, err);
|
|
160
|
+
throw err;
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
exports.createServerInterceptor = createServerInterceptor;
|
|
138
166
|
//# sourceMappingURL=interceptor.js.map
|
package/dist/interceptor.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interceptor.js","sourceRoot":"","sources":["../src/interceptor.ts"],"names":[],"mappings":";;;AAAA,+EAQwD;AAGxD,4CAAqG;AAGrG,mCAAiG;AACjG,mCAAiC;AACjC,oEAAwE;AAExE,MAAM,iCAAiC,GAAG,CACxC,MAAwC,EACxC,IAAa,EACb,KAAe,EACf,EAAE;IACF,MAAM,wBAAwB,GAAG,MAAM,CAAC,wBAAwB,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;IACxF,MAAM,QAAQ,GAAG,IAAI,GAAG,CACtB,wBAAwB,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,WAAW,EAAE,EAAE,KAAK,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC,CACrG,CAAC;IAE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C_SYSTEM,\n ATTR_SERVER_ADDRESS,\n ATTR_SERVER_PORT\n} from '@opentelemetry/semantic-conventions/incubating';\nimport { ConnectError, StreamRequest, StreamResponse, UnaryRequest, UnaryResponse } from '@connectrpc/connect';\nimport { ConnectNodeInstrumentationConfig } from './types';\nimport { DiagLogger, SpanStatusCode, Tracer, context, propagation, trace } from '@opentelemetry/api';\nimport { InterceptorAnyFn, RpcKind, RpcPhase } from './internal-types';\nimport { Span } from '@opentelemetry/sdk-trace-base';\nimport { errorCodeToString, isConnectError, resolveRpcSystem, rpcKindToSpanKind } from './utils';\nimport { memoize } from 'lodash';\nimport { safeExecuteInTheMiddle } from '@opentelemetry/instrumentation';\n\nconst createMetadataAttributesExtractor = (\n config: ConnectNodeInstrumentationConfig,\n kind: RpcKind,\n phase: RpcPhase\n) => {\n const metadataToSpanAttributes = config.metadataToSpanAttributes?.[kind]?.[phase] ?? [];\n const mappings = new Map(\n metadataToSpanAttributes.map(value => [value.toLowerCase(), value.toLowerCase().replace(/-/g, '_')])\n );\n\n if (mappings.size === 0) {\n return () => ({});\n }\n\n // See: https://opentelemetry.io/docs/specs/semconv/rpc/\n return (span: Span, metadata?: Headers) => {\n const rpcSystem = span.attributes[ATTR_RPC_SYSTEM];\n const attributes: Record<string, string> = {};\n\n if (rpcSystem && metadata) {\n for (const [key, mappedKey] of mappings) {\n const value = metadata.get(key);\n\n if (value != null) {\n attributes[`grpc.${rpcSystem}.${phase}.metadata.${mappedKey}`] = value;\n }\n }\n }\n\n return attributes;\n };\n};\n\nconst createStartSpan = (config: ConnectNodeInstrumentationConfig, tracer: Tracer, kind: RpcKind) => {\n const extractMetadata = createMetadataAttributesExtractor(config, kind, 'request');\n const parseUrl = memoize((url: string) => new URL(url));\n\n // See: https://opentelemetry.io/docs/specs/semconv/rpc/\n return (req: UnaryRequest | StreamRequest) => {\n const fullName = `${req.service.typeName}/${req.method.name}`;\n const url = parseUrl(req.url);\n const rpcSystem = resolveRpcSystem(req.header);\n\n const span = tracer.startSpan(fullName, {\n attributes: {\n [ATTR_RPC_METHOD]: req.method.name,\n [ATTR_RPC_SERVICE]: req.service.typeName,\n [ATTR_RPC_SYSTEM]: rpcSystem,\n [ATTR_SERVER_ADDRESS]: url.hostname,\n [ATTR_SERVER_PORT]: url.port || undefined\n },\n kind: rpcKindToSpanKind(kind)\n });\n\n span.setAttributes(extractMetadata(span as Span, req.header));\n\n return span;\n };\n};\n\nconst createEndSpanWithSuccess = (config: ConnectNodeInstrumentationConfig, kind: RpcKind) => {\n const extractMetadata = createMetadataAttributesExtractor(config, kind, 'response');\n\n // See: https://opentelemetry.io/docs/specs/semconv/rpc/\n return (span: Span, res: UnaryResponse | StreamResponse) => {\n if (span.ended) {\n return;\n }\n\n const rpcSystem = span.attributes[ATTR_RPC_SYSTEM];\n\n span.setStatus({ code: SpanStatusCode.OK });\n\n if (rpcSystem === 'grpc') {\n span.setAttribute(ATTR_RPC_GRPC_STATUS_CODE, 0);\n }\n\n span.setAttributes(extractMetadata(span as Span, res.header));\n span.end();\n };\n};\n\nconst createEndSpanWithError = (config: ConnectNodeInstrumentationConfig, kind: RpcKind) => {\n const extractMetadata = createMetadataAttributesExtractor(config, kind, 'response');\n\n // See: https://opentelemetry.io/docs/specs/semconv/rpc/\n return (span: Span, err: unknown) => {\n if (span.ended) {\n return;\n }\n\n const error = err instanceof Error ? (err as Error) : undefined;\n const connectError = isConnectError(error) ? (error as ConnectError) : undefined;\n const rpcSystem = span.attributes[ATTR_RPC_SYSTEM];\n\n span.setStatus({ code: SpanStatusCode.ERROR, message: error?.message });\n\n if (rpcSystem === 'grpc') {\n span.setAttribute(ATTR_RPC_GRPC_STATUS_CODE, connectError?.code ?? 2);\n } else if (rpcSystem === 'connect_rpc') {\n span.setAttribute(ATTR_RPC_CONNECT_RPC_ERROR_CODE, errorCodeToString(connectError?.code));\n }\n\n span.setAttributes(extractMetadata(span as Span, connectError?.metadata));\n\n if (error) {\n span.recordException(error);\n }\n\n span.end();\n };\n};\n\nconst carrierSetterAndGetter = {\n get: (carrier: Headers, key: string) => carrier.get(key) ?? undefined,\n keys: (carrier: Headers) => Array.from(carrier.keys()),\n set: (carrier: Headers, key: string, value: string) => carrier.set(key, value)\n};\n\nexport const createInterceptor = (\n config: ConnectNodeInstrumentationConfig,\n diag: DiagLogger,\n tracer: Tracer,\n kind: RpcKind\n) => {\n const startSpan = createStartSpan(config, tracer, kind);\n const endSpanWithSuccess = createEndSpanWithSuccess(config, kind);\n const endSpanWithError = createEndSpanWithError(config, kind);\n\n return (next: InterceptorAnyFn): InterceptorAnyFn =>\n async req => {\n // Only unary requests are supported due to a bug in Node.js async context propagation on generator functions.\n // See https://github.com/open-telemetry/opentelemetry-js/issues/2951 and https://github.com/nodejs/node/issues/42237\n if (req.method.methodKind !== 'unary') {\n return await next(req);\n }\n\n const shouldIgnoreRequest = safeExecuteInTheMiddle(\n () => config.ignoreRequest?.(req) === true,\n (err: unknown) => {\n if (err != null) {\n diag.error('caught ignoreRequest error: ', err);\n }\n },\n true\n );\n\n if (shouldIgnoreRequest) {\n return await next(req);\n }\n\n let ctx = context.active();\n const span = startSpan(req);\n\n if (kind === 'server') {\n ctx = propagation.extract(ctx, req.header, carrierSetterAndGetter);\n } else {\n propagation.inject(ctx, req.header, carrierSetterAndGetter);\n }\n\n try {\n return await context.with(trace.setSpan(ctx, span), async () => {\n next = context.bind(ctx, next);\n\n const res = await next(req);\n\n endSpanWithSuccess(span as Span, res);\n\n return res;\n });\n } catch (err) {\n endSpanWithError(span as Span, err);\n\n throw err;\n }\n };\n};\n"]}
|
|
1
|
+
{"version":3,"file":"interceptor.js","sourceRoot":"","sources":["../src/interceptor.ts"],"names":[],"mappings":";;;AAAA,+EAQwD;AAGxD,4CAAqG;AAGrG,mCAAiG;AACjG,mCAAiC;AACjC,oEAAwE;AAExE,MAAM,iCAAiC,GAAG,CACxC,MAAwC,EACxC,IAAa,EACb,KAAe,EACf,EAAE;IACF,MAAM,wBAAwB,GAAG,MAAM,CAAC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G,CAAC,CAAC;oBAE5B,kBAAkB,CAAC,IAAY,EAAE,GAAG,CAAC,CAAC;oBAEtC,OAAO,GAAG,CAAC;gBACb,CAAC,CAAC,CAAC;YACL,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,gBAAgB,CAAC,IAAY,EAAE,GAAG,CAAC,CAAC;gBAEpC,MAAM,GAAG,CAAC;YACZ,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACN,CAAC,CAAC;AA7CW,QAAA,uBAAuB,2BA6ClC","sourcesContent":["import {\n ATTR_RPC_CONNECT_RPC_ERROR_CODE,\n ATTR_RPC_GRPC_STATUS_CODE,\n ATTR_RPC_METHOD,\n ATTR_RPC_SERVICE,\n ATTR_RPC_SYSTEM,\n ATTR_SERVER_ADDRESS,\n ATTR_SERVER_PORT\n} from '@opentelemetry/semantic-conventions/incubating';\nimport { ConnectError, StreamRequest, StreamResponse, UnaryRequest, UnaryResponse } from '@connectrpc/connect';\nimport { ConnectNodeInstrumentationConfig } from './types';\nimport { DiagLogger, SpanStatusCode, Tracer, context, propagation, trace } from '@opentelemetry/api';\nimport { InterceptorAnyFn, RpcKind, RpcPhase } from './internal-types';\nimport { Span } from '@opentelemetry/sdk-trace-base';\nimport { errorCodeToString, isConnectError, resolveRpcSystem, rpcKindToSpanKind } from './utils';\nimport { memoize } from 'lodash';\nimport { safeExecuteInTheMiddle } from '@opentelemetry/instrumentation';\n\nconst createMetadataAttributesExtractor = (\n config: ConnectNodeInstrumentationConfig,\n kind: RpcKind,\n phase: RpcPhase\n) => {\n const metadataToSpanAttributes = config.metadataToSpanAttributes?.[kind]?.[phase] ?? [];\n const mappings = new Map(\n metadataToSpanAttributes.map(value => [value.toLowerCase(), value.toLowerCase().replace(/-/g, '_')])\n );\n\n if (mappings.size === 0) {\n return () => ({});\n }\n\n // See: https://opentelemetry.io/docs/specs/semconv/rpc/\n return (span: Span, metadata?: Headers) => {\n const rpcSystem = span.attributes[ATTR_RPC_SYSTEM];\n const attributes: Record<string, string> = {};\n\n if (rpcSystem && metadata) {\n for (const [key, mappedKey] of mappings) {\n const value = metadata.get(key);\n\n if (value != null) {\n attributes[`grpc.${rpcSystem}.${phase}.metadata.${mappedKey}`] = value;\n }\n }\n }\n\n return attributes;\n };\n};\n\nconst createStartSpan = (config: ConnectNodeInstrumentationConfig, tracer: Tracer, kind: RpcKind) => {\n const extractMetadata = createMetadataAttributesExtractor(config, kind, 'request');\n const parseUrl = memoize((url: string) => new URL(url));\n\n // See: https://opentelemetry.io/docs/specs/semconv/rpc/\n return (req: UnaryRequest | StreamRequest) => {\n const fullName = `${req.service.typeName}/${req.method.name}`;\n const url = parseUrl(req.url);\n const rpcSystem = resolveRpcSystem(req.header);\n\n const span = tracer.startSpan(fullName, {\n attributes: {\n [ATTR_RPC_METHOD]: req.method.name,\n [ATTR_RPC_SERVICE]: req.service.typeName,\n [ATTR_RPC_SYSTEM]: rpcSystem,\n [ATTR_SERVER_ADDRESS]: url.hostname,\n [ATTR_SERVER_PORT]: url.port || undefined\n },\n kind: rpcKindToSpanKind(kind)\n });\n\n span.setAttributes(extractMetadata(span as Span, req.header));\n\n return span;\n };\n};\n\nconst createEndSpanWithSuccess = (config: ConnectNodeInstrumentationConfig, kind: RpcKind) => {\n const extractMetadata = createMetadataAttributesExtractor(config, kind, 'response');\n\n // See: https://opentelemetry.io/docs/specs/semconv/rpc/\n return (span: Span, res: UnaryResponse | StreamResponse) => {\n if (span.ended) {\n return;\n }\n\n const rpcSystem = span.attributes[ATTR_RPC_SYSTEM];\n\n span.setStatus({ code: SpanStatusCode.OK });\n\n if (rpcSystem === 'grpc') {\n span.setAttribute(ATTR_RPC_GRPC_STATUS_CODE, 0);\n }\n\n span.setAttributes(extractMetadata(span as Span, res.header));\n span.end();\n };\n};\n\nconst createEndSpanWithError = (config: ConnectNodeInstrumentationConfig, kind: RpcKind) => {\n const extractMetadata = createMetadataAttributesExtractor(config, kind, 'response');\n\n // See: https://opentelemetry.io/docs/specs/semconv/rpc/\n return (span: Span, err: unknown) => {\n if (span.ended) {\n return;\n }\n\n const error = err instanceof Error ? (err as Error) : undefined;\n const connectError = isConnectError(error) ? (error as ConnectError) : undefined;\n const rpcSystem = span.attributes[ATTR_RPC_SYSTEM];\n\n span.setStatus({ code: SpanStatusCode.ERROR, message: error?.message });\n\n if (rpcSystem === 'grpc') {\n span.setAttribute(ATTR_RPC_GRPC_STATUS_CODE, connectError?.code ?? 2);\n } else if (rpcSystem === 'connect_rpc') {\n span.setAttribute(ATTR_RPC_CONNECT_RPC_ERROR_CODE, errorCodeToString(connectError?.code));\n }\n\n span.setAttributes(extractMetadata(span as Span, connectError?.metadata));\n\n if (error) {\n span.recordException(error);\n }\n\n span.end();\n };\n};\n\nconst carrierSetterAndGetter = {\n get: (carrier: Headers, key: string) => carrier.get(key) ?? undefined,\n keys: (carrier: Headers) => Array.from(carrier.keys()),\n set: (carrier: Headers, key: string, value: string) => carrier.set(key, value)\n};\n\nexport const createClientInterceptor = (config: ConnectNodeInstrumentationConfig, diag: DiagLogger, tracer: Tracer) => {\n const startSpan = createStartSpan(config, tracer, 'client');\n const endSpanWithSuccess = createEndSpanWithSuccess(config, 'client');\n const endSpanWithError = createEndSpanWithError(config, 'client');\n\n return (next: InterceptorAnyFn): InterceptorAnyFn =>\n async req => {\n // Only unary requests are supported due to a bug in Node.js async context propagation on generator functions.\n // See https://github.com/open-telemetry/opentelemetry-js/issues/2951 and https://github.com/nodejs/node/issues/42237\n if (req.method.methodKind !== 'unary') {\n return await next(req);\n }\n\n const shouldIgnoreRequest = safeExecuteInTheMiddle(\n () => config.ignoreRequest?.(req) === true,\n (err: unknown) => {\n if (err != null) {\n diag.error('caught ignoreRequest error: ', err);\n }\n },\n true\n );\n\n if (shouldIgnoreRequest) {\n return await next(req);\n }\n\n const span = startSpan(req);\n\n try {\n return await context.with(trace.setSpan(context.active(), span), async () => {\n propagation.inject(context.active(), req.header, carrierSetterAndGetter);\n\n const res = await next(req);\n\n endSpanWithSuccess(span as Span, res);\n\n return res;\n });\n } catch (err) {\n endSpanWithError(span as Span, err);\n\n throw err;\n }\n };\n};\n\nexport const createServerInterceptor = (config: ConnectNodeInstrumentationConfig, diag: DiagLogger, tracer: Tracer) => {\n const startSpan = createStartSpan(config, tracer, 'server');\n const endSpanWithSuccess = createEndSpanWithSuccess(config, 'server');\n const endSpanWithError = createEndSpanWithError(config, 'server');\n\n return (next: InterceptorAnyFn): InterceptorAnyFn =>\n async req => {\n // Only unary requests are supported due to a bug in Node.js async context propagation on generator functions.\n // See https://github.com/open-telemetry/opentelemetry-js/issues/2951 and https://github.com/nodejs/node/issues/42237\n if (req.method.methodKind !== 'unary') {\n return await next(req);\n }\n\n const shouldIgnoreRequest = safeExecuteInTheMiddle(\n () => config.ignoreRequest?.(req) === true,\n (err: unknown) => {\n if (err != null) {\n diag.error('caught ignoreRequest error: ', err);\n }\n },\n true\n );\n\n if (shouldIgnoreRequest) {\n return await next(req);\n }\n\n return await context.with(propagation.extract(context.active(), req.header, carrierSetterAndGetter), async () => {\n const span = startSpan(req);\n\n try {\n return await context.with(trace.setSpan(context.active(), span), async () => {\n const res = await next(req);\n\n endSpanWithSuccess(span as Span, res);\n\n return res;\n });\n } catch (err) {\n endSpanWithError(span as Span, err);\n\n throw err;\n }\n });\n };\n};\n"]}
|
package/package.json
CHANGED