From 772ca6a530385c3bcac79a4239f9a81420edef63 Mon Sep 17 00:00:00 2001 From: Lukasz Mierzwa Date: Fri, 11 Feb 2022 15:04:23 +0000 Subject: [PATCH] Fix promapi metric usage --- CHANGELOG.md | 7 +++++++ internal/promapi/config.go | 4 ++-- internal/promapi/query.go | 6 +++--- internal/promapi/range.go | 6 +++--- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index adebbdbb..55bca275 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/internal/promapi/config.go b/internal/promapi/config.go index d42063e1..79424bb4 100644 --- a/internal/promapi/config.go +++ b/internal/promapi/config.go @@ -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) } diff --git a/internal/promapi/query.go b/internal/promapi/query.go index 4fd0ae03..00908089 100644 --- a/internal/promapi/query.go +++ b/internal/promapi/query.go @@ -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) @@ -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 } @@ -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") diff --git a/internal/promapi/range.go b/internal/promapi/range.go index d6b86c79..a584d9d7 100644 --- a/internal/promapi/range.go +++ b/internal/promapi/range.go @@ -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, @@ -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") @@ -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")