Skip to content

Commit

Permalink
Configure timeouts and resilience.
Browse files Browse the repository at this point in the history
Query timeout per individual GraphQL query.
Configure if should attempt to execute as many as possible, or bail out
with the first error.
Rename "cache on error" config variable for better clarity.
  • Loading branch information
vbalys committed Oct 9, 2024
1 parent fee0e4d commit 720dee6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
3 changes: 2 additions & 1 deletion config_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"graphqlAPIToken": "Token SECRET",
"cacheExpire": 0,
"queryTimeout": 60,
"retryOnError": false,
"failFast": false,
"extendCacheOnError": false,
"queries":[
{
"query": "query {device_list {name serial custom_fields}} {{NOW \"-1h\"}}",
Expand Down
15 changes: 8 additions & 7 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
)

type Cfg struct {
GraphqlURL string
GraphqlAPIToken string
CacheExpire int64
QueryTimeout int64
RetryOnError bool
MetricsPrefix string
Queries []Query
MetricsPrefix string
GraphqlURL string
GraphqlAPIToken string
CacheExpire int64
QueryTimeout int64
FailFast bool
ExtendCacheOnError bool
Queries []Query
}

type Query struct {
Expand Down
18 changes: 13 additions & 5 deletions internal/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,21 @@ func (collector *GraphqlCollector) getMetrics() ([]Metric, error) {
result, err := graphql.GraphqlQuery(ctx, q.Query)
cancel()
if err != nil {
slog.Error(fmt.Sprintf("query error: %s", err))
continue
if config.Config.FailFast {
return nil, err
} else {
slog.Error(fmt.Sprintf("query error: %s", err))
continue
}
}
err = json.Unmarshal(result, &gql)
if err != nil {
slog.Error(fmt.Sprintf("unmarshal error: %s", err))
continue
if config.Config.FailFast {
return nil, err
} else {
slog.Error(fmt.Sprintf("unmarshal error: %s", err))
continue
}
}
data := gql.Data.(map[string]interface{})
for _, m := range q.Metrics {
Expand Down Expand Up @@ -167,7 +175,7 @@ func (collector *GraphqlCollector) updateMetrics() error {
defer collector.accessMu.Unlock()
if err != nil {
slog.Error(fmt.Sprintf("error collecting metrics: %s", err))
if !config.Config.RetryOnError {
if config.Config.ExtendCacheOnError {
collector.cachedAt = time.Now().Unix()
}
return err
Expand Down

0 comments on commit 720dee6

Please sign in to comment.