Skip to content

Commit 364ff86

Browse files
committed
refactor: move pub/sub and stream metrics recording to command wrapper
1 parent 284c5c8 commit 364ff86

5 files changed

Lines changed: 237 additions & 144 deletions

File tree

packages/client/lib/client/index.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,31 +1027,6 @@ export default class RedisClient<
10271027
} else {
10281028
const reply = await fn();
10291029

1030-
// Record pub/sub messages for PUBLISH and SPUBLISH commands (after successful send)
1031-
const cmdName = parser.redisArgs[0]?.toString().toUpperCase();
1032-
if (cmdName === 'PUBLISH' || cmdName === 'SPUBLISH') {
1033-
const channel = parser.redisArgs[1]?.toString();
1034-
const sharded = cmdName === 'SPUBLISH';
1035-
OTelMetrics.instance.pubSubMetrics.recordPubSubMessage(
1036-
'out',
1037-
channel,
1038-
sharded,
1039-
this._getClientOTelAttributes()
1040-
);
1041-
}
1042-
1043-
// Record stream messages for XADD command (after successful send)
1044-
if (cmdName === 'XADD') {
1045-
const stream = parser.redisArgs[1]?.toString();
1046-
if (stream) {
1047-
OTelMetrics.instance.streamMetrics.recordStreamProduced(
1048-
stream,
1049-
1,
1050-
this._getClientOTelAttributes()
1051-
);
1052-
}
1053-
}
1054-
10551030
if (transformReply) {
10561031
return transformReply(reply, parser.preserve, commandOptions?.typeMapping);
10571032
}

packages/client/lib/commander.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Command, CommanderConfig, RedisArgument, RedisCommands, RedisFunction, RedisFunctions, RedisModules, RedisScript, RedisScripts, RespVersions, TransformReply } from './RESP/types';
2+
import { OTelMetrics } from './opentelemetry';
23

34
interface AttachConfigOptions<
45
M extends RedisModules,
@@ -41,7 +42,7 @@ export function attachConfig<
4142
if (config?.RESP == 3 && command.unstableResp3 && !config.unstableResp3) {
4243
Class.prototype[name] = throwResp3SearchModuleUnstableError;
4344
} else {
44-
Class.prototype[name] = createCommand(command, RESP);
45+
Class.prototype[name] = OTelMetrics.wrapWithMetrics(name, createCommand(command, RESP)) as ReturnType<typeof createCommand>;
4546
}
4647
}
4748

0 commit comments

Comments
 (0)