Skip to content

Commit

Permalink
Fix promapi metric usage
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Feb 11, 2022
1 parent 030c3f4 commit 772ca6a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## v0.11.1

### Fixed

- `pint_prometheus_queries_total` and `pint_prometheus_query_errors_total` metrics
were not incremented correctly.

## v0.11.0

### Added
Expand Down
4 changes: 2 additions & 2 deletions internal/promapi/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ func (p *Prometheus) Config(ctx context.Context) (*PrometheusConfig, error) {
resp, err := p.api.Config(ctx)
if err != nil {
log.Error().Err(err).Str("uri", p.uri).Msg("Failed to query Prometheus configuration")
prometheusQueryErrorsTotal.WithLabelValues(p.name, "/api/v1/status/config", errReason(err))
prometheusQueryErrorsTotal.WithLabelValues(p.name, "/api/v1/status/config", errReason(err)).Inc()
return nil, fmt.Errorf("failed to query Prometheus config: %w", err)
}

var cfg PrometheusConfig
if err = yaml.Unmarshal([]byte(resp.YAML), &cfg); err != nil {
prometheusQueryErrorsTotal.WithLabelValues(p.name, "/api/v1/status/config", errReason(err))
prometheusQueryErrorsTotal.WithLabelValues(p.name, "/api/v1/status/config", errReason(err)).Inc()
return nil, fmt.Errorf("failed to decode config data in %s response: %w", p.uri, err)
}

Expand Down
6 changes: 3 additions & 3 deletions internal/promapi/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (p *Prometheus) Query(ctx context.Context, expr string) (*QueryResult, erro
ctx, cancel := context.WithTimeout(ctx, p.timeout)
defer cancel()

prometheusQueriesTotal.WithLabelValues(p.name, "/api/v1/query")
prometheusQueriesTotal.WithLabelValues(p.name, "/api/v1/query").Inc()
start := time.Now()
result, _, err := p.api.Query(ctx, expr, start)
duration := time.Since(start)
Expand All @@ -46,7 +46,7 @@ func (p *Prometheus) Query(ctx context.Context, expr string) (*QueryResult, erro
Str("uri", p.uri).
Str("query", expr).
Msg("Query failed")
prometheusQueryErrorsTotal.WithLabelValues(p.name, "/api/v1/query", errReason(err))
prometheusQueryErrorsTotal.WithLabelValues(p.name, "/api/v1/query", errReason(err)).Inc()
return nil, err
}

Expand All @@ -60,7 +60,7 @@ func (p *Prometheus) Query(ctx context.Context, expr string) (*QueryResult, erro
qr.Series = vectorVal
default:
log.Error().Str("uri", p.uri).Str("query", expr).Msgf("Query returned unknown result type: %v", result.Type())
prometheusQueryErrorsTotal.WithLabelValues(p.name, "/api/v1/query", "unknown result type")
prometheusQueryErrorsTotal.WithLabelValues(p.name, "/api/v1/query", "unknown result type").Inc()
return nil, fmt.Errorf("unknown result type: %v", result.Type())
}
log.Debug().Str("uri", p.uri).Str("query", expr).Int("series", len(qr.Series)).Msg("Parsed response")
Expand Down
6 changes: 3 additions & 3 deletions internal/promapi/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (p *Prometheus) RangeQuery(ctx context.Context, expr string, start, end tim
rctx, cancel := context.WithTimeout(ctx, p.timeout)
defer cancel()

prometheusQueriesTotal.WithLabelValues(p.name, "/api/v1/query_range")
prometheusQueriesTotal.WithLabelValues(p.name, "/api/v1/query_range").Inc()
r := v1.Range{
Start: start,
End: end,
Expand All @@ -60,7 +60,7 @@ func (p *Prometheus) RangeQuery(ctx context.Context, expr string, start, end tim
Msg("Range query completed")
if err != nil {
log.Error().Err(err).Str("uri", p.uri).Str("query", expr).Msg("Range query failed")
prometheusQueryErrorsTotal.WithLabelValues(p.name, "/api/v1/query_range", errReason(err))
prometheusQueryErrorsTotal.WithLabelValues(p.name, "/api/v1/query_range", errReason(err)).Inc()
if delta, retryOK := canRetry(err, end.Sub(start)); retryOK {
if delta < step*2 {
log.Error().Str("uri", p.uri).Str("query", expr).Msg("No more retries possible")
Expand All @@ -84,7 +84,7 @@ func (p *Prometheus) RangeQuery(ctx context.Context, expr string, start, end tim
qr.Samples = samples
default:
log.Error().Err(err).Str("uri", p.uri).Str("query", expr).Msgf("Range query returned unknown result type: %v", result.Type())
prometheusQueryErrorsTotal.WithLabelValues(p.name, "/api/v1/query_range", "unknown result type")
prometheusQueryErrorsTotal.WithLabelValues(p.name, "/api/v1/query_range", "unknown result type").Inc()
return nil, fmt.Errorf("unknown result type: %v", result.Type())
}
log.Debug().Str("uri", p.uri).Str("query", expr).Int("samples", len(qr.Samples)).Msg("Parsed range response")
Expand Down

0 comments on commit 772ca6a

Please sign in to comment.