Skip to content

Commit

Permalink
only register metric for new frontendProcessor (#3526)
Browse files Browse the repository at this point in the history
* only register metric for new frontendProcessor

* fix test

* fix panic in tests
  • Loading branch information
electron0zero authored Mar 28, 2024
1 parent 75baee4 commit 087a140
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
25 changes: 13 additions & 12 deletions modules/querier/worker/frontend_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ var processorBackoffConfig = backoff.Config{
MaxBackoff: 1 * time.Second,
}

var metricWorkerRequests = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "tempo",
Name: "querier_worker_request_executed_total",
Help: "The total number of requests executed by the querier worker.",
})

func newFrontendProcessor(cfg Config, handler RequestHandler, log log.Logger) processor {
metricWorkerRequests := promauto.NewCounter(prometheus.CounterOpts{
Namespace: "tempo",
Name: "querier_worker_request_executed_total",
Help: "The total number of requests executed by the querier worker.",
})
return &frontendProcessor{
log: log,
handler: handler,
maxMessageSize: cfg.GRPCClientConfig.MaxSendMsgSize,
querierID: cfg.QuerierID,
log: log,
handler: handler,
maxMessageSize: cfg.GRPCClientConfig.MaxSendMsgSize,
querierID: cfg.QuerierID,
metricRequestsTotal: metricWorkerRequests,
}
}

Expand All @@ -50,7 +50,8 @@ type frontendProcessor struct {
maxMessageSize int
querierID string

log log.Logger
metricRequestsTotal prometheus.Counter
log log.Logger
}

// notifyShutdown implements processor.
Expand Down Expand Up @@ -193,7 +194,7 @@ func (fp *frontendProcessor) runRequest(ctx context.Context, request *httpgrpc.H
level.Error(fp.log).Log("msg", "error processing query", "err", errMsg)
}

metricWorkerRequests.Inc()
fp.metricRequestsTotal.Inc()

return response
}
Expand Down
7 changes: 6 additions & 1 deletion modules/querier/worker/frontend_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/go-kit/log"
"github.com/grafana/dskit/grpcclient"
"github.com/grafana/dskit/httpgrpc"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/require"
)
Expand All @@ -32,6 +33,8 @@ func TestRunRequests(t *testing.T) {

inf := newFrontendProcessor(Config{GRPCClientConfig: grpcclient.Config{MaxSendMsgSize: 10}}, RequestHandlerFunc(handler), log.NewNopLogger())
fp := inf.(*frontendProcessor)
// unregister metric in test avoid panic due to registering it twice in tests
defer prometheus.Unregister(fp.metricRequestsTotal)

totalRequests := byte(10)
reqs := []*httpgrpc.HTTPRequest{}
Expand All @@ -50,14 +53,16 @@ func TestRunRequests(t *testing.T) {

// check that counter metric is working
m := &dto.Metric{}
err := metricWorkerRequests.Write(m)
err := fp.metricRequestsTotal.Write(m)
require.NoError(t, err)
require.Equal(t, float64(totalRequests), m.Counter.GetValue())
}

func TestHandleSendError(t *testing.T) {
inf := newFrontendProcessor(Config{}, nil, log.NewNopLogger())
fp := inf.(*frontendProcessor)
// unregister metric in test avoid panic due to registering it twice in tests
defer prometheus.Unregister(fp.metricRequestsTotal)

err := fp.handleSendError(nil)
require.NoError(t, err)
Expand Down

0 comments on commit 087a140

Please sign in to comment.