Skip to content

Commit

Permalink
add grpc_client_handling_seconds metrics for nice-grpc
Browse files Browse the repository at this point in the history
  • Loading branch information
iQQBot authored and roboquat committed Dec 8, 2022
1 parent ea6081e commit abdf31d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
17 changes: 17 additions & 0 deletions components/gitpod-protocol/src/messaging/client-call-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class PrometheusClientCallMetrics implements IClientCallMetrics {
readonly sentCounter: prometheusClient.Counter<string>;
readonly receivedCounter: prometheusClient.Counter<string>;
readonly handledCounter: prometheusClient.Counter<string>;
readonly handledSecondsHistogram: prometheusClient.Histogram<string>;

constructor() {
this.startedCounter = new prometheusClient.Counter({
Expand All @@ -40,6 +41,12 @@ export class PrometheusClientCallMetrics implements IClientCallMetrics {
labelNames: ["grpc_service", "grpc_method", "grpc_type", "grpc_code"],
registers: [prometheusClient.register],
});
this.handledSecondsHistogram = new prometheusClient.Histogram({
name: "grpc_client_handling_seconds",
help: "Histogram of response latency (seconds) of the gRPC until it is finished by the application.",
labelNames: ["grpc_service", "grpc_method", "grpc_type", "grpc_code"],
registers: [prometheusClient.register],
});
}

started(labels: IGrpcCallMetricsLabels): void {
Expand Down Expand Up @@ -74,4 +81,14 @@ export class PrometheusClientCallMetrics implements IClientCallMetrics {
grpc_code: labels.code,
});
}

startHandleTimer(
labels: IGrpcCallMetricsLabelsWithCode,
): (labels?: Partial<Record<string, string | number>> | undefined) => number {
return this.handledSecondsHistogram.startTimer({
grpc_service: labels.service,
grpc_method: labels.method,
grpc_type: labels.type,
});
}
}
3 changes: 3 additions & 0 deletions components/gitpod-protocol/src/util/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export interface IClientCallMetrics {
sent(labels: IGrpcCallMetricsLabels): void;
received(labels: IGrpcCallMetricsLabels): void;
handled(labels: IGrpcCallMetricsLabelsWithCode): void;
startHandleTimer(
labels: IGrpcCallMetricsLabels,
): (labels?: Partial<Record<string, string | number>> | undefined) => number;
}

export function getGrpcMethodType(requestStream: boolean, responseStream: boolean): GrpcMethodType {
Expand Down
3 changes: 3 additions & 0 deletions components/gitpod-protocol/src/util/nice-grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export function prometheusClientMiddleware(metrics: IClientCallMetrics): ClientM

metrics.started(labels);

const stopTimer = metrics.startHandleTimer(labels);

let settled = false;
let status: Status = Status.OK;

Expand Down Expand Up @@ -87,6 +89,7 @@ export function prometheusClientMiddleware(metrics: IClientCallMetrics): ClientM
if (!settled) {
status = Status.CANCELLED;
}
stopTimer({ grpc_code: Status[status] });
metrics.handled({ ...labels, code: Status[status] });
}
};
Expand Down

0 comments on commit abdf31d

Please sign in to comment.