Skip to content

Commit

Permalink
Merge pull request #5 from DataDog/compact-log-fmt-for-metric
Browse files Browse the repository at this point in the history
Shorten the metric log keys
  • Loading branch information
tianchu authored Jun 20, 2019
2 parents 9846c96 + 347176c commit 4b27d6e
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
CHANGELOG
=========
# v3 / 2019-06-18

* Log metrics in a compact format

# v2 / 2019-06-10

* Support submitting metrics through CloudWatch Logs
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Set the following Datadog environment variable to `datadoghq.eu` to send your da

If your Lambda function powers a performance-critical task (e.g., a consumer-facing API). You can avoid the added latencies of metric-submitting API calls, by setting the following Datadog environment variable to `True`. Custom metrics will be submitted asynchronously through CloudWatch Logs and [the Datadog log forwarder](https://github.com/DataDog/datadog-serverless-functions/tree/master/aws/logs_monitoring).

* DATADOG_FLUSH_TO_LOG
* DD_FLUSH_TO_LOG

### The Serverless Framework

Expand Down
2 changes: 1 addition & 1 deletion datadog_lambda/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2"
__version__ = "3"
12 changes: 6 additions & 6 deletions datadog_lambda/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def lambda_metric(metric_name, value, timestamp=None, tags=None):
Submit a data point to Datadog distribution metrics.
https://docs.datadoghq.com/graphing/metrics/distributions/
When DATADOG_LOG_FORWARDER is True, write metric to log, and
When DD_FLUSH_TO_LOG is True, write metric to log, and
wait for the Datadog Log Forwarder Lambda function to submit
the metrics asynchronously.
Expand All @@ -51,12 +51,12 @@ def lambda_metric(metric_name, value, timestamp=None, tags=None):
background thread.
"""
tags = _tag_dd_lambda_layer(tags)
if os.environ.get('DATADOG_FLUSH_TO_LOG') == 'True':
if os.environ.get('DD_FLUSH_TO_LOG', '').lower() == 'true':
print(json.dumps({
'metric_name': metric_name,
'value': value,
'timestamp': timestamp or int(time.time()),
'tags': tags
'm': metric_name,
'v': value,
'e': timestamp or int(time.time()),
't': tags
}))
else:
lambda_stats.distribution(
Expand Down
2 changes: 1 addition & 1 deletion datadog_lambda/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class _LambdaDecorator(object):

def __init__(self, func):
self.func = func
self.flush_to_log = os.environ.get('DATADOG_FLUSH_TO_LOG') == 'True'
self.flush_to_log = os.environ.get('DD_FLUSH_TO_LOG', '').lower() == 'true'

def _before(self, event, context):
try:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def test_lambda_metric_tagged_with_dd_lambda_layer(self):
])

def test_lambda_metric_flush_to_log(self):
os.environ["DATADOG_FLUSH_TO_LOG"] = 'True'
os.environ["DD_FLUSH_TO_LOG"] = 'True'

lambda_metric('test', 1)
self.mock_metric_lambda_stats.distribution.assert_not_called()

del os.environ["DATADOG_FLUSH_TO_LOG"]
del os.environ["DD_FLUSH_TO_LOG"]
4 changes: 2 additions & 2 deletions tests/test_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def lambda_handler(event, context):
self.mock_patch_all.assert_called()

def test_datadog_lambda_wrapper_flush_to_log(self):
os.environ["DATADOG_FLUSH_TO_LOG"] = 'True'
os.environ["DD_FLUSH_TO_LOG"] = 'True'

@datadog_lambda_wrapper
def lambda_handler(event, context):
Expand All @@ -60,4 +60,4 @@ def lambda_handler(event, context):
self.mock_extract_dd_trace_context.assert_called_with(lambda_event)
self.mock_patch_all.assert_called()

del os.environ["DATADOG_FLUSH_TO_LOG"]
del os.environ["DD_FLUSH_TO_LOG"]

0 comments on commit 4b27d6e

Please sign in to comment.