fix(inputs.gnmi): Refactor alias handling (#14553)
This commit is contained in:
parent
435199c4ed
commit
03eaae6812
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"path"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -231,30 +230,34 @@ func (h *handler) handleSubscribeResponseUpdate(acc telegraf.Accumulator, respon
|
||||||
h.emptyNameWarnShown = true
|
h.emptyNameWarnShown = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
aliasInfo := newInfoFromString(aliasPath)
|
||||||
|
|
||||||
// Group metrics
|
// Group metrics
|
||||||
fieldPath := field.path.String()
|
var key string
|
||||||
key := strings.ReplaceAll(fieldPath, "-", "_")
|
|
||||||
if h.canonicalFieldNames {
|
if h.canonicalFieldNames {
|
||||||
// Strip the origin is any for the field names
|
// Strip the origin is any for the field names
|
||||||
if parts := strings.SplitN(key, ":", 2); len(parts) == 2 {
|
if parts := strings.SplitN(strings.ReplaceAll(field.path.String(), "-", "_"), ":", 2); len(parts) == 2 {
|
||||||
key = parts[1]
|
key = parts[1]
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if len(aliasPath) < len(key) && len(aliasPath) != 0 {
|
// If the alias is a subpath of the field path and the alias is
|
||||||
// This may not be an exact prefix, due to naming style
|
// shorter than the full path to avoid an empty key, then strip the
|
||||||
// conversion on the key.
|
// common part of the field is prefixed with the alias path. Note
|
||||||
key = key[len(aliasPath)+1:]
|
// the origins can match or be empty and be considered equal.
|
||||||
} else if len(aliasPath) >= len(key) {
|
if aliasInfo.isSubPathOf(field.path) && len(aliasInfo.segments) < len(field.path.segments) {
|
||||||
|
relative := field.path.segments[len(aliasInfo.segments):len(field.path.segments)]
|
||||||
|
key = strings.Join(relative, "/")
|
||||||
|
} else {
|
||||||
// Otherwise use the last path element as the field key.
|
// Otherwise use the last path element as the field key.
|
||||||
key = path.Base(key)
|
key = field.path.segments[len(field.path.segments)-1]
|
||||||
}
|
}
|
||||||
|
key = strings.ReplaceAll(key, "-", "_")
|
||||||
}
|
}
|
||||||
if h.trimSlash {
|
if h.trimSlash {
|
||||||
key = strings.TrimLeft(key, "/.")
|
key = strings.TrimLeft(key, "/.")
|
||||||
}
|
}
|
||||||
if key == "" {
|
if key == "" {
|
||||||
h.log.Errorf("Invalid empty path %q with alias %q", fieldPath, aliasPath)
|
h.log.Errorf("Invalid empty path %q with alias %q", field.path.String(), aliasPath)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
grouper.Add(name, tags, timestamp, key, field.value)
|
grouper.Add(name, tags, timestamp, key, field.value)
|
||||||
|
|
|
||||||
|
|
@ -187,6 +187,11 @@ func (pi *pathInfo) normalize() {
|
||||||
if len(groups) == 2 {
|
if len(groups) == 2 {
|
||||||
pi.origin = groups[1]
|
pi.origin = groups[1]
|
||||||
pi.segments[0] = pi.segments[0][len(groups[1])+1:]
|
pi.segments[0] = pi.segments[0][len(groups[1])+1:]
|
||||||
|
|
||||||
|
// if we get empty string back, remove the segment
|
||||||
|
if pi.segments[0] == "" {
|
||||||
|
pi.segments = pi.segments[1:]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
ifcounters,name=Ethernet35,path=/interfaces/interface/state/counters,source=127.0.0.1 in_broadcast_pkts=0u,in_discards=0u,in_errors=0u,in_fcs_errors=0u,in_multicast_pkts=0u,in_octets=0u,in_pkts=0u,in_unicast_pkts=0u,out_broadcast_pkts=0u,out_discards=0u,out_errors=0u,out_multicast_pkts=0u,out_octets=0u,out_pkts=0u,out_unicast_pkts=0u 1704442117721474264
|
||||||
|
|
@ -0,0 +1,208 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"update": {
|
||||||
|
"timestamp": "1704442117721474264",
|
||||||
|
"prefix": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "interfaces"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "interface",
|
||||||
|
"key": {
|
||||||
|
"name": "Ethernet35"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "state"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "counters"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"update": [
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "in-broadcast-pkts"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"uintVal": "0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "in-discards"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"uintVal": "0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "in-errors"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"uintVal": "0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "in-fcs-errors"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"uintVal": "0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "in-multicast-pkts"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"uintVal": "0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "in-octets"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"uintVal": "0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "in-pkts"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"uintVal": "0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "in-unicast-pkts"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"uintVal": "0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "out-broadcast-pkts"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"uintVal": "0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "out-discards"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"uintVal": "0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "out-errors"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"uintVal": "0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "out-multicast-pkts"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"uintVal": "0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "out-octets"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"uintVal": "0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "out-pkts"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"uintVal": "0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "out-unicast-pkts"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"uintVal": "0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
[[inputs.gnmi]]
|
||||||
|
addresses = ["dummy"]
|
||||||
|
encoding = "json_ietf"
|
||||||
|
tagexclude = ["path"]
|
||||||
|
|
||||||
|
[inputs.gnmi.tags]
|
||||||
|
test_tag = "test"
|
||||||
|
|
||||||
|
[[inputs.gnmi.subscription]]
|
||||||
|
name = "ifcounters"
|
||||||
|
origin = "openconfig"
|
||||||
|
path = "/interfaces/interface/state/counters"
|
||||||
|
subscription_mode = "sample"
|
||||||
|
sample_interval = "10s"
|
||||||
Loading…
Reference in New Issue