Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying New Relic with GraphQL in Python

I just started using GraphQL with New Relic and I'm having troubles trying to pass variables to the GraphQL query. I need to retrieve the NR Synthetics monitors GUID's based on their names, for which I thought of searching for the entity and then get the guid from the query response. This is my piece of code containing the query:

    query = '''
    query($query: String!) {
      actor {
        entitySearch(query: $query) {
          results {
            entities {
              guid
            }
          }
        }
      }
      variables { query: "name LIKE \u0027MYMONITORNAME\u0027" }
    }'''

Then I call the NR API:

    my_response = requests.get(
    url=my_url,
    headers=header,
    json={"query": query}
)

And I get this back:

{'errors': [{'locations': [{'column': 30, 'line': 12}], 'message': 'syntax error before: "\\"name LIKE \'MYMONITORNAME\'\\""'}]}

I've tried many escape sequences, double quotes/single quotes/no quotes combinations but can't seem to nail the right format. Any guidance would be much appreciated.

Thanks in advance, Sebastian

like image 781
bahrt Avatar asked Jul 19 '26 23:07

bahrt


1 Answers

I'm going to leave this here in case it helps anyone else.
This is so convoluted, but here's what worked for me to use the API

my_account_id = "ACCOUNT_ID" # looks like "1234567890"
my_graph_api_key = "API_KEY" # looks like "NRAK-XYZ12345678901234567890"

nrql = "SELECT count(*) FROM Log WHERE allColumnSearch('thing I want to count', insensitive: true) SINCE 10 minutes ago"""

graph_query = """{
    actor {
        account(id: %s) {
            nrql(query: \"%s\") {
                results
            }
        }
    }
}""" % (my_account_id, nrql)

url = "https://api.newrelic.com/graphql"
headers = {
    "Content-Type": "application/json",
    "API-Key": my_graph_api_key,
}
payload = json.dumps({"query": graph_query})
response = requests.request("POST", url, headers=headers, data=payload)
print(response.json())

Getting results for a long-running query

To run the query asynchronously, if it times out after 1 minute, change the graph query to run with:

graph_query = """{
    actor {
        account(id: %s) {
            nrql(query: \"%s\", async: true) {
                results
                queryProgress {
                    queryId
                    completed
                    retryAfter
                    retryDeadline
                    resultExpiration
                }
            }
        }
    }
}""" % (my_account_id, nrql)

It returns something like this:

{
  "data": {
    "actor": {
      "account": {
        "nrql": {
          "queryProgress": {
            "completed": false,
            "queryId": "MV84ZGQxODI1MS1iODE3LTRlYzgtYmI5ZC02ZmNjZjI2MWU0ZjU",
            "resultExpiration": null,
            "retryAfter": 10,
            "retryDeadline": 60
          },
          "results": null
        }
      }
    }
  }
}

Then you can query again with:

graph_query = """{
    actor {
        account(id: %s) {
            nrqlQueryProgress(queryId: \"%s\") {
                results
                queryProgress {
                    queryId
                    completed
                    retryAfter
                    retryDeadline
                    resultExpiration
                }
            }
        }
    }
}""" % (my_account_id, query_id)
like image 136
bubbassauro Avatar answered Jul 22 '26 11:07

bubbassauro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!