From 85fbee9c08094d5cee3a2460055a22d185d36287 Mon Sep 17 00:00:00 2001 From: Shriyansh Kothari Date: Fri, 1 Sep 2023 15:54:10 -0400 Subject: [PATCH] Added a global variable for plygins to share metrics WAN-2321 #time 1h --- plugins/inputs/t128_graphql/t128_graphql.go | 18 ++++++++++++++++++ .../inputs/t128_peer_path/t128_peer_path.go | 4 +++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/plugins/inputs/t128_graphql/t128_graphql.go b/plugins/inputs/t128_graphql/t128_graphql.go index 256ea45d10899..0c9b28f31fa2a 100644 --- a/plugins/inputs/t128_graphql/t128_graphql.go +++ b/plugins/inputs/t128_graphql/t128_graphql.go @@ -16,9 +16,12 @@ import ( "github.com/Jeffail/gabs" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/config" + "github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/plugins/inputs" ) +var T128GraphQLMetrics []telegraf.Metric + const ( //DefaultRequestTimeout is the request timeout if none is configured DefaultRequestTimeout = 5 * time.Second @@ -214,6 +217,21 @@ func (plugin *T128GraphQL) Gather(acc telegraf.Accumulator) error { processedResponse.Tags, ) } + + //Converting fields to [string]interface{} + fieldsInterface := make(map[string]interface{}, len(plugin.Config.Fields)) + for k, v := range plugin.Config.Fields { + fieldsInterface[k] = v + } + + //Metric creation for use by other plugins + metric := metric.New( + plugin.CollectorName, // Measurement name + plugin.Config.Tags, // Tags + fieldsInterface, // Fields + time.Now(), // Timestamp + ) + T128GraphQLMetrics = append(T128GraphQLMetrics, metric) return nil } diff --git a/plugins/inputs/t128_peer_path/t128_peer_path.go b/plugins/inputs/t128_peer_path/t128_peer_path.go index e1d12601bf450..a87b49604224f 100644 --- a/plugins/inputs/t128_peer_path/t128_peer_path.go +++ b/plugins/inputs/t128_peer_path/t128_peer_path.go @@ -80,7 +80,6 @@ func (plugin *T128PeerPath) Init() error { RetryIfNotFound: plugin.RetryIfNotFound, Query: plugin.Query, } - //fmt.Print(plugin.gqlCollector.endpointNotFound) err := plugin.gqlCollector.Init() if err != nil { return err @@ -91,6 +90,9 @@ func (plugin *T128PeerPath) Init() error { // Gather takes in an accumulator and adds the metrics that the Input gathers func (plugin *T128PeerPath) Gather(acc telegraf.Accumulator) error { plugin.gqlCollector.Gather(acc) + for _, metrics := range t128_graphql.T128GraphQLMetrics { + //perform operation + } return nil }