Skip to content

Commit 8226aaf

Browse files
committed
fixup! fixup! fixup! feat(deno): redis diagnostics channel based integration for deno (#21087)
1 parent 0f21211 commit 8226aaf

5 files changed

Lines changed: 23 additions & 7 deletions

File tree

packages/core/src/integrations/redis/redis-statement-serializer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export function defaultDbStatementSerializer(
4141
if (!Array.isArray(cmdArgs) || cmdArgs.length === 0) return cmdName;
4242

4343
const budget = SERIALIZATION_SUBSETS.find(({ regex }) => regex.test(cmdName))?.args ?? 0;
44-
const argsToSerialize: Array<string | Uint8Array | number | unknown[]> =
45-
budget >= 0 ? cmdArgs.slice(0, budget) : cmdArgs.slice();
44+
const argsToSerialize: Array<string | number | unknown[]> = (budget >= 0 ? cmdArgs.slice(0, budget) : cmdArgs.slice())
45+
.map(a => (a instanceof Uint8Array ? '<binary>' : a)) as Array<string | number | unknown[]>;
4646
if (cmdArgs.length > argsToSerialize.length) {
4747
argsToSerialize.push(`[${cmdArgs.length - budget} other arguments]`);
4848
}

packages/deno/src/denoVersion.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ export const HTTP_CLIENT_DIAGNOSTICS_CHANNEL_SUPPORTED = gte(2, 7, 13);
2020

2121
/** Whether `http.server.request.start` fires (Deno 2.8.0+). */
2222
export const HTTP_SERVER_DIAGNOSTICS_CHANNEL_SUPPORTED = gte(2, 8, 0);
23+
24+
/** Whether `node:diagnostics_channel.tracingChannel` exists (Deno 1.44.3+). */
25+
export const TRACING_CHANNEL_SUPPORTED = gte(1, 44, 3);

packages/deno/src/integrations/redis.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { tracingChannel as nativeTracingChannel } from 'node:diagnostics_channel';
1+
// * import so that loading this module doesn't error on Deno versions
2+
// lacking `tracingChannel` (added in Deno 1.44.3).
3+
// On older runtimes the integration becomes a no-op.
4+
import * as dc from 'node:diagnostics_channel';
25
import type {
36
Integration,
47
IntegrationFn,
@@ -34,7 +37,7 @@ const portableTracingChannel: RedisTracingChannelFactory = <T extends object>(
3437
name: string,
3538
transformStart: (data: T) => Span,
3639
): RedisTracingChannel<T> => {
37-
const channel = nativeTracingChannel<DataWithSpan<T>>(name);
40+
const channel = dc.tracingChannel<DataWithSpan<T>>(name);
3841
return {
3942
subscribe(subs: Partial<RedisTracingChannelSubscribers<T>>): void {
4043
const userStart = subs.start as SubscriberFn<T> | undefined;
@@ -59,6 +62,9 @@ const _denoRedisIntegration = ((options: DenoRedisIntegrationOptions = {}) => {
5962
return {
6063
name: INTEGRATION_NAME,
6164
setupOnce() {
65+
if (!dc.tracingChannel) {
66+
return;
67+
}
6268
setAsyncLocalStorageAsyncContextStrategy();
6369
subscribeRedisDiagnosticChannels(portableTracingChannel, options.responseHook);
6470
},

packages/deno/src/sdk.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import { DenoClient } from './client';
1616
import { breadcrumbsIntegration } from './integrations/breadcrumbs';
1717
import { denoContextIntegration } from './integrations/context';
1818
import { contextLinesIntegration } from './integrations/contextlines';
19-
import { HTTP_CLIENT_DIAGNOSTICS_CHANNEL_SUPPORTED, HTTP_SERVER_DIAGNOSTICS_CHANNEL_SUPPORTED } from './denoVersion';
19+
import {
20+
HTTP_CLIENT_DIAGNOSTICS_CHANNEL_SUPPORTED,
21+
HTTP_SERVER_DIAGNOSTICS_CHANNEL_SUPPORTED,
22+
TRACING_CHANNEL_SUPPORTED,
23+
} from './denoVersion';
2024
import { denoServeIntegration } from './integrations/deno-serve';
2125
import { denoHttpIntegration } from './integrations/http';
2226
import { denoRedisIntegration } from './integrations/redis';
@@ -48,7 +52,8 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
4852
...(HTTP_CLIENT_DIAGNOSTICS_CHANNEL_SUPPORTED || HTTP_SERVER_DIAGNOSTICS_CHANNEL_SUPPORTED
4953
? [denoHttpIntegration()]
5054
: []),
51-
denoRedisIntegration(),
55+
// node:diagnostics_channel.tracingChannel exists on Deno 1.44.3+.
56+
...(TRACING_CHANNEL_SUPPORTED ? [denoRedisIntegration()] : []),
5257
contextLinesIntegration(),
5358
normalizePathsIntegration(),
5459
globalHandlersIntegration(),

packages/node/src/utils/redisCache.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ export function getCacheKeySafely(redisCommand: string, cmdArgs: IORedisCommandA
3838

3939
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4040
const processArg = (arg: string | Buffer | Uint8Array | number | any[]): string[] => {
41-
if (typeof arg === 'string' || typeof arg === 'number' || Buffer.isBuffer(arg)) {
41+
if (typeof arg === 'string' || typeof arg === 'number') {
4242
return [arg.toString()];
43+
} else if (arg instanceof Uint8Array) {
44+
return [new TextDecoder().decode(arg)];
4345
} else if (Array.isArray(arg)) {
4446
return flatten(arg.map(arg => processArg(arg)));
4547
} else {

0 commit comments

Comments
 (0)