forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabels.go
52 lines (49 loc) · 1.46 KB
/
labels.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package collectd // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/collectd"
import (
"strings"
)
// LabelsFromName tries to pull out dimensions out of name in the format
// "name[k=v,f=x]-more_name".
// For the example above it would return "name-more_name" and extract dimensions
// (k,v) and (f,x).
// If something unexpected is encountered it returns the original metric name.
//
// The code tries to avoid allocation by using local slices and avoiding calls
// to functions like strings.Slice.
func LabelsFromName(val *string) (metricName string, labels map[string]string) {
metricName = *val
index := strings.Index(*val, "[")
if index > -1 {
left := (*val)[:index]
rest := (*val)[index+1:]
index = strings.Index(rest, "]")
if index > -1 {
working := make(map[string]string)
dimensions := rest[:index]
rest = rest[index+1:]
cindex := strings.Index(dimensions, ",")
prev := 0
for {
if cindex < prev {
cindex = len(dimensions)
}
piece := dimensions[prev:cindex]
tindex := strings.Index(piece, "=")
if tindex == -1 || strings.Contains(piece[tindex+1:], "=") {
return
}
working[piece[:tindex]] = piece[tindex+1:]
if cindex == len(dimensions) {
break
}
prev = cindex + 1
cindex = strings.Index(dimensions[prev:], ",") + prev
}
labels = working
metricName = left + rest
}
}
return
}