github.com/newrelic/newrelic-client-go@v1.1.0/pkg/nerdgraph/example_query_test.go (about) 1 package nerdgraph 2 3 import ( 4 "fmt" 5 "log" 6 "os" 7 8 "github.com/newrelic/newrelic-client-go/pkg/config" 9 ) 10 11 func Example_query() { 12 // Initialize the client configuration. A Personal API key is required to 13 // communicate with the backend API. 14 cfg := config.New() 15 cfg.PersonalAPIKey = os.Getenv("NEW_RELIC_API_KEY") 16 17 // Initialize the client. 18 client := New(cfg) 19 20 // Execute a NRQL query to retrieve the average duration of transactions for 21 // the "Example application" app. 22 query := ` 23 query($accountId: Int!, $nrqlQuery: Nrql!) { 24 actor { 25 account(id: $accountId) { 26 nrql(query: $nrqlQuery, timeout: 5) { 27 results 28 } 29 } 30 } 31 }` 32 33 variables := map[string]interface{}{ 34 "accountId": 12345678, 35 "nrqlQuery": "SELECT average(duration) FROM Transaction TIMESERIES WHERE appName = 'Example application'", 36 } 37 38 resp, err := client.Query(query, variables) 39 if err != nil { 40 log.Fatal("error running NerdGraph query: ", err) 41 } 42 43 queryResp := resp.(QueryResponse) 44 actor := queryResp.Actor.(map[string]interface{}) 45 account := actor["account"].(map[string]interface{}) 46 nrql := account["nrql"].(map[string]interface{}) 47 results := nrql["results"].([]interface{}) 48 49 var durations []float64 50 for _, r := range results { 51 data := r.(map[string]interface{}) 52 durations = append(durations, data["average.duration"].(float64)) 53 } 54 55 // Output the raw time series values for transaction duration. 56 fmt.Printf("durations: %v\n", durations) 57 }