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"
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
}