Graphite Render API: Add custom functions for better metric formatting

icingaweb2
graphite
metrics

(Michael Friedrich) #1

Author: @mfriedrich
Version: v0.1

Tested with:

  • Graphite 1.1.5 in Vagrant, requires #175

Context

While reading a question about the Graphite web module integration and templates, I’ve found out that current versions of Graphite also allow to extend the Render API with custom functions.

This can be used to extend the Graphite API and use these functions from the web module’s templates.

Resources

Implementation

Preparations

Vagrant with Graphite

Take the Icinga Vagrant box “standalone” which provisions Graphite already.

git clone https://github.com/icinga/icinga-vagrant
cd icinga-vagrant/standalone
vagrant up

Inspect the Render API

Open Grafana and pick a metric from the dashboard. Then adopt this into a render API target like this:

http://192.168.33.5:8003/render?target=icinga2.icinga2_vagrant_demo_icinga_com.services.icinga.icinga.perfdata.active_host_checks.value&title=count

Inspect existing Functions

http://192.168.33.5:8003//functions?pretty=1

Core function code is located in /opt/graphite/webapp/graphite/render/functions.py and allows to understand the ideas, or copy something.

Example

Custom Function to replace Underscores in Metric Names

Navigate into /opt/graphite/webapp/graphite/functions/custom and start your work.

vagrant ssh
sudo -i
cd /opt/graphite/webapp/graphite/functions/custom

vim formathostlegend.py

from graphite.functions.params import Param, ParamTypes

def formatHostLegend(requestContext, seriesList):
        """Custom function that prints a pretty-fied legend name"""
        for series in seriesList:
                pos = series.name.find(".perfdata")
                first = series.name[0:pos]
                second = series.name[pos:]

                series.name = first.replace('_', '.') + second

        return seriesList

# Define group
formatHostLegend.group = 'Custom'

# Define parameters for the callback
formatHostLegend.params = [
        Param('seriesList', ParamTypes.seriesList, required=True)
]

# Register the callback function
SeriesFunctions = {
        'formathostlegend': formatHostLegend
}

Restart Graphite Web / Apache

systemctl restart httpd

Verify the Function

Navigate to http://192.168.33.5:8003//functions?pretty=1 and search for the exported function.

Test the Function

http://192.168.33.5:8003/render?target=formathostlegend(icinga2.icinga2_vagrant_demo_icinga_com.services.icinga.icinga.perfdata.*checks.value)&title=count&width=1024&from=-1h

Your implementation?

Please share your custom functions and more with the Graphite Render API :slight_smile: