Skip to content

Commit

Permalink
chore: add QueriesMetrics test (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored Jun 28, 2024
1 parent 560faca commit 87241cf
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
78 changes: 78 additions & 0 deletions pkg/querier_metrics_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
8 changes: 4 additions & 4 deletions pkg/queries_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 87241cf

Please sign in to comment.