|
1 | 1 | package ccloudv2 |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | 4 | "context" |
6 | 5 | "encoding/json" |
7 | | - "fmt" |
8 | 6 | "io" |
9 | 7 | "net/http" |
10 | 8 | "time" |
@@ -51,52 +49,6 @@ func (c *MetricsClient) MetricsDatasetQuery(dataset string, query metricsv2.Quer |
51 | 49 | return c.Version2Api.V2MetricsDatasetQueryPost(c.context(), dataset).QueryRequest(query).Execute() |
52 | 50 | } |
53 | 51 |
|
54 | | -// MetricsDatasetQueryRaw posts a hand-built JSON body to /v2/metrics/{dataset}/query. |
55 | | -// Use this when the request needs a field the typed SDK doesn't expose (e.g. the |
56 | | -// undocumented "time_agg" knob that the Metrics API requires to override the |
57 | | -// gauge MEAN time aggregation; see schema-registry cluster describe). |
58 | | -func (c *MetricsClient) MetricsDatasetQueryRaw(dataset string, body []byte) (*metricsv2.QueryResponse, *http.Response, error) { |
59 | | - cfg := c.GetConfig() |
60 | | - if len(cfg.Servers) == 0 { |
61 | | - return nil, nil, fmt.Errorf("metrics client has no configured server") |
62 | | - } |
63 | | - url := fmt.Sprintf("%s/v2/metrics/%s/query", cfg.Servers[0].URL, dataset) |
64 | | - |
65 | | - req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, url, bytes.NewReader(body)) |
66 | | - if err != nil { |
67 | | - return nil, nil, err |
68 | | - } |
69 | | - req.Header.Set("Content-Type", "application/json") |
70 | | - req.Header.Set("Accept", "application/json") |
71 | | - if token, err := auth.GetDataplaneToken(c.cfg.Context()); err == nil { |
72 | | - req.Header.Set("Authorization", "Bearer "+token) |
73 | | - } |
74 | | - |
75 | | - httpResp, err := cfg.HTTPClient.Do(req) |
76 | | - if err != nil { |
77 | | - return nil, httpResp, err |
78 | | - } |
79 | | - defer httpResp.Body.Close() |
80 | | - |
81 | | - respBody, err := io.ReadAll(httpResp.Body) |
82 | | - if err != nil { |
83 | | - return nil, httpResp, err |
84 | | - } |
85 | | - if httpResp.StatusCode >= 400 { |
86 | | - return nil, httpResp, fmt.Errorf("metrics API returned %d: %s", httpResp.StatusCode, string(respBody)) |
87 | | - } |
88 | | - |
89 | | - var flat flatQueryResponse |
90 | | - if err := json.Unmarshal(respBody, &flat); err != nil { |
91 | | - return nil, httpResp, err |
92 | | - } |
93 | | - points := make([]metricsv2.Point, len(flat.Data)) |
94 | | - for i, p := range flat.Data { |
95 | | - points[i] = metricsv2.Point{Value: p.Value, Timestamp: p.Timestamp} |
96 | | - } |
97 | | - return &metricsv2.QueryResponse{FlatQueryResponse: metricsv2.NewFlatQueryResponse(points)}, httpResp, nil |
98 | | -} |
99 | | - |
100 | 52 | func UnmarshalFlatQueryResponseIfDataSchemaMatchError(err error, metricsResponse *metricsv2.QueryResponse, httpResp *http.Response) error { |
101 | 53 | if !IsDataMatchesMoreThanOneSchemaError(err) { |
102 | 54 | return nil |
|
0 commit comments