Skip to content

Commit f82b489

Browse files
committed
Add org_id support
1 parent 1f537de commit f82b489

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

sentry_sdk/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,9 @@ def _record_lost_event(
382382
_client_init_debug.set(self.options["debug"])
383383
self.transport = make_transport(self.options)
384384

385+
if not self.options["org_id"] and self.transport and self.transport.parsed_dsn:
386+
self.options["org_id"] = self.transport.parsed_dsn.org_id
387+
385388
self.monitor = None
386389
if self.transport:
387390
if self.options["enable_backpressure_handling"]:

sentry_sdk/consts.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,7 @@ def __init__(
10231023
trace_ignore_status_codes=frozenset(), # type: AbstractSet[int]
10241024
enable_metrics=True, # type: bool
10251025
before_send_metric=None, # type: Optional[Callable[[Metric, Hint], Optional[Metric]]]
1026+
org_id=None, # type: Optional[str]
10261027
):
10271028
# type: (...) -> None
10281029
"""Initialize the Sentry SDK with the given parameters. All parameters described here can be used in a call to `sentry_sdk.init()`.
@@ -1426,6 +1427,10 @@ def __init__(
14261427
If `trace_ignore_status_codes` is not provided, requests with any status code
14271428
may be traced.
14281429
1430+
:param org_id: An optional organization ID. The SDK will try to extract if from the DSN in most cases
1431+
but you can provide it explicitly for self-hosted and Relay setups. This value is used for
1432+
trace propagation and for features like `strict_trace_continuation`.
1433+
14291434
:param _experiments:
14301435
"""
14311436
pass

sentry_sdk/tracing_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,8 @@ def from_options(cls, scope):
666666

667667
if client.parsed_dsn:
668668
sentry_items["public_key"] = client.parsed_dsn.public_key
669+
if client.parsed_dsn.org_id:
670+
sentry_items["org_id"] = client.parsed_dsn.org_id
669671

670672
if options.get("traces_sample_rate"):
671673
sentry_items["sample_rate"] = str(options["traces_sample_rate"])
@@ -698,6 +700,8 @@ def populate_from_transaction(cls, transaction):
698700

699701
if client.parsed_dsn:
700702
sentry_items["public_key"] = client.parsed_dsn.public_key
703+
if client.parsed_dsn.org_id:
704+
sentry_items["org_id"] = client.parsed_dsn.org_id
701705

702706
if (
703707
transaction.name

sentry_sdk/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ class BadDsn(ValueError):
294294
class Dsn:
295295
"""Represents a DSN."""
296296

297+
ORG_ID_REGEX = re.compile(r"^o(\d+)\.")
298+
297299
def __init__(self, value):
298300
# type: (Union[Dsn, str]) -> None
299301
if isinstance(value, Dsn):
@@ -310,6 +312,9 @@ def __init__(self, value):
310312

311313
self.host = parts.hostname
312314

315+
org_id_match = Dsn.ORG_ID_REGEX.match(self.host)
316+
self.org_id = org_id_match.group(1) if org_id_match else None # type: Optional[str]
317+
313318
if parts.port is None:
314319
self.port = self.scheme == "https" and 443 or 80 # type: int
315320
else:

0 commit comments

Comments
 (0)