Skip to content

Commit

Permalink
Merge pull request #654 from DataDog/optional_sqlserver_tags
Browse files Browse the repository at this point in the history
Allow optional custom tags in SQL Server check.
  • Loading branch information
Remi Hakim committed Sep 11, 2013
2 parents 7715d0c + 3deab44 commit e72edf6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 7 additions & 6 deletions checks.d/sqlserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def check(self, instance):
username = instance.get('username')
password = instance.get('password')
database = instance.get('database', 'master')
tags = instance.get('tags', [])
conn_key = self._conn_key(host, username, password, database)

if conn_key not in self.connections:
Expand All @@ -75,9 +76,9 @@ def check(self, instance):

conn = self.connections[conn_key]
cursor = conn.cursor()
self._fetch_metrics(cursor)
self._fetch_metrics(cursor, tags)

def _fetch_metrics(self, cursor):
def _fetch_metrics(self, cursor, custom_tags):
''' Fetch the metrics from the sys.dm_os_performance_counters table
'''
for metric in self.METRICS:
Expand All @@ -93,7 +94,7 @@ def _fetch_metrics(self, cursor):
# to loop over multiple results and tag the metrics
if instance_n == ALL_INSTANCES:
try:
self._fetch_all_instances(metric, cursor)
self._fetch_all_instances(metric, cursor, custom_tags)
except Exception, e:
self.log.exception('Unable to fetch metric: %s' % mname)
self.warning('Unable to fetch metric: %s' % mname)
Expand All @@ -113,9 +114,9 @@ def _fetch_metrics(self, cursor):

# Save the metric
metric_func = getattr(self, mtype)
metric_func(mname, value)
metric_func(mname, value, tags=custom_tags)

def _fetch_all_instances(self, metric, cursor):
def _fetch_all_instances(self, metric, cursor, custom_tags):
mname, mtype, counter, instance_n, tag_by = metric
cursor.execute("""
select instance_name, cntr_value
Expand All @@ -127,7 +128,7 @@ def _fetch_all_instances(self, metric, cursor):

for instance_name, cntr_value in rows:
value = cntr_value
tags = ['%s:%s' % (tag_by, instance_name.strip())]
tags = ['%s:%s' % (tag_by, instance_name.strip())] + custom_tags
metric_func = getattr(self, mtype)
metric_func(mname, value, tags=tags)

2 changes: 2 additions & 0 deletions conf.d/sqlserver.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ instances:
- host: HOST,PORT
username: my_username
password: my_password
tags:
- optional_tag

0 comments on commit e72edf6

Please sign in to comment.