This repository has been archived by the owner on Jul 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphdef.go
181 lines (160 loc) · 3.84 KB
/
graphdef.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
package mackerel
import (
"errors"
"fmt"
"math"
"strings"
"go.opentelemetry.io/otel/api/core"
"go.opentelemetry.io/otel/api/unit"
export "go.opentelemetry.io/otel/sdk/export/metric"
"github.com/mackerelio/mackerel-client-go"
)
// OpenTelemetry naming
// https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/api-metrics-user.md
const (
UnitDimensionless = unit.Dimensionless
UnitBytes = unit.Bytes
UnitMilliseconds = unit.Milliseconds
metricNameSep = "."
)
// JoinMetricName joins any number of name elements int a single name.
func JoinMetricName(elem ...string) string {
a := make([]string, len(elem))
for i, s := range elem {
a[i] = s
}
return strings.Join(a, metricNameSep)
}
func metricNamePrefix(s string) string {
a := strings.Split(s, metricNameSep)
if len(a) == 0 {
return ""
}
return strings.Join(a[:len(a)-1], metricNameSep)
}
// GraphDefOptions represents options for customizing Mackerel's Graph Definition.
type GraphDefOptions struct {
Name string
Unit unit.Unit
Kind core.NumberKind
Quantiles []float64
}
var errMismatch = errors.New("mismatched metric names")
// NewGraphDef returns Mackerel's Graph Definition. Each names in arguments must be canonicalized.
func NewGraphDef(name string, kind export.MetricKind, opts GraphDefOptions) (*mackerel.GraphDefsParam, error) {
if opts.Unit == "" {
opts.Unit = UnitDimensionless
}
if kind == export.MeasureKind {
name = JoinMetricName(name, "max") // Anything is fine
}
if opts.Name == "" {
opts.Name = metricNamePrefix(name)
}
r := JoinMetricName(opts.Name, "*")
if !MetricName(r).Match(name) {
return nil, errMismatch
}
return &mackerel.GraphDefsParam{
Name: opts.Name,
DisplayName: opts.Name,
Unit: graphUnit(opts.Unit, opts.Kind),
Metrics: []*mackerel.GraphDefsMetric{
{Name: r, DisplayName: metricDisplayName(r)},
},
}, nil
}
func metricDisplayName(name string) string {
a := strings.Split(name, metricNameSep)
if len(a) == 0 {
return ""
}
var n int
for _, s := range a {
if s == "*" {
n++
}
}
if n == 0 {
return a[len(a)-1]
}
return fmt.Sprintf("%%%d", n)
}
// PercentileName returns "percentile_xx".
func PercentileName(q float64) string {
return fmt.Sprintf("percentile_%.0f", math.Floor(q*100))
}
func graphUnit(u unit.Unit, kind core.NumberKind) string {
switch u {
case unit.Bytes:
return "bytes"
case unit.Dimensionless, unit.Milliseconds:
if kind == core.Float64NumberKind {
return "float"
}
return "integer"
default:
if kind == core.Float64NumberKind {
return "float"
}
return "float"
}
}
// CanonicalMetricName returns canonical metric name.
func CanonicalMetricName(s string) string {
s = sanitizeMetricName(s)
if isSystemMetric(s) {
return s
}
return JoinMetricName("custom", s)
}
func sanitizeMetricName(s string) string {
sanitize := func(c rune) rune {
switch {
case c >= '0' && c <= '9':
return c
case c >= 'a' && c <= 'z':
return c
case c >= 'A' && c <= 'Z':
return c
case c == '-' || c == '_' || c == '.' || c == '#' || c == '*':
return c
default:
return '_'
}
}
return strings.Map(sanitize, s)
}
type MetricName string
func (g MetricName) Match(s string) bool {
expr := strings.Split(string(g), metricNameSep)
a := strings.Split(s, metricNameSep)
if len(expr) != len(a) {
return false
}
for i := range expr {
if expr[i] == "#" || expr[i] == "*" {
continue
}
if expr[i] != a[i] {
return false
}
}
return true
}
var systemMetricNames map[MetricName]struct{}
func init() {
systemMetricNames = make(map[MetricName]struct{})
for _, s := range systemMetrics {
systemMetricNames[s] = struct{}{}
}
}
// isSystemMetric returns whether s is system metric in Mackerel.
func isSystemMetric(s string) bool {
for m := range systemMetricNames {
if m.Match(s) {
return true
}
}
return false
}