Skip to content

Commit

Permalink
Merge pull request #4 from juvenn/feat/auto-remove
Browse files Browse the repository at this point in the history
Auto remove metric from registry
  • Loading branch information
juvenn authored Feb 11, 2023
2 parents a3effa1 + 086f39d commit 3cf677f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

* @juvenn
14 changes: 8 additions & 6 deletions emitters/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ func TestReportToFile(t *testing.T) {
stdout := NewStdoutEmitter()
rep, err := exporters.NewReporter(reg, 800*time.Millisecond,
exporters.WithLabels("host", "localhost"),
exporters.WithAutoReset(true),
exporters.WithAutoRemove(true),
exporters.WithEmitters(piper, stdout))
if err != nil {
t.Fatalf("%#v\n", err)
}
rep.Start()
counter := metrics.NewCounter()
reg.Register("req", counter)
counter := metrics.GetOrRegisterCounter("req", reg)
counter.Inc(1)
go func() {
time.Sleep(1 * time.Second)
rep.Close()
}()
time.Sleep(900 * time.Millisecond)
counter = metrics.GetOrRegisterCounter("req", reg)
counter.Inc(2)

scanner := bufio.NewScanner(pr)
data := make([]exporters.Metric, 0, 8)
Expand All @@ -44,7 +46,7 @@ func TestReportToFile(t *testing.T) {
}
data = append(data, *metric)
}
if len(data) != 2 {
if len(data) < 2 {
t.Fatalf("Should report 2 times with last graceful report but got %d", len(data))
}
for _, metric := range data {
Expand All @@ -58,7 +60,7 @@ func TestReportToFile(t *testing.T) {
t.Errorf("Labels.host localhost != %s\n", host)
}
}
if count := data[1].Fields["count"]; count != 0 {
t.Errorf("Counter should be reset to 0, but got %f\n", count)
if val := data[1].Fields["count"]; val != 2 {
t.Errorf("Counter should be reset and incremented, but got %f\n", val)
}
}
8 changes: 1 addition & 7 deletions metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,14 @@ type Metric struct {
Fields map[string]float64 `json:"fields"`
}

func CollectMetric(name string, metric any, reset bool) *Metric {
func CollectMetric(name string, metric any) *Metric {
now := time.Now()
switch metric := metric.(type) {
case metrics.Counter:
ms := metric.Snapshot()
fields := map[string]float64{
"count": float64(ms.Count()),
}
if reset {
metric.Clear()
}
return &Metric{Name: name, Type: TypeCounter, Time: now, Fields: fields}
case metrics.Histogram:
ms := metric.Snapshot()
Expand All @@ -56,9 +53,6 @@ func CollectMetric(name string, metric any, reset bool) *Metric {
"p999": ps[4],
"p9999": ps[5],
}
if reset {
metric.Clear()
}
return &Metric{Name: name, Type: TypeHistogram, Time: now, Fields: fields}
case metrics.Meter:
ms := metric.Snapshot()
Expand Down
26 changes: 15 additions & 11 deletions reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ import (

// A reporter periodically cut metrics and publish to given publishers.
type Reporter struct {
registry metrics.Registry
interval time.Duration // poll and report interval
autoReset bool // auto reset metric such as counter
emitters []Emitter
exit chan struct{} // signal when shutting down
labels map[string]string // global labels attach to each metric
logf func(format string, a ...any)
registry metrics.Registry
interval time.Duration // poll and report interval
autoRemove bool // auto remove metric such as counter
emitters []Emitter
exit chan struct{} // signal when shutting down
labels map[string]string // global labels attach to each metric
logf func(format string, a ...any)
}

func (rep *Reporter) pollMetrics() []*Metric {
points := make([]*Metric, 0, 128)
rep.registry.Each(func(name string, metrik any) {
metric := CollectMetric(name, metrik, rep.autoReset)
metric := CollectMetric(name, metrik)
if rep.autoRemove {
// remove metric to keep zero metrics from hanging all time
rep.registry.Unregister(name)
}
if metric != nil {
if metric.Labels == nil && len(rep.labels) > 0 {
metric.Labels = make(map[string]string)
Expand Down Expand Up @@ -112,10 +116,10 @@ func WithPollInterval(interval time.Duration) Option {
}
}

// Auto reset metric after each report, such as Counter.
func WithAutoReset(flag bool) Option {
// Auto remove metric after each report, such as Counter.
func WithAutoRemove(flag bool) Option {
return func(rep *Reporter) {
rep.autoReset = flag
rep.autoRemove = flag
}
}

Expand Down

0 comments on commit 3cf677f

Please sign in to comment.