Use the Metric API to send custom metrics to the New Relic platform. This document includes a quick start to send your first custom metric, plus detailed information on how to format and send your metric data.
Quick start: Send metric data
We report the metric types count
, gauge
, and summary
. For more information on metrics see our documentation.
Metric data is submitted to New Relic through an HTTP POST request. Each request is composed of one or more metric data points, which consist of a metric name, a timestamp, and a value.
Follow this example to send your first metric data points to New Relic:
Get the for the account you want to report data to.
Insert the license key into the following JSON, and then send the JSON to our endpoint.
Update the
timestamp
with a valid epoch timestamp. This example creates a single metric data point for a metric namedmemory.heap
, but you can create additional attributes or data points by specifying metric types or adding optionalcommon
blocks.bash$curl -vvv -k -H "Content-Type: application/json" \>-H "Api-Key: NEW_RELIC_LICENSE_KEY" \>-X POST https://metric-api.newrelic.com/metric/v1 \>--data '[{$"metrics":[{$"name":"memory.heap",$"type":"gauge",$"value":2.3,$"timestamp":CURRENT_TIME,$"attributes":{"host.name":"dev.server.com"}$}]$}]'
The metric should be available in New Relic in a few seconds. You can query the data from any NRQL interface using this query:
FROM Metric SELECT max(memory.heap) TIMESERIES
For more on where data shows up, see Find Metric API data.
Endpoint URL
Use an HTTP POST when sending metric data to the metric API endpoint:
https://metric-api.newrelic.com/metric/v1
Tip
If your organization hosts data in the EU data center, ensure you're using the EU region endpoints. For this API, the EU endpoint is:
https://metric-api.eu.newrelic.com/metric/v1
HTTP request headers
Include the following HTTP request headers with the POST request. You can send some parameters as query parameters instead of request headers.
Header | Send as a query parameter? | Details |
---|---|---|
| No | Required. Must be |
| No | Required (usually set automatically by the HTTP client). The length of the request body in octets (8-bit bytes) unless sent with chunked encoding. This header is generally set by default by the underlying HTTP client sending the data and in most cases should not require any additional effort by the end user. |
| Yes | Required. A for the account you want to report data to. If this is provided as both a header and a query parameter, the values must match. |
| No | Required if GZIP. The value must be |
| No | Optional - Reserved for future use. The value must be a valid |
HTTP request body
The body of the HTTP POST request must be in JSON format. The following describes the requirements and recommendations for the JSON payload.
The payload must be encoded as UTF-8.
Structure
The JSON payload uses this structure:
- The JSON payload is an array of maps.
- Each map must contain a
metrics
key whose value is an array containing one or more metric data points. - A metric data point is identified by a
name
,value
, andtimestamp
along with an optional set of attributes.
Required key-value pairs
Each metric data point map in the metrics
array uses the following key-value structure:
Key | Description |
---|---|
string | Required. The name of the metric. The value must be less than 255 characters. |
number or map | Required. The value varies depending on the metric type. For |
long | Required. The metric's start time in Unix time. The default uses UTC time zone. This field also support seconds, microseconds, and nanoseconds. However, the data will be converted to milliseconds for storage and query. Metrics reported with a timestamp older than 48 hours ago or newer than 24 hours from the time they are reported are dropped. |
positive long | Required for |
| Recommended. This should be one of the supported metric types. If you do not specify a type, then this will default to a |
strings, JSON numbers, or booleans | Recommended. A map of key value pairs associated with this specific metric. Values can be strings, JSON numbers, or booleans. Keys are case-sensitive and must be less than 255 characters. |
Share attributes across metrics with common
If you want to include a set of attributes on multiple metrics (and not add the same attributes for each metric), you can use the common
block. This is an optional map that specifies information that applies to all associated metric data points. Values in the common section will be overridden if the same key exists on a metric data point.
The block can include:
Attribute | Description |
---|---|
long | The metric's start time in Unix time. This defaults to the current time in the UTC time zone. This field also supports seconds, microseconds, and nanoseconds. However, the data will be converted to milliseconds for storage and later querying. |
positive long | Required for |
strings, JSON numbers, or booleans | A map of key-value pairs associated with this specific metric. Values can be strings, JSON numbers, or booleans. |
Response validation and status codes
The Metric API returns a 202
response code for successful requests. When your data is accepted, an HTTP 202
response code is returned with a response structure like this:
HTTP/1.1 202 AcceptedContent-Type: application/json; charset=UTF-8Content-Length: 52Access-Control-Allow-Methods: GET, POST, PUT, HEAD, OPTIONSAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Origin: *Connection: keep-alive
{"requestId":"f0e7bfff-001a-b000-0000-01682bcf4565"}
Missing data with 202
response
A 202
code indicates the API did receive your data, and that the data passed basic validation checks. Normally, your data will be available for querying within a few seconds. However, New Relic runs additional validation asynchronously after receiving your data. If you receive a 202
response but can't find your metric, this indicates that New Relic found an error during this asynchronous validation.
You can find these errors by querying NrIntegrationError
events in the account associated with the Insert API key you used. The requestId
for each request will be tagged on the NrIntegrationError
event. For more information, see Troubleshoot an NRIntegrationError
event.
Status codes
The Metric API can return the following HTTP status codes:
Status code | Definition |
---|---|
| Data accepted. |
| Structure of the request is invalid. |
| Authentication failure. |
| The request path is incorrect. |
| Used a request method other than POST. |
| The request took too long to reach the endpoint. |
| The |
| The payload was too big. Payloads must be under 1MB (10^6 bytes). |
| The request URI was too long. |
| The |
| The request rate quota has been exceeded. |
| The request headers are too long. |
| There was a server error (please retry). |