Skip to content

Instantly share code, notes, and snippets.

@j-mcnally
Last active July 24, 2019 17:20
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save j-mcnally/0978be6740218bd8bf36af4d2a34ea5c to your computer and use it in GitHub Desktop.
Monitor Queue Stats in Azure via Azure Functions
{
"disabled": false,
"scriptFile": "../dist/CheckQueues/index.js",
"bindings": [
{
"schedule": "*/15 * * * * *",
"name": "snapshotSidekiqQueues",
"type": "timerTrigger",
"direction": "in"
}
],
"_originalEntryPoint": false,
"_originalScriptFile": "../dist/CheckQueues/index.js",
"entryPoint": "CheckQueues"
}
import { TelemetryClient } from 'applicationinsights'
import { promisify } from 'util'
import _ from 'lodash'
import Redis from 'redis'
module.exports = async function (context, req) {
const appInsightsKey = process.env['ApplicationInsightsKey']
const insightsClient = new TelemetryClient(appInsightsKey);
const redis_url = process.env['ConnectionStrings:RedisConnection'] || process.env['CUSTOMCONNSTR_RedisConnection']
const redis_url_url = URL.parse(redis_url);
const redis_host = redis_url_url.hostname;
const client = Redis.createClient(redis_url, {tls: {servername: redis_host}, detect_buffers: true});
const smembersAsync = promisify(client.smembers).bind(client)
const lrangeAsync = promisify(client.lrange).bind(client)
const llenAsync = promisify(client.llen).bind(client)
const queues = await smembersAsync('queues')
_.each(queues, async (queue) => {
const queueName = `queue:${queue}`
const lastJobRaw = await lrangeAsync(queueName, -1, -1)
let latency = 0
if (lastJobRaw.length == 1) {
const lastJob = JSON.parse(lastJobRaw[0])
const now = new Date() / 1000;
const thence = lastJob["enqueued_at"] || now
latency = now - thence
}
const queueSize = await llenAsync(queueName)
await insightsClient.trackMetric({name: `${queueName}:latency`, value: latency})
await insightsClient.trackMetric({name: `${queueName}:size`, value: queueSize})
})
await insightsClient.flush()
// Write values out to AppInsights
context.res = {
status: 200,
body: 'OK!'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment