@@ -34,23 +34,26 @@ import {
3434class OTelCommandMetrics implements IOTelCommandMetrics {
3535 readonly #instruments: MetricInstruments ;
3636 readonly #options: MetricOptions ;
37+ public readonly createRecordOperationDuration : (
38+ args : ReadonlyArray < RedisArgument > ,
39+ clientAttributes ?: OTelClientAttributes
40+ ) => ( error ?: Error ) => void ;
3741
3842 constructor ( options : MetricOptions , instruments : MetricInstruments ) {
3943 this . #options = options ;
4044 this . #instruments = instruments ;
41- }
4245
43- private isCommandExcluded ( commandName : string ) {
44- return (
45- // It's not explicitly included
46- ( this . #options . hasIncludeCommands &&
47- ! this . #options . includeCommands [ commandName ] ) ||
48- // it's explicitly excluded
49- this . #options . excludeCommands [ commandName ]
50- ) ;
46+ // Build the appropriate function based on options
47+ if ( options . hasIncludeCommands || options . hasExcludeCommands ) {
48+ // Version with filtering
49+ this . createRecordOperationDuration = this . #createWithFiltering . bind ( this ) ;
50+ } else {
51+ this . createRecordOperationDuration =
52+ this . #createWithoutFiltering . bind ( this ) ;
53+ }
5154 }
5255
53- public createRecordOperationDuration (
56+ #createWithFiltering (
5457 args : ReadonlyArray < RedisArgument > ,
5558 clientAttributes ?: OTelClientAttributes
5659 ) : ( error ?: Error ) => void {
@@ -60,25 +63,45 @@ class OTelCommandMetrics implements IOTelCommandMetrics {
6063 return noopFunction ;
6164 }
6265
63- const startTime = performance . now ( ) ;
66+ return this . #recordOperation( commandName , clientAttributes ) ;
67+ }
6468
65- const baseAttributes = {
66- [ OTEL_ATTRIBUTES . dbOperationName ] : commandName ,
67- ...parseClientAttributes ( clientAttributes ) ,
68- } ;
69+ #createWithoutFiltering(
70+ args : ReadonlyArray < RedisArgument > ,
71+ clientAttributes ?: OTelClientAttributes
72+ ) : ( error ?: Error ) => void {
73+ const commandName = args [ 0 ] ?. toString ( ) || "UNKNOWN" ;
74+ return this . #recordOperation( commandName , clientAttributes ) ;
75+ }
76+
77+ #recordOperation(
78+ commandName : string ,
79+ clientAttributes ?: OTelClientAttributes
80+ ) : ( error ?: Error ) => void {
81+ const startTime = performance . now ( ) ;
6982
7083 return ( error ?: Error ) => {
7184 this . #instruments. dbClientOperationDuration . record (
72- ( performance . now ( ) - startTime ) / 1000 , // convert to seconds
85+ ( performance . now ( ) - startTime ) / 1000 ,
7386 {
7487 ...this . #options. attributes ,
75- ...baseAttributes ,
76- // TODO add error types
88+ [ OTEL_ATTRIBUTES . dbOperationName ] : commandName ,
89+ [ OTEL_ATTRIBUTES . dbNamespace ] : clientAttributes ?. db ,
90+ [ OTEL_ATTRIBUTES . serverAddress ] : clientAttributes ?. host ,
91+ [ OTEL_ATTRIBUTES . serverPort ] : clientAttributes ?. port ,
7792 ...( error ? { [ OTEL_ATTRIBUTES . errorType ] : error . message } : { } ) ,
7893 }
7994 ) ;
8095 } ;
8196 }
97+
98+ private isCommandExcluded ( commandName : string ) {
99+ return (
100+ ( this . #options. hasIncludeCommands &&
101+ ! this . #options. includeCommands [ commandName ] ) ||
102+ this . #options. excludeCommands [ commandName ]
103+ ) ;
104+ }
82105}
83106
84107class OTelConnectionBasicMetrics implements IOTelConnectionBasicMetrics {
@@ -92,6 +115,7 @@ class OTelConnectionBasicMetrics implements IOTelConnectionBasicMetrics {
92115
93116 public recordConnectionCount (
94117 value : number ,
118+
95119 clientAttributes ?: OTelClientAttributes
96120 ) {
97121 this . #instruments. dbClientConnectionCount . add ( value , {
0 commit comments