# New Relic Integration

Toollake enables seamless connectivity between various business tools, allowing AI agents to interact with observability platforms like **New Relic** for performance monitoring and analytics.

This section outlines how to execute **NRQL queries** using the `Newrelic` class from `apm.newrelic`.

#### 📦 Requirements

* New Relic API Key
* New Relic Account ID
* `apm.newrelic` module available in your environment

***

#### 🔧 Class: `Newrelic`

A lightweight utility for querying New Relic via NRQL within Toollake.

**Constructor**

```python
Newrelic(api_key: str, account_id: str)
```

* **api\_key** (`str`): Your New Relic User API Key.
  * *Example:* `"NRAK-XXXXXXXXXXXXXX"`
* **account\_id** (`str`): Your New Relic account identifier.
  * *Example:* `"1234567"`

***

#### 📡 Method: `execute_nrql(nrql_query: str) → dict`

Executes a NRQL (New Relic Query Language) query to fetch custom telemetry metrics.

**Parameters**

* `nrql_query` (`str`): A valid NRQL query string.
  * *Example:*

    ```sql
    SELECT percentage(count(*), WHERE httpResponseCode = '5%') AS 'http500_percentage', 
           percentage(count(*), WHERE httpResponseCode = '4%') AS 'http400_percentage', 
           count(*) AS 'total_requests' 
    FROM Transaction 
    SINCE 60 minutes ago
    ```

**Returns**

* `dict`: A structured JSON response containing query results.

***

#### ✅ Example Usage

```python
from apm.newrelic import Newrelic

api_key = "NRAK-XXXXXXXXXXXXXX"  # Replace with your secure API key
account_id = "1234567"           # Replace with your account ID

nr = Newrelic(api_key, account_id)

nrql_query = """
SELECT percentage(count(*), WHERE httpResponseCode = '5%') AS 'http500_percentage', 
       percentage(count(*), WHERE httpResponseCode = '4%') AS 'http400_percentage', 
       count(*) AS 'total_requests' 
FROM Transaction 
SINCE 60 minutes ago
"""

print("Executing NRQL Query:")
print(nr.execute_nrql(nrql_query))
```

***

#### 📈 Use Case in Toollake

Toollake agents can leverage this method to:

* Monitor API health and error rates in real time
* Trigger alerts based on custom thresholds
* Optimize routing or performance decisions dynamically

***

#### 🔐 Security Tip

Never hard-code your API key in production. Use secure storage solutions like environment variables or secrets managers.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.groclake.ai/lakes/tool-management-and-gateway/toollake/new-relic-integration.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
