github.com/newrelic/newrelic-client-go@v1.1.0/pkg/nrdb/nrdb_query.go (about)

     1  // Package nrdb provides a programmatic API for interacting with NRDB, New Relic's Datastore
     2  package nrdb
     3  
     4  import "context"
     5  
     6  func (n *Nrdb) Query(accountID int, query NRQL) (*NRDBResultContainer, error) {
     7  	return n.QueryWithContext(context.Background(), accountID, query)
     8  }
     9  
    10  // QueryWithContext facilitates making a NRQL query.
    11  func (n *Nrdb) QueryWithContext(ctx context.Context, accountID int, query NRQL) (*NRDBResultContainer, error) {
    12  	respBody := gqlNrglQueryResponse{}
    13  
    14  	vars := map[string]interface{}{
    15  		"accountId": accountID,
    16  		"query":     query,
    17  	}
    18  
    19  	if err := n.client.NerdGraphQueryWithContext(ctx, gqlNrqlQuery, vars, &respBody); err != nil {
    20  		return nil, err
    21  	}
    22  
    23  	return &respBody.Actor.Account.NRQL, nil
    24  }
    25  
    26  func (n *Nrdb) QueryHistory() (*[]NRQLHistoricalQuery, error) {
    27  	return n.QueryHistoryWithContext(context.Background())
    28  }
    29  
    30  func (n *Nrdb) QueryHistoryWithContext(ctx context.Context) (*[]NRQLHistoricalQuery, error) {
    31  	respBody := gqlNrglQueryHistoryResponse{}
    32  	vars := map[string]interface{}{}
    33  
    34  	if err := n.client.NerdGraphQueryWithContext(ctx, gqlNrqlQueryHistoryQuery, vars, &respBody); err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	return &respBody.Actor.NRQLQueryHistory, nil
    39  }
    40  
    41  const (
    42  	gqlNrqlQueryHistoryQuery = `{ actor { nrqlQueryHistory { accountId nrql timestamp } } }`
    43  
    44  	gqlNrqlQuery = `query($query: Nrql!, $accountId: Int!) { actor { account(id: $accountId) { nrql(query: $query) {
    45      currentResults otherResult previousResults results totalResult
    46      metadata { eventTypes facets messages timeWindow { begin compareWith end since until } }
    47    } } } }`
    48  )
    49  
    50  type gqlNrglQueryResponse struct {
    51  	Actor struct {
    52  		Account struct {
    53  			NRQL NRDBResultContainer
    54  		}
    55  	}
    56  }
    57  
    58  type gqlNrglQueryHistoryResponse struct {
    59  	Actor struct {
    60  		NRQLQueryHistory []NRQLHistoricalQuery
    61  	}
    62  }