grafana / grafana-openapi-client-go

Grafana OpenAPI Client for Go
Apache License 2.0
49 stars 7 forks source link

`client.Ds.QueryMetricsWithExpressions()` returns empty frames #91

Open troibe opened 7 months ago

troibe commented 7 months ago

I'm trying to use client.Ds.QueryMetricsWithExpressions() to see if my metrics data source can be queried with a custom query from Grafana.

    type Datasource struct {
        Uid string `json:"uid"`
    }

    type Query struct {
        Datasource Datasource `json:"datasource"`
        Expr       string     `json:"expr"`
        RefId      string     `json:"refId"`
    }

    var queries []models.JSON
    queries = append(queries, Query{
        Datasource: Datasource{Uid: dataSource.Payload.UID},
        Expr:       "up",
        RefId:      "A",
    })

    nowTimestamp := "now"
    priorTimestamp := "now-5m"

    metricRequest := &models.MetricRequest{
        From:    &priorTimestamp,
        Queries: queries,
        To:      &nowTimestamp,
    }
    queryResultOK, queryResultMultiStatus, err := client.Ds.QueryMetricsWithExpressions(metricRequest)

Now using queryResultOK.Payload.Results["A"].Frames[0] to access the frames results in *models.Frame(&models.Frame{Fields:[]*models.Field(nil), Meta:(*models.FrameMeta)(nil), Name:"", RefID:""}). Running the same query in the browser or with postman yields valid results for me.

As described in https://github.com/grafana/grafana-openapi-client-go/pull/90#issuecomment-2014803007 this seems to be an issue with the go data model and JSON not corresponding.

For now I'm just relying on the health check for that metrics data source.

troibe commented 7 months ago

@dhftah Do you think we could take the Frame related changes from https://github.com/grafana/grafana/compare/main...dhftah:frk.grafana:fix-openapi-specification and already make PR out of them?

dhftah commented 7 months ago

@troibe go for it; I couldn't PR grafana itself though as I am not a contributor. I will ask some people

dhftah commented 7 months ago

(you'll also need to regenerate this client when it gets merged in)

dhftah commented 7 months ago

hey it worked now :laughing: https://github.com/grafana/grafana/pull/86456 will submit it

tskdsb commented 2 months ago

+1

pointerpower commented 1 month ago

I'm running into the same problem. Any updates here? I noticed https://github.com/grafana/grafana/pull/86456 was closed. How can we get around this issue?

muroj commented 1 month ago

I'm running into the same problem. Any updates here? I noticed grafana/grafana#86456 was closed. How can we get around this issue?

What worked for me:

  1. Download the spec file from the referenced (closed) PR.
  2. Generate the client against the custom spec: update the Makefile to point at the custom spec.
  3. Point your code at the locally built API client. I added a replace directive to my go.mod:
    replace github.com/grafana/grafana-openapi-client-go => ../grafana-openapi-client-go

Note: I found that the Frame struct field names were different from the standard spec file.