grpc-ecosystem / grpc-ecosystem/java-grpc-prometheus

Can I create custom label to metric by GrpcMonitoringInterceptor?

Open
#25 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
236
Forks
80
PR merge metrics
No merged PRs in 30d

Description

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 Listener interceptCall(ServerCall call, Metadata headers,
ServerCallHandler 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);
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.