forked from yvasiyarov/gorelic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gometrica.go
105 lines (91 loc) · 2.74 KB
/
gometrica.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
package gorelic
import (
"fmt"
metrics "github.com/yvasiyarov/go-metrics"
)
const (
histogramMin = iota
histogramMax
histogramMean
histogramPercentile
histogramStdDev
histogramVariance
noHistogramFunctions
)
type goMetricaDataSource struct {
metrics.Registry
}
func (ds goMetricaDataSource) GetGaugeValue(key string) (float64, error) {
if valueContainer := ds.Get(key); valueContainer == nil {
return 0, fmt.Errorf("metrica with name %s is not registered\n", key)
} else if gauge, ok := valueContainer.(metrics.Gauge); ok {
return float64(gauge.Value()), nil
} else {
return 0, fmt.Errorf("metrica container has unexpected type: %T\n", valueContainer)
}
}
func (ds goMetricaDataSource) GetHistogramValue(key string, statFunction int, percentile float64) (float64, error) {
if valueContainer := ds.Get(key); valueContainer == nil {
return 0, fmt.Errorf("metrica with name %s is not registered\n", key)
} else if histogram, ok := valueContainer.(metrics.Histogram); ok {
switch statFunction {
default:
return 0, fmt.Errorf("unsupported stat function for histogram: %d\n", statFunction)
case histogramMax:
return float64(histogram.Max()), nil
case histogramMin:
return float64(histogram.Min()), nil
case histogramMean:
return float64(histogram.Mean()), nil
case histogramStdDev:
return float64(histogram.StdDev()), nil
case histogramVariance:
return float64(histogram.Variance()), nil
case histogramPercentile:
return float64(histogram.Percentile(percentile)), nil
}
} else {
return 0, fmt.Errorf("metrica container has unexpected type: %T\n", valueContainer)
}
}
type baseGoMetrica struct {
dataSource goMetricaDataSource
basePath string
name string
units string
dataSourceKey string
}
func (metrica *baseGoMetrica) GetName() string {
return metrica.basePath + metrica.name
}
func (metrica *baseGoMetrica) GetUnits() string {
return metrica.units
}
type gaugeMetrica struct {
*baseGoMetrica
}
func (metrica *gaugeMetrica) GetValue() (float64, error) {
return metrica.dataSource.GetGaugeValue(metrica.dataSourceKey)
}
type gaugeIncMetrica struct {
*baseGoMetrica
previousValue float64
}
func (metrica *gaugeIncMetrica) GetValue() (float64, error) {
var value float64
var currentValue float64
var err error
if currentValue, err = metrica.dataSource.GetGaugeValue(metrica.dataSourceKey); err == nil {
value = currentValue - metrica.previousValue
metrica.previousValue = currentValue
}
return value, err
}
type histogramMetrica struct {
*baseGoMetrica
statFunction int
percentileValue float64
}
func (metrica *histogramMetrica) GetValue() (float64, error) {
return metrica.dataSource.GetHistogramValue(metrica.dataSourceKey, metrica.statFunction, metrica.percentileValue)
}