forked from ruiaylin/pgparser
/
counter.go
124 lines (102 loc) · 3.91 KB
/
counter.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package aggmetric
import (
"sync/atomic"
"github.com/ruiaylin/pgparser/utils/metric"
"github.com/gogo/protobuf/proto"
io_prometheus_client "github.com/prometheus/client_model/go"
)
// AggCounter maintains a value as the sum of its children. The counter will
// report to crdb-internal time series only the aggregate sum of all of its
// children, while its children are additionally exported to prometheus via the
// PrometheusIterable interface.
type AggCounter struct {
g metric.Counter
childSet
}
var _ metric.Iterable = (*AggCounter)(nil)
var _ metric.PrometheusIterable = (*AggCounter)(nil)
var _ metric.PrometheusExportable = (*AggCounter)(nil)
// NewCounter constructs a new AggCounter.
func NewCounter(metadata metric.Metadata, childLabels ...string) *AggCounter {
c := &AggCounter{g: *metric.NewCounter(metadata)}
c.init(childLabels)
return c
}
// GetName is part of the metric.Iterable interface.
func (c *AggCounter) GetName() string { return c.g.GetName() }
// GetHelp is part of the metric.Iterable interface.
func (c *AggCounter) GetHelp() string { return c.g.GetHelp() }
// GetMeasurement is part of the metric.Iterable interface.
func (c *AggCounter) GetMeasurement() string { return c.g.GetMeasurement() }
// GetUnit is part of the metric.Iterable interface.
func (c *AggCounter) GetUnit() metric.Unit { return c.g.GetUnit() }
// GetMetadata is part of the metric.Iterable interface.
func (c *AggCounter) GetMetadata() metric.Metadata { return c.g.GetMetadata() }
// Inspect is part of the metric.Iterable interface.
func (c *AggCounter) Inspect(f func(interface{})) { f(c) }
// GetType is part of the metric.PrometheusExportable interface.
func (c *AggCounter) GetType() *io_prometheus_client.MetricType {
return c.g.GetType()
}
// GetLabels is part of the metric.PrometheusExportable interface.
func (c *AggCounter) GetLabels() []*io_prometheus_client.LabelPair {
return c.g.GetLabels()
}
// ToPrometheusMetric is part of the metric.PrometheusExportable interface.
func (c *AggCounter) ToPrometheusMetric() *io_prometheus_client.Metric {
return c.g.ToPrometheusMetric()
}
// Count returns the aggregate count of all of its current and past children.
func (c *AggCounter) Count() int64 {
return c.g.Count()
}
// AddChild adds a Counter to this AggCounter. This method panics if a Counter
// already exists for this set of labelVals.
func (c *AggCounter) AddChild(labelVals ...string) *Counter {
child := &Counter{
parent: c,
labelValuesSlice: labelValuesSlice(labelVals),
}
c.add(child)
return child
}
// Counter is a child of a AggCounter. When it is incremented, so too is the
// parent. When metrics are collected by prometheus, each of the children will
// appear with a distinct label, however, when cockroach internally collects
// metrics, only the parent is collected.
type Counter struct {
parent *AggCounter
labelValuesSlice
value int64
}
// ToPrometheusMetric constructs a prometheus metric for this Counter.
func (g *Counter) ToPrometheusMetric() *io_prometheus_client.Metric {
return &io_prometheus_client.Metric{
Counter: &io_prometheus_client.Counter{
Value: proto.Float64(float64(g.Value())),
},
}
}
// Destroy disconnects this Counter from its parents. Unlike Gauge.Destroy, it
// does not decrement its value from its parent.
func (g *Counter) Destroy() {
g.parent.remove(g)
}
// Value returns the AggCounter's current value.
func (g *Counter) Value() int64 {
return atomic.LoadInt64(&g.value)
}
// Inc increments the AggCounter's value.
func (g *Counter) Inc(i int64) {
g.parent.g.Inc(i)
atomic.AddInt64(&g.value, i)
}