Skip to content

Commit

Permalink
refactor: injecting only metric struct and substitution metric and co…
Browse files Browse the repository at this point in the history
…llector files
  • Loading branch information
Abdulsametileri committed Aug 19, 2023
1 parent d0788a8 commit 9ab3263
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 51 deletions.
49 changes: 49 additions & 0 deletions internal/collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package internal

import "github.com/prometheus/client_golang/prometheus"

type Collector struct {
cronsumerMetric *CronsumerMetric

totalRetriedMessagesCounter *prometheus.Desc
totalDiscardedMessagesCounter *prometheus.Desc
}

func NewCollector(cronsumerMetric *CronsumerMetric) *Collector {
return &Collector{
cronsumerMetric: cronsumerMetric,

Check warning on line 14 in internal/collector.go

View check run for this annotation

Codecov / codecov/patch

internal/collector.go#L12-L14

Added lines #L12 - L14 were not covered by tests

totalRetriedMessagesCounter: prometheus.NewDesc(
prometheus.BuildFQName(Name, "retried_messages_total", "current"),
"Total number of retried messages.",
[]string{},
nil,
),
totalDiscardedMessagesCounter: prometheus.NewDesc(
prometheus.BuildFQName(Name, "discarded_messages_total", "current"),
"Total number of discarded messages.",
[]string{},
nil,
),

Check warning on line 27 in internal/collector.go

View check run for this annotation

Codecov / codecov/patch

internal/collector.go#L16-L27

Added lines #L16 - L27 were not covered by tests
}
}

func (s *Collector) Describe(ch chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(s, ch)

Check warning on line 32 in internal/collector.go

View check run for this annotation

Codecov / codecov/patch

internal/collector.go#L31-L32

Added lines #L31 - L32 were not covered by tests
}

func (s *Collector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(
s.totalRetriedMessagesCounter,
prometheus.CounterValue,
float64(s.cronsumerMetric.TotalRetriedMessagesCounter),
[]string{}...,
)

Check warning on line 41 in internal/collector.go

View check run for this annotation

Codecov / codecov/patch

internal/collector.go#L35-L41

Added lines #L35 - L41 were not covered by tests

ch <- prometheus.MustNewConstMetric(
s.totalDiscardedMessagesCounter,
prometheus.CounterValue,
float64(s.cronsumerMetric.TotalDiscardedMessagesCounter),
[]string{}...,
)

Check warning on line 48 in internal/collector.go

View check run for this annotation

Codecov / codecov/patch

internal/collector.go#L43-L48

Added lines #L43 - L48 were not covered by tests
}
3 changes: 2 additions & 1 deletion internal/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ type cronsumer struct {
func NewCronsumer(cfg *kafka.Config, fn kafka.ConsumeFn) kafka.Cronsumer {
cfg.Logger = logger.New(cfg.LogLevel)
c := newKafkaCronsumer(cfg, fn)

Check warning on line 25 in internal/cron.go

View check run for this annotation

Codecov / codecov/patch

internal/cron.go#L25

Added line #L25 was not covered by tests

return &cronsumer{
cron: gocron.New(),
consumer: c,
cfg: cfg,
metricCollectors: []prometheus.Collector{NewCollector(*c)},
metricCollectors: []prometheus.Collector{NewCollector(c.metric)},

Check warning on line 31 in internal/cron.go

View check run for this annotation

Codecov / codecov/patch

internal/cron.go#L28-L31

Added lines #L28 - L31 were not covered by tests
}
}

Expand Down
50 changes: 0 additions & 50 deletions internal/metric.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,8 @@
package internal

import "github.com/prometheus/client_golang/prometheus"

const Name = "kafka_cronsumer_"

type Collector struct {
kafkaCronsumer kafkaCronsumer

totalRetriedMessagesCounter *prometheus.Desc
totalDiscardedMessagesCounter *prometheus.Desc
}

func (s *Collector) Describe(ch chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(s, ch)
}

func (s *Collector) Collect(ch chan<- prometheus.Metric) {
producerMetric := s.kafkaCronsumer.GetMetric()

ch <- prometheus.MustNewConstMetric(
s.totalRetriedMessagesCounter,
prometheus.CounterValue,
float64(producerMetric.TotalRetriedMessagesCounter),
[]string{}...,
)

ch <- prometheus.MustNewConstMetric(
s.totalDiscardedMessagesCounter,
prometheus.CounterValue,
float64(producerMetric.TotalDiscardedMessagesCounter),
[]string{}...,
)
}

type CronsumerMetric struct {
TotalRetriedMessagesCounter int64
TotalDiscardedMessagesCounter int64
}

func NewCollector(kafkaCronsumer kafkaCronsumer) *Collector {
return &Collector{
kafkaCronsumer: kafkaCronsumer,

totalRetriedMessagesCounter: prometheus.NewDesc(
prometheus.BuildFQName(Name, "retried_messages_total", "current"),
"Total number of retried messages.",
[]string{},
nil,
),
totalDiscardedMessagesCounter: prometheus.NewDesc(
prometheus.BuildFQName(Name, "discarded_messages_total", "current"),
"Total number of discarded messages.",
[]string{},
nil,
),
}
}

0 comments on commit 9ab3263

Please sign in to comment.