-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
109 lines (95 loc) · 2.6 KB
/
util.go
File metadata and controls
109 lines (95 loc) · 2.6 KB
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
package main
import (
"context"
"fmt"
"os"
"regexp"
"time"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/prompb"
)
var invalidMetricNamePattern = regexp.MustCompile(`[^a-zA-Z0-9:_]`)
func fromLabelMatchers(matchers []*prompb.LabelMatcher) ([]*labels.Matcher, error) {
result := make([]*labels.Matcher, 0, len(matchers))
for _, matcher := range matchers {
var m *labels.Matcher
switch matcher.Type {
case prompb.LabelMatcher_EQ:
m = labels.MustNewMatcher(labels.MatchEqual, matcher.Name, matcher.Value)
case prompb.LabelMatcher_NEQ:
m = labels.MustNewMatcher(labels.MatchNotEqual, matcher.Name, matcher.Value)
case prompb.LabelMatcher_RE:
m = labels.MustNewMatcher(labels.MatchRegexp, matcher.Name, "^(?:"+matcher.Value+")$")
case prompb.LabelMatcher_NRE:
m = labels.MustNewMatcher(labels.MatchNotRegexp, matcher.Name, "^(?:"+matcher.Value+")$")
default:
return nil, fmt.Errorf("invalid matcher type")
}
result = append(result, m)
}
return result, nil
}
func isExtendedStatistics(s string) bool {
return s != "Sum" && s != "SampleCount" && s != "Maximum" && s != "Minimum" && s != "Average"
}
var regionCache = ""
func GetDefaultRegion() (string, error) {
var region string
if regionCache != "" {
return regionCache, nil
}
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
return "", err
}
client := imds.NewFromConfig(cfg)
response, err := client.GetRegion(ctx, &imds.GetRegionInput{})
if err != nil {
region = os.Getenv("AWS_REGION")
if region == "" {
region = "us-east-1"
}
} else {
region = response.Region
if region != "" {
regionCache = region
}
}
return region, nil
}
func SafeMetricName(name string) string {
if len(name) == 0 {
return ""
}
name = invalidMetricNamePattern.ReplaceAllString(name, "_")
if '0' <= name[0] && name[0] <= '9' {
name = "_" + name
}
return name
}
type resultMap map[string]*prompb.TimeSeries
func (x resultMap) append(y resultMap) {
for id, yts := range y {
if xts, ok := x[id]; ok {
if (len(xts.Samples) > 0 && len(yts.Samples) > 0) && xts.Samples[0].Timestamp < yts.Samples[0].Timestamp {
xts.Samples = append(xts.Samples, yts.Samples...)
} else {
xts.Samples = append(yts.Samples, xts.Samples...)
}
} else {
x[id] = yts
}
}
}
func (x resultMap) slice() []*prompb.TimeSeries {
s := []*prompb.TimeSeries{}
for _, v := range x {
s = append(s, v)
}
return s
}