-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node): Add
lru-memoizer
instrumentation (#13796)
Resolves: #13309 Adds integration for `lru-memoizer` using [@opentelemetry/instrumentation-lru-memoizer](https://www.npmjs.com/package/@opentelemetry/instrumentation-lru-memoizer). This instrumentation does not create any spans. It only assigns the span context into memoized callbacks used in `lru-memoizer`'s `load`. Ported a test case from the original implementation and tested manually to validate.
- Loading branch information
1 parent
5e2bc47
commit 2343380
Showing
12 changed files
with
133 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
dev-packages/node-integration-tests/suites/tracing/lru-memoizer/scenario.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
release: '1.0', | ||
tracesSampleRate: 1.0, | ||
transport: loggingTransport, | ||
}); | ||
|
||
// Stop the process from exiting before the transaction is sent | ||
setInterval(() => {}, 1000); | ||
|
||
const run = async () => { | ||
// Test ported from the OTEL implementation: | ||
// https://github.com/open-telemetry/opentelemetry-js-contrib/blob/0d6ebded313bb75b5a0e7a6422206c922daf3943/plugins/node/instrumentation-lru-memoizer/test/index.test.ts#L28 | ||
const memoizer = require('lru-memoizer'); | ||
|
||
let memoizerLoadCallback; | ||
const memoizedFoo = memoizer({ | ||
load: (_param, callback) => { | ||
memoizerLoadCallback = callback; | ||
}, | ||
hash: () => 'bar', | ||
}); | ||
|
||
Sentry.startSpan({ op: 'run' }, async span => { | ||
const outerSpanContext = span.spanContext(); | ||
|
||
memoizedFoo({ foo: 'bar' }, () => { | ||
const innerContext = Sentry.getActiveSpan().spanContext(); | ||
|
||
// The span context should be the same as the outer span | ||
// Throwing an error here will cause the test to fail | ||
if (outerSpanContext !== innerContext) { | ||
throw new Error('Outer and inner span context should match'); | ||
} | ||
}); | ||
|
||
span.end(); | ||
}); | ||
|
||
// Invoking the load callback outside the span | ||
memoizerLoadCallback(); | ||
}; | ||
|
||
run(); |
29 changes: 29 additions & 0 deletions
29
dev-packages/node-integration-tests/suites/tracing/lru-memoizer/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; | ||
|
||
describe('lru-memoizer', () => { | ||
afterAll(() => { | ||
cleanupChildProcesses(); | ||
}); | ||
|
||
test('keeps outer context inside the memoized inner functions', done => { | ||
createRunner(__dirname, 'scenario.js') | ||
// We expect only one transaction and nothing else. | ||
// A failed test will result in an error event being sent to Sentry. | ||
// Which will fail this suite. | ||
.expect({ | ||
transaction: { | ||
transaction: '<unknown>', | ||
contexts: { | ||
trace: expect.objectContaining({ | ||
op: 'run', | ||
data: expect.objectContaining({ | ||
'sentry.op': 'run', | ||
'sentry.origin': 'manual', | ||
}), | ||
}), | ||
}, | ||
}, | ||
}) | ||
.start(done); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { LruMemoizerInstrumentation } from '@opentelemetry/instrumentation-lru-memoizer'; | ||
|
||
import { defineIntegration } from '@sentry/core'; | ||
import type { IntegrationFn } from '@sentry/types'; | ||
import { generateInstrumentOnce } from '../../otel/instrument'; | ||
|
||
const INTEGRATION_NAME = 'LruMemoizer'; | ||
|
||
export const instrumentLruMemoizer = generateInstrumentOnce(INTEGRATION_NAME, () => new LruMemoizerInstrumentation()); | ||
|
||
const _lruMemoizerIntegration = (() => { | ||
return { | ||
name: INTEGRATION_NAME, | ||
setupOnce() { | ||
instrumentLruMemoizer(); | ||
}, | ||
}; | ||
}) satisfies IntegrationFn; | ||
|
||
/** | ||
* LruMemoizer integration | ||
* | ||
* Propagate traces through LruMemoizer. | ||
*/ | ||
export const lruMemoizerIntegration = defineIntegration(_lruMemoizerIntegration); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters