本文整理汇总了Python中datadog.dogstatsd.base.DogStatsd.__enter__方法的典型用法代码示例。如果您正苦于以下问题:Python DogStatsd.__enter__方法的具体用法?Python DogStatsd.__enter__怎么用?Python DogStatsd.__enter__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类datadog.dogstatsd.base.DogStatsd
的用法示例。
在下文中一共展示了DogStatsd.__enter__方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: StatsD
# 需要导入模块: from datadog.dogstatsd.base import DogStatsd [as 别名]
# 或者: from datadog.dogstatsd.base.DogStatsd import __enter__ [as 别名]
#.........这里部分代码省略.........
def before_request(self):
"""
Flask-Datadog middleware handle for before each request
"""
# Set the request start time
g.flask_datadog_start_time = time.time()
g.flask_datadog_request_tags = []
# Add some default request tags
if self.config['DATADOG_RESPONSE_AUTO_TAG']:
self.add_request_tags([
# Endpoint tag
'{tag_name}:{endpoint}'.format(tag_name=self.config['DATADOG_RESPONSE_ENDPOINT_TAG_NAME'],
endpoint=str(request.endpoint).lower()),
# Method tag
'{tag_name}:{method}'.format(tag_name=self.config['DATADOG_RESPONSE_METHOD_TAG_NAME'],
method=request.method.lower()),
])
def after_request(self, response):
"""
Flask-Datadog middleware handler for after each request
:param response: the response to be sent to the client
:type response: ``flask.Response``
:rtype: ``flask.Response``
"""
# Return early if we don't have the start time
if not hasattr(g, 'flask_datadog_start_time'):
return response
# Get the response time for this request
elapsed = time.time() - g.flask_datadog_start_time
# Convert the elapsed time to milliseconds if they want them
if self.use_ms:
elapsed = int(round(1000 * elapsed))
# Add some additional response tags
if self.config['DATADOG_RESPONSE_AUTO_TAG']:
self.add_request_tags(['status_code:%s' % (response.status_code, )])
# Emit our timing metric
self.statsd.timing(self.config['DATADOG_RESPONSE_METRIC_NAME'],
elapsed,
self.get_request_tags(),
self.config['DATADOG_RESPONSE_SAMPLE_RATE'])
# We ALWAYS have to return the original response
return response
def get_request_tags(self):
"""
Get the current list of tags set for this request
:rtype: list
"""
return getattr(g, 'flask_datadog_request_tags', [])
def add_request_tags(self, tags):
"""
Add the provided list of tags to the tags stored for this request
:param tags: tags to add to this requests tags
:type tags: list
:rtype: list
"""
# Get the current list of tags to append to
# DEV: We use this method since ``self.get_request_tags`` will ensure that we get a list back
current_tags = self.get_request_tags()
# Append our new tags, and return the new full list of tags for this request
g.flask_datadog_request_tags = current_tags + tags
return g.flask_datadog_request_tags
def __getattr__(self, name):
"""
Magic method for fetching any underlying attributes from `self.statsd`
We utilize `__getattr__` to ensure that we are always compatible with
the `DogStatsd` client.
"""
# If `self.statsd` has the attribute then return that attribute
if self.statsd and hasattr(self.statsd, name):
return getattr(self.statsd, name)
raise AttributeError('\'StatsD\' has has attribute \'{name}\''.format(name=name))
def __enter__(self):
"""
Helper to expose the underlying `DogStatsd` client for context managing
>>> statsd = StatsD(app=app)
>>> # Batch any metrics within the `with` block
>>> with statsd:
>>> statsd.increment('metric')
"""
return self.statsd.__enter__()
def __exit__(self, *args, **kwargs):
"""Helper to expose the underlying `DogStatsd` client for context managing"""
return self.statsd.__exit__(*args, **kwargs)