-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Querying managed Prometheus metrics error: json: cannot unmarshal object into Go struct field HttpBody.data of type string #2304
Comments
By forming the query manually it works: package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"path"
"testing"
"time"
monv1 "google.golang.org/api/monitoring/v1"
"google.golang.org/api/option"
apihttp "google.golang.org/api/transport/http"
)
func main() {
fmt.Println("Hello, 世界")
}
const projectID = "XXX"
func TestPromMetrics(t *testing.T) {
opts := []option.ClientOption{
option.WithScopes("https://www.googleapis.com/auth/monitoring.read"),
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
targetx := fmt.Sprintf("https://monitoring.googleapis.com/v1/projects/%s/location/global/prometheus", projectID)
transport, err := apihttp.NewTransport(ctx, http.DefaultTransport, opts...)
if err != nil {
t.Fatal("create HTTP transport", "err", err)
}
client := http.Client{Transport: transport}
u, err := url.Parse(targetx)
if err != nil {
t.Fatal("parse target URL", "err", err)
}
u.Path = path.Join(u.Path, "/api/v1/query_range")
q := &monv1.QueryRangeRequest{
Query: "up",
Start: time.Now().UTC().Add(-5 * time.Minute).Format(time.RFC3339),
End: time.Now().UTC().Format(time.RFC3339),
Step: "60s",
Timeout: "60s",
// ForceSendFields: nil,
// NullFields: nil,
}
body, err := json.Marshal(q)
if err != nil {
t.Fatal("marshal query", "err", err)
}
newReq, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), bytes.NewReader(body))
resp, err := client.Do(newReq)
if err != nil {
t.Fatal("do request", "err", err)
}
defer resp.Body.Close()
res, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal("read response", "err", err)
}
t.Log(string(res))
} |
Thank you for reporting this issue.
I looked through the samples in https://github.com/GoogleCloudPlatform/golang-samples/tree/main/monitoring but couldn't find anything for queries. |
Thank you for providing the manual workaround. Question: Can https://pkg.go.dev/cloud.google.com/go/monitoring be used for this use case? Unfortunately, due to the maintenance mode status of this project, I won't be able to prioritize reproducing this issue right now. If you can still reproduce it, can you by any chance provide a deeper analysis of where the |
That package is for dealing with metrics stored in Cloud Operations as far as I understood. The metrics I used are store in Google Managed Prometheus which, apparently, is a different APIs.
The error occurs because the That's the fix as far as I can tell. |
@veggiemonk Thank you for the link to the incorrect type. This is helpful, I will continue to investigate. |
Closing as this issue has had not recent traffic, feel free to reopen if you are still having this problem. |
Just ran into this issue today so this is still relevant. I would love to use the new /apiv3/v2 for this but it doesn't expose anything that can be used to actually run PromQL. I don't even use managed Prometheus but I still need to query the monitoring API with a PromQL query for my use case. |
@hanneshayashi Are you also using |
Yes, I am using QueryRange and I also ended up using a similar workaround to the above. I will try the other methods as well but I won't be able to before Monday. |
@quartzmo I just tried
|
@hanneshayashi Thanks for verifying that. I doubt there will be any quick fix for this issue, but I will continue to investigate. |
@quartzmo I thought @veggiemonk already identified the cause in his original comment with
v0.154.0 , the current position should be
v0.193.0 )
|
|
Have not looked into the full details, but this seems directly related to #346 |
Looking into this a little more a quick fix for this would be to change HttpBody.Data to |
The following RPC methods all share the
|
This seems correct to me. proceeding with PR |
🤖 I have created a release *beep* *boop* --- ## [0.194.0](https://github.com/googleapis/google-api-go-client/compare/v0.193.0...v0.194.0) (2024-08-22) ### Features * **all:** Auto-regenerate discovery clients ([#2746](https://github.com/googleapis/google-api-go-client/issues/2746)) ([5d61f08](https://github.com/googleapis/google-api-go-client/commit/5d61f08943c359fa97c9764f9e7e6592894b251e)) ### Bug Fixes * **gen:** Change HttpBody.Data from string to any for monitoring:v1 ([#2744](https://github.com/googleapis/google-api-go-client/issues/2744)) ([eda6a59](https://github.com/googleapis/google-api-go-client/commit/eda6a594194b59d5089c3bfbe52bd125a542da4e)), refs [#2304](https://github.com/googleapis/google-api-go-client/issues/2304) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
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 ofHttpBody
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#L1518After checking the header of the response,
Content-Type
isapplication/json
Could you recommend a way to query metrics without a front-end ?
Much appreciated
Environment details
Steps to reproduce
go test -run TestPromMetrics
The full error message:
The text was updated successfully, but these errors were encountered: