|
11 | 11 | import os
|
12 | 12 | import socket
|
13 | 13 | import requests
|
| 14 | +import time |
14 | 15 | import ssl
|
15 | 16 | import re
|
16 | 17 | from io import BytesIO, BufferedReader
|
@@ -142,46 +143,57 @@ class DatadogHTTPClient(object):
|
142 | 143 |
|
143 | 144 | def __init__(self, host, apiKey, max_retries=1, backoff=0.5):
|
144 | 145 | self._url = "https://{}/v1/input/{}".format(host, apiKey)
|
145 |
| - self._max_retries = max_retries |
146 |
| - self._backoff = backoff |
147 |
| - |
148 |
| - def send(self, content, metadata=None): |
149 |
| - retries = 0 |
150 |
| - body = json.dumps(content) |
151 |
| - while retries <= self._max_retries: |
| 146 | + self._max_retries = max_retries if max_retries >= 0 else 1 |
| 147 | + self._backoff = backoff if backoff > 0 else 0.5 |
| 148 | + |
| 149 | + def send(self, batch, metadata=None): |
| 150 | + """ |
| 151 | + Attemps to send a log with a linear retry strategy, |
| 152 | + only retries on server and network errors. |
| 153 | + """ |
| 154 | + body = json.dumps(batch) |
| 155 | + for retry in range(1 + self._max_retries): |
| 156 | + if retry > 0: |
| 157 | + print("Retrying, retry: {}, max: {}".format(retry, self._max_retries)) |
| 158 | + time.sleep(self._backoff) |
152 | 159 | try:
|
153 | 160 | resp = requests.post(
|
154 | 161 | self._url, headers=self._HEADERS, data=body, params=metadata
|
155 | 162 | )
|
156 |
| - if resp.status_code >= 500: |
157 |
| - # server error |
158 |
| - print( |
159 |
| - "Server error, status: {}, reason {}".format( |
160 |
| - resp.status_code, resp.reason |
161 |
| - ) |
162 |
| - ) |
163 |
| - elif resp.status_code >= 400: |
164 |
| - # client error |
165 |
| - print( |
166 |
| - "Client error, status: {}, reason {}".format( |
167 |
| - resp.status_code, resp.reason |
168 |
| - ) |
169 |
| - ) |
170 |
| - break |
171 |
| - else: |
172 |
| - # success |
173 |
| - return |
174 | 163 | except Exception as e:
|
| 164 | + # most likely a network error |
175 | 165 | print("Unexpected exception: {}".format(str(e)))
|
176 |
| - retries += 1 |
177 |
| - print("Could not send batch, dropping it") |
| 166 | + continue |
| 167 | + if resp.status_code >= 500: |
| 168 | + # server error |
| 169 | + print( |
| 170 | + "Server error, status {}, reason: {}".format( |
| 171 | + resp.status_code, resp.reason |
| 172 | + ) |
| 173 | + ) |
| 174 | + continue |
| 175 | + elif resp.status_code >= 400: |
| 176 | + # client error |
| 177 | + raise Exception( |
| 178 | + "client error, status: {}, reason {}".format( |
| 179 | + resp.status_code, resp.reason |
| 180 | + ) |
| 181 | + ) |
| 182 | + else: |
| 183 | + # success |
| 184 | + return |
| 185 | + raise Exception("max number of retries reached: {}".format(self._max_retries)) |
178 | 186 |
|
179 | 187 |
|
180 | 188 | class DatadogBatcher(object):
|
181 | 189 | def __init__(self, max_size=25):
|
182 | 190 | self._max_size = max_size
|
183 | 191 |
|
184 | 192 | def batch(self, logs):
|
| 193 | + """ |
| 194 | + Returns an array of batches, |
| 195 | + each batch contains at most max_size logs. |
| 196 | + """ |
185 | 197 | batches = []
|
186 | 198 | if len(logs) % self._max_size != 0:
|
187 | 199 | nb_batchs = int(len(logs) / self._max_size) + 1
|
|
0 commit comments