Skip to content

Commit

Permalink
Push graphs to statsd (#3148)
Browse files Browse the repository at this point in the history
Add integration dashboard's graph with statsd
  • Loading branch information
ar4s committed Aug 25, 2017
1 parent 7162cc9 commit 3c50ab9
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/user/dashboards.md
Expand Up @@ -2,6 +2,9 @@

The dashboard provide basic mechanism for displaying data via bar or pie charts.

# Integration with statsd
Each graph can push data to statsd. You must add ``STATSD_GRAPHS_PREFIX`` to yours settings and set ``ALLOW_PUSH_GRAPHS_DATA_TO_STATSD`` and ``COLLECT_METRICS`` to ``True``. Next, check ``Push to statsd`` on concrete graph and use Ralph's management command ``push_graphs_to_statsd`` to push your data to statsd.


## Getting started
All example data in this tutorial was generated by Ralph's command - ``ralph make_demo_data``.
Expand Down
14 changes: 13 additions & 1 deletion src/ralph/dashboards/admin.py
Expand Up @@ -2,6 +2,7 @@
from itertools import groupby

from django import forms
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _
Expand Down Expand Up @@ -78,14 +79,25 @@ class GraphAdmin(RalphAdmin):
'aggregate_type',
'chart_type',
'params',
'active'
'active',
'push_to_statsd',
)
}),
('Preview', {
'fields': ('get_preview',),
}),
)

def get_readonly_fields(self, *args, **kwargs):
readonly_fields = super().get_readonly_fields(*args, **kwargs)
allow_push_graphs_data_to_statsd = (
not settings.ALLOW_PUSH_GRAPHS_DATA_TO_STATSD and
not settings.COLLECT_METRICS
)
if allow_push_graphs_data_to_statsd:
readonly_fields.append('push_to_statsd')
return readonly_fields

def get_preview(self, obj):
return obj.render(name='preview')

Expand Down
Empty file.
Empty file.
33 changes: 33 additions & 0 deletions src/ralph/dashboards/management/commands/push_graphs_to_statsd.py
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
import logging
import textwrap

from django.conf import settings
from django.core.management.base import BaseCommand
from django.utils.text import slugify

from ralph.dashboards.models import Graph
from ralph.lib.metrics import statsd

logger = logging.getLogger(__name__)
PREFIX = settings.STATSD_GRAPHS_PREFIX
STATSD_PATH = '{}.{{}}.{{}}'.format(PREFIX)


def normalize(s):
s = slugify(s)
return s.replace('-', '_')


class Command(BaseCommand):
"""Push to statsd data generated by graphs."""
help = textwrap.dedent(__doc__).strip()

def handle(self, *args, **kwargs):
graphs = Graph.objects.filter(push_to_statsd=True)
for graph in graphs:
graph_data = graph.get_data()
graph_name = normalize(graph.name)
for label, value in zip(graph_data['labels'], graph_data['series']):
path = STATSD_PATH.format(graph_name, normalize(label))
statsd.gauge(path, value)
19 changes: 19 additions & 0 deletions src/ralph/dashboards/migrations/0003_graph_push_to_statsd.py
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('dashboards', '0002_auto_20170509_1404'),
]

operations = [
migrations.AddField(
model_name='graph',
name='push_to_statsd',
field=models.BooleanField(default=False, help_text="Push graph's data to statsd."),
),
]
4 changes: 4 additions & 0 deletions src/ralph/dashboards/models.py
Expand Up @@ -126,6 +126,10 @@ class Graph(AdminAbsoluteUrlMixin, NamedMixin, TimeStampMixin, models.Model):
chart_type = models.PositiveIntegerField(choices=ChartType())
params = JSONField(blank=True)
active = models.BooleanField(default=True)
push_to_statsd = models.BooleanField(
default=False,
help_text='Push graph\'s data to statsd.'
)

def pop_annotate_filters(self, filters):
annotate_filters = {}
Expand Down
2 changes: 2 additions & 0 deletions src/ralph/settings/base.py
Expand Up @@ -533,3 +533,5 @@ def str_to_bool(s: str) -> bool:

# METRICS
COLLECT_METRICS = False
ALLOW_PUSH_GRAPHS_DATA_TO_STATSD = False
STATSD_GRAPHS_PREFIX = 'ralph.graphs'
8 changes: 8 additions & 0 deletions src/ralph/settings/prod.py
Expand Up @@ -68,3 +68,11 @@
MIDDLEWARE_CLASSES = (
'ralph.lib.metrics.middlewares.RequestMetricsMiddleware',
) + MIDDLEWARE_CLASSES

ALLOW_PUSH_GRAPHS_DATA_TO_STATSD = bool_from_env(
'ALLOW_PUSH_GRAPHS_DATA_TO_STATSD'
)
if ALLOW_PUSH_GRAPHS_DATA_TO_STATSD:
STATSD_GRAPHS_PREFIX = os.environ.get(
'STATSD_GRAPHS_PREFIX', 'ralph.graphs'
)

0 comments on commit 3c50ab9

Please sign in to comment.