forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
132 lines (108 loc) · 3 KB
/
provider.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
125
126
127
128
129
130
131
132
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
/*
Notice: This file has been modified for Hyperledger Fabric SDK Go usage.
Please review third_party pinning scripts and patches for more details.
*/
package statsd
import (
"github.com/go-kit/kit/metrics/statsd"
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/common/metrics"
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/common/metrics/internal/namer"
)
const defaultFormat = "%{#fqname}"
type Provider struct {
Statsd *statsd.Statsd
}
func (p *Provider) NewCounter(o metrics.CounterOpts) metrics.Counter {
if o.StatsdFormat == "" {
o.StatsdFormat = defaultFormat
}
counter := &Counter{
statsdProvider: p.Statsd,
namer: namer.NewCounterNamer(o),
}
if len(o.LabelNames) == 0 {
counter.Counter = p.Statsd.NewCounter(counter.namer.Format(), 1)
}
return counter
}
func (p *Provider) NewGauge(o metrics.GaugeOpts) metrics.Gauge {
if o.StatsdFormat == "" {
o.StatsdFormat = defaultFormat
}
gauge := &Gauge{
statsdProvider: p.Statsd,
namer: namer.NewGaugeNamer(o),
}
if len(o.LabelNames) == 0 {
gauge.Gauge = p.Statsd.NewGauge(gauge.namer.Format())
}
return gauge
}
func (p *Provider) NewHistogram(o metrics.HistogramOpts) metrics.Histogram {
if o.StatsdFormat == "" {
o.StatsdFormat = defaultFormat
}
histogram := &Histogram{
statsdProvider: p.Statsd,
namer: namer.NewHistogramNamer(o),
}
if len(o.LabelNames) == 0 {
histogram.Timing = p.Statsd.NewTiming(histogram.namer.Format(), 1.0)
}
return histogram
}
type Counter struct {
Counter *statsd.Counter
namer *namer.Namer
statsdProvider *statsd.Statsd
}
func (c *Counter) Add(delta float64) {
if c.Counter == nil {
panic("label values must be provided by calling With")
}
c.Counter.Add(delta)
}
func (c *Counter) With(labelValues ...string) metrics.Counter {
name := c.namer.Format(labelValues...)
return &Counter{Counter: c.statsdProvider.NewCounter(name, 1)}
}
type Gauge struct {
Gauge *statsd.Gauge
namer *namer.Namer
statsdProvider *statsd.Statsd
}
func (g *Gauge) Add(delta float64) {
if g.Gauge == nil {
panic("label values must be provided by calling With")
}
g.Gauge.Add(delta)
}
func (g *Gauge) Set(value float64) {
if g.Gauge == nil {
panic("label values must be provided by calling With")
}
g.Gauge.Set(value)
}
func (g *Gauge) With(labelValues ...string) metrics.Gauge {
name := g.namer.Format(labelValues...)
return &Gauge{Gauge: g.statsdProvider.NewGauge(name)}
}
type Histogram struct {
Timing *statsd.Timing
namer *namer.Namer
statsdProvider *statsd.Statsd
}
func (h *Histogram) With(labelValues ...string) metrics.Histogram {
name := h.namer.Format(labelValues...)
return &Histogram{Timing: h.statsdProvider.NewTiming(name, 1)}
}
func (h *Histogram) Observe(value float64) {
if h.Timing == nil {
panic("label values must be provided by calling With")
}
h.Timing.Observe(value)
}