-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metric): add prometheus interceptor
- Loading branch information
1 parent
d383177
commit 8fd2829
Showing
5 changed files
with
73 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
framework/src/main/java/org/tron/core/services/ratelimiter/PrometheusInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
package org.tron.core.services.ratelimiter; | ||
|
||
import io.grpc.ForwardingServerCall; | ||
import io.grpc.Metadata; | ||
import io.grpc.ServerCall; | ||
import io.grpc.ServerCallHandler; | ||
import io.grpc.ServerInterceptor; | ||
import io.grpc.Status; | ||
import io.prometheus.client.Histogram; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import org.tron.common.prometheus.MetricKeys; | ||
import org.tron.common.prometheus.Metrics; | ||
|
||
/** | ||
* A {@link ServerInterceptor} which sends latency stats about incoming grpc calls to Prometheus. | ||
*/ | ||
@Slf4j(topic = "metrics") | ||
@Component | ||
public class PrometheusInterceptor implements ServerInterceptor { | ||
|
||
@Override | ||
public <R, S> ServerCall.Listener<R> interceptCall( | ||
ServerCall<R, S> call, Metadata requestMetadata, ServerCallHandler<R, S> next) { | ||
return next.startCall(new MonitoringServerCall<>(call), requestMetadata); | ||
} | ||
|
||
static class MonitoringServerCall<R, S> extends ForwardingServerCall | ||
.SimpleForwardingServerCall<R, S> { | ||
|
||
private final Histogram.Timer requestTimer; | ||
|
||
MonitoringServerCall(ServerCall<R, S> delegate) { | ||
super(delegate); | ||
this.requestTimer = Metrics.histogramStartTimer( | ||
MetricKeys.Histogram.GRPC_SERVICE_LATENCY, getMethodDescriptor().getFullMethodName()); | ||
} | ||
|
||
@Override | ||
public void close(Status status, Metadata responseHeaders) { | ||
Metrics.histogramObserve(requestTimer); | ||
super.close(status, responseHeaders); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters