libo11y 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- libo11y/__init__.py +67 -0
- libo11y/_internal/__init__.py +3 -0
- libo11y/_internal/endpoint.py +421 -0
- libo11y/_internal/env.py +103 -0
- libo11y/_internal/exporters.py +278 -0
- libo11y/_internal/http_common.py +269 -0
- libo11y/_internal/identity.py +30 -0
- libo11y/_internal/logging_handler.py +292 -0
- libo11y/_internal/logs_bridge.py +174 -0
- libo11y/_internal/metrics_bridge.py +337 -0
- libo11y/_internal/pii.py +202 -0
- libo11y/_internal/policy.py +267 -0
- libo11y/_internal/propagators.py +88 -0
- libo11y/_internal/resource.py +199 -0
- libo11y/_internal/runtime_metrics.py +122 -0
- libo11y/_internal/sqltext.py +441 -0
- libo11y/_internal/trace_bridge.py +225 -0
- libo11y/_internal/yamlv3.py +523 -0
- libo11y/asgi.py +303 -0
- libo11y/buildinfo.py +96 -0
- libo11y/connect.py +546 -0
- libo11y/dbapi.py +435 -0
- libo11y/detached.py +330 -0
- libo11y/errors.py +83 -0
- libo11y/grpc.py +913 -0
- libo11y/httpx.py +197 -0
- libo11y/options.py +137 -0
- libo11y/proc.py +122 -0
- libo11y/py.typed +0 -0
- libo11y/record.py +87 -0
- libo11y/redact.py +267 -0
- libo11y/requests.py +168 -0
- libo11y/signals.py +102 -0
- libo11y/spec/__init__.py +269 -0
- libo11y/spec/_generated/__init__.py +3 -0
- libo11y/spec/_generated/goprint.py +1430 -0
- libo11y/spec/_generated/spec.py +246 -0
- libo11y/spec/_generated/tables.py +784 -0
- libo11y/spec/_goquote.py +44 -0
- libo11y/spec/appregistry.py +524 -0
- libo11y/telemetry.py +553 -0
- libo11y/testing/__init__.py +19 -0
- libo11y/testing/_pytest_plugin.py +20 -0
- libo11y/testing/receivers.py +257 -0
- libo11y/testing/recorder.py +337 -0
- libo11y/wsgi.py +330 -0
- libo11y-0.1.0.dist-info/METADATA +137 -0
- libo11y-0.1.0.dist-info/RECORD +50 -0
- libo11y-0.1.0.dist-info/WHEEL +4 -0
- libo11y-0.1.0.dist-info/entry_points.txt +2 -0
libo11y/__init__.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""Opinionated, elegant observability for Python built purely on OpenTelemetry."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from libo11y import buildinfo, detached, proc, redact, spec
|
|
6
|
+
from libo11y._internal.logging_handler import force_emit
|
|
7
|
+
from libo11y.errors import (
|
|
8
|
+
AlreadySetup,
|
|
9
|
+
CanaryRejected,
|
|
10
|
+
ExporterError,
|
|
11
|
+
InvalidAppRegistry,
|
|
12
|
+
InvalidCarrier,
|
|
13
|
+
InvalidEnvironment,
|
|
14
|
+
NoEnvironment,
|
|
15
|
+
NoServiceName,
|
|
16
|
+
O11yError,
|
|
17
|
+
PIIHashKey,
|
|
18
|
+
UnsupportedProtocol,
|
|
19
|
+
)
|
|
20
|
+
from libo11y.options import (
|
|
21
|
+
HERMETIC,
|
|
22
|
+
AttrReplacer,
|
|
23
|
+
BaggagePredicate,
|
|
24
|
+
ConsoleFormat,
|
|
25
|
+
ErrorHandler,
|
|
26
|
+
Options,
|
|
27
|
+
PIIMode,
|
|
28
|
+
)
|
|
29
|
+
from libo11y.record import Record, current_record, start_record
|
|
30
|
+
from libo11y.signals import ashutdown_on_signals, shutdown_on_signals
|
|
31
|
+
from libo11y.telemetry import ShutdownReport, Telemetry, init, setup
|
|
32
|
+
|
|
33
|
+
__all__ = [
|
|
34
|
+
"HERMETIC",
|
|
35
|
+
"AlreadySetup",
|
|
36
|
+
"AttrReplacer",
|
|
37
|
+
"BaggagePredicate",
|
|
38
|
+
"CanaryRejected",
|
|
39
|
+
"ConsoleFormat",
|
|
40
|
+
"ErrorHandler",
|
|
41
|
+
"ExporterError",
|
|
42
|
+
"InvalidAppRegistry",
|
|
43
|
+
"InvalidCarrier",
|
|
44
|
+
"InvalidEnvironment",
|
|
45
|
+
"NoEnvironment",
|
|
46
|
+
"NoServiceName",
|
|
47
|
+
"O11yError",
|
|
48
|
+
"Options",
|
|
49
|
+
"PIIHashKey",
|
|
50
|
+
"PIIMode",
|
|
51
|
+
"Record",
|
|
52
|
+
"ShutdownReport",
|
|
53
|
+
"Telemetry",
|
|
54
|
+
"UnsupportedProtocol",
|
|
55
|
+
"ashutdown_on_signals",
|
|
56
|
+
"buildinfo",
|
|
57
|
+
"current_record",
|
|
58
|
+
"detached",
|
|
59
|
+
"force_emit",
|
|
60
|
+
"init",
|
|
61
|
+
"proc",
|
|
62
|
+
"redact",
|
|
63
|
+
"setup",
|
|
64
|
+
"shutdown_on_signals",
|
|
65
|
+
"spec",
|
|
66
|
+
"start_record",
|
|
67
|
+
]
|
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
"""OTLP endpoint sanitization matching Go's net/url.Parse and URL.String()."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from enum import Enum
|
|
7
|
+
from typing import Final
|
|
8
|
+
|
|
9
|
+
WITHHELD: Final[str] = "unparseable"
|
|
10
|
+
|
|
11
|
+
_HEX_UPPER: Final[str] = "0123456789ABCDEF"
|
|
12
|
+
_HOST_OK: Final[frozenset[int]] = frozenset(b"!$&'()*+,;=:<>\"" + b"[]")
|
|
13
|
+
_GEN_DELIMS: Final[frozenset[int]] = frozenset(b"$&+,/:;=?@")
|
|
14
|
+
_UNRESERVED_PUNCT: Final[frozenset[int]] = frozenset(b"-_.~")
|
|
15
|
+
_FRAG_OK: Final[frozenset[int]] = frozenset(b"!()*")
|
|
16
|
+
_USERINFO_PUNCT: Final[frozenset[int]] = frozenset(b"-._:~!$&'()*+,;=%@")
|
|
17
|
+
_VALID_ENC_EXTRA: Final[frozenset[int]] = frozenset(b"!$&'()*+,;=:@[]%")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class _Mode(Enum):
|
|
21
|
+
PATH = 1
|
|
22
|
+
HOST = 2
|
|
23
|
+
ZONE = 3
|
|
24
|
+
USER_PASSWORD = 4
|
|
25
|
+
FRAGMENT = 5
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class _InvalidURLError(Exception):
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass
|
|
33
|
+
class _Url:
|
|
34
|
+
scheme: str = ""
|
|
35
|
+
opaque_text: bytes = b""
|
|
36
|
+
has_user: bool = False
|
|
37
|
+
host: bytes = b""
|
|
38
|
+
path: bytes = b""
|
|
39
|
+
raw_path: bytes = b""
|
|
40
|
+
omit_host: bool = False
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _is_alnum(c: int) -> bool:
|
|
44
|
+
return (0x30 <= c <= 0x39) or (0x41 <= c <= 0x5A) or (0x61 <= c <= 0x7A)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _is_hex(c: int) -> bool:
|
|
48
|
+
return (0x30 <= c <= 0x39) or (0x41 <= c <= 0x46) or (0x61 <= c <= 0x66)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _unhex(c: int) -> int:
|
|
52
|
+
if 0x30 <= c <= 0x39:
|
|
53
|
+
return c - 0x30
|
|
54
|
+
if 0x61 <= c <= 0x66:
|
|
55
|
+
return c - 0x61 + 10
|
|
56
|
+
return c - 0x41 + 10
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _should_escape(c: int, mode: _Mode) -> bool:
|
|
60
|
+
if _is_alnum(c):
|
|
61
|
+
return False
|
|
62
|
+
if mode in (_Mode.HOST, _Mode.ZONE) and c in _HOST_OK:
|
|
63
|
+
return False
|
|
64
|
+
if c in _UNRESERVED_PUNCT:
|
|
65
|
+
return False
|
|
66
|
+
if c in _GEN_DELIMS:
|
|
67
|
+
if mode is _Mode.PATH:
|
|
68
|
+
return c == ord("?")
|
|
69
|
+
if mode is _Mode.USER_PASSWORD:
|
|
70
|
+
return c in (ord("@"), ord("/"), ord("?"), ord(":"))
|
|
71
|
+
return mode is not _Mode.FRAGMENT
|
|
72
|
+
return not (mode is _Mode.FRAGMENT and c in _FRAG_OK)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _unescape(s: bytes, mode: _Mode) -> bytes:
|
|
76
|
+
n = 0
|
|
77
|
+
i = 0
|
|
78
|
+
length = len(s)
|
|
79
|
+
while i < length:
|
|
80
|
+
c = s[i]
|
|
81
|
+
if c == 0x25: # '%'
|
|
82
|
+
n += 1
|
|
83
|
+
if i + 2 >= length or not _is_hex(s[i + 1]) or not _is_hex(s[i + 2]):
|
|
84
|
+
raise _InvalidURLError
|
|
85
|
+
pct25 = s[i : i + 3] == b"%25"
|
|
86
|
+
if mode is _Mode.HOST and _unhex(s[i + 1]) < 8 and not pct25:
|
|
87
|
+
raise _InvalidURLError
|
|
88
|
+
if mode is _Mode.ZONE:
|
|
89
|
+
v = (_unhex(s[i + 1]) << 4) | _unhex(s[i + 2])
|
|
90
|
+
if not pct25 and v != 0x20 and _should_escape(v, _Mode.HOST):
|
|
91
|
+
raise _InvalidURLError
|
|
92
|
+
i += 3
|
|
93
|
+
else:
|
|
94
|
+
if mode in (_Mode.HOST, _Mode.ZONE) and c < 0x80 and _should_escape(c, mode):
|
|
95
|
+
raise _InvalidURLError
|
|
96
|
+
i += 1
|
|
97
|
+
if n == 0:
|
|
98
|
+
return s
|
|
99
|
+
out = bytearray(length - 2 * n)
|
|
100
|
+
j = 0
|
|
101
|
+
i = 0
|
|
102
|
+
while i < length:
|
|
103
|
+
if s[i] == 0x25:
|
|
104
|
+
out[j] = (_unhex(s[i + 1]) << 4) | _unhex(s[i + 2])
|
|
105
|
+
i += 3
|
|
106
|
+
else:
|
|
107
|
+
out[j] = s[i]
|
|
108
|
+
i += 1
|
|
109
|
+
j += 1
|
|
110
|
+
return bytes(out)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _escape(s: bytes, mode: _Mode) -> bytes:
|
|
114
|
+
count = sum(1 for c in s if _should_escape(c, mode))
|
|
115
|
+
if count == 0:
|
|
116
|
+
return s
|
|
117
|
+
out = bytearray(len(s) + 2 * count)
|
|
118
|
+
j = 0
|
|
119
|
+
for c in s:
|
|
120
|
+
if _should_escape(c, mode):
|
|
121
|
+
out[j] = 0x25
|
|
122
|
+
out[j + 1] = ord(_HEX_UPPER[c >> 4])
|
|
123
|
+
out[j + 2] = ord(_HEX_UPPER[c & 0x0F])
|
|
124
|
+
j += 3
|
|
125
|
+
else:
|
|
126
|
+
out[j] = c
|
|
127
|
+
j += 1
|
|
128
|
+
return bytes(out)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _valid_encoded(s: bytes, mode: _Mode) -> bool:
|
|
132
|
+
for c in s:
|
|
133
|
+
if c in _VALID_ENC_EXTRA:
|
|
134
|
+
continue
|
|
135
|
+
if _should_escape(c, mode):
|
|
136
|
+
return False
|
|
137
|
+
return True
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def _valid_optional_port(port: bytes) -> bool:
|
|
141
|
+
if not port:
|
|
142
|
+
return True
|
|
143
|
+
if port[0] != ord(":"):
|
|
144
|
+
return False
|
|
145
|
+
return all(0x30 <= c <= 0x39 for c in port[1:])
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _valid_userinfo(s: bytes) -> bool:
|
|
149
|
+
return all(_is_alnum(c) or c in _USERINFO_PUNCT for c in s)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def _get_scheme(raw: bytes) -> tuple[bytes, bytes]:
|
|
153
|
+
for i, c in enumerate(raw):
|
|
154
|
+
if (0x41 <= c <= 0x5A) or (0x61 <= c <= 0x7A):
|
|
155
|
+
continue
|
|
156
|
+
if (0x30 <= c <= 0x39) or c in (ord("+"), ord("-"), ord(".")):
|
|
157
|
+
if i == 0:
|
|
158
|
+
return b"", raw
|
|
159
|
+
continue
|
|
160
|
+
if c == ord(":"):
|
|
161
|
+
if i == 0:
|
|
162
|
+
raise _InvalidURLError
|
|
163
|
+
return raw[:i], raw[i + 1 :]
|
|
164
|
+
return b"", raw
|
|
165
|
+
return b"", raw
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _valid_ipv4(s: bytes) -> bool:
|
|
169
|
+
val = 0
|
|
170
|
+
pos = 0
|
|
171
|
+
dig_len = 0
|
|
172
|
+
length = len(s)
|
|
173
|
+
for i, c in enumerate(s):
|
|
174
|
+
if 0x30 <= c <= 0x39:
|
|
175
|
+
if dig_len == 1 and val == 0:
|
|
176
|
+
return False
|
|
177
|
+
val = val * 10 + (c - 0x30)
|
|
178
|
+
dig_len += 1
|
|
179
|
+
if val > 255:
|
|
180
|
+
return False
|
|
181
|
+
elif c == ord("."):
|
|
182
|
+
if i == 0 or i == length - 1 or s[i - 1] == ord("."):
|
|
183
|
+
return False
|
|
184
|
+
if pos == 3:
|
|
185
|
+
return False
|
|
186
|
+
pos += 1
|
|
187
|
+
val = 0
|
|
188
|
+
dig_len = 0
|
|
189
|
+
else:
|
|
190
|
+
return False
|
|
191
|
+
return pos == 3
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _parse_ipv6(data: bytes) -> bool:
|
|
195
|
+
s = data
|
|
196
|
+
pct = s.find(b"%")
|
|
197
|
+
if pct != -1:
|
|
198
|
+
if pct + 1 == len(s):
|
|
199
|
+
return False
|
|
200
|
+
s = s[:pct]
|
|
201
|
+
ellipsis: int | None = None
|
|
202
|
+
if len(s) >= 2 and s[0] == ord(":") and s[1] == ord(":"):
|
|
203
|
+
ellipsis = 0
|
|
204
|
+
s = s[2:]
|
|
205
|
+
if not s:
|
|
206
|
+
return True
|
|
207
|
+
i = 0
|
|
208
|
+
while i < 16:
|
|
209
|
+
off = 0
|
|
210
|
+
acc = 0
|
|
211
|
+
while off < len(s):
|
|
212
|
+
c = s[off]
|
|
213
|
+
if not _is_hex(c):
|
|
214
|
+
break
|
|
215
|
+
acc = (acc << 4) + _unhex(c)
|
|
216
|
+
if off > 3 or acc > 0xFFFF:
|
|
217
|
+
return False
|
|
218
|
+
off += 1
|
|
219
|
+
if off == 0:
|
|
220
|
+
return False
|
|
221
|
+
if off < len(s) and s[off] == ord("."):
|
|
222
|
+
if ellipsis is None and i != 12:
|
|
223
|
+
return False
|
|
224
|
+
if i + 4 > 16 or not _valid_ipv4(s):
|
|
225
|
+
return False
|
|
226
|
+
s = b""
|
|
227
|
+
i += 4
|
|
228
|
+
break
|
|
229
|
+
i += 2
|
|
230
|
+
s = s[off:]
|
|
231
|
+
if not s:
|
|
232
|
+
break
|
|
233
|
+
if s[0] != ord(":") or len(s) == 1:
|
|
234
|
+
return False
|
|
235
|
+
s = s[1:]
|
|
236
|
+
if s[0] == ord(":"):
|
|
237
|
+
if ellipsis is not None:
|
|
238
|
+
return False
|
|
239
|
+
ellipsis = i
|
|
240
|
+
s = s[1:]
|
|
241
|
+
if not s:
|
|
242
|
+
break
|
|
243
|
+
if s:
|
|
244
|
+
return False
|
|
245
|
+
return (i < 16) == (ellipsis is not None)
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def _is_ipv6_literal(s: bytes) -> bool:
|
|
249
|
+
for c in s:
|
|
250
|
+
if c in (ord("."), ord("%")):
|
|
251
|
+
return False
|
|
252
|
+
if c == ord(":"):
|
|
253
|
+
return _parse_ipv6(s)
|
|
254
|
+
return False
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def _parse_host(scheme: str, host: bytes) -> bytes:
|
|
258
|
+
open_idx = host.rfind(b"[")
|
|
259
|
+
if open_idx != -1:
|
|
260
|
+
if open_idx > 0:
|
|
261
|
+
raise _InvalidURLError
|
|
262
|
+
close_idx = host.rfind(b"]")
|
|
263
|
+
if close_idx == -1:
|
|
264
|
+
raise _InvalidURLError
|
|
265
|
+
colon_port = host[close_idx + 1 :]
|
|
266
|
+
if not _valid_optional_port(colon_port):
|
|
267
|
+
raise _InvalidURLError
|
|
268
|
+
port = _unescape(colon_port, _Mode.HOST)
|
|
269
|
+
hostname = host[1:close_idx]
|
|
270
|
+
zone = hostname.find(b"%25")
|
|
271
|
+
if zone != -1:
|
|
272
|
+
unescaped = _unescape(hostname[:zone], _Mode.HOST) + _unescape(
|
|
273
|
+
hostname[zone:], _Mode.ZONE
|
|
274
|
+
)
|
|
275
|
+
else:
|
|
276
|
+
unescaped = _unescape(hostname, _Mode.HOST)
|
|
277
|
+
if not _is_ipv6_literal(unescaped):
|
|
278
|
+
raise _InvalidURLError
|
|
279
|
+
return b"[" + unescaped + b"]" + port
|
|
280
|
+
first = host.find(b":")
|
|
281
|
+
if first != -1:
|
|
282
|
+
idx = first
|
|
283
|
+
last = host.rfind(b":")
|
|
284
|
+
if last != first and scheme not in ("http", "https"):
|
|
285
|
+
idx = last
|
|
286
|
+
if not _valid_optional_port(host[idx:]):
|
|
287
|
+
raise _InvalidURLError
|
|
288
|
+
return _unescape(host, _Mode.HOST)
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def _parse_authority(url: _Url, authority: bytes) -> None:
|
|
292
|
+
at = authority.rfind(b"@")
|
|
293
|
+
url.host = _parse_host(url.scheme, authority[at + 1 :] if at != -1 else authority)
|
|
294
|
+
if at == -1:
|
|
295
|
+
return
|
|
296
|
+
userinfo = authority[:at]
|
|
297
|
+
if not _valid_userinfo(userinfo):
|
|
298
|
+
raise _InvalidURLError
|
|
299
|
+
colon = userinfo.find(b":")
|
|
300
|
+
if colon != -1:
|
|
301
|
+
_unescape(userinfo[:colon], _Mode.USER_PASSWORD)
|
|
302
|
+
_unescape(userinfo[colon + 1 :], _Mode.USER_PASSWORD)
|
|
303
|
+
else:
|
|
304
|
+
_unescape(userinfo, _Mode.USER_PASSWORD)
|
|
305
|
+
url.has_user = True
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def _parse_reference(raw: bytes) -> _Url:
|
|
309
|
+
for c in raw:
|
|
310
|
+
if c < 0x20 or c == 0x7F:
|
|
311
|
+
raise _InvalidURLError
|
|
312
|
+
url = _Url()
|
|
313
|
+
if raw == b"*":
|
|
314
|
+
url.path = b"*"
|
|
315
|
+
return url
|
|
316
|
+
scheme_bytes, rest = _get_scheme(raw)
|
|
317
|
+
url.scheme = scheme_bytes.decode("ascii").lower()
|
|
318
|
+
if rest.endswith(b"?") and rest.count(b"?") == 1:
|
|
319
|
+
rest = rest[:-1]
|
|
320
|
+
else:
|
|
321
|
+
q = rest.find(b"?")
|
|
322
|
+
if q != -1:
|
|
323
|
+
rest = rest[:q]
|
|
324
|
+
if not rest.startswith(b"/"):
|
|
325
|
+
if url.scheme:
|
|
326
|
+
url.opaque_text = rest
|
|
327
|
+
return url
|
|
328
|
+
slash = rest.find(b"/")
|
|
329
|
+
segment = rest[:slash] if slash != -1 else rest
|
|
330
|
+
if b":" in segment:
|
|
331
|
+
raise _InvalidURLError
|
|
332
|
+
if (url.scheme or not rest.startswith(b"///")) and rest.startswith(b"//"):
|
|
333
|
+
authority = rest[2:]
|
|
334
|
+
rest = b""
|
|
335
|
+
slash = authority.find(b"/")
|
|
336
|
+
if slash != -1:
|
|
337
|
+
rest = authority[slash:]
|
|
338
|
+
authority = authority[:slash]
|
|
339
|
+
_parse_authority(url, authority)
|
|
340
|
+
elif url.scheme and rest.startswith(b"/"):
|
|
341
|
+
url.omit_host = True
|
|
342
|
+
url.path = _unescape(rest, _Mode.PATH)
|
|
343
|
+
escaped = _escape(url.path, _Mode.PATH)
|
|
344
|
+
url.raw_path = b"" if rest == escaped else rest
|
|
345
|
+
return url
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def _parse(raw: bytes) -> _Url:
|
|
349
|
+
h = raw.find(b"#")
|
|
350
|
+
url = _parse_reference(raw[:h] if h != -1 else raw)
|
|
351
|
+
if h != -1 and h + 1 < len(raw):
|
|
352
|
+
_unescape(raw[h + 1 :], _Mode.FRAGMENT)
|
|
353
|
+
return url
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def _escaped_path(url: _Url) -> bytes:
|
|
357
|
+
if url.raw_path and _valid_encoded(url.raw_path, _Mode.PATH):
|
|
358
|
+
try:
|
|
359
|
+
p = _unescape(url.raw_path, _Mode.PATH)
|
|
360
|
+
except _InvalidURLError:
|
|
361
|
+
p = None
|
|
362
|
+
if p == url.path:
|
|
363
|
+
return url.raw_path
|
|
364
|
+
if url.path == b"*":
|
|
365
|
+
return b"*"
|
|
366
|
+
return _escape(url.path, _Mode.PATH)
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def _format(url: _Url) -> bytes:
|
|
370
|
+
out = bytearray()
|
|
371
|
+
if url.scheme:
|
|
372
|
+
out.extend(url.scheme.encode("ascii"))
|
|
373
|
+
out.append(ord(":"))
|
|
374
|
+
if url.opaque_text:
|
|
375
|
+
out.extend(url.opaque_text)
|
|
376
|
+
return bytes(out)
|
|
377
|
+
if (url.scheme or url.host) and not (url.omit_host and not url.host):
|
|
378
|
+
if url.host or url.path:
|
|
379
|
+
out.extend(b"//")
|
|
380
|
+
if url.host:
|
|
381
|
+
out.extend(_escape(url.host, _Mode.HOST))
|
|
382
|
+
path = _escaped_path(url)
|
|
383
|
+
if path and path[0] != ord("/") and url.host:
|
|
384
|
+
out.append(ord("/"))
|
|
385
|
+
if not out:
|
|
386
|
+
slash = path.find(b"/")
|
|
387
|
+
segment = path[:slash] if slash != -1 else path
|
|
388
|
+
if b":" in segment:
|
|
389
|
+
out.extend(b"./")
|
|
390
|
+
out.extend(path)
|
|
391
|
+
return bytes(out)
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
def sanitize_endpoint(raw: str) -> str:
|
|
395
|
+
"""Strip userinfo, query, and fragment from an OTLP endpoint, matching Go.
|
|
396
|
+
|
|
397
|
+
Any ambiguous or malformed value returns "unparseable" so credentials are
|
|
398
|
+
never echoed.
|
|
399
|
+
"""
|
|
400
|
+
return sanitize_endpoint_bytes(raw.encode("utf-8"))
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def sanitize_endpoint_bytes(raw: bytes) -> str:
|
|
404
|
+
"""Byte-level implementation of sanitize_endpoint matching Go's net/url."""
|
|
405
|
+
schemeless = b"://" not in raw
|
|
406
|
+
data = b"//" + raw if schemeless else raw
|
|
407
|
+
try:
|
|
408
|
+
url = _parse(data)
|
|
409
|
+
except _InvalidURLError:
|
|
410
|
+
return WITHHELD
|
|
411
|
+
if not url.host:
|
|
412
|
+
return WITHHELD
|
|
413
|
+
ats = data.count(b"@")
|
|
414
|
+
if ats > 1 or (ats == 1 and not url.has_user):
|
|
415
|
+
return WITHHELD
|
|
416
|
+
out = _format(url)
|
|
417
|
+
if b"@" in out:
|
|
418
|
+
return WITHHELD
|
|
419
|
+
if schemeless and out.startswith(b"//"):
|
|
420
|
+
out = out[2:]
|
|
421
|
+
return out.decode("ascii")
|
libo11y/_internal/env.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""Explicit environment reading and OTEL_* parsing for libo11y.
|
|
2
|
+
|
|
3
|
+
All environment reads pass through EnvView so hermetic mode (Options(env=HERMETIC))
|
|
4
|
+
never touches os.environ, and every setting is forwarded explicitly to the SDK.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import os
|
|
10
|
+
from collections.abc import Mapping
|
|
11
|
+
from urllib.parse import unquote
|
|
12
|
+
|
|
13
|
+
from libo11y.options import HERMETIC, is_hermetic
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class EnvView:
|
|
17
|
+
"""Explicit view over an environment mapping (os.environ or caller-supplied)."""
|
|
18
|
+
|
|
19
|
+
def __init__(self, env: Mapping[str, str] | None) -> None:
|
|
20
|
+
self.hermetic = is_hermetic(env)
|
|
21
|
+
self._env: Mapping[str, str] = {} if self.hermetic else (os.environ if env is None else env)
|
|
22
|
+
|
|
23
|
+
def get(self, key: str, default: str = "") -> str:
|
|
24
|
+
val = self._env.get(key)
|
|
25
|
+
return default if val is None else val
|
|
26
|
+
|
|
27
|
+
def get_opt(self, key: str) -> str | None:
|
|
28
|
+
val = self._env.get(key)
|
|
29
|
+
if val is None or val == "":
|
|
30
|
+
return None
|
|
31
|
+
return val
|
|
32
|
+
|
|
33
|
+
def get_bool(self, key: str, default: bool = False) -> bool:
|
|
34
|
+
raw = self.get_opt(key)
|
|
35
|
+
if raw is None:
|
|
36
|
+
return default
|
|
37
|
+
low = raw.strip().lower()
|
|
38
|
+
if low in ("true", "1", "yes"):
|
|
39
|
+
return True
|
|
40
|
+
if low in ("false", "0", "no"):
|
|
41
|
+
return False
|
|
42
|
+
return default
|
|
43
|
+
|
|
44
|
+
def get_int(self, key: str, default: int) -> int:
|
|
45
|
+
raw = self.get_opt(key)
|
|
46
|
+
if raw is None:
|
|
47
|
+
return default
|
|
48
|
+
try:
|
|
49
|
+
return int(raw.strip())
|
|
50
|
+
except ValueError:
|
|
51
|
+
return default
|
|
52
|
+
|
|
53
|
+
def get_float(self, key: str, default: float) -> float:
|
|
54
|
+
raw = self.get_opt(key)
|
|
55
|
+
if raw is None:
|
|
56
|
+
return default
|
|
57
|
+
try:
|
|
58
|
+
return float(raw.strip())
|
|
59
|
+
except ValueError:
|
|
60
|
+
return default
|
|
61
|
+
|
|
62
|
+
def sdk_disabled(self) -> bool:
|
|
63
|
+
return self.get_bool("OTEL_SDK_DISABLED", False)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def parse_baggage_pairs(raw: str) -> dict[str, str]:
|
|
67
|
+
"""Parse W3C/OTEL comma-separated key=value pairs (headers or resource attributes)."""
|
|
68
|
+
out: dict[str, str] = {}
|
|
69
|
+
if not raw.strip():
|
|
70
|
+
return out
|
|
71
|
+
for item in raw.split(","):
|
|
72
|
+
pair = item.strip()
|
|
73
|
+
if not pair or "=" not in pair:
|
|
74
|
+
continue
|
|
75
|
+
k, v = pair.split("=", 1)
|
|
76
|
+
key = k.strip()
|
|
77
|
+
if not key:
|
|
78
|
+
continue
|
|
79
|
+
out[key] = unquote(v.strip())
|
|
80
|
+
return out
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def parse_resource_attributes(env: EnvView) -> dict[str, str]:
|
|
84
|
+
"""Parse OTEL_RESOURCE_ATTRIBUTES from env."""
|
|
85
|
+
raw = env.get("OTEL_RESOURCE_ATTRIBUTES")
|
|
86
|
+
return parse_baggage_pairs(raw)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def parse_otlp_headers(env: EnvView, signal_upper: str) -> dict[str, str]:
|
|
90
|
+
"""Parse OTEL_EXPORTER_OTLP_HEADERS merged with signal-specific headers."""
|
|
91
|
+
headers = parse_baggage_pairs(env.get("OTEL_EXPORTER_OTLP_HEADERS"))
|
|
92
|
+
sig = parse_baggage_pairs(env.get(f"OTEL_EXPORTER_OTLP_{signal_upper}_HEADERS"))
|
|
93
|
+
headers.update(sig)
|
|
94
|
+
return headers
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
__all__ = [
|
|
98
|
+
"HERMETIC",
|
|
99
|
+
"EnvView",
|
|
100
|
+
"parse_baggage_pairs",
|
|
101
|
+
"parse_otlp_headers",
|
|
102
|
+
"parse_resource_attributes",
|
|
103
|
+
]
|