diff --git a/model/metadata/metadata.go b/model/metadata/metadata.go index f6f2827a4..1b7e63e0f 100644 --- a/model/metadata/metadata.go +++ b/model/metadata/metadata.go @@ -17,7 +17,7 @@ import "github.com/prometheus/common/model" // Metadata stores a series' metadata information. type Metadata struct { - Type model.MetricType - Unit string - Help string + Type model.MetricType `json:"type"` + Unit string `json:"unit"` + Help string `json:"help"` } diff --git a/web/api/v1/api_test.go b/web/api/v1/api_test.go index c9ab84087..c3f88e20d 100644 --- a/web/api/v1/api_test.go +++ b/web/api/v1/api_test.go @@ -1066,6 +1066,7 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E response interface{} responseLen int responseMetadataTotal int + responseAsJSON string errType errorType sorter func(interface{}) 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: ""}}, "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. { @@ -1767,6 +1771,8 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E response: map[string][]metadata.Metadata{ "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. { @@ -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."}, }, }, + 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{}) { 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", Type: model.MetricTypeSummary, - Help: "Query Timmings.", + Help: "Query Timings.", 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."}, }, }, + 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. { @@ -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."}, }, }, + 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{}) { 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) } + + if test.responseAsJSON != "" { + s, err := json.Marshal(res.data) + require.NoError(t, err) + require.JSONEq(t, test.responseAsJSON, string(s)) + } }) } })