本文整理汇总了Python中datadog.dogstatsd.base.DogStatsd类的典型用法代码示例。如果您正苦于以下问题:Python DogStatsd类的具体用法?Python DogStatsd怎么用?Python DogStatsd使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DogStatsd类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_tags_from_environment_and_constant
def test_tags_from_environment_and_constant(self):
with preserve_environment_variable('DATADOG_TAGS'):
os.environ['DATADOG_TAGS'] = 'country:china,age:45,blue'
statsd = DogStatsd(constant_tags=['country:canada', 'red'])
statsd.socket = FakeSocket()
statsd.gauge('gt', 123.4)
t.assert_equal('gt:123.4|g|#country:canada,red,country:china,age:45,blue', statsd.socket.recv())
示例2: test_timing
def test_timing(self):
"""
Timings can be submitted from concurrent threads.
"""
# Create a DogStatsd client with a mocked socket
statsd = DogStatsd()
statsd.socket = self.socket
# Samples
values = set(range(10000))
# Submit metrics from different threads
self._submit_with_multiple_threads(statsd, "timing", values)
# All metrics were properly submitted
self.assertMetrics(values)
示例3: init_app
def init_app(self, app, config=None):
"""
Initialize Datadog DogStatsd client from Flask app
>>> from flask.ext.datadog import StatsD
>>> app = Flask(__name__)
>>> statsd = StatsD()
>>> statsd.init_app(app=app)
Available DogStatsd config settings:
STATSD_HOST - statsd host to send metrics to (default: 'localhost')
STATSD_MAX_BUFFER_SIZE - max number of metrics to buffer before sending, only used when batching (default: 50)
STATSD_NAMESPACE - metric name prefix to use, e.g. 'app_name' (default: None)
STATSD_PORT - statsd port to send metrics to (default: 8125)
STATSD_TAGS - list of tags to include by default, e.g. ['env:prod'] (default: None)
STATSD_USEMS - whether or not to report timing in milliseconds (default: False)
Available Flask-Datadog config settings:
DATADOG_CONFIGURE_MIDDLEWARE - whether or not to setup response timing middleware (default: True)
DATADOG_RESPONSE_METRIC_NAME - the name of the response time metric (default: 'flask.response.time')
DATADOG_RESPONSE_SAMPLE_RATE - the sample rate to use for response timing middleware (default: 1)
DATADOG_RESPONSE_AUTO_TAG - whether to auto-add request/response tags to response metrics (default: True)
DATADOG_RESPONSE_ENDPOINT_TAG_NAME - tag name to use for request endpoint tag name (default: 'endpoint')
DATADOG_RESPONSE_METHOD_TAG_NAME - tag name to use for the request method tag name (default: 'method')
:param app: Flask app to configure this client for
:type app: flask.Flask
:param config: optional, dictionary of config values (defaults to `app.config`)
:type config: dict
"""
# Used passed in config if provided, otherwise use the config from `app`
if config is not None:
self.config = config
elif self.config is None:
self.config = app.config
# Set default values for expected config properties
self.config.setdefault('STATSD_HOST', 'localhost')
self.config.setdefault('STATSD_MAX_BUFFER_SIZE', 50)
self.config.setdefault('STATSD_NAMESPACE', None)
self.config.setdefault('STATSD_PORT', 8125)
self.config.setdefault('STATSD_TAGS', None)
self.config.setdefault('STATSD_USEMS', False)
self.app = app
# Configure DogStatsd client
# https://github.com/DataDog/datadogpy/blob/v0.11.0/datadog/dogstatsd/base.py
self.statsd = DogStatsd(host=self.config['STATSD_HOST'],
port=self.config['STATSD_PORT'],
max_buffer_size=self.config['STATSD_MAX_BUFFER_SIZE'],
namespace=self.config['STATSD_NAMESPACE'],
constant_tags=self.config['STATSD_TAGS'],
use_ms=self.config['STATSD_USEMS'])
# Configure any of our middleware
self.setup_middleware()
示例4: test_decrement
def test_decrement(self):
"""
Decrements can be submitted from concurrent threads.
"""
# Create a DogStatsd client with a mocked socket
statsd = DogStatsd()
statsd.socket = self.socket
# Samples
values = set(range(10000))
expected_value = set([-value for value in values])
# Submit metrics from different threads
self._submit_with_multiple_threads(statsd, "decrement", expected_value)
# All metrics were properly submitted
self.assertMetrics(values)
示例5: test_timed_context_manager_threaded
def test_timed_context_manager_threaded(self, mock_time):
"""
`timed` context manager plays well with concurrent threads.
"""
# Create a DogStatsd client with a mocked socket
statsd = DogStatsd()
statsd.socket = self.socket
# Set up the mocked time
mock_time.return_value = 0
# Method to time
def bar():
"""
Wait 5 time units and return.
"""
initial_time = mock_time.return_value
with statsd.timed("foo"):
while mock_time.return_value < initial_time + 2:
pass
# Run the method within multiple threads
threads = []
for value in range(10):
t = threading.Thread(target=bar)
threads.append(t)
# Bump time so that previous thread can complete
mock_time.return_value += 1
t.start()
# Sleep to let the threads start
time.sleep(0.1)
# Bump time so that all threads completes
time.sleep(0.1)
mock_time.return_value += 1
time.sleep(0.1)
mock_time.return_value += 1
for t in threads:
t.join()
# All metrics were properly submitted
expected_values = [2 for _ in xrange(0, 10)]
self.assertMetrics(expected_values)
示例6: setUp
def setUp(self):
"""
Set up a default Dogstatsd instance and mock the proc filesystem.
"""
#
self.statsd = DogStatsd()
self.statsd.socket = FakeSocket()
# Mock the proc filesystem
route_data = load_fixtures('route')
self._procfs_mock = patch('datadog.util.compat.builtins.open', mock_open())
self._procfs_mock.__enter__().return_value.readlines.return_value = route_data.split("\n")
示例7: setUp
def setUp(self):
self.statsd = DogStatsd()
self.statsd.socket = FakeSocket()
示例8: TestDogStatsd
class TestDogStatsd(object):
def setUp(self):
self.statsd = DogStatsd()
self.statsd.socket = FakeSocket()
def recv(self):
return self.statsd.socket.recv()
def test_initialization(self):
options = {
'statsd_host': "myhost",
'statsd_port': 1234
}
t.assert_equal(statsd.host, "localhost")
t.assert_equal(statsd.port, 8125)
initialize(**options)
t.assert_equal(statsd.host, "myhost")
t.assert_equal(statsd.port, 1234)
def test_set(self):
self.statsd.set('set', 123)
assert self.recv() == 'set:123|s'
def test_gauge(self):
self.statsd.gauge('gauge', 123.4)
assert self.recv() == 'gauge:123.4|g'
def test_counter(self):
self.statsd.increment('page.views')
t.assert_equal('page.views:1|c', self.recv())
self.statsd.increment('page.views', 11)
t.assert_equal('page.views:11|c', self.recv())
self.statsd.decrement('page.views')
t.assert_equal('page.views:-1|c', self.recv())
self.statsd.decrement('page.views', 12)
t.assert_equal('page.views:-12|c', self.recv())
def test_histogram(self):
self.statsd.histogram('histo', 123.4)
t.assert_equal('histo:123.4|h', self.recv())
def test_tagged_gauge(self):
self.statsd.gauge('gt', 123.4, tags=['country:china', 'age:45', 'blue'])
t.assert_equal('gt:123.4|g|#country:china,age:45,blue', self.recv())
def test_tagged_counter(self):
self.statsd.increment('ct', tags=['country:canada', 'red'])
t.assert_equal('ct:1|c|#country:canada,red', self.recv())
def test_tagged_histogram(self):
self.statsd.histogram('h', 1, tags=['red'])
t.assert_equal('h:1|h|#red', self.recv())
def test_sample_rate(self):
self.statsd.increment('c', sample_rate=0)
assert not self.recv()
for i in range(10000):
self.statsd.increment('sampled_counter', sample_rate=0.3)
self.assert_almost_equal(3000, len(self.statsd.socket.payloads), 150)
t.assert_equal('sampled_counter:1|c|@0.3', self.recv())
def test_tags_and_samples(self):
for i in range(100):
self.statsd.gauge('gst', 23, tags=["sampled"], sample_rate=0.9)
def test_tags_and_samples(self):
for i in range(100):
self.statsd.gauge('gst', 23, tags=["sampled"], sample_rate=0.9)
t.assert_equal('gst:23|g|@0.9|#sampled')
def test_timing(self):
self.statsd.timing('t', 123)
t.assert_equal('t:123|ms', self.recv())
def test_event(self):
self.statsd.event('Title', u'L1\nL2', priority='low', date_happened=1375296969)
t.assert_equal(u'_e{5,6}:Title|L1\\nL2|d:1375296969|p:low', self.recv())
self.statsd.event('Title', u'♬ †øU †øU ¥ºu T0µ ♪',
aggregation_key='key', tags=['t1', 't2:v2'])
t.assert_equal(u'_e{5,19}:Title|♬ †øU †øU ¥ºu T0µ ♪|k:key|#t1,t2:v2', self.recv())
def test_event_constant_tags(self):
self.statsd.constant_tags = ['bar:baz', 'foo']
self.statsd.event('Title', u'L1\nL2', priority='low', date_happened=1375296969)
t.assert_equal(u'_e{5,6}:Title|L1\\nL2|d:1375296969|p:low|#bar:baz,foo', self.recv())
self.statsd.event('Title', u'♬ †øU †øU ¥ºu T0µ ♪',
aggregation_key='key', tags=['t1', 't2:v2'])
t.assert_equal(u'_e{5,19}:Title|♬ †øU †øU ¥ºu T0µ ♪|k:key|#t1,t2:v2,bar:baz,foo', self.recv())
def test_service_check(self):
now = int(time.time())
self.statsd.service_check(
'my_check.name', self.statsd.WARNING,
#.........这里部分代码省略.........
示例9: test_accessing_socket_multiple_times_returns_same_socket
def test_accessing_socket_multiple_times_returns_same_socket(self):
dogpound = DogStatsd()
fresh_socket = FakeSocket()
dogpound.socket = fresh_socket
t.assert_equal(fresh_socket, dogpound.get_socket())
t.assert_not_equal(FakeSocket(), dogpound.get_socket())
示例10: test_accessing_socket_opens_socket
def test_accessing_socket_opens_socket(self):
dogpound = DogStatsd()
try:
t.assert_not_equal(None, dogpound.get_socket())
finally:
dogpound.socket.close()
示例11: TestDogStatsd
class TestDogStatsd(object):
def setUp(self):
self.statsd = DogStatsd()
self.statsd.socket = FakeSocket()
def recv(self):
return self.statsd.socket.recv()
def test_initialization(self):
options = {
'statsd_host': "myhost",
'statsd_port': 1234
}
t.assert_equal(statsd.host, "localhost")
t.assert_equal(statsd.port, 8125)
initialize(**options)
t.assert_equal(statsd.host, "myhost")
t.assert_equal(statsd.port, 1234)
def test_set(self):
self.statsd.set('set', 123)
assert self.recv() == 'set:123|s'
def test_gauge(self):
self.statsd.gauge('gauge', 123.4)
assert self.recv() == 'gauge:123.4|g'
def test_counter(self):
self.statsd.increment('page.views')
t.assert_equal('page.views:1|c', self.recv())
self.statsd.increment('page.views', 11)
t.assert_equal('page.views:11|c', self.recv())
self.statsd.decrement('page.views')
t.assert_equal('page.views:-1|c', self.recv())
self.statsd.decrement('page.views', 12)
t.assert_equal('page.views:-12|c', self.recv())
def test_histogram(self):
self.statsd.histogram('histo', 123.4)
t.assert_equal('histo:123.4|h', self.recv())
def test_tagged_gauge(self):
self.statsd.gauge('gt', 123.4, tags=['country:china', 'age:45', 'blue'])
t.assert_equal('gt:123.4|g|#country:china,age:45,blue', self.recv())
def test_tagged_counter(self):
self.statsd.increment('ct', tags=['country:canada', 'red'])
t.assert_equal('ct:1|c|#country:canada,red', self.recv())
def test_tagged_histogram(self):
self.statsd.histogram('h', 1, tags=['red'])
t.assert_equal('h:1|h|#red', self.recv())
def test_sample_rate(self):
self.statsd.increment('c', sample_rate=0)
assert not self.recv()
for i in range(10000):
self.statsd.increment('sampled_counter', sample_rate=0.3)
self.assert_almost_equal(3000, len(self.statsd.socket.payloads), 150)
t.assert_equal('sampled_counter:1|c|@0.3', self.recv())
def test_tags_and_samples(self):
for i in range(100):
self.statsd.gauge('gst', 23, tags=["sampled"], sample_rate=0.9)
def test_tags_and_samples(self):
for i in range(100):
self.statsd.gauge('gst', 23, tags=["sampled"], sample_rate=0.9)
t.assert_equal('gst:23|g|@0.9|#sampled')
def test_timing(self):
self.statsd.timing('t', 123)
t.assert_equal('t:123|ms', self.recv())
def test_event(self):
self.statsd.event('Title', u'L1\nL2', priority='low', date_happened=1375296969)
t.assert_equal(u'_e{5,6}:Title|L1\\nL2|d:1375296969|p:low', self.recv())
self.statsd.event('Title', u'♬ †øU †øU ¥ºu T0µ ♪',
aggregation_key='key', tags=['t1', 't2:v2'])
t.assert_equal(u'_e{5,19}:Title|♬ †øU †øU ¥ºu T0µ ♪|k:key|#t1,t2:v2', self.recv())
def test_service_check(self):
now = int(time.time())
self.statsd.service_check(
'my_check.name', self.statsd.WARNING,
tags=['key1:val1', 'key2:val2'], timestamp=now,
hostname='i-abcd1234', message=u"♬ †øU \n†øU ¥ºu|m: T0µ ♪")
t.assert_equal(
u'_sc|my_check.name|{0}|d:{1}|h:i-abcd1234|#key1:val1,key2:val2|m:{2}'
.format(self.statsd.WARNING, now, u"♬ †øU \\n†øU ¥ºu|m\: T0µ ♪"), self.recv())
@staticmethod
def assert_almost_equal(a, b, delta):
assert 0 <= abs(a - b) <= delta, "%s - %s not within %s" % (a, b, delta)
#.........这里部分代码省略.........
示例12: StatsD
class StatsD(object):
def __init__(self, app=None, config=None):
"""
Constructor for `flask.ext.datadog.StatsD`
>>> from flask.ext.datadog import StatsD
>>> app = Flask(__name__)
>>> statsd = StatsD(app=app)
:param app: Flask app to configure this client for, if `app` is `None`, then do not
configure yet (call `init_app` manually instead)
:type app: flask.Flask or None
:param config: Configuration for this client to use instead of `app.config`
:type config: dict or None
"""
self.config = config
self.statsd = None
# If an app was provided, then call `init_app` for them
if app is not None:
self.init_app(app)
else:
self.app = None
def init_app(self, app, config=None):
"""
Initialize Datadog DogStatsd client from Flask app
>>> from flask.ext.datadog import StatsD
>>> app = Flask(__name__)
>>> statsd = StatsD()
>>> statsd.init_app(app=app)
Available DogStatsd config settings:
STATSD_HOST - statsd host to send metrics to (default: 'localhost')
STATSD_MAX_BUFFER_SIZE - max number of metrics to buffer before sending, only used when batching (default: 50)
STATSD_NAMESPACE - metric name prefix to use, e.g. 'app_name' (default: None)
STATSD_PORT - statsd port to send metrics to (default: 8125)
STATSD_TAGS - list of tags to include by default, e.g. ['env:prod'] (default: None)
STATSD_USEMS - whether or not to report timing in milliseconds (default: False)
Available Flask-Datadog config settings:
DATADOG_CONFIGURE_MIDDLEWARE - whether or not to setup response timing middleware (default: True)
DATADOG_RESPONSE_METRIC_NAME - the name of the response time metric (default: 'flask.response.time')
DATADOG_RESPONSE_SAMPLE_RATE - the sample rate to use for response timing middleware (default: 1)
DATADOG_RESPONSE_AUTO_TAG - whether to auto-add request/response tags to response metrics (default: True)
DATADOG_RESPONSE_ENDPOINT_TAG_NAME - tag name to use for request endpoint tag name (default: 'endpoint')
DATADOG_RESPONSE_METHOD_TAG_NAME - tag name to use for the request method tag name (default: 'method')
:param app: Flask app to configure this client for
:type app: flask.Flask
:param config: optional, dictionary of config values (defaults to `app.config`)
:type config: dict
"""
# Used passed in config if provided, otherwise use the config from `app`
if config is not None:
self.config = config
elif self.config is None:
self.config = app.config
# Set default values for expected config properties
self.config.setdefault('STATSD_HOST', 'localhost')
self.config.setdefault('STATSD_MAX_BUFFER_SIZE', 50)
self.config.setdefault('STATSD_NAMESPACE', None)
self.config.setdefault('STATSD_PORT', 8125)
self.config.setdefault('STATSD_TAGS', None)
self.config.setdefault('STATSD_USEMS', False)
self.app = app
# Configure DogStatsd client
# https://github.com/DataDog/datadogpy/blob/v0.11.0/datadog/dogstatsd/base.py
self.statsd = DogStatsd(host=self.config['STATSD_HOST'],
port=self.config['STATSD_PORT'],
max_buffer_size=self.config['STATSD_MAX_BUFFER_SIZE'],
namespace=self.config['STATSD_NAMESPACE'],
constant_tags=self.config['STATSD_TAGS'],
use_ms=self.config['STATSD_USEMS'])
# Configure any of our middleware
self.setup_middleware()
def timer(self, *args, **kwargs):
"""Helper to get a `flask_datadog.TimerWrapper` for this `DogStatsd` client"""
return TimerWrapper(self.statsd, *args, **kwargs)
def incr(self, *args, **kwargs):
"""Helper to expose `self.statsd.increment` under a shorter name"""
return self.statsd.increment(*args, **kwargs)
def decr(self, *args, **kwargs):
"""Helper to expose `self.statsd.decrement` under a shorter name"""
return self.statsd.decrement(*args, **kwargs)
def setup_middleware(self):
"""Helper to configure/setup any Flask-Datadog middleware"""
#.........这里部分代码省略.........