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

  1. 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)
}
  1. Set up Application Default Credentials
  2. 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

Most upvoted comments

The following RPC methods all share the HttpBody response type. Will it be correct to apply the fix of changing HttpBody.Data from string to any for all of them?

  • Labels
  • Query
  • QueryExemplars
  • QueryRange
  • Series
  • Values
  • MetadataList

@veggiemonk Thank you for the link to the incorrect type. This is helpful, I will continue to investigate.