Skip to content

Commit 4eb96a0

Browse files
committed
Fix metrics error code format
1 parent 5bd0e83 commit 4eb96a0

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/metrics.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as grpc from '@grpc/grpc-js';
88
import { Registry, Counter, Histogram, metric } from 'prom-client';
99
import { Code, connectErrorFromReason, Interceptor, StreamRequest, UnaryRequest } from '@bufbuild/connect-node';
1010
import { MethodKind } from '@bufbuild/protobuf';
11-
import { codeToString, StreamResponse, UnaryResponse } from '@bufbuild/connect-core';
11+
import { StreamResponse, UnaryResponse } from '@bufbuild/connect-core';
1212

1313
export type GrpcMethodType = 'unary' | 'client_stream' | 'server_stream' | 'bidi_stream';
1414

@@ -141,8 +141,8 @@ export function getConnectMetricsInterceptor(): Interceptor {
141141
throw e;
142142
} finally {
143143
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' });
146146
}
147147
}
148148
}
@@ -185,8 +185,8 @@ export function getConnectMetricsInterceptor(): Interceptor {
185185
throw err;
186186
} finally {
187187
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' });
190190
}
191191
}
192192
};
@@ -216,6 +216,9 @@ export function getGrpcMetricsInterceptor(): grpc.Interceptor {
216216
method,
217217
};
218218
};
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+
};
219222

220223
return (options, nextCall): grpc.InterceptingCall => {
221224
const methodDef = options.method_definition;
@@ -226,9 +229,9 @@ export function getGrpcMetricsInterceptor(): grpc.Interceptor {
226229
.withOnReceiveStatus((status, next) => {
227230
GRPCMetrics.handled({
228231
...labels,
229-
code: grpc.status[status.code],
232+
code: formatErrorCode(grpc.status[status.code]),
230233
});
231-
stopTimer({ grpc_code: grpc.status[status.code] });
234+
stopTimer({ grpc_code: formatErrorCode(grpc.status[status.code]) });
232235
next(status);
233236
})
234237
.withOnReceiveMessage((message, next) => {

0 commit comments

Comments
 (0)