Also support ajson scripting on the HTTP collector

Signed-off-by: Angel Alonso <angel.alonso@automattic.com>
This commit is contained in:
Angel Alonso
2025-01-29 13:10:43 +00:00
parent 5f6a683d64
commit 5da796fc9a

View File

@ -19,6 +19,7 @@ const (
HTTPMetricNameLegacy = "http" HTTPMetricNameLegacy = "http"
HTTPEndpointAnnotationKey = "endpoint" HTTPEndpointAnnotationKey = "endpoint"
HTTPJsonPathAnnotationKey = "json-key" HTTPJsonPathAnnotationKey = "json-key"
HTTPJsonEvalAnnotationKey = "json-eval"
) )
type HTTPCollectorPlugin struct{} type HTTPCollectorPlugin struct{}
@ -31,14 +32,27 @@ func (p *HTTPCollectorPlugin) NewCollector(_ context.Context, hpa *autoscalingv2
collector := &HTTPCollector{ collector := &HTTPCollector{
namespace: hpa.Namespace, namespace: hpa.Namespace,
} }
var ( var (
value string value string
ok bool 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 { if value, ok = config.Config[HTTPEndpointAnnotationKey]; !ok {
return nil, fmt.Errorf("config value %s not found", HTTPEndpointAnnotationKey) 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 return nil, err
} }
} }
jsonPathGetter, err := httpmetrics.NewJSONPathMetricsGetter(httpmetrics.DefaultMetricsHTTPClient(), aggFunc, jsonPath, "") jsonPathGetter, err := httpmetrics.NewJSONPathMetricsGetter(httpmetrics.DefaultMetricsHTTPClient(), aggFunc, jsonPath, jsonEval)
if err != nil { if err != nil {
return nil, err return nil, err
} }