java-grpc-prometheus icon indicating copy to clipboard operation
java-grpc-prometheus copied to clipboard

Can I create custom label to metric by GrpcMonitoringInterceptor?

Open kkpolak opened this issue 5 years ago • 0 comments

Hi @dinowernli and other creators. I want to add custom label "errorCode" to my metric. Is it even possible? I have such Configuration of GrpcMonitoringInterceptor:

@GRpcGlobalInterceptor
public class GrpcMonitoringInterceptor implements ServerInterceptor {

  private final MonitoringServerInterceptor interceptor;

  public GrpcMonitoringInterceptor(CollectorRegistry collectorRegistry) {
    Configuration configuration = Configuration
        .cheapMetricsOnly().withCollectorRegistry(collectorRegistry);
    interceptor = MonitoringServerInterceptor.create(configuration);
  }

  @Override
  public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
      ServerCallHandler<ReqT, RespT> next) {
    return interceptor.interceptCall(call, headers, next);
  }
}

I have such grpc_server_handled_total metric:

expr: |
              ((sum by (job, namespace, grpc_method) ((grpc_server_handled_total{job=~"wallet.*",code!="OK"}) - (grpc_server_handled_total{job=~"wallet.*",code!="OK"}  offset 5m))) / sum by (job, namespace, grpc_method) ((grpc_server_handled_total{job=~"wallet.*"}) - (grpc_server_handled_total{job=~"wallet.*"} offset 5m)) * 100) > 10 and (sum by (job, namespace, grpc_method) ((grpc_server_handled_total{job=~"wallet.*"}) - (grpc_server_handled_total{job=~"wallet.*"} offset 5m)) > 20)

I want to change the metric to this:

((sum by (job, namespace, grpc_method) ((grpc_server_handled_total{job=~"wallet.*",code!="OK",errorCode!="IP_BLOCKED"}) - (grpc_server_handled_total{job=~"wallet.*",code!="OK",errorCode!="IP_BLOCKED"}  offset 5m))) / sum by (job, namespace, grpc_method) ((grpc_server_handled_total{job=~"wallet.*"}) - (grpc_server_handled_total{job=~"wallet.*"} offset 5m)) * 100) > 10 and (sum by (job, namespace, grpc_method) ((grpc_server_handled_total{job=~"wallet.*"}) - (grpc_server_handled_total{job=~"wallet.*"} offset 5m)) > 20)

I tried to make some changes with collectorRegistry when intercepotr is creating, but it doesn't work.

  public GrpcMonitoringInterceptor(CollectorRegistry collectorRegistry) {
    Counter tmp = Counter.build()
        .namespace("grpc")
        .subsystem("server")
        .name("handled_total")
        .labelNames("grpc_type", "grpc_service", "grpc_method", "code", "errorCode")
        .help("Total number of RPCs completed on the server, regardless of success or failure.").register();
    collectorRegistry.register(tmp);
  public GrpcMonitoringInterceptor(CollectorRegistry collectorRegistry) {
    Counter tmp = Counter.build()
        .namespace("grpc")
        .subsystem("server")
        .name("handled_total")
        .labelNames("grpc_type", "grpc_service", "grpc_method", "code", "errorCode")
        .help("Total number of RPCs completed on the server, regardless of success or failure.").register();
    collectorRegistry.register(tmp);

    Configuration configuration = Configuration
        .cheapMetricsOnly().withCollectorRegistry(collectorRegistry);
    interceptor = MonitoringServerInterceptor.create(configuration);
  }
    Configuration configuration = Configuration
        .cheapMetricsOnly().withCollectorRegistry(collectorRegistry);
    interceptor = MonitoringServerInterceptor.create(configuration);
  }

kkpolak avatar Nov 19 '20 08:11 kkpolak