Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions plugins/inputs/gnmi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
48 changes: 25 additions & 23 deletions plugins/inputs/gnmi/gnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down
5 changes: 3 additions & 2 deletions plugins/inputs/gnmi/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type handler struct {
trace bool
canonicalFieldNames bool
trimSlash bool
tagPathPrefix bool
guessPathStrategy string
log telegraf.Logger
keepalive.ClientParameters
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -213,7 +214,7 @@ func (h *handler) handleSubscribeResponseUpdate(acc telegraf.Accumulator, respon
// Parse individual update message and create measurements
for _, field := range valueFields {
// 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
Expand Down
8 changes: 6 additions & 2 deletions plugins/inputs/gnmi/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions plugins/inputs/gnmi/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions plugins/inputs/gnmi/testcases/issue_12831/expected.out
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading