diff --git a/src/main/kotlin/io/emeraldpay/dshackle/monitoring/accesslog/AccessHandlerGrpc.kt b/src/main/kotlin/io/emeraldpay/dshackle/monitoring/accesslog/AccessHandlerGrpc.kt index aac2d8c7..952f2250 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/monitoring/accesslog/AccessHandlerGrpc.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/monitoring/accesslog/AccessHandlerGrpc.kt @@ -50,6 +50,7 @@ class AccessHandlerGrpc( "NativeSubscribe" -> processNativeSubscribe(call, headers, next) "Describe" -> processDescribe(call, headers, next) "SubscribeStatus" -> processStatus(call, headers, next) + "EstimateFee" -> processEstimateFee(call, headers, next) else -> { log.warn("unsupported method `{}`", method) next.startCall(call, headers) @@ -158,6 +159,18 @@ class AccessHandlerGrpc( ) } + @Suppress("UNCHECKED_CAST") + private fun processEstimateFee( + call: ServerCall, + headers: Metadata, + next: ServerCallHandler + ): ServerCall.Listener { + return process( + call, headers, next, + EventsBuilder.EstimateFee() as EventsBuilder.RequestReply<*, ReqT, RespT> + ) + } + open class StdCallListener>( val next: ServerCall.Listener, val builder: EB diff --git a/src/main/kotlin/io/emeraldpay/dshackle/monitoring/accesslog/Events.kt b/src/main/kotlin/io/emeraldpay/dshackle/monitoring/accesslog/Events.kt index d3d95268..4f6daefe 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/monitoring/accesslog/Events.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/monitoring/accesslog/Events.kt @@ -136,6 +136,14 @@ class Events { val request: StreamRequestDetails ) : ChainBase(blockchain, "Status", id, Channel.GRPC) + @JsonInclude(JsonInclude.Include.NON_NULL) + class EstimateFee( + blockchain: Chain, + id: UUID, + val request: StreamRequestDetails, + val estimateFee: EstimateFeeDetails + ) : ChainBase(blockchain, "EstimateFee", id, Channel.GRPC) + data class StreamRequestDetails( val id: UUID, val start: Instant, @@ -180,4 +188,9 @@ class Events { val asset: String, val address: String ) + + data class EstimateFeeDetails( + val mode: String, + val blocks: Int + ) } diff --git a/src/main/kotlin/io/emeraldpay/dshackle/monitoring/accesslog/EventsBuilder.kt b/src/main/kotlin/io/emeraldpay/dshackle/monitoring/accesslog/EventsBuilder.kt index 375d1386..17bd3227 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/monitoring/accesslog/EventsBuilder.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/monitoring/accesslog/EventsBuilder.kt @@ -452,4 +452,34 @@ class EventsBuilder { ) } } + + class EstimateFee : + Base(), + RequestReply { + + private var mode: String = "UNKNOWN" + private var blocks: Int = 0 + + override fun getT(): EstimateFee { + return this + } + + override fun onRequest(msg: BlockchainOuterClass.EstimateFeeRequest) { + this.chain = Chain.byId(msg.chain.number) + this.mode = msg.mode.name + this.blocks = msg.blocks + } + + override fun onReply(msg: BlockchainOuterClass.EstimateFeeResponse): Events.EstimateFee { + return Events.EstimateFee( + blockchain = chain, + request = requestDetails, + id = UUID.randomUUID(), + estimateFee = Events.EstimateFeeDetails( + mode = mode, + blocks = blocks + ) + ) + } + } } diff --git a/src/main/kotlin/io/emeraldpay/dshackle/rpc/BlockchainRpc.kt b/src/main/kotlin/io/emeraldpay/dshackle/rpc/BlockchainRpc.kt index 0c779e51..69dad367 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/rpc/BlockchainRpc.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/rpc/BlockchainRpc.kt @@ -167,7 +167,23 @@ class BlockchainRpc( } override fun estimateFee(request: Mono): Mono { - return request.flatMap { estimateFee.estimateFee(it) } + return request + .flatMap { + val chain = Chain.byId(it.chainValue) + val metrics = chainMetrics.get(chain) + metrics.estimateFeeMetric.increment() + val startTime = System.currentTimeMillis() + estimateFee.estimateFee(it).doFinally { + metrics.estimateFeeRespMetric.record( + System.currentTimeMillis() - startTime, + TimeUnit.MILLISECONDS + ) + } + } + .doOnError { t -> + log.error("Internal error during Fee Estimation", t) + errorMetric.increment() + } } override fun describe(request: Mono): Mono { @@ -229,5 +245,14 @@ class BlockchainRpc( .tag("chain", chain.chainCode) .publishPercentileHistogram() .register(Metrics.globalRegistry) + val estimateFeeMetric = Counter.builder("request.grpc.request") + .tag("type", "estimateFee") + .tag("chain", chain.chainCode) + .register(Metrics.globalRegistry) + val estimateFeeRespMetric = Timer.builder("request.grpc.response") + .tag("type", "estimateFee") + .tag("chain", chain.chainCode) + .publishPercentileHistogram() + .register(Metrics.globalRegistry) } }