Skip to content

Commit 4a37c4c

Browse files
nicohrubecclaude
andcommitted
ref(node): Vendor lru-memoizer instrumentation
Closes #20156 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 46385f0 commit 4a37c4c

5 files changed

Lines changed: 78 additions & 9 deletions

File tree

.oxlintrc.base.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@
140140
"no-bitwise": "off"
141141
}
142142
},
143+
{
144+
"files": ["**/integrations/tracing/lrumemoizer/vendored/**/*.ts"],
145+
"rules": {
146+
"typescript/no-explicit-any": "off",
147+
"max-lines": "off"
148+
}
149+
},
143150
{
144151
"files": ["**/scenarios/**", "**/rollup-utils/**"],
145152
"rules": {

packages/node/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
"@opentelemetry/instrumentation-kafkajs": "0.23.0",
8080
"@opentelemetry/instrumentation-knex": "0.58.0",
8181
"@opentelemetry/instrumentation-koa": "0.62.0",
82-
"@opentelemetry/instrumentation-lru-memoizer": "0.58.0",
8382
"@opentelemetry/instrumentation-mongodb": "0.67.0",
8483
"@opentelemetry/instrumentation-mongoose": "0.60.0",
8584
"@opentelemetry/instrumentation-mysql": "0.60.0",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LruMemoizerInstrumentation } from '@opentelemetry/instrumentation-lru-memoizer';
1+
import { LruMemoizerInstrumentation } from './vendored/instrumentation';
22
import type { IntegrationFn } from '@sentry/core';
33
import { defineIntegration } from '@sentry/core';
44
import { generateInstrumentOnce } from '@sentry/node-core';
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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/ed97091c9890dd18e52759f2ea98e9d7593b3ae4/packages/instrumentation-lru-memoizer
18+
* - Upstream version: @opentelemetry/instrumentation-lru-memoizer@0.58.0
19+
*/
20+
/* eslint-disable */
21+
22+
import { context } from '@opentelemetry/api';
23+
import {
24+
InstrumentationBase,
25+
InstrumentationConfig,
26+
InstrumentationNodeModuleDefinition,
27+
} from '@opentelemetry/instrumentation';
28+
import { SDK_VERSION } from '@sentry/core';
29+
30+
const PACKAGE_NAME = '@sentry/instrumentation-lru-memoizer';
31+
32+
export class LruMemoizerInstrumentation extends InstrumentationBase {
33+
constructor(config: InstrumentationConfig = {}) {
34+
super(PACKAGE_NAME, SDK_VERSION, config);
35+
}
36+
37+
init(): InstrumentationNodeModuleDefinition[] {
38+
return [
39+
new InstrumentationNodeModuleDefinition(
40+
'lru-memoizer',
41+
['>=1.3 <4'],
42+
moduleExports => {
43+
// moduleExports is a function which receives an options object,
44+
// and returns a "memoizer" function upon invocation.
45+
// We want to patch this "memoizer's" internal function
46+
const asyncMemoizer = function (this: unknown) {
47+
// This following function is invoked every time the user wants to get a (possible) memoized value
48+
// We replace it with another function in which we bind the current context to the last argument (callback)
49+
const origMemoizer = moduleExports.apply(this, arguments);
50+
return function (this: unknown) {
51+
const modifiedArguments = [...arguments];
52+
// last argument is the callback
53+
const origCallback = modifiedArguments.pop();
54+
const callbackWithContext =
55+
typeof origCallback === 'function' ? context.bind(context.active(), origCallback) : origCallback;
56+
modifiedArguments.push(callbackWithContext);
57+
return origMemoizer.apply(this, modifiedArguments);
58+
};
59+
};
60+
61+
// sync function preserves context, but we still need to export it
62+
// as the lru-memoizer package does
63+
asyncMemoizer.sync = moduleExports.sync;
64+
return asyncMemoizer;
65+
},
66+
undefined, // no need to disable as this instrumentation does not create any spans
67+
),
68+
];
69+
}
70+
}

yarn.lock

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6289,13 +6289,6 @@
62896289
"@opentelemetry/instrumentation" "^0.214.0"
62906290
"@opentelemetry/semantic-conventions" "^1.36.0"
62916291

6292-
"@opentelemetry/instrumentation-lru-memoizer@0.58.0":
6293-
version "0.58.0"
6294-
resolved "https://registry.yarnpkg.com/@opentelemetry/instrumentation-lru-memoizer/-/instrumentation-lru-memoizer-0.58.0.tgz#7c730a0cb963e8ac5f3d11023518050e5f124a6a"
6295-
integrity sha512-6grM3TdMyHzlGY1cUA+mwoPueB1F3dYKgKtZIH6jOFXqfHAByyLTc+6PFjGM9tKh52CFBJaDwodNlL/Td39z7Q==
6296-
dependencies:
6297-
"@opentelemetry/instrumentation" "^0.214.0"
6298-
62996292
"@opentelemetry/instrumentation-mongodb@0.67.0":
63006293
version "0.67.0"
63016294
resolved "https://registry.yarnpkg.com/@opentelemetry/instrumentation-mongodb/-/instrumentation-mongodb-0.67.0.tgz#ac45611586e363e2d96c735d50f97556dd33c37e"

0 commit comments

Comments
 (0)