Skip to content

Commit

Permalink
[fluentd] limit tag_by param to a specific scope
Browse files Browse the repository at this point in the history
Limit `tag_by` parameter to `plugin_id` or `type` only, to prevent
empty or bad tagging. Minor cosmetic changes too.
  • Loading branch information
yannmh authored and LeoCavaille committed May 14, 2015
1 parent 61ee49e commit 58c3993
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
23 changes: 14 additions & 9 deletions checks.d/fluentd.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# stdlib
from collections import defaultdict
import urlparse

# 3rd party
import requests

# project
from util import headers
from checks import AgentCheck

# 3rd party
import simplejson as json
import requests

class Fluentd(AgentCheck):
SERVICE_CHECK_NAME = 'fluentd.is_ok'
GAUGES = ['retry_count', 'buffer_total_queued_size', 'buffer_queue_length']
_AVAILABLE_TAGS = frozenset(['plugin_id', 'type'])

"""Tracks basic fluentd metrics via the monitor_agent plugin
* number of retry_count
Expand All @@ -29,12 +29,16 @@ def check(self, instance):
try:
url = instance.get('monitor_agent_url')
plugin_ids = instance.get('plugin_ids', [])
tag_by = instance.get('tag_by', 'plugin_id')

# Fallback with `tag_by: plugin_id`
tag_by = instance.get('tag_by')
tag_by = tag_by if tag_by in self._AVAILABLE_TAGS else 'plugin_id'

parsed_url = urlparse.urlparse(url)
monitor_agent_host = parsed_url.hostname
monitor_agent_port = parsed_url.port or 24220
service_check_tags = ['fluentd_host:%s' % monitor_agent_host, 'fluentd_port:%s' % monitor_agent_port]
service_check_tags = ['fluentd_host:%s' % monitor_agent_host, 'fluentd_port:%s'
% monitor_agent_port]

r = requests.get(url, headers=headers(self.agentConfig))
r.raise_for_status()
Expand All @@ -46,11 +50,12 @@ def check(self, instance):
if p.get(m) is None:
continue
# Filter unspecified plugins to keep backward compatibility.
if tag_by == 'type' or len(plugin_ids) == 0 or p.get('plugin_id') in plugin_ids:
if len(plugin_ids) == 0 or p.get('plugin_id') in plugin_ids:
self.gauge('fluentd.%s' % (m), p.get(m), [tag])
except Exception, e:
msg = "No stats could be retrieved from %s : %s" % (url, str(e))
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL, tags=service_check_tags, message=msg)
raise e
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL,
tags=service_check_tags, message=msg)
raise
else:
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK, tags=service_check_tags)
15 changes: 6 additions & 9 deletions conf.d/fluentd.yaml.example
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
init_config:

instances:
# For every instance, you have an `monitor_agent_url`
# Use fluend 'plugin_id' as a tag.
# You need to specify plugin id in fluentd configuration file.
- monitor_agent_url: http://example.com:24220/api/plugins.json
tag_by: plugin_id
# Use fluend plugin 'type' as a tag.
- monitor_agent_url: http://example.org:24220/api/plugins.json
tag_by: type
# For backward compatibilty, you can specify plugin_ids that are used as tags.
# Every instance requires a `monitor_agent_url`
# Optional, set `plugin_ids` to monitor a specific scope of plugins.
- monitor_agent_url: http://example.com:24220/api/plugins.json
plugin_ids:
- plg1
- plg2
# Optional, set 'tag_by' to specify how to tag metrics. By default, metrics are tagged with `plugin_id`
- monitor_agent_url: http://example.com:24220/api/plugins.json
tag_by: type

3 changes: 0 additions & 3 deletions tests/checks/integration/test_fluentd.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,3 @@ def test_fluentd_with_tag_by_plugin_id(self):
for m in metrics:
self.assertEquals(len(m[3]['tags']), 1)
self.assertTrue(p.match(m[3]['tags'][0]))

if __name__ == '__main__':
unittest.main()

0 comments on commit 58c3993

Please sign in to comment.