newrelic / newrelic-client-go

New Relic Client for the Go programming language
https://newrelic.github.io/observability-as-code
Apache License 2.0
74 stars 94 forks source link

Queries with both FACET and TIMESERIES fails on JSON unmarshalling #1078

Closed dsabsay closed 3 months ago

dsabsay commented 9 months ago

Description

When running a NRQL query that uses both FACET and TIMESERIES, the library returns an error because it cannot encode the results into the structs.

Go Version

go version go1.21.3 darwin/amd64

Current behavior

The following error is returned:

json: cannot unmarshal array into Go struct field NRDBResultContainer.data.Actor.Account.NRQL.otherResult of type nrdb.NRDBResult

Expected behavior

The results are returned without error (as they are in the New Relic UI).

Steps To Reproduce

package main

import (
    "fmt"
    "os"
    "time"

    "github.com/newrelic/newrelic-client-go/v2/newrelic"
    "github.com/newrelic/newrelic-client-go/v2/pkg/nrdb"
)

func threshold(client *newrelic.NewRelic) {
    accountID := 1234567

        // This query fails:
    // query := "SELECT percentile(duration, 95) AS 'foobar' FROM Transaction FACET request.method TIMESERIES 1 hours SINCE '2024-01-17 14:51:00 +0000' UNTIL '2024-01-17 19:51:00 +0000'"

        // These queries work:
    // query := "SELECT percentile(duration, 95) AS 'foobar' FROM Transaction FACET request.method SINCE '2024-01-17 14:51:00 +0000' UNTIL '2024-01-17 19:51:00 +0000'"
    query := "SELECT percentile(duration, 95) AS 'foobar' FROM Transaction TIMESERIES 1 hours SINCE '2024-01-17 14:51:00 +0000' UNTIL '2024-01-17 19:51:00 +0000'"

    resultContainer, err := client.Nrdb.Query(accountID, nrdb.NRQL(query))
    if err != nil {
        fmt.Printf("error: %v\n", err)
    }

    fmt.Printf("resultContainer: %#v\n", resultContainer)
}

func main() {
    key := os.Getenv("NR_API_KEY")
    client, err := newrelic.New(
        newrelic.ConfigPersonalAPIKey(key),
        newrelic.ConfigHTTPTimeout(time.Second*30),
    )
    if err != nil {
        fmt.Println(err)
        return
    }

    threshold(client)
}

Set NR_API_KEY env var. Then use comments to try each of the 3 queries. The first one (with FACET and TIMESERIES) does not work.

shashank-reddy-nr commented 3 months ago

Hi @dsabsay,

Query1:

SELECT percentile(duration, 95) AS 'foobar' FROM Transaction FACET request.method TIMESERIES 1 hours SINCE '2024-01-17 14:51:00 +0000' UNTIL '2024-01-17 19:51:00 +0000'

Query2:

SELECT percentile(duration, 95) AS 'foobar' FROM Transaction FACET request.method SINCE '2024-01-17 14:51:00 +0000' UNTIL '2024-01-17 19:51:00 +0000'

Image1:

image

Image2:

image