|
5 | 5 | import os
|
6 | 6 | import re
|
7 | 7 | import time
|
8 |
| -import urllib.request |
9 |
| -from urllib.error import URLError |
| 8 | +import http.client |
10 | 9 | from collections import OrderedDict
|
11 | 10 | from typing import Union, List, Optional, Dict, Any
|
12 | 11 | from contextlib import contextmanager
|
13 | 12 | from base64 import b64encode
|
14 | 13 | import inspect
|
15 | 14 |
|
16 | 15 | EXECUTION_TAGS_KEY = "lumigo_execution_tags_no_scrub"
|
17 |
| -EDGE_HOST = "https://{region}.lumigo-tracer-edge.golumigo.com/api/spans" |
| 16 | +EDGE_HOST = "{region}.lumigo-tracer-edge.golumigo.com" |
| 17 | +EDGE_PATH = "/api/spans" |
18 | 18 | LOG_FORMAT = "#LUMIGO# - %(asctime)s - %(levelname)s - %(message)s"
|
19 | 19 | SECONDS_TO_TIMEOUT = 0.5
|
20 | 20 | LUMIGO_EVENT_KEY = "_lumigo"
|
21 | 21 | STEP_FUNCTION_UID_KEY = "step_function_uid"
|
22 | 22 | # number of spans that are too big to enter the reported message before break
|
23 | 23 | TOO_BIG_SPANS_THRESHOLD = 5
|
24 | 24 | MAX_SIZE_FOR_REQUEST: int = int(os.environ.get("LUMIGO_MAX_SIZE_FOR_REQUEST", 900_000))
|
| 25 | +EDGE_TIMEOUT = float(os.environ.get("LUMIGO_EDGE_TIMEOUT", SECONDS_TO_TIMEOUT)) |
25 | 26 | MAX_VARS_SIZE = 100_000
|
26 | 27 | MAX_VAR_LEN = 200
|
27 | 28 | MAX_ENTRY_SIZE = 2048
|
|
50 | 51 |
|
51 | 52 | _logger: Union[logging.Logger, None] = None
|
52 | 53 |
|
| 54 | +edge_connection = None |
| 55 | +try: |
| 56 | + # Try to establish the connection in initialization |
| 57 | + if os.environ.get("LUMIGO_INITIALIZATION_CONNECTION", "").lower() != "false": |
| 58 | + edge_connection = http.client.HTTPSConnection( # type: ignore |
| 59 | + EDGE_HOST.format(region=os.environ.get("AWS_REGION")), timeout=EDGE_TIMEOUT |
| 60 | + ) |
| 61 | + edge_connection.connect() |
| 62 | +except Exception: |
| 63 | + pass |
| 64 | + |
53 | 65 |
|
54 | 66 | class Configuration:
|
55 | 67 | should_report: bool = True
|
@@ -182,24 +194,32 @@ def report_json(region: Union[None, str], msgs: List[dict]) -> int:
|
182 | 194 | :return: The duration of reporting (in milliseconds),
|
183 | 195 | or 0 if we didn't send (due to configuration or fail).
|
184 | 196 | """
|
| 197 | + global edge_connection |
185 | 198 | get_logger().info(f"reporting the messages: {msgs[:10]}")
|
186 | 199 | host = Configuration.host or EDGE_HOST.format(region=region)
|
187 | 200 | duration = 0
|
| 201 | + if not edge_connection or edge_connection.host != host: |
| 202 | + try: |
| 203 | + edge_connection = http.client.HTTPSConnection( # type: ignore |
| 204 | + host.lstrip("https://").rstrip(EDGE_PATH), timeout=EDGE_TIMEOUT |
| 205 | + ) |
| 206 | + except Exception as e: |
| 207 | + get_logger().exception(f"Could not establish connection to {host}", exc_info=e) |
| 208 | + return duration |
188 | 209 | if Configuration.should_report:
|
189 | 210 | try:
|
190 | 211 | prune_trace: bool = not os.environ.get("LUMIGO_PRUNE_TRACE_OFF", "").lower() == "true"
|
191 | 212 | to_send = _create_request_body(msgs, prune_trace).encode()
|
192 | 213 | start_time = time.time()
|
193 |
| - response = urllib.request.urlopen( |
194 |
| - urllib.request.Request(host, to_send, headers={"Content-Type": "application/json"}), |
195 |
| - timeout=float(os.environ.get("LUMIGO_EDGE_TIMEOUT", SECONDS_TO_TIMEOUT)), |
| 214 | + edge_connection.request( |
| 215 | + "POST", EDGE_PATH, to_send, headers={"Content-Type": "application/json"} |
196 | 216 | )
|
| 217 | + response = edge_connection.getresponse() |
| 218 | + response.read() # We most read the response to keep the connection available |
197 | 219 | duration = int((time.time() - start_time) * 1000)
|
198 | 220 | get_logger().info(f"successful reporting, code: {getattr(response, 'code', 'unknown')}")
|
199 |
| - except URLError as e: |
200 |
| - get_logger().exception(f"Timeout when reporting to {host}", exc_info=e) |
201 | 221 | except Exception as e:
|
202 |
| - get_logger().exception(f"could not report json to {host}", exc_info=e) |
| 222 | + get_logger().exception(f"Could not report json to {host}", exc_info=e) |
203 | 223 | return duration
|
204 | 224 |
|
205 | 225 |
|
|
0 commit comments