diff --git a/pkg/app.go b/pkg/app.go index b044a8f..d3056e4 100644 --- a/pkg/app.go +++ b/pkg/app.go @@ -192,7 +192,7 @@ func (a *App) Handler(w http.ResponseWriter, r *http.Request) { wg.Wait() - queriesMetrics := NewQueriesMetrics(a.Config, queryInfos) + queriesMetrics := NewQueriesMetrics(a.Config.Chains, queryInfos) registry.MustRegister(queriesMetrics.GetMetrics(rootSpanCtx)...) for _, generator := range a.Generators { diff --git a/pkg/querier_metrics_test.go b/pkg/querier_metrics_test.go new file mode 100644 index 0000000..f1e718c --- /dev/null +++ b/pkg/querier_metrics_test.go @@ -0,0 +1,78 @@ +package pkg + +import ( + "context" + "main/pkg/config" + "main/pkg/types" + "testing" + "time" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/testutil" + "github.com/stretchr/testify/assert" +) + +func TestQuerierMetrics(t *testing.T) { + t.Parallel() + + queryInfos := []*types.QueryInfo{ + {Success: true, Chain: "chain", Duration: 2 * time.Second, URL: "url1"}, + {Success: true, Chain: "chain", Duration: 4 * time.Second, URL: "url2"}, + {Success: false, Chain: "chain", Duration: 6 * time.Second, URL: "url3"}, + } + + chains := []*config.Chain{ + {Name: "chain"}, + {Name: "chain2"}, + } + + generator := NewQueriesMetrics(chains, queryInfos) + metrics := generator.GetMetrics(context.Background()) + assert.Len(t, metrics, 4) + + queriesCountGauge, ok := metrics[0].(*prometheus.GaugeVec) + assert.True(t, ok) + assert.Equal(t, 2, testutil.CollectAndCount(queriesCountGauge)) + assert.InDelta(t, 3, testutil.ToFloat64(queriesCountGauge.With(prometheus.Labels{ + "chain": "chain", + })), 0.01) + assert.Zero(t, testutil.ToFloat64(queriesCountGauge.With(prometheus.Labels{ + "chain": "chain2", + }))) + + queriesSuccess, ok := metrics[1].(*prometheus.GaugeVec) + assert.True(t, ok) + assert.Equal(t, 2, testutil.CollectAndCount(queriesSuccess)) + assert.InDelta(t, 2, testutil.ToFloat64(queriesSuccess.With(prometheus.Labels{ + "chain": "chain", + })), 0.01) + assert.Zero(t, testutil.ToFloat64(queriesSuccess.With(prometheus.Labels{ + "chain": "chain2", + }))) + + queriesFailed, ok := metrics[2].(*prometheus.GaugeVec) + assert.True(t, ok) + assert.Equal(t, 2, testutil.CollectAndCount(queriesFailed)) + assert.InDelta(t, 1, testutil.ToFloat64(queriesFailed.With(prometheus.Labels{ + "chain": "chain", + })), 0.01) + assert.Zero(t, testutil.ToFloat64(queriesFailed.With(prometheus.Labels{ + "chain": "chain2", + }))) + + timings, ok := metrics[3].(*prometheus.GaugeVec) + assert.True(t, ok) + assert.Equal(t, 3, testutil.CollectAndCount(timings)) + assert.InDelta(t, 2, testutil.ToFloat64(timings.With(prometheus.Labels{ + "chain": "chain", + "url": "url1", + })), 0.01) + assert.InDelta(t, 4, testutil.ToFloat64(timings.With(prometheus.Labels{ + "chain": "chain", + "url": "url2", + })), 0.01) + assert.InDelta(t, 6, testutil.ToFloat64(timings.With(prometheus.Labels{ + "chain": "chain", + "url": "url3", + })), 0.01) +} diff --git a/pkg/queries_metrics.go b/pkg/queries_metrics.go index 05cd8cb..8f152ee 100644 --- a/pkg/queries_metrics.go +++ b/pkg/queries_metrics.go @@ -9,13 +9,13 @@ import ( ) type QueriesMetrics struct { - Config *config.Config + Chains []*config.Chain Infos []*types.QueryInfo } -func NewQueriesMetrics(appConfig *config.Config, queryInfos []*types.QueryInfo) *QueriesMetrics { +func NewQueriesMetrics(chains []*config.Chain, queryInfos []*types.QueryInfo) *QueriesMetrics { return &QueriesMetrics{ - Config: appConfig, + Chains: chains, Infos: queryInfos, } } @@ -54,7 +54,7 @@ func (q *QueriesMetrics) GetMetrics(ctx context.Context) []prometheus.Collector ) // so we would have this metrics even if there are no requests - for _, chain := range q.Config.Chains { + for _, chain := range q.Chains { queriesCountGauge.With(prometheus.Labels{ "chain": chain.Name, }).Set(0)