From c6a34b033438d38ceb443da3f7c7751f421467a2 Mon Sep 17 00:00:00 2001 From: Robert Pirtle Date: Mon, 15 Apr 2024 12:03:10 -0700 Subject: [PATCH] add part_of_batch column to metrics (#91) --- ...2_add_proxied_request_metrics_partofbatch_column.up.sql | 4 ++++ clients/database/request_metric.go | 1 + service/batchmdw/batchmdw.go | 7 +++++++ service/middleware.go | 4 ++++ 4 files changed, 16 insertions(+) create mode 100644 clients/database/migrations/20240415111242_add_proxied_request_metrics_partofbatch_column.up.sql diff --git a/clients/database/migrations/20240415111242_add_proxied_request_metrics_partofbatch_column.up.sql b/clients/database/migrations/20240415111242_add_proxied_request_metrics_partofbatch_column.up.sql new file mode 100644 index 0000000..c87e947 --- /dev/null +++ b/clients/database/migrations/20240415111242_add_proxied_request_metrics_partofbatch_column.up.sql @@ -0,0 +1,4 @@ +ALTER TABLE + IF EXISTS proxied_request_metrics +ADD + part_of_batch boolean NOT NULL DEFAULT false; diff --git a/clients/database/request_metric.go b/clients/database/request_metric.go index 93f8ff9..2ec3e9c 100644 --- a/clients/database/request_metric.go +++ b/clients/database/request_metric.go @@ -30,6 +30,7 @@ type ProxiedRequestMetric struct { ResponseBackend string ResponseBackendRoute string CacheHit bool + PartOfBatch bool } // Save saves the current ProxiedRequestMetric to diff --git a/service/batchmdw/batchmdw.go b/service/batchmdw/batchmdw.go index fa9cd52..ad2b166 100644 --- a/service/batchmdw/batchmdw.go +++ b/service/batchmdw/batchmdw.go @@ -83,3 +83,10 @@ func CreateBatchProcessingMiddleware( batchProcessor.RequestAndServe(w) } } + +// IsBatchContext returns true when the passed in context is for a batch EVM request +func IsBatchContext(ctx context.Context, decodedBatchContextKey string) bool { + batch := ctx.Value(decodedBatchContextKey) + batchReq, ok := (batch).([]*decode.EVMRPCRequestEnvelope) + return ok && len(batchReq) > 0 +} diff --git a/service/middleware.go b/service/middleware.go index 406c960..8df5d9b 100644 --- a/service/middleware.go +++ b/service/middleware.go @@ -15,6 +15,7 @@ import ( "github.com/kava-labs/kava-proxy-service/config" "github.com/kava-labs/kava-proxy-service/decode" "github.com/kava-labs/kava-proxy-service/logging" + "github.com/kava-labs/kava-proxy-service/service/batchmdw" "github.com/kava-labs/kava-proxy-service/service/cachemdw" ) @@ -486,6 +487,8 @@ func createAfterProxyFinalizer(service *ProxyService, config config.Config) http blockNumber = &rawBlockNumber } + partOfBatch := batchmdw.IsBatchContext(r.Context(), DecodedBatchRequestContextKey) + isCached := cachemdw.IsRequestCached(r.Context()) // create a metric for the request @@ -502,6 +505,7 @@ func createAfterProxyFinalizer(service *ProxyService, config config.Config) http ResponseBackend: proxyMetadata.BackendName, ResponseBackendRoute: proxyMetadata.BackendRoute.String(), CacheHit: isCached, + PartOfBatch: partOfBatch, } // save metric to database async