feat(inputs.gnmi): Allow canonical field names (#13326)
This commit is contained in:
parent
60ee14d50b
commit
8f07761cba
|
|
@ -52,6 +52,9 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
|
||||||
## gRPC Maximum Message Size
|
## gRPC Maximum Message Size
|
||||||
# max_msg_size = "4MB"
|
# max_msg_size = "4MB"
|
||||||
|
|
||||||
|
## Enable to get the canonical path as field-name
|
||||||
|
# canonical_field_names = false
|
||||||
|
|
||||||
## enable client-side TLS and define CA to authenticate the device
|
## enable client-side TLS and define CA to authenticate the device
|
||||||
# enable_tls = false
|
# enable_tls = false
|
||||||
# tls_ca = "/etc/telegraf/ca.pem"
|
# tls_ca = "/etc/telegraf/ca.pem"
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,7 @@ type GNMI struct {
|
||||||
Redial config.Duration `toml:"redial"`
|
Redial config.Duration `toml:"redial"`
|
||||||
MaxMsgSize config.Size `toml:"max_msg_size"`
|
MaxMsgSize config.Size `toml:"max_msg_size"`
|
||||||
Trace bool `toml:"dump_responses"`
|
Trace bool `toml:"dump_responses"`
|
||||||
|
CanonicalFieldNames bool `toml:"canonical_field_names"`
|
||||||
EnableTLS bool `toml:"enable_tls"`
|
EnableTLS bool `toml:"enable_tls"`
|
||||||
Log telegraf.Logger `toml:"-"`
|
Log telegraf.Logger `toml:"-"`
|
||||||
internaltls.ClientConfig
|
internaltls.ClientConfig
|
||||||
|
|
@ -192,15 +193,18 @@ func (c *GNMI) Start(acc telegraf.Accumulator) error {
|
||||||
for _, addr := range c.Addresses {
|
for _, addr := range c.Addresses {
|
||||||
go func(addr string) {
|
go func(addr string) {
|
||||||
defer c.wg.Done()
|
defer c.wg.Done()
|
||||||
confHandler := configHandler{
|
|
||||||
|
h := handler{
|
||||||
|
address: addr,
|
||||||
aliases: c.internalAliases,
|
aliases: c.internalAliases,
|
||||||
subscriptions: c.TagSubscriptions,
|
tagsubs: c.TagSubscriptions,
|
||||||
maxSize: int(c.MaxMsgSize),
|
maxMsgSize: int(c.MaxMsgSize),
|
||||||
log: c.Log,
|
|
||||||
trace: c.Trace,
|
|
||||||
vendorExt: c.VendorSpecific,
|
vendorExt: c.VendorSpecific,
|
||||||
|
tagStore: newTagStore(c.TagSubscriptions),
|
||||||
|
trace: c.Trace,
|
||||||
|
canonicalFieldNames: c.CanonicalFieldNames,
|
||||||
|
log: c.Log,
|
||||||
}
|
}
|
||||||
h := newHandler(addr, confHandler)
|
|
||||||
for ctx.Err() == nil {
|
for ctx.Err() == nil {
|
||||||
if err := h.subscribeGNMI(ctx, acc, tlscfg, request); err != nil && ctx.Err() == nil {
|
if err := h.subscribeGNMI(ctx, acc, tlscfg, request); err != nil && ctx.Err() == nil {
|
||||||
acc.AddError(err)
|
acc.AddError(err)
|
||||||
|
|
|
||||||
|
|
@ -36,32 +36,10 @@ type handler struct {
|
||||||
vendorExt []string
|
vendorExt []string
|
||||||
tagStore *tagStore
|
tagStore *tagStore
|
||||||
trace bool
|
trace bool
|
||||||
|
canonicalFieldNames bool
|
||||||
log telegraf.Logger
|
log telegraf.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allow to convey additionnal configuration elements
|
|
||||||
type configHandler struct {
|
|
||||||
aliases map[string]string
|
|
||||||
subscriptions []TagSubscription
|
|
||||||
maxSize int
|
|
||||||
log telegraf.Logger
|
|
||||||
trace bool
|
|
||||||
vendorExt []string
|
|
||||||
}
|
|
||||||
|
|
||||||
func newHandler(addr string, confHandler configHandler) *handler {
|
|
||||||
return &handler{
|
|
||||||
address: addr,
|
|
||||||
aliases: confHandler.aliases,
|
|
||||||
tagsubs: confHandler.subscriptions,
|
|
||||||
maxMsgSize: confHandler.maxSize,
|
|
||||||
vendorExt: confHandler.vendorExt,
|
|
||||||
tagStore: newTagStore(confHandler.subscriptions),
|
|
||||||
trace: confHandler.trace,
|
|
||||||
log: confHandler.log,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SubscribeGNMI and extract telemetry data
|
// SubscribeGNMI and extract telemetry data
|
||||||
func (h *handler) subscribeGNMI(ctx context.Context, acc telegraf.Accumulator, tlscfg *tls.Config, request *gnmiLib.SubscribeRequest) error {
|
func (h *handler) subscribeGNMI(ctx context.Context, acc telegraf.Accumulator, tlscfg *tls.Config, request *gnmiLib.SubscribeRequest) error {
|
||||||
var creds credentials.TransportCredentials
|
var creds credentials.TransportCredentials
|
||||||
|
|
@ -266,6 +244,12 @@ func (h *handler) handleSubscribeResponseUpdate(acc telegraf.Accumulator, respon
|
||||||
// conversion on the key.
|
// conversion on the key.
|
||||||
key = key[len(aliasPath)+1:]
|
key = key[len(aliasPath)+1:]
|
||||||
} else if len(aliasPath) >= len(key) {
|
} else if len(aliasPath) >= len(key) {
|
||||||
|
if h.canonicalFieldNames {
|
||||||
|
// Strip the origin is any for the field names
|
||||||
|
if parts := strings.SplitN(key, ":", 2); len(parts) == 2 {
|
||||||
|
key = parts[1]
|
||||||
|
}
|
||||||
|
} 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 = path.Base(key)
|
||||||
|
|
||||||
|
|
@ -277,6 +261,7 @@ func (h *handler) handleSubscribeResponseUpdate(acc telegraf.Accumulator, respon
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
grouper.Add(name, tags, timestamp, key, v)
|
grouper.Add(name, tags, timestamp, key, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,9 @@
|
||||||
## gRPC Maximum Message Size
|
## gRPC Maximum Message Size
|
||||||
# max_msg_size = "4MB"
|
# max_msg_size = "4MB"
|
||||||
|
|
||||||
|
## Enable to get the canonical path as field-name
|
||||||
|
# canonical_field_names = false
|
||||||
|
|
||||||
## enable client-side TLS and define CA to authenticate the device
|
## enable client-side TLS and define CA to authenticate the device
|
||||||
# enable_tls = false
|
# enable_tls = false
|
||||||
# tls_ca = "/etc/telegraf/ca.pem"
|
# tls_ca = "/etc/telegraf/ca.pem"
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
interfaces,name=eth42,path=openconfig-interfaces:/interfaces/interface,source=127.0.0.1 /interfaces/interface/descr="eth42",/interfaces/interface/config/id=42425u,/interfaces/interface/state/in_pkts=5678u,/interfaces/interface/state/out_pkts=5125u 1673608605875353770
|
||||||
|
|
@ -0,0 +1,79 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"update": {
|
||||||
|
"timestamp": "1673608605875353770",
|
||||||
|
"prefix": {
|
||||||
|
"origin": "openconfig-interfaces",
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "interfaces"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "interface",
|
||||||
|
"key":{"name":"eth42"}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"target": "subscription"
|
||||||
|
},
|
||||||
|
"update": [
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "descr"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"stringVal": "eth42"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "config"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "id"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"uintVal": "42425"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "state"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "in-pkts"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"uintVal": "5678"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": {
|
||||||
|
"elem": [
|
||||||
|
{
|
||||||
|
"name": "state"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "out-pkts"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"val": {
|
||||||
|
"uintVal": "5125"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
[[inputs.gnmi]]
|
||||||
|
addresses = ["dummy"]
|
||||||
|
name_override = "gnmi"
|
||||||
|
redial = "10s"
|
||||||
|
canonical_field_names = true
|
||||||
|
[[inputs.gnmi.subscription]]
|
||||||
|
name = "interfaces"
|
||||||
|
origin = "openconfig-interfaces"
|
||||||
|
path = "/interfaces/interface/descr"
|
||||||
|
subscription_mode = "sample"
|
||||||
|
sample_interval = "10s"
|
||||||
|
[[inputs.gnmi.subscription]]
|
||||||
|
name = "interfaces"
|
||||||
|
origin = "openconfig-interfaces"
|
||||||
|
path = "/interfaces/interface/config/id"
|
||||||
|
subscription_mode = "sample"
|
||||||
|
sample_interval = "10s"
|
||||||
|
[[inputs.gnmi.subscription]]
|
||||||
|
name = "interfaces"
|
||||||
|
origin = "openconfig-interfaces"
|
||||||
|
path = "/interfaces/interface/state/in-pkts"
|
||||||
|
subscription_mode = "sample"
|
||||||
|
sample_interval = "10s"
|
||||||
|
[[inputs.gnmi.subscription]]
|
||||||
|
name = "interfaces"
|
||||||
|
origin = "openconfig-interfaces"
|
||||||
|
path = "/interfaces/interface/state/out-pkts"
|
||||||
|
subscription_mode = "sample"
|
||||||
|
sample_interval = "10s"
|
||||||
Loading…
Reference in New Issue