feat(inputs.gnmi): Add option to create more descriptive tags (#15278)

This commit is contained in:
Sven Rebhan 2024-05-03 15:09:00 -04:00 committed by GitHub
parent ab6133b123
commit 7966827530
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 1158 additions and 27 deletions

View File

@ -88,6 +88,9 @@ details on how to use them.
## subscription -- use the subscription path
# path_guessing_strategy = "none"
## Prefix tags from path keys with the path element
# prefix_tag_key_with_path = false
## enable client-side TLS and define CA to authenticate the device
# enable_tls = false
# tls_ca = "/etc/telegraf/ca.pem"

View File

@ -38,29 +38,30 @@ var supportedExtensions = []string{"juniper_header"}
// gNMI plugin instance
type GNMI struct {
Addresses []string `toml:"addresses"`
Subscriptions []Subscription `toml:"subscription"`
TagSubscriptions []TagSubscription `toml:"tag_subscription"`
Aliases map[string]string `toml:"aliases"`
Encoding string `toml:"encoding"`
Origin string `toml:"origin"`
Prefix string `toml:"prefix"`
Target string `toml:"target"`
UpdatesOnly bool `toml:"updates_only"`
VendorSpecific []string `toml:"vendor_specific"`
Username config.Secret `toml:"username"`
Password config.Secret `toml:"password"`
Redial config.Duration `toml:"redial"`
MaxMsgSize config.Size `toml:"max_msg_size"`
Trace bool `toml:"dump_responses"`
CanonicalFieldNames bool `toml:"canonical_field_names"`
TrimFieldNames bool `toml:"trim_field_names"`
GuessPathTag bool `toml:"guess_path_tag" deprecated:"1.30.0;use 'path_guessing_strategy' instead"`
GuessPathStrategy string `toml:"path_guessing_strategy"`
EnableTLS bool `toml:"enable_tls" deprecated:"1.27.0;use 'tls_enable' instead"`
KeepaliveTime config.Duration `toml:"keepalive_time"`
KeepaliveTimeout config.Duration `toml:"keepalive_timeout"`
Log telegraf.Logger `toml:"-"`
Addresses []string `toml:"addresses"`
Subscriptions []Subscription `toml:"subscription"`
TagSubscriptions []TagSubscription `toml:"tag_subscription"`
Aliases map[string]string `toml:"aliases"`
Encoding string `toml:"encoding"`
Origin string `toml:"origin"`
Prefix string `toml:"prefix"`
Target string `toml:"target"`
UpdatesOnly bool `toml:"updates_only"`
VendorSpecific []string `toml:"vendor_specific"`
Username config.Secret `toml:"username"`
Password config.Secret `toml:"password"`
Redial config.Duration `toml:"redial"`
MaxMsgSize config.Size `toml:"max_msg_size"`
Trace bool `toml:"dump_responses"`
CanonicalFieldNames bool `toml:"canonical_field_names"`
TrimFieldNames bool `toml:"trim_field_names"`
PrefixTagKeyWithPath bool `toml:"prefix_tag_key_with_path"`
GuessPathTag bool `toml:"guess_path_tag" deprecated:"1.30.0;use 'path_guessing_strategy' instead"`
GuessPathStrategy string `toml:"path_guessing_strategy"`
EnableTLS bool `toml:"enable_tls" deprecated:"1.27.0;use 'tls_enable' instead"`
KeepaliveTime config.Duration `toml:"keepalive_time"`
KeepaliveTimeout config.Duration `toml:"keepalive_timeout"`
Log telegraf.Logger `toml:"-"`
internaltls.ClientConfig
// Internal state
@ -249,6 +250,7 @@ func (c *GNMI) Start(acc telegraf.Accumulator) error {
trace: c.Trace,
canonicalFieldNames: c.CanonicalFieldNames,
trimSlash: c.TrimFieldNames,
tagPathPrefix: c.PrefixTagKeyWithPath,
guessPathStrategy: c.GuessPathStrategy,
log: c.Log,
ClientParameters: keepalive.ClientParameters{

View File

@ -41,6 +41,7 @@ type handler struct {
trace bool
canonicalFieldNames bool
trimSlash bool
tagPathPrefix bool
guessPathStrategy string
log telegraf.Logger
keepalive.ClientParameters
@ -180,7 +181,7 @@ func (h *handler) handleSubscribeResponseUpdate(acc telegraf.Accumulator, respon
for key, val := range headerTags {
tags[key] = val
}
for key, val := range fullPath.Tags() {
for key, val := range fullPath.Tags(h.tagPathPrefix) {
tags[key] = val
}
@ -217,7 +218,7 @@ func (h *handler) handleSubscribeResponseUpdate(acc telegraf.Accumulator, respon
}
// Prepare tags from prefix
fieldTags := field.path.Tags()
fieldTags := field.path.Tags(h.tagPathPrefix)
tags := make(map[string]string, len(headerTags)+len(fieldTags))
for key, val := range headerTags {
tags[key] = val

View File

@ -279,11 +279,15 @@ func (pi *pathInfo) String() string {
return out
}
func (pi *pathInfo) Tags() map[string]string {
func (pi *pathInfo) Tags(pathPrefix bool) map[string]string {
tags := make(map[string]string, len(pi.keyValues))
for _, s := range pi.keyValues {
var prefix string
if pathPrefix && s.name != "" {
prefix = s.name + "_"
}
for k, v := range s.kv {
key := strings.ReplaceAll(k, "-", "_")
key := strings.ReplaceAll(prefix+k, "-", "_")
// Use short-form of key if possible
if _, exists := tags[key]; !exists {

View File

@ -41,6 +41,9 @@
## subscription -- use the subscription path
# path_guessing_strategy = "none"
## Prefix tags from path keys with the path element
# prefix_tag_key_with_path = false
## enable client-side TLS and define CA to authenticate the device
# enable_tls = false
# tls_ca = "/etc/telegraf/ca.pem"

View File

@ -0,0 +1,7 @@
optical_channel,component_name=CM-1,source=127.0.0.1 cpu/openconfig_platform_cpu:utilization/state/avg=13u,cpu/openconfig_platform_cpu:utilization/state/instant=16u,cpu/openconfig_platform_cpu:utilization/state/interval=3600000000000u,cpu/openconfig_platform_cpu:utilization/state/max=17u,cpu/openconfig_platform_cpu:utilization/state/min=8u,name="CM-1",state/memory/available=5041100000u,state/memory/utilized=3099808000u,state/temperature/avg=20.5,state/temperature/instant=20.399999618530273,state/temperature/interval=3600000000000u,state/temperature/max=20.700000762939453,state/temperature/min=20.399999618530273 1714598222912553000
optical_channel,component_name=AP-1,source=127.0.0.1 name="AP-1",state/temperature/avg=34.099998474121094,state/temperature/instant=34.29999923706055,state/temperature/interval=3600000000000u,state/temperature/max=34.599998474121094,state/temperature/min=33.599998474121094 1714598222912553000
optical_channel,component_name=LM-1,source=127.0.0.1 name="LM-1",state/temperature/avg=40.79999923706055,state/temperature/instant=40.599998474121094,state/temperature/interval=3600000000000u,state/temperature/max=41.400001525878906,state/temperature/min=40.400001525878906 1714598222912553000
optical_channel,component_name=LM-1,source=127.0.0.1,subcomponent_name=PORT-1-1 subcomponents/subcomponent/config/name="PORT-1-1",subcomponents/subcomponent/name="PORT-1-1",subcomponents/subcomponent/state/name="PORT-1-1" 1714598222912553000
optical_channel,component_name=LM-1,source=127.0.0.1,subcomponent_name=PORT-1-2 subcomponents/subcomponent/config/name="PORT-1-2",subcomponents/subcomponent/name="PORT-1-2",subcomponents/subcomponent/state/name="PORT-1-2" 1714598222912553000
optical_channel,component_name=LM-1,source=127.0.0.1,subcomponent_name=PORT-1-4 subcomponents/subcomponent/config/name="PORT-1-4",subcomponents/subcomponent/name="PORT-1-4",subcomponents/subcomponent/state/name="PORT-1-4" 1714598222912553000
optical_channel,component_name=LM-1,source=127.0.0.1,subcomponent_name=PORT-1-12 subcomponents/subcomponent/config/name="PORT-1-12",subcomponents/subcomponent/name="PORT-1-12",subcomponents/subcomponent/state/name="PORT-1-12" 1714598222912553000

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,10 @@
[[inputs.gnmi]]
addresses = ["dummy"]
prefix_tag_key_with_path = true
[[inputs.gnmi.subscription]]
name = "optical_channel"
path = "/components/component[name=*]"
subscription_mode = "sample"
origin = "openconfig-platform"
sample_interval = "10s"