-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(server-utils): Migrate @opentelemetry/instrumentation-ioredis to orchestrion
#21849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chargome
wants to merge
5
commits into
develop
Choose a base branch
from
cg/redisio-orchestrion
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+702
−156
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d05dad8
wip ci check
chargome e650eee
Merge branch 'develop' into cg/redisio-orchestrion
chargome a4c561e
test(nuxt): Inline standard-as-callback so ioredis connect interop su…
chargome 5eb6e88
fix(server-utils): Address bugbot review on ioredis orchestrion
chargome da258e2
ref(node): Extract cacheResponseHook to a module free of OTel redis i…
chargome File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,103 @@ | ||
| import type { Span } from '@sentry/core'; | ||
| import { | ||
| SEMANTIC_ATTRIBUTE_CACHE_HIT, | ||
| SEMANTIC_ATTRIBUTE_CACHE_ITEM_SIZE, | ||
| SEMANTIC_ATTRIBUTE_CACHE_KEY, | ||
| SEMANTIC_ATTRIBUTE_SENTRY_OP, | ||
| spanToJSON, | ||
| truncate, | ||
| } from '@sentry/core'; | ||
| import type { IORedisCommandArgs } from '../../../utils/redisCache'; | ||
| import { | ||
| calculateCacheItemSize, | ||
| GET_COMMANDS, | ||
| getCacheKeySafely, | ||
| getCacheOperation, | ||
| isInCommands, | ||
| shouldConsiderForCache, | ||
| } from '../../../utils/redisCache'; | ||
| import type { IORedisResponseCustomAttributeFunction } from './vendored/types'; | ||
|
|
||
| // This module deliberately does NOT import the vendored OTel `IORedisInstrumentation`/ | ||
| // `RedisInstrumentation`, so the orchestrion opt-in can pull `cacheResponseHook` | ||
| // without dragging the OTel redis instrumentation into its module graph. | ||
|
|
||
| export interface RedisOptions { | ||
| /** | ||
| * Define cache prefixes for cache keys that should be captured as a cache span. | ||
| * | ||
| * Setting this to, for example, `['user:']` will capture cache keys that start with `user:`. | ||
| */ | ||
| cachePrefixes?: string[]; | ||
| /** | ||
| * Maximum length of the cache key added to the span description. If the key exceeds this length, it will be truncated. | ||
| * | ||
| * Passing `0` will use the full cache key without truncation. | ||
| * | ||
| * By default, the full cache key is used. | ||
| */ | ||
| maxCacheKeyLength?: number; | ||
| } | ||
|
|
||
| /* Only exported for testing purposes */ | ||
| export let _redisOptions: RedisOptions = {}; | ||
|
|
||
| /** Set the options consumed by {@link cacheResponseHook}. */ | ||
| export function setRedisOptions(options: RedisOptions): void { | ||
| _redisOptions = options; | ||
| } | ||
|
|
||
| /* Only exported for testing purposes */ | ||
| export const cacheResponseHook: IORedisResponseCustomAttributeFunction = ( | ||
| span: Span, | ||
| redisCommand: string, | ||
| cmdArgs: IORedisCommandArgs, | ||
| response: unknown, | ||
| ) => { | ||
| const safeKey = getCacheKeySafely(redisCommand, cmdArgs); | ||
| const cacheOperation = getCacheOperation(redisCommand); | ||
|
|
||
| if ( | ||
| !safeKey || | ||
| !cacheOperation || | ||
| !_redisOptions.cachePrefixes || | ||
| !shouldConsiderForCache(redisCommand, safeKey, _redisOptions.cachePrefixes) | ||
| ) { | ||
| // not relevant for cache | ||
| return; | ||
| } | ||
|
|
||
| // otel/ioredis seems to be using the old standard, as there was a change to those params: https://github.com/open-telemetry/opentelemetry-specification/issues/3199 | ||
| // We are using params based on the docs: https://opentelemetry.io/docs/specs/semconv/attributes-registry/network/ | ||
| // Fall back to stable semconv attributes (server.address/server.port) when | ||
| // old-semconv ones are absent, eg OTEL_SEMCONV_STABILITY_OPT_IN=database | ||
| // set for node-redis v4/v5. | ||
| const spanData = spanToJSON(span).data; | ||
| const networkPeerAddress = spanData['net.peer.name'] ?? spanData['server.address']; | ||
| const networkPeerPort = spanData['net.peer.port'] ?? spanData['server.port']; | ||
| if (networkPeerPort && networkPeerAddress) { | ||
| span.setAttributes({ 'network.peer.address': networkPeerAddress, 'network.peer.port': networkPeerPort }); | ||
| } | ||
|
|
||
| const cacheItemSize = calculateCacheItemSize(response); | ||
|
|
||
| if (cacheItemSize) { | ||
| span.setAttribute(SEMANTIC_ATTRIBUTE_CACHE_ITEM_SIZE, cacheItemSize); | ||
| } | ||
|
|
||
| if (isInCommands(GET_COMMANDS, redisCommand) && cacheItemSize !== undefined) { | ||
| span.setAttribute(SEMANTIC_ATTRIBUTE_CACHE_HIT, cacheItemSize > 0); | ||
| } | ||
|
|
||
| span.setAttributes({ | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_OP]: cacheOperation, | ||
| [SEMANTIC_ATTRIBUTE_CACHE_KEY]: safeKey, | ||
| }); | ||
|
|
||
| // todo: change to string[] once EAP supports it | ||
| const spanDescription = safeKey.join(', '); | ||
|
|
||
| span.updateName( | ||
| _redisOptions.maxCacheKeyLength ? truncate(spanDescription, _redisOptions.maxCacheKeyLength) : spanDescription, | ||
| ); | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.