Percentiles of values from Metrics API with NRQL not working

I’m using the Metrics API (specifically through https://github.com/newrelic/newrelic-telemetry-sdk-node) to push some values into New Relic for querying through Insights. When I do that, the percentile function does not appear to work. Should I be using a different function?

Example:

  1. Create some metrics
curl --location --request POST 'https://metric-api.newrelic.com/metric/v1' \
    --header 'Content-Type: application/json' \
    --header 'Api-Key: ...' \
    --data-raw '[
    {
        "metrics": [
            {
                "name": "memory.heap",
                "type": "gauge",
                "value": 2.3,
                "timestamp": 1582847403,
                "attributes": {
                    "host.name": "dev.server.com"
                }
            },
            {
                "name": "memory.heap",
                "type": "gauge",
                "value": 12.2,
                "timestamp": 1582847404,
                "attributes": {
                    "host.name": "dev.server.com"
                }
            },
            {
                "name": "memory.heap",
                "type": "gauge",
                "value": 12.5,
                "timestamp": 1582847405,
                "attributes": {
                    "host.name": "dev.server.com"
                }
            },
            {
                "name": "memory.heap",
                "type": "gauge",
                "value": 10.1,
                "timestamp": 1582847406,
                "attributes": {
                    "host.name": "dev.server.com"
                }
            },
            {
                "name": "memory.heap",
                "type": "gauge",
                "value": 5.7,
                "timestamp": 1582847407,
                "attributes": {
                    "host.name": "dev.server.com"
                }
            }
        ]
    }
]'
  1. Then query through Insights
    SELECT min(`memory.heap`), average(`memory.heap`), percentile(`memory.heap`, 50, 95, 99), max(`memory.heap`) FROM Metric WHERE `memory.heap` IS NOT NULL

  2. Results

{
  "results": [
    {
      "min": 2.3
    },
    {
      "average": 8.56
    },
    {
      "percentiles": {
        "50": 0,
        "95": 0,
        "99": 0
      }
    },
    {
      "max": 12.5
    }
  ],
  "metadata": {
    "eventTypes": [
      "Metric"
    ],
    "eventType": "Metric",
    "timeAggregations": [
      "raw metrics"
    ],
    "openEnded": true,
    "beginTime": "2020-02-27T22:52:15Z",
    "endTime": "2020-02-27T23:52:15Z",
    "beginTimeMillis": 1582843935044,
    "endTimeMillis": 1582847535044,
    "rawSince": "60 MINUTES AGO",
    "rawUntil": "NOW",
    "rawCompareWith": "",
    "guid": "82d9e6f5-7dae-598d-0b14-a3748480ab7f",
    "routerGuid": "ec3389f5-5d2a-0802-30c1-f30378b64d96",
    "messages": [],
    "contents": [
      {
        "function": "min",
        "attribute": "memory.heap",
        "simple": true
      },
      {
        "function": "average",
        "attribute": "memory.heap",
        "simple": true
      },
      {
        "function": "percentile",
        "attribute": "memory.heap",
        "thresholds": [
          50,
          95,
          99
        ]
      },
      {
        "function": "max",
        "attribute": "memory.heap",
        "simple": true
      }
    ]
  }
}
3 Likes

Based on https://docs.newrelic.com/docs/data-ingest-apis/get-data-new-relic/metric-api/view-query-you-metric-data, I found that precentile() is not a currently support function for Metrics data.

4 Likes

Hey @sash, you are correct. Metric queries only support a limited number of functions, depending on the metric type. Thanks for sharing what you found with the community!