Skip to content

Commit

Permalink
Merge pull request #7 from vinted/feature/rework_structure
Browse files Browse the repository at this point in the history
Rework structure
  • Loading branch information
vbalys authored Oct 9, 2024
2 parents 310a0e0 + b947127 commit 9e158f1
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 95 deletions.
1 change: 1 addition & 0 deletions .go-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.23.0
29 changes: 29 additions & 0 deletions cmd/graphql-exporter/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"flag"
"fmt"
"log/slog"
"os"

"github.com/vinted/graphql-exporter/internal/config"
"github.com/vinted/graphql-exporter/internal/prometheus"
)

func main() {
var (
configPath string
httpListenAddress string
)
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
slog.SetDefault(logger)
flag.StringVar(&configPath, "config_path", "/etc/graphql-exporter/config.json", "Path to config directory.")
flag.StringVar(&httpListenAddress, "http_listen_address", "0.0.0.0:9353", "Address to bind to.")
flag.Parse()
err := config.Init(configPath)
if err != nil {
slog.Error(fmt.Sprintf("failed to read configuration: %s", err))
os.Exit(1)
}
prometheus.Start(httpListenAddress)
}
24 changes: 0 additions & 24 deletions cmd/grapql-exporter/main.go

This file was deleted.

7 changes: 4 additions & 3 deletions config_example.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"GraphqlURL": "http://localhost:8090/graphql/",
"GraphqlAPIToken": "Token SECRET",
"CacheExpire": 0,
"graphqlURL": "http://localhost:8090/graphql/",
"graphqlAPIToken": "Token SECRET",
"cacheExpire": 0,
"metricsPrefix": "graphql_exporter_",
"queries":[
{
"query": "query {device_list {name serial custom_fields}} {{NOW \"-1h\"}}",
Expand Down
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
module github.com/vinted/graphql-exporter

go 1.19
go 1.23

require (
github.com/gorilla/mux v1.8.0
github.com/prometheus/client_golang v1.17.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
Expand Down
6 changes: 4 additions & 2 deletions pkg/config/config.go → internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package config

import (
"encoding/json"
"log"
"fmt"
"log/slog"
"os"
)

type Cfg struct {
GraphqlURL string
GraphqlAPIToken string
CacheExpire int64
MetricsPrefix string
Queries []Query
}

Expand Down Expand Up @@ -54,6 +56,6 @@ func Init(configPath string) error {
Config.GraphqlAPIToken = val
}

log.Printf("Finished reading config.")
slog.Info(fmt.Sprintf("Finished reading config from %s", configPath))
return nil
}
27 changes: 18 additions & 9 deletions pkg/graphql/graphql.go → internal/graphql/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package graphql

import (
"bytes"
"context"
"fmt"
"github.com/vinted/graphql-exporter/pkg/config"
"io/ioutil"
"log"
"io"
"net/http"
"net/url"
"strings"
"text/template"
"time"

"github.com/vinted/graphql-exporter/internal/config"
)

var funcMap = template.FuncMap{
Expand All @@ -20,36 +21,44 @@ var funcMap = template.FuncMap{
},
}

func GraphqlQuery(query string) ([]byte, error) {
func GraphqlQuery(ctx context.Context, query string) ([]byte, error) {
params := url.Values{}
tpl, err := template.New("query").Funcs(funcMap).Parse(query)
if err != nil {
return nil, fmt.Errorf("%s", err)
}

var templateBuffer bytes.Buffer
err = tpl.Execute(&templateBuffer, nil)
if err != nil {
return nil, fmt.Errorf("Template error %s", err)
return nil, fmt.Errorf("template error %s", err)
}

params.Add("query", templateBuffer.String())
u, err := url.ParseRequestURI(config.Config.GraphqlURL)
if err != nil {
log.Printf("Error parsing URL: %s\n", err)
return nil, fmt.Errorf("error parsing URL: %s", err)
}

urlStr := u.String()
client := &http.Client{}
req, err := http.NewRequest(http.MethodPost, urlStr, strings.NewReader(params.Encode()))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, urlStr, strings.NewReader(params.Encode()))
if err != nil {
log.Printf("HTTP requrest error: %s\n", err)
return nil, fmt.Errorf("HTTP request error: %s", err)
}

req.Header.Add("Authorization", config.Config.GraphqlAPIToken)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r, err := client.Do(req)
if err != nil {
return nil, err
}
if r.StatusCode != 200 {
return nil, fmt.Errorf(r.Status)
}

defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 9e158f1

Please sign in to comment.