|
8 | 8 | import json |
9 | 9 | import time |
10 | 10 | import base64 |
| 11 | +import logging |
11 | 12 |
|
12 | 13 | import boto3 |
13 | 14 | from datadog import api |
14 | 15 | from datadog.threadstats import ThreadStats |
15 | 16 | from datadog_lambda import __version__ |
16 | 17 |
|
| 18 | +logger = logging.getLogger(__name__) |
| 19 | + |
17 | 20 | lambda_stats = ThreadStats() |
18 | 21 | lambda_stats.start() |
19 | 22 |
|
@@ -52,32 +55,38 @@ def lambda_metric(metric_name, value, timestamp=None, tags=None): |
52 | 55 | """ |
53 | 56 | tags = _tag_dd_lambda_layer(tags) |
54 | 57 | if os.environ.get('DD_FLUSH_TO_LOG', '').lower() == 'true': |
| 58 | + logger.debug('Sending metric %s to Datadog via log forwarder', metric_name) |
55 | 59 | print(json.dumps({ |
56 | 60 | 'm': metric_name, |
57 | 61 | 'v': value, |
58 | 62 | 'e': timestamp or int(time.time()), |
59 | 63 | 't': tags |
60 | 64 | })) |
61 | 65 | else: |
| 66 | + logger.debug('Sending metric %s to Datadog via lambda layer', metric_name) |
62 | 67 | lambda_stats.distribution( |
63 | 68 | metric_name, value, timestamp=timestamp, tags=tags |
64 | 69 | ) |
65 | 70 |
|
66 | 71 |
|
67 | 72 | # Decrypt code should run once and variables stored outside of the function |
68 | 73 | # handler so that these are decrypted once per container |
69 | | -DD_KMS_API_KEY = os.environ.get("DD_KMS_API_KEY") |
| 74 | +DD_KMS_API_KEY = os.environ.get('DD_KMS_API_KEY', '') |
70 | 75 | if DD_KMS_API_KEY: |
71 | | - DD_KMS_API_KEY = boto3.client("kms").decrypt( |
| 76 | + DD_KMS_API_KEY = boto3.client('kms').decrypt( |
72 | 77 | CiphertextBlob=base64.b64decode(DD_KMS_API_KEY) |
73 | | - )["Plaintext"] |
| 78 | + )['Plaintext'] |
74 | 79 |
|
75 | 80 | # Set API Key and Host in the module, so they only set once per container |
76 | 81 | api._api_key = os.environ.get( |
77 | 82 | 'DATADOG_API_KEY', |
78 | 83 | os.environ.get('DD_API_KEY', DD_KMS_API_KEY), |
79 | 84 | ) |
| 85 | +logger.debug('Setting DATADOG_API_KEY of length %d', len(api._api_key)) |
| 86 | + |
| 87 | +# Set DATADOG_HOST, to send data to a non-default Datadog datacenter |
80 | 88 | api._api_host = os.environ.get( |
81 | 89 | 'DATADOG_HOST', |
82 | 90 | 'https://api.' + os.environ.get('DD_SITE', 'datadoghq.com') |
83 | 91 | ) |
| 92 | +logger.debug('Setting DATADOG_HOST to %s', api._api_host) |
0 commit comments