-
Notifications
You must be signed in to change notification settings - Fork 204
/
metric_decoration.go
111 lines (95 loc) · 2.72 KB
/
metric_decoration.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
package cloudwatch
import (
"fmt"
"log"
"strings"
)
type MetricDecorationConfig struct {
Category string `toml:"category"`
Metric string `toml:"name"`
Rename string `toml:"rename"`
Unit string `toml:"unit"`
}
var supportedUnits = []string{"Seconds", "Microseconds", "Milliseconds", "Bytes", "Kilobytes", "Megabytes",
"Gigabytes", "Terabytes", "Bits", "Kilobits", "Megabits", "Gigabits", "Terabits",
"Percent", "Count", "Bytes/Second", "Kilobytes/Second", "Megabytes/Second",
"Gigabytes/Second", "Terabytes/Second", "Bits/Second", "Kilobits/Second",
"Megabits/Second", "Gigabits/Second", "Terabits/Second", "Count/Second", "None"}
func NewMetricDecorations(metricConfigs []MetricDecorationConfig) (*MetricDecorations, error) {
result := &MetricDecorations{
decorationNames: make(map[string]map[string]string),
decorationUnits: make(map[string]map[string]string),
}
for k, v := range defaultUnits {
res := strings.SplitN(k, "_", 2)
if len(res) != 2 {
return result, fmt.Errorf("invalid default unit format in default_unit config")
}
err := result.addDecorations(res[0], res[1], "", v)
if err != nil {
return result, err
}
}
for _, metricConfig := range metricConfigs {
err := result.addDecorations(metricConfig.Category, metricConfig.Metric, metricConfig.Rename, metricConfig.Unit)
if err != nil {
return result, err
}
}
return result, nil
}
type MetricDecorations struct {
decorationNames map[string]map[string]string
decorationUnits map[string]map[string]string
}
func (m *MetricDecorations) getUnit(category string, metric string) string {
if val, ok := m.decorationUnits[category]; ok {
return val[metric]
}
return ""
}
func (m *MetricDecorations) getRename(category string, metric string) string {
if val, ok := m.decorationNames[category]; ok {
return val[metric]
}
return ""
}
func isUnitInvalid(unit string) bool {
if unit == "" {
return false
}
for _, v := range supportedUnits {
if v == unit {
return false
}
}
return true
}
func (m *MetricDecorations) addDecorations(category string, name string, rename string, unit string) error {
if category == "" || name == "" {
log.Println("W! Metric config miss key identification... ")
return nil
}
if rename != "" {
val, ok := m.decorationNames[category]
if !ok {
val = make(map[string]string)
m.decorationNames[category] = val
}
val[name] = rename
}
if unit != "" {
if isUnitInvalid(unit) {
return fmt.Errorf("detect unsupported unit")
}
val, ok := m.decorationUnits[category]
if !ok {
val = make(map[string]string)
m.decorationUnits[category] = val
}
val[name] = unit
}
return nil
}