File: counter.go

package info (click to toggle)
golang-github-docker-go-metrics 0.0~git20180209.399ea8c-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, experimental, sid
  • size: 152 kB
  • sloc: makefile: 2
file content (52 lines) | stat: -rw-r--r-- 1,107 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package metrics

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

// Counter is a metrics that can only increment its current count
type Counter interface {
	// Inc adds Sum(vs) to the counter. Sum(vs) must be positive.
	//
	// If len(vs) == 0, increments the counter by 1.
	Inc(vs ...float64)
}

// LabeledCounter is counter that must have labels populated before use.
type LabeledCounter interface {
	WithValues(vs ...string) Counter
}

type labeledCounter struct {
	pc *prometheus.CounterVec
}

func (lc *labeledCounter) WithValues(vs ...string) Counter {
	return &counter{pc: lc.pc.WithLabelValues(vs...)}
}

func (lc *labeledCounter) Describe(ch chan<- *prometheus.Desc) {
	lc.pc.Describe(ch)
}

func (lc *labeledCounter) Collect(ch chan<- prometheus.Metric) {
	lc.pc.Collect(ch)
}

type counter struct {
	pc prometheus.Counter
}

func (c *counter) Inc(vs ...float64) {
	if len(vs) == 0 {
		c.pc.Inc()
	}

	c.pc.Add(sumFloat64(vs...))
}

func (c *counter) Describe(ch chan<- *prometheus.Desc) {
	c.pc.Describe(ch)
}

func (c *counter) Collect(ch chan<- prometheus.Metric) {
	c.pc.Collect(ch)
}