Skip to content

Commit

Permalink
Add RPC errors metric (hyperledger#6919)
Browse files Browse the repository at this point in the history
* Add RPC errors metric.
This metric has two labels : RPC method and RPC error type.

Signed-off-by: Ameziane H <[email protected]>

* Spotless.

Signed-off-by: Ameziane H <[email protected]>

* Add changelog.

Signed-off-by: Ameziane H <[email protected]>

---------

Signed-off-by: Ameziane H <[email protected]>
Signed-off-by: Sally MacFarlane <[email protected]>
Co-authored-by: Sally MacFarlane <[email protected]>
  • Loading branch information
2 people authored and matthew1001 committed Jun 7, 2024
1 parent f746d5a commit 1ea445b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
- Log detailed timing of block creation steps [#6880](https://github.com/hyperledger/besu/pull/6880)
- Expose transaction count by type metrics for the layered txpool [#6903](https://github.com/hyperledger/besu/pull/6903)
- Expose bad block events via the BesuEvents plugin API [#6848](https://github.com/hyperledger/besu/pull/6848)
- Add RPC errors metric [#6919](https://github.com/hyperledger/besu/pull/6919/)

### Bug fixes
- Fix txpool dump/restore race condition [#6665](https://github.com/hyperledger/besu/pull/6665)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ public String get(final @Nullable HttpServerRequest carrier, final String key) {
private final HealthService livenessService;
private final HealthService readinessService;

private final MetricsSystem metricsSystem;

/**
* Construct a EngineJsonRpcService to handle either http or websocket clients
*
Expand Down Expand Up @@ -214,6 +216,7 @@ public EngineJsonRpcService(
this.livenessService = livenessService;
this.readinessService = readinessService;
this.maxActiveConnections = config.getMaxActiveConnections();
this.metricsSystem = metricsSystem;
}

public CompletableFuture<Void> start() {
Expand Down Expand Up @@ -451,7 +454,8 @@ private Router buildRouter() {
new JsonRpcExecutor(
new AuthenticatedJsonRpcProcessor(
new TimedJsonRpcProcessor(
new TracedJsonRpcProcessor(new BaseJsonRpcProcessor()), requestTimer),
new TracedJsonRpcProcessor(new BaseJsonRpcProcessor(), metricsSystem),
requestTimer),
authenticationService.get(),
config.getNoAuthRpcApis()),
rpcMethods),
Expand All @@ -463,7 +467,8 @@ private Router buildRouter() {
HandlerFactory.jsonRpcExecutor(
new JsonRpcExecutor(
new TimedJsonRpcProcessor(
new TracedJsonRpcProcessor(new BaseJsonRpcProcessor()), requestTimer),
new TracedJsonRpcProcessor(new BaseJsonRpcProcessor(), metricsSystem),
requestTimer),
rpcMethods),
tracer,
config),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public String get(final @Nullable HttpServerRequest carrier, final String key) {
private HttpServer httpServer;
private final HealthService livenessService;
private final HealthService readinessService;
private final MetricsSystem metricsSystem;

/**
* Construct a JsonRpcHttpService handler
Expand Down Expand Up @@ -204,6 +205,7 @@ public JsonRpcHttpService(
if (metricsSystem instanceof OpenTelemetrySystem) {
this.tracerProvider = ((OpenTelemetrySystem) metricsSystem).getTracerProvider();
}
this.metricsSystem = metricsSystem;
}

private void validateConfig(final JsonRpcConfiguration config) {
Expand Down Expand Up @@ -344,7 +346,8 @@ private Router buildRouter() {
new JsonRpcExecutor(
new AuthenticatedJsonRpcProcessor(
new TimedJsonRpcProcessor(
new TracedJsonRpcProcessor(new BaseJsonRpcProcessor()), requestTimer),
new TracedJsonRpcProcessor(new BaseJsonRpcProcessor(), metricsSystem),
requestTimer),
authenticationService.get(),
config.getNoAuthRpcApis()),
rpcMethods),
Expand All @@ -356,7 +359,8 @@ private Router buildRouter() {
HandlerFactory.jsonRpcExecutor(
new JsonRpcExecutor(
new TimedJsonRpcProcessor(
new TracedJsonRpcProcessor(new BaseJsonRpcProcessor()), requestTimer),
new TracedJsonRpcProcessor(new BaseJsonRpcProcessor(), metricsSystem),
requestTimer),
rpcMethods),
tracer,
config),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,29 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponseType;
import org.hyperledger.besu.metrics.BesuMetricCategory;
import org.hyperledger.besu.plugin.services.MetricsSystem;
import org.hyperledger.besu.plugin.services.metrics.Counter;
import org.hyperledger.besu.plugin.services.metrics.LabelledMetric;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;

public class TracedJsonRpcProcessor implements JsonRpcProcessor {

private final JsonRpcProcessor rpcProcessor;
protected final LabelledMetric<Counter> rpcErrorsCounter;

public TracedJsonRpcProcessor(final JsonRpcProcessor rpcProcessor) {
public TracedJsonRpcProcessor(
final JsonRpcProcessor rpcProcessor, final MetricsSystem metricsSystem) {
this.rpcProcessor = rpcProcessor;
this.rpcErrorsCounter =
metricsSystem.createLabelledCounter(
BesuMetricCategory.RPC,
"errors_count",
"Number of errors per RPC method and RPC error type",
"rpcMethod",
"errorType");
}

@Override
Expand All @@ -41,6 +54,7 @@ public JsonRpcResponse process(
JsonRpcResponse jsonRpcResponse = rpcProcessor.process(id, method, metricSpan, request);
if (JsonRpcResponseType.ERROR == jsonRpcResponse.getType()) {
JsonRpcErrorResponse errorResponse = (JsonRpcErrorResponse) jsonRpcResponse;
this.rpcErrorsCounter.labels(method.getName(), errorResponse.getErrorType().name()).inc();
switch (errorResponse.getErrorType()) {
case INVALID_PARAMS:
metricSpan.setStatus(StatusCode.ERROR, "Invalid Params");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ private JsonRpcErrorResponse errorResponse(
} else {
final TransactionProcessingResult resultTrx = result.result();
if (resultTrx != null && resultTrx.getRevertReason().isPresent()) {

return errorResponse(
request,
new JsonRpcError(
Expand Down

0 comments on commit 1ea445b

Please sign in to comment.