google-api-go-client: Querying managed Prometheus metrics error: json: cannot unmarshal object into Go struct field HttpBody.data of type string
Maybe it is something I did wrong, I couldn’t find an example on how to query Google Managed Prometheus programmatically.
The error occurs because the Data field of HttpBody of type string which cause decoding JSON to fail while the request has succeeded (status 200). See https://github.com/googleapis/google-api-go-client/blob/main/monitoring/v1/monitoring-gen.go#L1518
After checking the header of the response, Content-Type is application/json
Could you recommend a way to query metrics without a front-end ?
Much appreciated
Environment details
- Programming language: Go
- OS: linux/amd64
- Language runtime version: go1.21.5
- Package version: google.golang.org/api v0.154.0
Steps to reproduce
- Replace “XXX_project_id_XXX” with a project ID where Google Managed Prometheus is enabled.
package main
import (
"context"
"fmt"
"testing"
"time"
monv1 "google.golang.org/api/monitoring/v1"
)
func main() {
fmt.Println("Hello, 世界")
}
func TestPromMetrics(t *testing.T) {
ctx := context.Background()
svc, err := monv1.NewService(ctx)
if err != nil {
t.Fatal(err)
}
s := monv1.NewProjectsLocationPrometheusApiV1Service(svc)
q := &monv1.QueryRangeRequest{
Query: "agones_gameservers_total",
Start: time.Now().UTC().Add(-5 * time.Minute).Format(time.RFC3339),
End: time.Now().UTC().Format(time.RFC3339),
Step: "60s",
Timeout: "60s",
}
resp, err := s.QueryRange("projects/XXX_project_id_XXX", "global", q).Do()
if err != nil {
t.Fatal(err, resp)
}
t.Logf("%#v", resp)
}
- Set up Application Default Credentials
- Run
go test -run TestPromMetrics
The full error message:
--- FAIL: TestPromMetrics (0.23s)
metrics_test.go:39: json: cannot unmarshal object into Go struct field HttpBody.data of type string <nil>
FAIL
exit status 1
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 17 (11 by maintainers)
Commits related to this issue
- fix(gen): change HttpBody.Data from string to any for monitoring:v1 (#2744) fixes: #2304 — committed to renovate-bot/google-api-go-client by quartzmo 2 years ago
- chore(main): release 0.194.0 (#2747) :robot: I have created a release *beep* *boop* --- ## [0.194.0](https://togithub.com/googleapis/google-api-go-client/compare/v0.193.0...v0.194.0) (2024-08-22) ... — committed to googleapis/google-api-go-client by release-please[bot] 2 years ago
The following RPC methods all share the
HttpBodyresponse type. Will it be correct to apply the fix of changingHttpBody.Datafromstringtoanyfor all of them?LabelsQueryQueryExemplarsQueryRangeSeriesValuesMetadataList@veggiemonk Thank you for the link to the incorrect type. This is helpful, I will continue to investigate.