Created
September 13, 2016 03:19
-
-
Save n0ts/771f7b9c3fc7528018d2d63fa03e76ee to your computer and use it in GitHub Desktop.
Datadog Agent ThreadStats sample code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# test for Threadstat - a tool for collecting metrics in high performance applications | |
# http://datadogpy.readthedocs.io/en/latest/#datadog-threadstats-module | |
import random, time, os, yaml | |
# Configure the module according to your needs | |
from datadog import initialize | |
from datadog.util.config import get_config | |
# initialize(api_key=None, app_key=None, host_name=None, api_host=None, statsd_host=None, statsd_port=None, statsd_use_default_route=False, **kwargs) | |
config = get_config() | |
if 'api_key' in config: | |
api_key = config['api_key'] | |
else: | |
api_key = os.environ['DD_API_KEY'] | |
initialize(api_key=api_key) | |
try: | |
yaml_file = os.environ('DATADOG_CONF', '') | |
yaml = yaml.load(file(file_file)) | |
interval = yaml['init_config']['min_collection_interval'] | |
# TODO more configuration | |
except: | |
interval = 60 | |
# https://help.datadoghq.com/hc/en-us/articles/204590189-Is-there-an-alternative-to-dogstatsd-and-the-api-to-submit-metrics-Threadstats- | |
from logging import getLogger, StreamHandler, DEBUG | |
import logging | |
log = logging.getLogger('dd.datadogpy') | |
handler = StreamHandler() | |
handler.setLevel(DEBUG) | |
log.setLevel(DEBUG) | |
log.addHandler(handler) | |
from datadog import ThreadStats | |
stats = ThreadStats() | |
#start(self, flush_interval=10, roll_up_interval=10, device=None, | |
# flush_in_thread=True, flush_in_greenlet=False, disabled=False) | |
stats.start(flush_interval=interval, roll_up_interval=interval, device=None, | |
flush_in_thread=True, flush_in_greenlet=False, disabled=False) | |
# Send an event. Attributes are the same as the Event API. (http://docs.datadoghq.com/api/) | |
# event(self, title, text, alert_type=None, aggregation_key=None, | |
# source_type_name=None, date_happened=None, priority=None, | |
# tags=None, hostname=None): | |
# Record the current ``value`` of a metric. | |
# gauge(self, metric_name, value, timestamp=None, tags=None, sample_rate=1, host=None) | |
# Increment the counter by the given ``value``. | |
# increment(self, metric_name, value=1, timestamp=None, tags=None, sample_rate=1, host=None) | |
# Decrement a counter, optionally setting a value, tags and a sample rate. | |
# decrement(self, metric_name, value=1, timestamp=None, tags=None, sample_rate=1, host=None) | |
# Sample a histogram value. | |
# histogram(self, metric_name, value, timestamp=None, tags=None, sample_rate=1, host=None) | |
# Record a timing, optionally setting tags and a sample rate. | |
# timing(self, metric_name, value, timestamp=None, tags=None, sample_rate=1, host=None) | |
# A decorator that will track the distribution of a function's run time. | |
# timed(self, metric_name, sample_rate=1, tags=None, host=None) | |
# Flush and post all metrics to the server. | |
# flush(self, timestamp=None): | |
for i in range(2): | |
value = random.uniform(1, 100) | |
stats.gauge('home.page.hits', value) | |
log.info("%d\tSleep... %f" % (i, value)) | |
time.sleep(interval) | |
print "stats.stop is %s" % stats.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment