Unhelpful Graphite Tip #3 - JSON Output

2012-04-11 10:06:13 by jdixon

I love JSON. No really, I fucking love JSON. It might have something to do with its phonetic approximation to my own name. Or it might be my preference for anything that hastens the death of XML. Either way, it's a handy format that's become ubiquitous for data interchange. And fortunately for those of us who prefer our graphs rendered client-side, Graphite supports it as an output format.

Add format=json to any query and Graphite will magically convert your png output to JSON. Better yet, grab the URL with curl and pipe it to your favorite parser and bam, instant grits.

$ curl -s "https://graphite/render/?target=metric.foo&from=-5sec&format=json" | \
  python -mjson.tool

[
    {
        "datapoints": [
            [
                3.4285714285714288, 
                1334117659
            ], 
            [
                3.4285714285714288, 
                1334117660
            ], 
            [
                3.4285714285714288, 
                1334117661
            ], 
            [
                3.4285714285714288, 
                1334117662
            ], 
            [
                3.4285714285714288, 
                1334117663
            ]
        ], 
        "target": "metric.foo"
    }
]

STOP! I can hear you now. You're thinking "What the hell man, that was actually helpful." Not to worry, I've got you covered. Take that output and send it to Zach Holman's spark tool. Now you have some seriously unhelpful trending data. From the command-line. You're welcome.

$ curl -s "https://graphite/render/?target=metric.foo&from=-30min&format=json" | \
  python -mjson.tool | grep ',' | grep -v '\]' | spark



▂▅▇▄█▃▆▆▅▇▂▁▂▁▂▁▁▅▇▄▃▃▃▂▂▂▁▂▂▃▁▂▃▁▁▁▂▃▅▄▂▁▂▂▃▃▃▂▄▆▃

Comments

at 2012-04-11 12:06:18, Chip wrote in to say...

If JSON would get that fork out of its ass about commas trailing the last element in an array, maybe I'd love it too. I've lost so much time to that unnecessary syntax pedancy.

at 2012-04-23 12:49:15, David Blackman wrote in to say...

also, the jsonp format is &format=json&jsonp=callbackName ... super non-standard

at 2012-06-26 10:00:22, Andre Sachs wrote in to say...

To Chip,

pedancy is not a word, try pedantry :)

at 2013-02-25 12:23:55, Will Plaut wrote in to say...

Thanks for this, but also all your other unhelpful graphite wisdom : )

Add a comment:

  name

  email

  url

max length 4000 chars