-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate cronsumer metric collector to konsumer (#19)
- Loading branch information
1 parent
cf9bdfe
commit 68f6651
Showing
10 changed files
with
196 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
## help: print this help message | ||
help: | ||
@echo "Usage:" | ||
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ":" | sed -e 's/^/ /' | ||
|
||
## lint: runs golangci lint based on .golangci.yml configuration | ||
.PHONY: lint | ||
lint: | ||
@if ! test -f `go env GOPATH`/bin/golangci-lint; then go install github.com/golangci/golangci-lint/cmd/[email protected]; fi | ||
golangci-lint run -c .golangci.yml --fix -v | ||
|
||
## test: runs tests | ||
.PHONY: test | ||
test: | ||
go test -v ./... -coverprofile=unit_coverage.out -short | ||
|
||
## unit-coverage-html: extract unit tests coverage to html format | ||
.PHONY: unit-coverage-html | ||
unit-coverage-html: | ||
make test | ||
go tool cover -html=unit_coverage.out -o unit_coverage.html | ||
|
||
## godoc: generate documentation | ||
.PHONY: godoc | ||
godoc: | ||
@if ! test -f `go env GOPATH`/bin/godoc; then go install golang.org/x/tools/cmd/godoc; fi | ||
godoc -http=127.0.0.1:6060 | ||
|
||
.PHONY: integration-compose | ||
integration-compose: | ||
docker compose -f test/integration/docker-compose.yml up --wait --build --force-recreate --remove-orphans |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package kafka | ||
|
||
import ( | ||
"github.com/ansrivas/fiberprometheus/v2" | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
const Name = "kafka_konsumer_" | ||
|
||
type metricCollector struct { | ||
consumerMetric *ConsumerMetric | ||
|
||
totalUnprocessedMessagesCounter *prometheus.Desc | ||
totalProcessedMessagesCounter *prometheus.Desc | ||
totalUnprocessedBatchMessagesCounter *prometheus.Desc | ||
totalProcessedBatchMessagesCounter *prometheus.Desc | ||
} | ||
|
||
func (s *metricCollector) Describe(ch chan<- *prometheus.Desc) { | ||
prometheus.DescribeByCollect(s, ch) | ||
} | ||
|
||
func (s *metricCollector) Collect(ch chan<- prometheus.Metric) { | ||
ch <- prometheus.MustNewConstMetric( | ||
s.totalProcessedMessagesCounter, | ||
prometheus.CounterValue, | ||
float64(s.consumerMetric.TotalProcessedMessagesCounter), | ||
[]string{}..., | ||
) | ||
|
||
ch <- prometheus.MustNewConstMetric( | ||
s.totalUnprocessedMessagesCounter, | ||
prometheus.CounterValue, | ||
float64(s.consumerMetric.TotalUnprocessedMessagesCounter), | ||
[]string{}..., | ||
) | ||
ch <- prometheus.MustNewConstMetric( | ||
s.totalProcessedBatchMessagesCounter, | ||
prometheus.CounterValue, | ||
float64(s.consumerMetric.TotalProcessedBatchMessagesCounter), | ||
[]string{}..., | ||
) | ||
|
||
ch <- prometheus.MustNewConstMetric( | ||
s.totalUnprocessedBatchMessagesCounter, | ||
prometheus.CounterValue, | ||
float64(s.consumerMetric.TotalUnprocessedBatchMessagesCounter), | ||
[]string{}..., | ||
) | ||
} | ||
|
||
func newMetricCollector(consumerMetric *ConsumerMetric) *metricCollector { | ||
return &metricCollector{ | ||
consumerMetric: consumerMetric, | ||
|
||
totalProcessedMessagesCounter: prometheus.NewDesc( | ||
prometheus.BuildFQName(Name, "processed_messages_total", "current"), | ||
"Total number of processed messages.", | ||
[]string{}, | ||
nil, | ||
), | ||
totalUnprocessedMessagesCounter: prometheus.NewDesc( | ||
prometheus.BuildFQName(Name, "unprocessed_messages_total", "current"), | ||
"Total number of unprocessed messages.", | ||
[]string{}, | ||
nil, | ||
), | ||
totalProcessedBatchMessagesCounter: prometheus.NewDesc( | ||
prometheus.BuildFQName(Name, "processed_batch_messages_total", "current"), | ||
"Total number of processed batch messages.", | ||
[]string{}, | ||
nil, | ||
), | ||
totalUnprocessedBatchMessagesCounter: prometheus.NewDesc( | ||
prometheus.BuildFQName(Name, "unprocessed_batch_messages_total", "current"), | ||
"Total number of unprocessed batch messages.", | ||
[]string{}, | ||
nil, | ||
), | ||
} | ||
} | ||
|
||
func NewMetricMiddleware(cfg *ConsumerConfig, | ||
app *fiber.App, | ||
consumerMetric *ConsumerMetric, | ||
metricCollectors ...prometheus.Collector, | ||
) (func(ctx *fiber.Ctx) error, error) { | ||
prometheus.DefaultRegisterer.MustRegister(newMetricCollector(consumerMetric)) | ||
prometheus.DefaultRegisterer.MustRegister(metricCollectors...) | ||
|
||
fiberPrometheus := fiberprometheus.New(cfg.Reader.GroupID) | ||
fiberPrometheus.RegisterAt(app, *cfg.MetricConfiguration.Path) | ||
|
||
return fiberPrometheus.Middleware, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.