Your data. Anywhere you go.

New Relic for iOS or Android

Download on the App Store    Android App on Google play

New Relic Insights App for iOS

Download on the App Store

Learn more

Close icon

Calculate rate based on counter value


Is it possible (in NRQL) to calculate a rate, based on counter value from events? For example - I have K8sContainerSample events with restartCount field that is increasing over time (with every sample) and I want to calculate restarts per hour.


Hi @adam.m1, just wondering if you could explain your use case a bit more? Are you looking for the total number of restarts in the last 60 minutes? Would something like the following be along the lines of what you are looking for:

SELECT sum(restartCount) FROM K8sContainerSample SINCE 60 MINUTES AGO


Hi @rdouglas, thanks for the suggestion but it will not work - restartCount field stores the number of restarts from the start of the container. The field value may only increase or not change over time. Currently I use the following query:

SELECT max(restartCount) - min(restartCount) FROM K8sContainerSample FACET entityName 1 hour SINCE 1 HOUR AGO


Hmmm so the counter field is accumulative is that correct? Have you tried using something like: SELECT latest(restartCount)?


Yes it is accumulative. I need to detect whether the container has restarted in the last hour and I want to allow a few restarts on staging environments. The most straightforward solution (in my opinion) is to calculate rate of restarts per hour and set different rate alert thresholds for all my environments. Previously I used Graphite and Prometheus and they provide a function for converting accumulative counter metrics to rate per unit of time.


Hi, @adam.m1: Insights has a rate() function. Have you tried it?


@philweber yes I tried it, but I have no idea what function I should apply on restartCount field to get valid results (rate of restarts per hour). The function looks like it’s designed to work with whole event not with one specific event field.


HI @adam.m1 What are you missing in this query? Doesn’t it solve your problem?


Yes it solves my problem :slight_smile:


Okay great. I think your NRQL has a slight mistake in it. For reference for other folks stumbling upon this thread:

SELECT max(restartCount) - min(restartCount) FROM K8sContainerSample FACET entityName SINCE 1 HOUR AGO