From 5da796fc9affefd3d7d064b4948cf04ca9eec354 Mon Sep 17 00:00:00 2001 From: Angel Alonso Date: Wed, 29 Jan 2025 13:10:43 +0000 Subject: [PATCH] Also support ajson scripting on the HTTP collector Signed-off-by: Angel Alonso --- pkg/collector/http_collector.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/pkg/collector/http_collector.go b/pkg/collector/http_collector.go index eacb475..1fef37e 100644 --- a/pkg/collector/http_collector.go +++ b/pkg/collector/http_collector.go @@ -19,6 +19,7 @@ const ( HTTPMetricNameLegacy = "http" HTTPEndpointAnnotationKey = "endpoint" HTTPJsonPathAnnotationKey = "json-key" + HTTPJsonEvalAnnotationKey = "json-eval" ) type HTTPCollectorPlugin struct{} @@ -31,14 +32,27 @@ func (p *HTTPCollectorPlugin) NewCollector(_ context.Context, hpa *autoscalingv2 collector := &HTTPCollector{ namespace: hpa.Namespace, } + var ( value string ok bool + + jsonPath string + jsonEval string ) - if value, ok = config.Config[HTTPJsonPathAnnotationKey]; !ok { - return nil, fmt.Errorf("config value %s not found", HTTPJsonPathAnnotationKey) + + if value, ok = config.Config[HTTPJsonPathAnnotationKey]; ok { + jsonPath = value + } + if value, ok = config.Config[HTTPJsonEvalAnnotationKey]; ok { + jsonEval = value + } + if jsonPath == "" && jsonEval == "" { + return nil, fmt.Errorf("config value %s or %s not found", HTTPJsonPathAnnotationKey, HTTPJsonEvalAnnotationKey) + } + if jsonPath != "" && jsonEval != "" { + return nil, fmt.Errorf("config value %s and %s cannot be used together", HTTPJsonPathAnnotationKey, HTTPJsonEvalAnnotationKey) } - jsonPath := value if value, ok = config.Config[HTTPEndpointAnnotationKey]; !ok { return nil, fmt.Errorf("config value %s not found", HTTPEndpointAnnotationKey) @@ -62,7 +76,7 @@ func (p *HTTPCollectorPlugin) NewCollector(_ context.Context, hpa *autoscalingv2 return nil, err } } - jsonPathGetter, err := httpmetrics.NewJSONPathMetricsGetter(httpmetrics.DefaultMetricsHTTPClient(), aggFunc, jsonPath, "") + jsonPathGetter, err := httpmetrics.NewJSONPathMetricsGetter(httpmetrics.DefaultMetricsHTTPClient(), aggFunc, jsonPath, jsonEval) if err != nil { return nil, err }