|
15 | 15 | from logs.datadog_client import DatadogClient |
16 | 16 | from logs.datadog_tcp_client import DatadogTCPClient |
17 | 17 | from logs.datadog_scrubber import DatadogScrubber |
18 | | -from logs.helpers import filter_logs |
| 18 | +from logs.helpers import filter_logs, add_retry_tag |
| 19 | +from retry.storage import Storage |
| 20 | +from retry.enums import RetryPrefix |
19 | 21 | from settings import ( |
20 | 22 | DD_API_KEY, |
21 | 23 | DD_USE_TCP, |
|
25 | 27 | DD_PORT, |
26 | 28 | DD_TRACE_INTAKE_URL, |
27 | 29 | DD_FORWARD_LOG, |
| 30 | + DD_STORE_FAILED_EVENTS, |
28 | 31 | SCRUBBING_RULE_CONFIGS, |
29 | 32 | INCLUDE_AT_MATCH, |
30 | 33 | EXCLUDE_AT_MATCH, |
31 | 34 | ) |
32 | 35 |
|
33 | 36 | logger = logging.getLogger() |
34 | 37 | logger.setLevel(logging.getLevelName(os.environ.get("DD_LOG_LEVEL", "INFO").upper())) |
35 | | -trace_connection = TraceConnection( |
36 | | - DD_TRACE_INTAKE_URL, DD_API_KEY, DD_SKIP_SSL_VALIDATION |
37 | | -) |
38 | 38 |
|
39 | 39 |
|
40 | | -def forward(logs, metrics, traces): |
41 | | - """ |
42 | | - Forward logs, metrics, and traces to Datadog in a background thread. |
43 | | - """ |
44 | | - if DD_FORWARD_LOG: |
45 | | - _forward_logs(logs) |
46 | | - |
47 | | - _forward_metrics(metrics) |
48 | | - |
49 | | - if len(traces) > 0: |
50 | | - _forward_traces(traces) |
51 | | - |
52 | | - |
53 | | -def _forward_logs(logs): |
54 | | - """Forward logs to Datadog""" |
55 | | - if logger.isEnabledFor(logging.DEBUG): |
56 | | - logger.debug(f"Forwarding {len(logs)} logs") |
57 | | - logs_to_forward = filter_logs( |
58 | | - [json.dumps(log, ensure_ascii=False) for log in logs], |
59 | | - include_pattern=INCLUDE_AT_MATCH, |
60 | | - exclude_pattern=EXCLUDE_AT_MATCH, |
61 | | - ) |
62 | | - scrubber = DatadogScrubber(SCRUBBING_RULE_CONFIGS) |
63 | | - if DD_USE_TCP: |
64 | | - batcher = DatadogBatcher(256 * 1000, 256 * 1000, 1) |
65 | | - cli = DatadogTCPClient(DD_URL, DD_PORT, DD_NO_SSL, DD_API_KEY, scrubber) |
66 | | - else: |
67 | | - batcher = DatadogBatcher(512 * 1000, 4 * 1000 * 1000, 400) |
68 | | - cli = DatadogHTTPClient( |
69 | | - DD_URL, DD_PORT, DD_NO_SSL, DD_SKIP_SSL_VALIDATION, DD_API_KEY, scrubber |
| 40 | +class Forwarder(object): |
| 41 | + def __init__(self, function_prefix): |
| 42 | + self.trace_connection = TraceConnection( |
| 43 | + DD_TRACE_INTAKE_URL, DD_API_KEY, DD_SKIP_SSL_VALIDATION |
70 | 44 | ) |
| 45 | + self.storage = Storage(function_prefix) |
71 | 46 |
|
72 | | - with DatadogClient(cli) as client: |
73 | | - try: |
| 47 | + def forward(self, logs, metrics, traces): |
| 48 | + """ |
| 49 | + Forward logs, metrics, and traces to Datadog in a background thread. |
| 50 | + """ |
| 51 | + if DD_FORWARD_LOG: |
| 52 | + self._forward_logs(logs) |
| 53 | + self._forward_metrics(metrics) |
| 54 | + self._forward_traces(traces) |
| 55 | + |
| 56 | + def retry(self): |
| 57 | + """ |
| 58 | + Retry forwarding logs, metrics, and traces to Datadog. |
| 59 | + """ |
| 60 | + for prefix in RetryPrefix: |
| 61 | + self._retry_prefix(prefix) |
| 62 | + |
| 63 | + def _retry_prefix(self, prefix): |
| 64 | + if logger.isEnabledFor(logging.DEBUG): |
| 65 | + logger.debug(f"Retrying {prefix} data") |
| 66 | + |
| 67 | + key_data = self.storage.get_data(prefix) |
| 68 | + |
| 69 | + for k, d in key_data.items(): |
| 70 | + if d is None: |
| 71 | + continue |
| 72 | + match prefix: |
| 73 | + case RetryPrefix.LOGS: |
| 74 | + self._forward_logs(d, key=k) |
| 75 | + case RetryPrefix.METRICS: |
| 76 | + self._forward_metrics(d, key=k) |
| 77 | + case RetryPrefix.TRACES: |
| 78 | + self._forward_traces(d, key=k) |
| 79 | + |
| 80 | + def _forward_logs(self, logs, key=None): |
| 81 | + """Forward logs to Datadog""" |
| 82 | + if logger.isEnabledFor(logging.DEBUG): |
| 83 | + logger.debug(f"Forwarding {len(logs)} logs") |
| 84 | + |
| 85 | + logs_to_forward = [] |
| 86 | + for log in logs: |
| 87 | + if key: |
| 88 | + log = add_retry_tag(log) |
| 89 | + logs_to_forward.append(json.dumps(log, ensure_ascii=False)) |
| 90 | + |
| 91 | + logs_to_forward = filter_logs( |
| 92 | + logs_to_forward, INCLUDE_AT_MATCH, EXCLUDE_AT_MATCH |
| 93 | + ) |
| 94 | + |
| 95 | + scrubber = DatadogScrubber(SCRUBBING_RULE_CONFIGS) |
| 96 | + if DD_USE_TCP: |
| 97 | + batcher = DatadogBatcher(256 * 1000, 256 * 1000, 1) |
| 98 | + cli = DatadogTCPClient(DD_URL, DD_PORT, DD_NO_SSL, DD_API_KEY, scrubber) |
| 99 | + else: |
| 100 | + batcher = DatadogBatcher(512 * 1000, 4 * 1000 * 1000, 400) |
| 101 | + cli = DatadogHTTPClient( |
| 102 | + DD_URL, DD_PORT, DD_NO_SSL, DD_SKIP_SSL_VALIDATION, DD_API_KEY, scrubber |
| 103 | + ) |
| 104 | + |
| 105 | + failed_logs = [] |
| 106 | + with DatadogClient(cli) as client: |
74 | 107 | for batch in batcher.batch(logs_to_forward): |
75 | | - client.send(batch) |
| 108 | + try: |
| 109 | + client.send(batch) |
| 110 | + except Exception: |
| 111 | + logger.exception(f"Exception while forwarding log batch {batch}") |
| 112 | + failed_logs.extend(batch) |
| 113 | + else: |
| 114 | + if logger.isEnabledFor(logging.DEBUG): |
| 115 | + logger.debug(f"Forwarded log batch: {batch}") |
| 116 | + if key: |
| 117 | + self.storage.delete_data(key) |
| 118 | + |
| 119 | + if DD_STORE_FAILED_EVENTS and len(failed_logs) > 0 and not key: |
| 120 | + self.storage.store_data(RetryPrefix.LOGS, failed_logs) |
| 121 | + |
| 122 | + send_event_metric("logs_forwarded", len(logs_to_forward) - len(failed_logs)) |
| 123 | + |
| 124 | + def _forward_metrics(self, metrics, key=None): |
| 125 | + """ |
| 126 | + Forward custom metrics submitted via logs to Datadog in a background thread |
| 127 | + using `lambda_stats` that is provided by the Datadog Python Lambda Layer. |
| 128 | + """ |
| 129 | + if logger.isEnabledFor(logging.DEBUG): |
| 130 | + logger.debug(f"Forwarding {len(metrics)} metrics") |
| 131 | + |
| 132 | + failed_metrics = [] |
| 133 | + for metric in metrics: |
| 134 | + try: |
| 135 | + send_log_metric(metric) |
| 136 | + except Exception: |
| 137 | + logger.exception( |
| 138 | + f"Exception while forwarding metric {json.dumps(metric)}" |
| 139 | + ) |
| 140 | + failed_metrics.append(metric) |
| 141 | + else: |
76 | 142 | if logger.isEnabledFor(logging.DEBUG): |
77 | | - logger.debug(f"Forwarded log batch: {json.dumps(batch)}") |
| 143 | + logger.debug(f"Forwarded metric: {json.dumps(metric)}") |
| 144 | + if key: |
| 145 | + self.storage.delete_data(key) |
| 146 | + |
| 147 | + if DD_STORE_FAILED_EVENTS and len(failed_metrics) > 0 and not key: |
| 148 | + self.storage.store_data(RetryPrefix.METRICS, failed_metrics) |
| 149 | + |
| 150 | + send_event_metric("metrics_forwarded", len(metrics) - len(failed_metrics)) |
| 151 | + |
| 152 | + def _forward_traces(self, traces, key=None): |
| 153 | + if not len(traces) > 0: |
| 154 | + return |
| 155 | + |
| 156 | + if logger.isEnabledFor(logging.DEBUG): |
| 157 | + logger.debug(f"Forwarding {len(traces)} traces") |
| 158 | + |
| 159 | + try: |
| 160 | + serialized_trace_paylods = json.dumps(traces) |
| 161 | + self.trace_connection.send_traces(serialized_trace_paylods) |
78 | 162 | except Exception: |
79 | 163 | logger.exception( |
80 | | - f"Exception while forwarding log batch {json.dumps(batch)}" |
| 164 | + f"Exception while forwarding traces {serialized_trace_paylods}" |
81 | 165 | ) |
| 166 | + if DD_STORE_FAILED_EVENTS and not key: |
| 167 | + self.storage.store_data(RetryPrefix.TRACES, traces) |
82 | 168 | else: |
83 | | - send_event_metric("logs_forwarded", len(logs_to_forward)) |
84 | | - |
85 | | - |
86 | | -def _forward_metrics(metrics): |
87 | | - """ |
88 | | - Forward custom metrics submitted via logs to Datadog in a background thread |
89 | | - using `lambda_stats` that is provided by the Datadog Python Lambda Layer. |
90 | | - """ |
91 | | - if logger.isEnabledFor(logging.DEBUG): |
92 | | - logger.debug(f"Forwarding {len(metrics)} metrics") |
93 | | - try: |
94 | | - for metric in metrics: |
95 | | - send_log_metric(metric) |
96 | 169 | if logger.isEnabledFor(logging.DEBUG): |
97 | | - logger.debug(f"Forwarded metric: {json.dumps(metric)}") |
98 | | - except Exception: |
99 | | - logger.exception(f"Exception while forwarding metric {json.dumps(metric)}") |
100 | | - else: |
101 | | - send_event_metric("metrics_forwarded", len(metrics)) |
102 | | - |
103 | | - |
104 | | -def _forward_traces(trace_payloads): |
105 | | - if logger.isEnabledFor(logging.DEBUG): |
106 | | - logger.debug(f"Forwarding {len(trace_payloads)} traces") |
107 | | - try: |
108 | | - trace_connection.send_traces(trace_payloads) |
109 | | - except Exception: |
110 | | - logger.exception( |
111 | | - f"Exception while forwarding traces {json.dumps(trace_payloads)}" |
112 | | - ) |
113 | | - else: |
114 | | - if logger.isEnabledFor(logging.DEBUG): |
115 | | - logger.debug(f"Forwarded traces: {json.dumps(trace_payloads)}") |
116 | | - send_event_metric("traces_forwarded", len(trace_payloads)) |
| 170 | + logger.debug(f"Forwarded traces: {serialized_trace_paylods}") |
| 171 | + if key: |
| 172 | + self.storage.delete_data(key) |
| 173 | + send_event_metric("traces_forwarded", len(traces)) |
0 commit comments