-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhistogram.go
181 lines (148 loc) · 5.33 KB
/
histogram.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2016 Circonus, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package circonusgometrics
import (
"sync"
"time"
"github.com/openhistogram/circonusllhist"
"github.com/pkg/errors"
)
// Histogram measures the distribution of a stream of values.
type Histogram struct {
rw sync.RWMutex
hist *circonusllhist.Histogram
name string
}
// TimingWithTags adds a value to a histogram metric with tags
func (m *CirconusMetrics) TimingWithTags(metric string, tags Tags, val float64) {
m.SetHistogramValueWithTags(metric, tags, val)
}
// Timing adds a value to a histogram
func (m *CirconusMetrics) Timing(metric string, val float64) {
m.SetHistogramValue(metric, val)
}
// RecordValueWithTags adds a value to a histogram metric with tags
func (m *CirconusMetrics) RecordValueWithTags(metric string, tags Tags, val float64) {
m.SetHistogramValueWithTags(metric, tags, val)
}
// RecordValue adds a value to a histogram
func (m *CirconusMetrics) RecordValue(metric string, val float64) {
m.SetHistogramValue(metric, val)
}
// RecordDurationWithTags adds a time.Duration to a histogram metric with tags
// (duration is normalized to time.Second, but supports nanosecond granularity).
func (m *CirconusMetrics) RecordDurationWithTags(metric string, tags Tags, val time.Duration) {
m.SetHistogramDurationWithTags(metric, tags, val)
}
// RecordDuration adds a time.Duration to a histogram metric (duration is
// normalized to time.Second, but supports nanosecond granularity).
func (m *CirconusMetrics) RecordDuration(metric string, val time.Duration) {
m.SetHistogramDuration(metric, val)
}
// RecordCountForValueWithTags adds count n for value to a histogram metric with tags
func (m *CirconusMetrics) RecordCountForValueWithTags(metric string, tags Tags, val float64, n int64) {
m.RecordCountForValue(m.MetricNameWithStreamTags(metric, tags), val, n)
}
// RecordCountForValue adds count n for value to a histogram
func (m *CirconusMetrics) RecordCountForValue(metric string, val float64, n int64) {
hist := m.NewHistogram(metric)
m.hm.Lock()
hist.rw.Lock()
err := hist.hist.RecordValues(val, n)
if err != nil {
m.Log.Printf("error recording histogram values (%v)\n", err)
}
hist.rw.Unlock()
m.hm.Unlock()
}
// SetHistogramValueWithTags adds a value to a histogram metric with tags
func (m *CirconusMetrics) SetHistogramValueWithTags(metric string, tags Tags, val float64) {
m.SetHistogramValue(m.MetricNameWithStreamTags(metric, tags), val)
}
// SetHistogramValue adds a value to a histogram
func (m *CirconusMetrics) SetHistogramValue(metric string, val float64) {
hist := m.NewHistogram(metric)
m.hm.Lock()
hist.rw.Lock()
err := hist.hist.RecordValue(val)
if err != nil {
m.Log.Printf("error recording histogram value (%v)\n", err)
}
hist.rw.Unlock()
m.hm.Unlock()
}
// SetHistogramDurationWithTags adds a value to a histogram with tags
func (m *CirconusMetrics) SetHistogramDurationWithTags(metric string, tags Tags, val time.Duration) {
m.SetHistogramDuration(m.MetricNameWithStreamTags(metric, tags), val)
}
// SetHistogramDuration adds a value to a histogram
func (m *CirconusMetrics) SetHistogramDuration(metric string, val time.Duration) {
hist := m.NewHistogram(metric)
m.hm.Lock()
hist.rw.Lock()
err := hist.hist.RecordDuration(val)
if err != nil {
m.Log.Printf("error recording histogram duration (%v)\n", err)
}
hist.rw.Unlock()
m.hm.Unlock()
}
// RemoveHistogramWithTags removes a histogram metric with tags
func (m *CirconusMetrics) RemoveHistogramWithTags(metric string, tags Tags) {
m.RemoveHistogram(m.MetricNameWithStreamTags(metric, tags))
}
// RemoveHistogram removes a histogram
func (m *CirconusMetrics) RemoveHistogram(metric string) {
m.hm.Lock()
defer m.hm.Unlock()
delete(m.histograms, metric)
}
// NewHistogramWithTags returns a histogram metric with tags instance
func (m *CirconusMetrics) NewHistogramWithTags(metric string, tags Tags) *Histogram {
return m.NewHistogram(m.MetricNameWithStreamTags(metric, tags))
}
// NewHistogram returns a histogram instance.
func (m *CirconusMetrics) NewHistogram(metric string) *Histogram {
m.hm.Lock()
defer m.hm.Unlock()
if hist, ok := m.histograms[metric]; ok {
return hist
}
hist := &Histogram{
name: metric,
hist: circonusllhist.New(),
}
m.histograms[metric] = hist
return hist
}
// GetHistogramTest returns the current value for a histogram. (note: it is a function specifically for "testing", disable automatic submission during testing.)
func (m *CirconusMetrics) GetHistogramTest(metric string) ([]string, error) {
m.hm.Lock()
defer m.hm.Unlock()
if hist, ok := m.histograms[metric]; ok {
hist.rw.Lock()
defer hist.rw.Unlock()
return hist.hist.DecStrings(), nil
}
return []string{""}, errors.Errorf("Histogram metric '%s' not found", metric)
}
// Name returns the name from a histogram instance
func (h *Histogram) Name() string {
h.rw.Lock()
defer h.rw.Unlock()
return h.name
}
// RecordValue records the given value to a histogram instance
func (h *Histogram) RecordValue(v float64) {
h.rw.Lock()
defer h.rw.Unlock()
_ = h.hist.RecordValue(v)
}
// RecordDuration records the given time.Duration to a histogram instance.
// RecordDuration normalizes the value to seconds.
func (h *Histogram) RecordDuration(v time.Duration) {
h.rw.Lock()
defer h.rw.Unlock()
_ = h.hist.RecordDuration(v)
}