problem: doesn't increment error metrics for HTTP call with invalid JSON

rel: #150
This commit is contained in:
Igor Artamonov
2022-03-16 21:34:55 -04:00
parent bb45fa0699
commit eac2bbb2d5
2 changed files with 20 additions and 2 deletions

View File

@@ -41,7 +41,7 @@ class HttpHandler(
writeRpcJson: WriteRpcJson, writeRpcJson: WriteRpcJson,
nativeCall: NativeCall, nativeCall: NativeCall,
private val accessHandler: AccessHandlerHttp.HandlerFactory, private val accessHandler: AccessHandlerHttp.HandlerFactory,
requestMetrics: ProxyServer.RequestMetricsFactory, private val requestMetrics: ProxyServer.RequestMetricsFactory,
) : BaseHandler(writeRpcJson, nativeCall, requestMetrics) { ) : BaseHandler(writeRpcJson, nativeCall, requestMetrics) {
companion object { companion object {
@@ -70,6 +70,13 @@ class HttpHandler(
): Flux<ByteBuf> { ): Flux<ByteBuf> {
return request return request
.map(readRpcJson) .map(readRpcJson)
.doOnError {
log.debug("Failed to process request JSON with: ${it.javaClass} ${it.message}")
// most of the metrics are handled inside the execute method, including most of the errors.
// but if we have an invalid JSON it stops here, and we cannot handle it in the main onErrorResume
// because it's going to be a duplicate call for other errors
requestMetrics.get(chain, "invalid_method").errorMetric.increment()
}
.flatMapMany { call -> .flatMapMany { call ->
execute(chain, call, handler) execute(chain, call, handler)
} }

View File

@@ -23,6 +23,8 @@ import io.emeraldpay.dshackle.rpc.NativeCall
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcResponse import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcResponse
import io.emeraldpay.etherjar.rpc.RpcException import io.emeraldpay.etherjar.rpc.RpcException
import io.emeraldpay.grpc.Chain import io.emeraldpay.grpc.Chain
import io.micrometer.core.instrument.Counter
import io.micrometer.core.instrument.step.StepCounter
import reactor.core.publisher.Flux import reactor.core.publisher.Flux
import reactor.core.publisher.Mono import reactor.core.publisher.Mono
import reactor.test.StepVerifier import reactor.test.StepVerifier
@@ -72,10 +74,19 @@ class HttpHandlerSpec extends Specification {
ReadRpcJson read = Mock(ReadRpcJson) { ReadRpcJson read = Mock(ReadRpcJson) {
1 * apply(_) >> { throw new RpcException(-32123, "test", new JsonRpcResponse.NumberId(4)) } 1 * apply(_) >> { throw new RpcException(-32123, "test", new JsonRpcResponse.NumberId(4)) }
} }
Counter errorMetric = Mock(Counter) {
1 * increment()
}
ProxyServer.RequestMetricsFactory metrics = Mock(ProxyServer.RequestMetricsFactory) {
1 * get(Chain.ETHEREUM, "invalid_method") >> Mock(ProxyServer.RequestMetrics) {
1 * it.errorMetric >> errorMetric
}
}
def handler = new HttpHandler( def handler = new HttpHandler(
read, new WriteRpcJson(), read, new WriteRpcJson(),
Stub(NativeCall), Stub(AccessHandlerHttp.HandlerFactory), Stub(ProxyServer.RequestMetricsFactory) Stub(NativeCall), Stub(AccessHandlerHttp.HandlerFactory),
metrics
) )
when: when: