12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- // 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
- }
|