Skip to content

Commit f1f584c

Browse files
nicohrubecclaude
andauthored
ref(node): Vendor generic-pool instrumentation (#20949)
Vendors `@opentelemetry/instrumentation-generic-pool` into the SDK with no logic changes. Closes #20145 --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6968740 commit f1f584c

5 files changed

Lines changed: 167 additions & 9 deletions

File tree

.oxlintrc.base.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@
140140
"no-bitwise": "off"
141141
}
142142
},
143+
{
144+
"files": ["**/integrations/tracing/genericPool/vendored/**/*.ts"],
145+
"rules": {
146+
"typescript/no-explicit-any": "off"
147+
}
148+
},
143149
{
144150
"files": ["**/scenarios/**", "**/rollup-utils/**"],
145151
"rules": {

packages/node/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
"@opentelemetry/instrumentation-connect": "0.57.0",
7373
"@opentelemetry/instrumentation-dataloader": "0.31.0",
7474
"@opentelemetry/instrumentation-fs": "0.33.0",
75-
"@opentelemetry/instrumentation-generic-pool": "0.57.0",
7675
"@opentelemetry/instrumentation-graphql": "0.62.0",
7776
"@opentelemetry/instrumentation-hapi": "0.60.0",
7877
"@opentelemetry/instrumentation-http": "0.214.0",

packages/node/src/integrations/tracing/genericPool.ts renamed to packages/node/src/integrations/tracing/genericPool/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GenericPoolInstrumentation } from '@opentelemetry/instrumentation-generic-pool';
1+
import { GenericPoolInstrumentation } from './vendored/instrumentation';
22
import type { IntegrationFn } from '@sentry/core';
33
import { defineIntegration, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, spanToJSON } from '@sentry/core';
44
import { generateInstrumentOnce, instrumentWhenWrapped } from '@sentry/node-core';
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* NOTICE from the Sentry authors:
17+
* - Vendored from: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/15ef7506553f631ea4181391e0c5725a56f0d082/packages/instrumentation-generic-pool
18+
* - Upstream version: @opentelemetry/instrumentation-generic-pool@0.61.0
19+
* - Minor TypeScript strictness adjustments for this repository's compiler settings
20+
*/
21+
/* eslint-disable */
22+
23+
import * as api from '@opentelemetry/api';
24+
import {
25+
InstrumentationBase,
26+
InstrumentationConfig,
27+
InstrumentationNodeModuleDefinition,
28+
isWrapped,
29+
} from '@opentelemetry/instrumentation';
30+
31+
import type * as genericPool from 'generic-pool';
32+
33+
import { SDK_VERSION } from '@sentry/core';
34+
35+
const MODULE_NAME = 'generic-pool';
36+
const PACKAGE_NAME = '@sentry/instrumentation-generic-pool';
37+
38+
export class GenericPoolInstrumentation extends InstrumentationBase {
39+
// only used for v2 - v2.3)
40+
private _isDisabled = false;
41+
42+
constructor(config: InstrumentationConfig = {}) {
43+
super(PACKAGE_NAME, SDK_VERSION, config);
44+
}
45+
46+
init() {
47+
return [
48+
new InstrumentationNodeModuleDefinition(
49+
MODULE_NAME,
50+
['>=3.0.0 <4'],
51+
moduleExports => {
52+
const Pool: any = moduleExports.Pool;
53+
if (isWrapped(Pool.prototype.acquire)) {
54+
this._unwrap(Pool.prototype, 'acquire');
55+
}
56+
this._wrap(Pool.prototype, 'acquire', this._acquirePatcher.bind(this));
57+
return moduleExports;
58+
},
59+
moduleExports => {
60+
const Pool: any = moduleExports.Pool;
61+
this._unwrap(Pool.prototype, 'acquire');
62+
return moduleExports;
63+
},
64+
),
65+
new InstrumentationNodeModuleDefinition(
66+
MODULE_NAME,
67+
['>=2.4.0 <3'],
68+
moduleExports => {
69+
const Pool: any = moduleExports.Pool;
70+
if (isWrapped(Pool.prototype.acquire)) {
71+
this._unwrap(Pool.prototype, 'acquire');
72+
}
73+
this._wrap(Pool.prototype, 'acquire', this._acquireWithCallbacksPatcher.bind(this));
74+
return moduleExports;
75+
},
76+
moduleExports => {
77+
const Pool: any = moduleExports.Pool;
78+
this._unwrap(Pool.prototype, 'acquire');
79+
return moduleExports;
80+
},
81+
),
82+
new InstrumentationNodeModuleDefinition(
83+
MODULE_NAME,
84+
['>=2.0.0 <2.4'],
85+
moduleExports => {
86+
this._isDisabled = false;
87+
if (isWrapped(moduleExports.Pool)) {
88+
this._unwrap(moduleExports, 'Pool');
89+
}
90+
this._wrap(moduleExports, 'Pool', this._poolWrapper.bind(this));
91+
return moduleExports;
92+
},
93+
moduleExports => {
94+
// since the object is created on the fly every time, we need to use
95+
// a boolean switch here to disable the instrumentation
96+
this._isDisabled = true;
97+
return moduleExports;
98+
},
99+
),
100+
];
101+
}
102+
103+
private _acquirePatcher(original: genericPool.Pool<unknown>['acquire']) {
104+
const instrumentation = this;
105+
return function wrapped_acquire(this: genericPool.Pool<unknown>, ...args: any[]) {
106+
const parent = api.context.active();
107+
const span = instrumentation.tracer.startSpan('generic-pool.acquire', {}, parent);
108+
109+
return api.context.with(api.trace.setSpan(parent, span), () => {
110+
return original.call(this, ...args).then(
111+
(value: unknown) => {
112+
span.end();
113+
return value;
114+
},
115+
(err: unknown) => {
116+
span.recordException(err as Error);
117+
span.end();
118+
throw err;
119+
},
120+
);
121+
});
122+
};
123+
}
124+
125+
private _poolWrapper(original: any) {
126+
const instrumentation = this;
127+
return function wrapped_pool(this: any) {
128+
const pool = original.apply(this, arguments);
129+
instrumentation._wrap(pool, 'acquire', instrumentation._acquireWithCallbacksPatcher.bind(instrumentation));
130+
return pool;
131+
};
132+
}
133+
134+
private _acquireWithCallbacksPatcher(original: any) {
135+
const instrumentation = this;
136+
return function wrapped_acquire(this: genericPool.Pool<unknown>, cb: Function, priority: number) {
137+
// only used for v2 - v2.3
138+
if (instrumentation._isDisabled) {
139+
return original.call(this, cb, priority);
140+
}
141+
const parent = api.context.active();
142+
const span = instrumentation.tracer.startSpan('generic-pool.acquire', {}, parent);
143+
144+
return api.context.with(api.trace.setSpan(parent, span), () => {
145+
original.call(
146+
this,
147+
(err: unknown, client: unknown) => {
148+
span.end();
149+
// Not checking whether cb is a function because
150+
// the original code doesn't do that either.
151+
if (cb) {
152+
return cb(err, client);
153+
}
154+
},
155+
priority,
156+
);
157+
});
158+
};
159+
}
160+
}

yarn.lock

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6231,13 +6231,6 @@
62316231
"@opentelemetry/core" "^2.0.0"
62326232
"@opentelemetry/instrumentation" "^0.214.0"
62336233

6234-
"@opentelemetry/instrumentation-generic-pool@0.57.0":
6235-
version "0.57.0"
6236-
resolved "https://registry.yarnpkg.com/@opentelemetry/instrumentation-generic-pool/-/instrumentation-generic-pool-0.57.0.tgz#4220a2fc1974b40a989171a9b5f3d1eeab92683f"
6237-
integrity sha512-orhmlaK+ZIW9hKU+nHTbXrCSXZcH83AescTqmpamHRobRmYSQwRbD0a1odc0yAzuzOtxYiHiXAnpnIpaSSY7Ow==
6238-
dependencies:
6239-
"@opentelemetry/instrumentation" "^0.214.0"
6240-
62416234
"@opentelemetry/instrumentation-graphql@0.62.0":
62426235
version "0.62.0"
62436236
resolved "https://registry.yarnpkg.com/@opentelemetry/instrumentation-graphql/-/instrumentation-graphql-0.62.0.tgz#dc2fc92c6be331c4f95b62a40983c8aedb8f9bf9"

0 commit comments

Comments
 (0)