From c9cc8a73a7cafef1bd99dec096b0b46373e6cb17 Mon Sep 17 00:00:00 2001 From: Mike Lang Date: Mon, 7 Jan 2019 02:29:26 -0800 Subject: [PATCH] generate-flamegraph: Script to create a flamegraph by querying prometheus --- generate-flamegraph | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 generate-flamegraph diff --git a/generate-flamegraph b/generate-flamegraph new file mode 100755 index 0000000..08efe69 --- /dev/null +++ b/generate-flamegraph @@ -0,0 +1,45 @@ +#!/bin/bash + +set -eu + +USAGE="USAGE: $0 PROM_URL SERVICE DURATION [TIME [LABELS]] > flamegraph.svg +Generate a flamegraph for the given service using data from the given prometheus server. +Uses DURATION amount of data (in prometheus timespec format, eg '15m'), ending at TIME, +which may be unix time or RFC3339, defaults to now. +Labels is optional extra prometheus labels to match on, in promql format, +eg. 'foo=\"bar\",baz=\"123\"' +Note that in the resulting flamegraph, the 'samples' value is actually showing milliseconds +of cpu time over the given duration." + +if [ "$#" -lt 3 ]; then + echo "$USAGE" >&2 + exit 1 +fi + +PROM=$1 +SERVICE=$2 +DURATION=$3 +TIME=${4:-now} +LABELS=${5:-} + +if [ "$TIME" == "now" ]; then + TIME=$(date +%s) +fi + +if [ -n "$LABELS" ]; then + LABELS="job=\"$SERVICE\"" +else + LABELS="job=\"$SERVICE\",$LABELS" +fi + +docker build -t wubloader-flamegraph:latest . -f - 1>&2 < 0' | +jq -r '.data.result[]|\"\\(.metric.stack) \\(.value[1])\"' | +perl /flamegraph.pl +"