Fix: metadata API using wrong field names (#13633)

Fix is to add json tags to `Metadata` struct. Absence of these tags
causes Go to use the field name, which starts with an upper-case
letter and breaks the protocol.

Extend tests to verify the JSON response.

Signed-off-by: ismail simsek <ismailsimsek09@gmail.com>
This commit is contained in:
ismail simsek 2024-02-26 10:53:39 +01:00 committed by Bryan Boreham
parent 814b920e8a
commit eecaa71ff1
2 changed files with 21 additions and 4 deletions

View file

@ -17,7 +17,7 @@ import "github.com/prometheus/common/model"
// Metadata stores a series' metadata information. // Metadata stores a series' metadata information.
type Metadata struct { type Metadata struct {
Type model.MetricType Type model.MetricType `json:"type"`
Unit string Unit string `json:"unit"`
Help string Help string `json:"help"`
} }

View file

@ -1066,6 +1066,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
response interface{} response interface{}
responseLen int responseLen int
responseMetadataTotal int responseMetadataTotal int
responseAsJSON string
errType errorType errType errorType
sorter func(interface{}) sorter func(interface{})
metadata []targetMetadata metadata []targetMetadata
@ -1736,6 +1737,9 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
"prometheus_engine_query_duration_seconds": {{Type: model.MetricTypeSummary, Help: "Query timings", Unit: ""}}, "prometheus_engine_query_duration_seconds": {{Type: model.MetricTypeSummary, Help: "Query timings", Unit: ""}},
"go_info": {{Type: model.MetricTypeGauge, Help: "Information about the Go environment.", Unit: ""}}, "go_info": {{Type: model.MetricTypeGauge, Help: "Information about the Go environment.", Unit: ""}},
}, },
responseAsJSON: `{"prometheus_engine_query_duration_seconds":[{"type":"summary","unit":"",
"help":"Query timings"}], "go_info":[{"type":"gauge","unit":"",
"help":"Information about the Go environment."}]}`,
}, },
// With duplicate metadata for a metric that comes from different targets. // With duplicate metadata for a metric that comes from different targets.
{ {
@ -1767,6 +1771,8 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
response: map[string][]metadata.Metadata{ response: map[string][]metadata.Metadata{
"go_threads": {{Type: model.MetricTypeGauge, Help: "Number of OS threads created"}}, "go_threads": {{Type: model.MetricTypeGauge, Help: "Number of OS threads created"}},
}, },
responseAsJSON: `{"go_threads": [{"type":"gauge","unit":"",
"help":"Number of OS threads created"}]}`,
}, },
// With non-duplicate metadata for the same metric from different targets. // With non-duplicate metadata for the same metric from different targets.
{ {
@ -1801,6 +1807,9 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
{Type: model.MetricTypeGauge, Help: "Number of OS threads that were created."}, {Type: model.MetricTypeGauge, Help: "Number of OS threads that were created."},
}, },
}, },
responseAsJSON: `{"go_threads": [{"type":"gauge","unit":"",
"help":"Number of OS threads created"},{"type":"gauge","unit":"",
"help":"Number of OS threads that were created."}]}`,
sorter: func(m interface{}) { sorter: func(m interface{}) {
v := m.(map[string][]metadata.Metadata)["go_threads"] v := m.(map[string][]metadata.Metadata)["go_threads"]
@ -1828,7 +1837,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
{ {
Metric: "prometheus_engine_query_duration_seconds", Metric: "prometheus_engine_query_duration_seconds",
Type: model.MetricTypeSummary, Type: model.MetricTypeSummary,
Help: "Query Timmings.", Help: "Query Timings.",
Unit: "", Unit: "",
}, },
}, },
@ -1884,6 +1893,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
{Type: model.MetricTypeSummary, Help: "A summary of the GC invocation durations."}, {Type: model.MetricTypeSummary, Help: "A summary of the GC invocation durations."},
}, },
}, },
responseAsJSON: `{"go_gc_duration_seconds":[{"help":"A summary of the GC invocation durations.","type":"summary","unit":""}],"go_threads": [{"type":"gauge","unit":"","help":"Number of OS threads created"}]}`,
}, },
// With a limit for the number of metadata per metric and per metric. // With a limit for the number of metadata per metric and per metric.
{ {
@ -2007,6 +2017,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
{Type: model.MetricTypeGauge, Help: "Number of OS threads that were created."}, {Type: model.MetricTypeGauge, Help: "Number of OS threads that were created."},
}, },
}, },
responseAsJSON: `{"go_threads": [{"type":"gauge","unit":"","help":"Number of OS threads created"},{"type":"gauge","unit":"","help":"Number of OS threads that were created."}]}`,
sorter: func(m interface{}) { sorter: func(m interface{}) {
v := m.(map[string][]metadata.Metadata)["go_threads"] v := m.(map[string][]metadata.Metadata)["go_threads"]
@ -2880,6 +2891,12 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
} }
assertAPIResponse(t, res.data, test.response) assertAPIResponse(t, res.data, test.response)
} }
if test.responseAsJSON != "" {
s, err := json.Marshal(res.data)
require.NoError(t, err)
require.JSONEq(t, test.responseAsJSON, string(s))
}
}) })
} }
}) })