Skip to content

Instantly share code, notes, and snippets.

@tknerr
Last active November 8, 2018 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tknerr/de41f44f739f50284fb29c2ee11fa131 to your computer and use it in GitHub Desktop.
Save tknerr/de41f44f739f50284fb29c2ee11fa131 to your computer and use it in GitHub Desktop.
Show the actual requests of the past 5 minutes vs the average request count over the past day. The threshold we want to alert upon is the past day's average amplified by a factor of 10.
let lookbackWindow=30d;
let observationInterval=1d;
let sampleInterval=5m;
let thresholdFactor=10;
// 5-min request count average measured over the past 1 day
let averagedRequestsInObservationInterval = requests
| where timestamp > ago(lookbackWindow)
| summarize sum(itemCount) by bin(timestamp, sampleInterval)
| summarize averageRequestCount = avg(sum_itemCount) by bin(timestamp, observationInterval)
| extend threshold = averageRequestCount * thresholdFactor;
// actual request count of the last 5 min
let actualRequestsInSampleInterval = requests
| where timestamp > ago(lookbackWindow)
| summarize currentRequestCount = sum(itemCount) by bin(timestamp, sampleInterval);
// render
actualRequestsInSampleInterval
| union averagedRequestsInObservationInterval
| render timechart
@tknerr
Copy link
Author

tknerr commented Nov 7, 2018

The current version looks like this:

image

@tknerr
Copy link
Author

tknerr commented Nov 7, 2018

Now, rather than having 1 datapoint for the past day's average at 00:00 every day, I'd rather want to have the past day's average as a rolling window average every 5 minutes.

How would you do that?

@tknerr
Copy link
Author

tknerr commented Nov 8, 2018

Improved and simpler version is here, making use of series_fir() to compute the moving average:

let timeWindow=30d;
let averagingInterval=1d;
let sampleInterval=5m;
let thresholdFactor=10;
requests
| where timestamp > ago(timeWindow)
| make-series requestCount=sum(itemCount) default=0 on timestamp in range(ago(timeWindow), now(), sampleInterval)
| extend movingAverage=series_fir(requestCount, repeat(1, toint(averagingInterval/sampleInterval)))
| mvexpand timestamp to typeof(datetime), requestCount to typeof(int), movingAverage to typeof(double) limit 1000000
| extend threshold=movingAverage * thresholdFactor
| render timechart 

The result looks much better now:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment