Skip to content

Commit

Permalink
Merge pull request #2 from vinted/feature/add_some_error_handling
Browse files Browse the repository at this point in the history
graphql-exporter: add some error handling
  • Loading branch information
Seitanas authored Oct 11, 2023
2 parents ed1d63e + 9ee952f commit 9d6c785
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/grapql-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {
httpListenAddress string
)
flag.StringVar(&configPath, "configPath", "/etc/graphql-exporter/config.json", "Path to config directory.")
flag.StringVar(&httpListenAddress, "HTTPListenAddress", "127.0.0.1:9353", "Address to bind to.")
flag.StringVar(&httpListenAddress, "HTTPListenAddress", "0.0.0.0:9353", "Address to bind to.")
flag.Parse()
err := config.Init(configPath)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion pkg/graphql/graphql.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package graphql

import (
"fmt"
"github.com/vinted/graphql-exporter/pkg/config"
"io/ioutil"
"log"
Expand All @@ -25,10 +26,13 @@ func GraphqlQuery(query string) ([]byte, error) {
req.Header.Add("Authorization", config.Config.GraphqlAPIToken)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r, err := client.Do(req)
if r.StatusCode != 200 {
return nil, fmt.Errorf(r.Status)
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("Error rading responce body: %s\n", err)
return nil, err
}
return body, nil
}
14 changes: 7 additions & 7 deletions pkg/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func buildValueData(val_hash map[string]interface{}, m string) (string, string,
)
for _, v := range strings.Split(m, ",") {
if _, ok := val_hash[v]; !ok {
error_in_hash = fmt.Errorf("Missing keys in value hash: %s, key: %s", val_hash, v)
error_in_hash = fmt.Errorf("Missing keys in value hash: key: %s", v)
break
}
if val_hash[v] == nil {
Expand Down Expand Up @@ -73,7 +73,7 @@ func buildLabelData(val interface{}, m config.Metric) (map[string]string, error)
label_hash = val.(map[string]interface{})
for _, l := range strings.Split(labels, ",") {
if _, ok := label_hash[l]; !ok {
error_in_hash = fmt.Errorf("Missing keys in label hash: %s, key: %s", label_hash, l)
error_in_hash = fmt.Errorf("Missing keys in label hash. Key: %s", l)
break
}
if label_hash[l] == nil {
Expand Down Expand Up @@ -101,11 +101,11 @@ func getMetrics() ([]Metric, error) {
for _, q := range config.Config.Queries {
result, err := graphql.GraphqlQuery(q.Query)
if err != nil {
log.Printf("Query error: %s\n", err)
return nil, fmt.Errorf("Query error: %s\n", err)
}
err = json.Unmarshal(result, &gql)
if err != nil {
log.Printf("Unmarshal error: %s\n", err)
return nil, fmt.Errorf("Unmarshal error: %s\n", err)
}
data := gql.Data.(map[string]interface{})
for _, m := range q.Metrics {
Expand Down Expand Up @@ -152,9 +152,9 @@ func buildPromDesc(name string, description string, labels map[string]string) *p
}

func (collector *graphqlCollector) Collect(ch chan<- prometheus.Metric) {
metrics, error := getMetrics()
if error != nil {
log.Printf("%s", error)
metrics, err := getMetrics()
if err != nil {
log.Printf("%s", err)
}
for _, metric := range metrics {
var desc *prometheus.Desc
Expand Down

0 comments on commit 9d6c785

Please sign in to comment.