@@ -8,7 +8,7 @@ import * as grpc from '@grpc/grpc-js';
8
8
import { Registry , Counter , Histogram , metric } from 'prom-client' ;
9
9
import { Code , connectErrorFromReason , Interceptor , StreamRequest , UnaryRequest } from '@bufbuild/connect-node' ;
10
10
import { MethodKind } from '@bufbuild/protobuf' ;
11
- import { codeToString , StreamResponse , UnaryResponse } from '@bufbuild/connect-core' ;
11
+ import { StreamResponse , UnaryResponse } from '@bufbuild/connect-core' ;
12
12
13
13
export type GrpcMethodType = 'unary' | 'client_stream' | 'server_stream' | 'bidi_stream' ;
14
14
@@ -141,8 +141,8 @@ export function getConnectMetricsInterceptor(): Interceptor {
141
141
throw e ;
142
142
} finally {
143
143
if ( handleMetrics && ! settled ) {
144
- stopTimer ( { grpc_code : status ? codeToString ( status ) . toUpperCase ( ) : 'OK' } ) ;
145
- GRPCMetrics . handled ( { ...labels , code : status ? codeToString ( status ) . toUpperCase ( ) : 'OK' } ) ;
144
+ stopTimer ( { grpc_code : status ? Code [ status ] : 'OK' } ) ;
145
+ GRPCMetrics . handled ( { ...labels , code : status ? Code [ status ] : 'OK' } ) ;
146
146
}
147
147
}
148
148
}
@@ -185,8 +185,8 @@ export function getConnectMetricsInterceptor(): Interceptor {
185
185
throw err ;
186
186
} finally {
187
187
if ( settled ) {
188
- stopTimer ( { grpc_code : status ? codeToString ( status ) . toUpperCase ( ) : 'OK' } ) ;
189
- GRPCMetrics . handled ( { ...labels , code : status ? codeToString ( status ) . toUpperCase ( ) : 'OK' } ) ;
188
+ stopTimer ( { grpc_code : status ? Code [ status ] : 'OK' } ) ;
189
+ GRPCMetrics . handled ( { ...labels , code : status ? Code [ status ] : 'OK' } ) ;
190
190
}
191
191
}
192
192
} ;
@@ -216,6 +216,9 @@ export function getGrpcMetricsInterceptor(): grpc.Interceptor {
216
216
method,
217
217
} ;
218
218
} ;
219
+ const formatErrorCode = ( str : string ) : string => {
220
+ return str . toUpperCase ( ) === 'OK' ? str . toUpperCase ( ) : str [ 0 ] . toUpperCase ( ) + str . substring ( 1 ) . toLowerCase ( ) . replace ( / _ ( [ a - z ] ) / g, ( _ , c : string ) => c . toUpperCase ( ) ) ;
221
+ } ;
219
222
220
223
return ( options , nextCall ) : grpc . InterceptingCall => {
221
224
const methodDef = options . method_definition ;
@@ -226,9 +229,9 @@ export function getGrpcMetricsInterceptor(): grpc.Interceptor {
226
229
. withOnReceiveStatus ( ( status , next ) => {
227
230
GRPCMetrics . handled ( {
228
231
...labels ,
229
- code : grpc . status [ status . code ] ,
232
+ code : formatErrorCode ( grpc . status [ status . code ] ) ,
230
233
} ) ;
231
- stopTimer ( { grpc_code : grpc . status [ status . code ] } ) ;
234
+ stopTimer ( { grpc_code : formatErrorCode ( grpc . status [ status . code ] ) } ) ;
232
235
next ( status ) ;
233
236
} )
234
237
. withOnReceiveMessage ( ( message , next ) => {
0 commit comments