From abb2912cb1e5d47760eb3cb81155b28177fe6069 Mon Sep 17 00:00:00 2001 From: Feroze Mohideen Date: Mon, 5 Feb 2024 17:01:36 -0500 Subject: [PATCH] add telemetry to metrics (#4233) --- internal/kubernetes/prometheus/metrics.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/internal/kubernetes/prometheus/metrics.go b/internal/kubernetes/prometheus/metrics.go index 0d3abc1ff6..f3f13996d9 100644 --- a/internal/kubernetes/prometheus/metrics.go +++ b/internal/kubernetes/prometheus/metrics.go @@ -3,7 +3,6 @@ package prometheus import ( "context" "encoding/json" - "errors" "fmt" "sort" "strings" @@ -269,7 +268,7 @@ func QueryPrometheus( return nil, telemetry.Error(ctx, span, err, "failed to get raw query") } - parsedQuery, err := parseQuery(rawQuery, opts.Metric) + parsedQuery, err := parseQuery(ctx, rawQuery, opts.Metric) if err != nil { return nil, telemetry.Error(ctx, span, err, "failed to parse query") } @@ -322,16 +321,19 @@ type promParsedSingletonQuery struct { Results []promParsedSingletonQueryResult `json:"results"` } -func parseQuery(rawQuery []byte, metric string) ([]*promParsedSingletonQuery, error) { +func parseQuery(ctx context.Context, rawQuery []byte, metric string) ([]*promParsedSingletonQuery, error) { + ctx, span := telemetry.NewSpan(ctx, "parse-query") + defer span.End() + if metric == "nginx:status" { - return parseNginxStatusQuery(rawQuery) + return parseNginxStatusQuery(ctx, rawQuery) } rawQueryObj := &promRawQuery{} err := json.Unmarshal(rawQuery, rawQueryObj) if err != nil { - return nil, err + return nil, telemetry.Error(ctx, span, err, "failed to unmarshal raw query") } res := make([]*promParsedSingletonQuery, 0) @@ -379,12 +381,15 @@ func parseQuery(rawQuery []byte, metric string) ([]*promParsedSingletonQuery, er return res, nil } -func parseNginxStatusQuery(rawQuery []byte) ([]*promParsedSingletonQuery, error) { +func parseNginxStatusQuery(ctx context.Context, rawQuery []byte) ([]*promParsedSingletonQuery, error) { + ctx, span := telemetry.NewSpan(ctx, "parse-nginx-status-query") + defer span.End() + rawQueryObj := &promRawQuery{} err := json.Unmarshal(rawQuery, rawQueryObj) if err != nil { - return nil, err + return nil, telemetry.Error(ctx, span, err, "failed to unmarshal raw query") } singletonResultsByDate := make(map[string]*promParsedSingletonQueryResult, 0) @@ -413,7 +418,8 @@ func parseNginxStatusQuery(rawQuery []byte) ([]*promParsedSingletonQuery, error) case "5xx": singletonResultsByDate[dateKey].StatusCode5xx = values[1] default: - return nil, errors.New("invalid nginx status code") + telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "status-code", Value: result.Metric.StatusCode}) + return nil, telemetry.Error(ctx, span, nil, "unknown status code") } } }