Skip to content

Commit

Permalink
addressed PR comments
Browse files Browse the repository at this point in the history
WAN-2321 #time 10m
  • Loading branch information
shriyanshk128T committed Sep 7, 2023
1 parent a0629fe commit 2dbbad2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 42 deletions.
41 changes: 22 additions & 19 deletions plugins/inputs/t128_graphql/t128_graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
Expand Down Expand Up @@ -145,11 +146,10 @@ func (plugin *T128GraphQL) Gather(acc telegraf.Accumulator) error {
return nil
}
processedResponses, err := plugin.MakeRequest()
if err != nil && processedResponses == nil {
acc.AddError(err)
return nil
} else if err != nil && processedResponses != nil {
acc.AddError(err)
if err != nil {
for _, err := range err {
acc.AddError(err)
}
}
for _, processedResponse := range processedResponses {
acc.AddFields(
Expand All @@ -163,17 +163,19 @@ func (plugin *T128GraphQL) Gather(acc telegraf.Accumulator) error {
}

// MakeRequest can be used by other plugins using GraphQL functionality
func (plugin *T128GraphQL) MakeRequest() ([]*ProcessedResponse, error) {
var errstrings []string
func (plugin *T128GraphQL) MakeRequest() ([]*ProcessedResponse, []error) {
var errorstrings []error

request, err := plugin.createRequest()
if err != nil {
return nil, fmt.Errorf("failed to create a request for query %s: %w", plugin.Query, err)
errorstrings = append(errorstrings, fmt.Errorf("failed to create a request for query %s: %w", plugin.Query, err))
return nil, errorstrings
}

response, err := plugin.client.Do(request)
if err != nil {
return nil, fmt.Errorf("failed to make graphQL request for collector %s: %w", plugin.CollectorName, err)
errorstrings = append(errorstrings, fmt.Errorf("failed to make graphQL request for collector %s: %w", plugin.CollectorName, err))
return nil, errorstrings
}
defer response.Body.Close()

Expand All @@ -185,29 +187,30 @@ func (plugin *T128GraphQL) MakeRequest() ([]*ProcessedResponse, error) {
if response.StatusCode < 200 || response.StatusCode >= 300 {
template := fmt.Sprintf("status code %d not OK for collector ", response.StatusCode) + plugin.CollectorName + ": %s"
for _, err := range decodeAndReportJSONErrors(message, template) {
errstrings = append(errstrings, err.Error())
errorstrings = append(errorstrings, err)
}
return nil, fmt.Errorf(strings.Join(errstrings, "\n"))
return nil, errorstrings
}

//decode json
jsonParsed, err := gabs.ParseJSON(message)
if err != nil {
return nil, fmt.Errorf("invalid json response for collector %s: %w", plugin.CollectorName, err)
errorstrings = append(errorstrings, fmt.Errorf("invalid json response for collector %s: %w", plugin.CollectorName, err))
return nil, errorstrings
}

//look for other errors in response
exists := jsonParsed.Exists("errors")
if exists {
template := fmt.Sprintf("found errors in response for collector %s", plugin.CollectorName) + ": %s"
for _, err := range decodeAndReportJSONErrors(message, template) {
errstrings = append(errstrings, err.Error())
errorstrings = append(errorstrings, err)

if strings.Contains(err.Error(), "returned a 404") {
plugin.endpointNotFound = true

if !plugin.RetryIfNotFound {
errstrings = append(errstrings, "collector configured to not retry when endpoint not found (404), stopping queries")
errorstrings = append(errorstrings, errors.New("collector configured to not retry when endpoint not found (404), stopping queries"))
}
}
}
Expand All @@ -216,15 +219,15 @@ func (plugin *T128GraphQL) MakeRequest() ([]*ProcessedResponse, error) {
//look for empty response
dataExists := jsonParsed.Exists("data")
if !dataExists {
errorMessage := fmt.Errorf("no data found in response for collector %s", plugin.CollectorName)
errstrings = append(errstrings, errorMessage.Error())
return nil, fmt.Errorf(strings.Join(errstrings, ", "))
errorstrings = append(errorstrings, fmt.Errorf("no data found in response for collector %s", plugin.CollectorName))
return nil, errorstrings
}
processedResponses, err := ProcessResponse(jsonParsed, plugin.CollectorName, plugin.Config.Fields, plugin.Config.Tags)
if err != nil {
return nil, err
errorstrings = append(errorstrings, err)
return nil, errorstrings
} else if dataExists && exists {
return processedResponses, fmt.Errorf(strings.Join(errstrings, ", "))
return processedResponses, errorstrings
} else {
return processedResponses, nil
}
Expand Down
13 changes: 9 additions & 4 deletions plugins/inputs/t128_graphql/t128_graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ var CollectorTestCases = []struct {
}`},
ExpectedMetrics: nil,
ExpectedErrors: []string{
"found errors in response for collector test-collector: Cannot query field \"invalid-field\" on type \"ArpEntryType\"., no data found in response for collector test-collector",
"found errors in response for collector test-collector: Cannot query field \"invalid-field\" on type \"ArpEntryType\".",
"no data found in response for collector test-collector",
},
ExpectedRequests: []int{1},
},
Expand All @@ -157,8 +158,10 @@ var CollectorTestCases = []struct {
}`},
ExpectedMetrics: nil,
ExpectedErrors: []string{
"found errors in response for collector test-collector: highwayManager@CHSSDWCond01CHI.CHSSDWCondMD returned a 404, no data found in response for collector test-collector",
"found errors in response for collector test-collector: highwayManager@CHSSDWCond01CHI.CHSSDWCondMD returned a 404, no data found in response for collector test-collector",
"found errors in response for collector test-collector: highwayManager@CHSSDWCond01CHI.CHSSDWCondMD returned a 404",
"no data found in response for collector test-collector",
"found errors in response for collector test-collector: highwayManager@CHSSDWCond01CHI.CHSSDWCondMD returned a 404",
"no data found in response for collector test-collector",
},
RetryIfNotFound: true,
ExpectedRequests: []int{1, 2},
Expand All @@ -178,7 +181,9 @@ var CollectorTestCases = []struct {
}`},
ExpectedMetrics: nil,
ExpectedErrors: []string{
"found errors in response for collector test-collector: highwayManager@CHSSDWCond01CHI.CHSSDWCondMD returned a 404, collector configured to not retry when endpoint not found (404), stopping queries, no data found in response for collector test-collector",
"found errors in response for collector test-collector: highwayManager@CHSSDWCond01CHI.CHSSDWCondMD returned a 404",
"no data found in response for collector test-collector",
"collector configured to not retry when endpoint not found (404), stopping queries",
},
RetryIfNotFound: false,
ExpectedRequests: []int{1, 1},
Expand Down
4 changes: 0 additions & 4 deletions plugins/inputs/t128_peer_path/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ The peer path input plugin collects data from a 128T instance via graphQL.
## A socket to use for retrieving data - unused by default
# unix_socket = "/var/run/128technology/web-server.sock"

## Required. The path to a point in the graphQL tree from which extract_fields and extract_tags will
## be specified.
# entry_point = "allRouters/nodes/paths"

## Amount of time allowed before the client cancels the HTTP request
# timeout = "5s"

Expand Down
70 changes: 55 additions & 15 deletions plugins/inputs/t128_peer_path/t128_peer_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,41 @@ const (

//timeoutDeadlineDiff is the difference between plugin.Timeout and the request deadline header
timeoutDeadlineDiff = 1 * time.Second

peerPathQuery = `query {
allPeers{
nodes{
paths{
adjacentAddress
deviceInterface
enabled
networkInterface
node
status
vlan
}
routerName
}
}
}`

peerPathHostnameQuery = `query {
allPeers{
nodes{
paths{
adjacentAddress
adjacentHostname
deviceInterface
enabled
networkInterface
node
status
vlan
}
routerName
}
}
}`
)

// T128PeerPath is an input for metrics of a 128T router instance
Expand All @@ -30,7 +65,9 @@ type T128PeerPath struct {
}

var peerPathEntryPoint = "allPeers/nodes/paths"

var peerPathFields = map[string]string{"status": "status", "enabled": "enabled"}

var peerPathTags = map[string]string{
"node": "node",
"adjacentAddress": "adjacentAddress",
Expand All @@ -40,9 +77,15 @@ var peerPathTags = map[string]string{
"routerName": "allPeers/nodes/routerName",
}

var peerPathHostnameQuery = "query {\nallPeers{\nnodes{\npaths{\nadjacentAddress\nadjacentHostname\ndeviceInterface\nenabled\nnetworkInterface\nnode\nstatus\nvlan}\nrouterName}}}"

var peerPathQuery = "query {\nallPeers{\nnodes{\npaths{\nadjacentAddress\ndeviceInterface\nenabled\nnetworkInterface\nnode\nstatus\nvlan}\nrouterName}}}"
var peerPathHostnameTags = map[string]string{
"node": "node",
"adjacentAddress": "adjacentAddress",
"adjacentHostname": "adjacentHostname",
"deviceInterface": "deviceInterface",
"networkInterface": "networkInterface",
"vlan": "vlan",
"routerName": "allPeers/nodes/routerName",
}

var sampleConfig = `
[[inputs.t128_peer_path]]
Expand Down Expand Up @@ -77,23 +120,21 @@ func (*T128PeerPath) Description() string {
func (plugin *T128PeerPath) Init() error {

var query string
var tags map[string]string
if plugin.ExcludeHostname {
if _, found := peerPathTags["adjacentHostname"]; found {
delete(peerPathTags, "adjacentHostname")
}
query = peerPathQuery
tags = peerPathTags
} else {
if _, found := peerPathTags["adjacentHostname"]; !found {
peerPathTags["adjacentHostname"] = "adjacentHostname"
}
query = peerPathHostnameQuery
tags = peerPathHostnameTags
}
plugin.gqlCollector = &t128_graphql.T128GraphQL{
CollectorName: plugin.CollectorName,
BaseURL: plugin.BaseURL,
UnixSocket: plugin.UnixSocket,
EntryPoint: peerPathEntryPoint,
Fields: peerPathFields,
Tags: peerPathTags,
Tags: tags,
Timeout: plugin.Timeout,
RetryIfNotFound: plugin.RetryIfNotFound,
Query: query,
Expand All @@ -111,11 +152,10 @@ func (plugin *T128PeerPath) Gather(acc telegraf.Accumulator) error {
return nil
}
processedResponses, err := plugin.gqlCollector.MakeRequest()
if err != nil && processedResponses == nil {
acc.AddError(err)
return nil
} else if err != nil && processedResponses != nil {
acc.AddError(err)
if err != nil {
for _, err := range err {
acc.AddError(err)
}
}
for _, processedResponse := range processedResponses {
for k, v := range processedResponse.Tags {
Expand Down

0 comments on commit 2dbbad2

Please sign in to comment.