nerdgraph

package
v2.48.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2024 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Overview

Package nerdgraph provides a programmatic API for interacting with the New Relic NerdGraph GraphQL API. It can be used to run freeform queries and mutations. The GraphiQL API explorer is a useful resource for learning about what is available via the NerdGraph API:

https://api.newrelic.com/graphiql?#

Authentication

You will need a valid Personal API key to communicate with the backend New Relic API that provides this functionality. See the API key documentation below for more information on how to locate this key:

https://docs.newrelic.com/docs/apis/get-started/intro-apis/types-new-relic-api-keys

Package nerdgraph provides a programmatic API for interacting with NerdGraph, New Relic One's GraphQL API.

Example (Query)
// Initialize the client configuration.  A Personal API key is required to
// communicate with the backend API.
cfg := config.New()
cfg.PersonalAPIKey = os.Getenv("NEW_RELIC_API_KEY")

// Initialize the client.
client := New(cfg)

// Execute a NRQL query to retrieve the average duration of transactions for
// the "Example application" app.
query := `
	query($accountId: Int!, $nrqlQuery: Nrql!) {
		actor {
			account(id: $accountId) {
				nrql(query: $nrqlQuery, timeout: 5) {
					results
				}
			}
		}
	}`

variables := map[string]interface{}{
	"accountId": 12345678,
	"nrqlQuery": "SELECT average(duration) FROM Transaction TIMESERIES WHERE appName = 'Example application'",
}

resp, err := client.Query(query, variables)
if err != nil {
	log.Fatal("error running NerdGraph query: ", err)
}

queryResp := resp.(QueryResponse)
actor := queryResp.Actor.(map[string]interface{})
account := actor["account"].(map[string]interface{})
nrql := account["nrql"].(map[string]interface{})
results := nrql["results"].([]interface{})

var durations []float64
for _, r := range results {
	data := r.(map[string]interface{})
	durations = append(durations, data["average.duration"].(float64))
}

// Output the raw time series values for transaction duration.
fmt.Printf("durations: %v\n", durations)
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccountReference

type AccountReference struct {
	ID   int    `json:"id,omitempty" yaml:"id,omitempty"`
	Name string `json:"name,omitempty" yaml:"name,omitempty"`
}

AccountReference represents the NerdGraph schema for a New Relic account.

type NerdGraph

type NerdGraph struct {
	// contains filtered or unexported fields
}

NerdGraph is used to communicate with the New Relic's GraphQL API, NerdGraph.

func New

func New(config config.Config) NerdGraph

New returns a new GraphQL client for interacting with New Relic's GraphQL API, NerdGraph.

func (*NerdGraph) Query

func (n *NerdGraph) Query(query string, variables map[string]interface{}) (interface{}, error)

Query facilitates making a NerdGraph request with a raw GraphQL query. Variables may be provided in the form of a map. The response's data structure will vary based on the query provided.

func (*NerdGraph) QueryWithContext

func (n *NerdGraph) QueryWithContext(ctx context.Context, query string, variables map[string]interface{}) (interface{}, error)

QueryWithContext facilitates making a NerdGraph request with a raw GraphQL query. Variables may be provided in the form of a map. The response's data structure will vary based on the query provided.

func (*NerdGraph) QueryWithResponse

func (n *NerdGraph) QueryWithResponse(query string, variables map[string]interface{}, respBody interface{}) error

QueryWithResponse functions similarly to Query, but alows for full customization of the returned data payload. Query should be preferred most of the time.

func (*NerdGraph) QueryWithResponseAndContext

func (n *NerdGraph) QueryWithResponseAndContext(ctx context.Context, query string, variables map[string]interface{}, respBody interface{}) error

QueryWithResponseAndContext functions similarly to QueryWithContext, but alows for full customization of the returned data payload. QueryWithContext should be preferred most of the time.

type QueryResponse

type QueryResponse struct {
	Actor          interface{} `json:"actor,omitempty" yaml:"actor,omitempty"`
	Docs           interface{} `json:"docs,omitempty" yaml:"docs,omitempty"`
	RequestContext interface{} `json:"requestContext,omitempty" yaml:"requestContext,omitempty"`
}

QueryResponse represents the top-level GraphQL response object returned from a NerdGraph query request.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL