Skip to content

Commit 795006b

Browse files
authored
Add new orgId option (#1794)
1 parent 33a3011 commit 795006b

7 files changed

Lines changed: 94 additions & 6 deletions

File tree

phpstan-baseline.neon

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ parameters:
77

88
-
99
message: "#^Offset 'host' does not exist on array\\{scheme\\: 'http'\\|'https', host\\?\\: string, port\\?\\: int\\<0, 65535\\>, user\\?\\: string, pass\\?\\: string, path\\?\\: string, query\\?\\: string, fragment\\?\\: string\\}\\.$#"
10-
count: 1
10+
count: 2
1111
path: src/Dsn.php
1212

1313
-
@@ -200,6 +200,11 @@ parameters:
200200
count: 1
201201
path: src/Options.php
202202

203+
-
204+
message: "#^Method Sentry\\\\Options\\:\\:getOrgId\\(\\) should return int\\|null but returns mixed\\.$#"
205+
count: 1
206+
path: src/Options.php
207+
203208
-
204209
message: "#^Method Sentry\\\\Options\\:\\:getPrefixes\\(\\) should return array\\<string\\> but returns mixed\\.$#"
205210
count: 1

src/Dsn.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
*/
1313
final class Dsn implements \Stringable
1414
{
15+
/**
16+
* @var string Regex to match the organization ID in the host.
17+
* This only applies to Sentry SaaS DSNs that contain the organization ID.
18+
*/
19+
private const SENTRY_ORG_ID_REGEX = '/^o(\d+)\./';
20+
1521
/**
1622
* @var string The protocol to be used to access the resource
1723
*/
@@ -42,6 +48,11 @@ final class Dsn implements \Stringable
4248
*/
4349
private $path;
4450

51+
/**
52+
* @var int|null
53+
*/
54+
private $orgId;
55+
4556
/**
4657
* Class constructor.
4758
*
@@ -51,15 +62,17 @@ final class Dsn implements \Stringable
5162
* @param string $projectId The ID of the resource to access
5263
* @param string $path The specific resource that the web client wants to access
5364
* @param string $publicKey The public key to authenticate the SDK
65+
* @param ?int $orgId The org ID
5466
*/
55-
private function __construct(string $scheme, string $host, int $port, string $projectId, string $path, string $publicKey)
67+
private function __construct(string $scheme, string $host, int $port, string $projectId, string $path, string $publicKey, ?int $orgId = null)
5668
{
5769
$this->scheme = $scheme;
5870
$this->host = $host;
5971
$this->port = $port;
60-
$this->publicKey = $publicKey;
61-
$this->path = $path;
6272
$this->projectId = $projectId;
73+
$this->path = $path;
74+
$this->publicKey = $publicKey;
75+
$this->orgId = $orgId;
6376
}
6477

6578
/**
@@ -94,13 +107,19 @@ public static function createFromString(string $value): self
94107
$path = substr($parsedDsn['path'], 0, $lastSlashPosition);
95108
}
96109

110+
$orgId = null;
111+
if (preg_match(self::SENTRY_ORG_ID_REGEX, $parsedDsn['host'], $matches) == 1) {
112+
$orgId = (int) $matches[1];
113+
}
114+
97115
return new self(
98116
$parsedDsn['scheme'],
99117
$parsedDsn['host'],
100118
$parsedDsn['port'] ?? ($parsedDsn['scheme'] === 'http' ? 80 : 443),
101119
$projectId,
102120
$path,
103-
$parsedDsn['user']
121+
$parsedDsn['user'],
122+
$orgId
104123
);
105124
}
106125

@@ -152,6 +171,11 @@ public function getPublicKey(): string
152171
return $this->publicKey;
153172
}
154173

174+
public function getOrgId(): ?int
175+
{
176+
return $this->orgId;
177+
}
178+
155179
/**
156180
* Returns the URL of the API for the envelope endpoint.
157181
*/

src/Options.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,26 @@ public function getDsn(): ?Dsn
436436
return $this->options['dsn'];
437437
}
438438

439+
/**
440+
* Gets the Org ID.
441+
*/
442+
public function getOrgId(): ?int
443+
{
444+
return $this->options['org_id'];
445+
}
446+
447+
/**
448+
* Sets the Org ID.
449+
*/
450+
public function setOrgId(int $orgId): self
451+
{
452+
$options = array_merge($this->options, ['org_id' => $orgId]);
453+
454+
$this->options = $this->resolver->resolve($options);
455+
456+
return $this;
457+
}
458+
439459
/**
440460
* Gets the name of the server the SDK is running on (e.g. the hostname).
441461
*/
@@ -1146,6 +1166,7 @@ private function configureOptions(OptionsResolver $resolver): void
11461166
'spotlight_url' => 'http://localhost:8969',
11471167
'release' => $_SERVER['SENTRY_RELEASE'] ?? $_SERVER['AWS_LAMBDA_FUNCTION_VERSION'] ?? null,
11481168
'dsn' => $_SERVER['SENTRY_DSN'] ?? null,
1169+
'org_id' => null,
11491170
'server_name' => gethostname(),
11501171
'ignore_exceptions' => [],
11511172
'ignore_transactions' => [],
@@ -1206,6 +1227,7 @@ private function configureOptions(OptionsResolver $resolver): void
12061227
$resolver->setAllowedTypes('spotlight_url', 'string');
12071228
$resolver->setAllowedTypes('release', ['null', 'string']);
12081229
$resolver->setAllowedTypes('dsn', ['null', 'string', 'bool', Dsn::class]);
1230+
$resolver->setAllowedTypes('org_id', ['null', 'int']);
12091231
$resolver->setAllowedTypes('server_name', 'string');
12101232
$resolver->setAllowedTypes('before_send', ['callable']);
12111233
$resolver->setAllowedTypes('before_send_transaction', ['callable']);

src/Tracing/DynamicSamplingContext.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ public static function fromTransaction(Transaction $transaction, HubInterface $h
172172
if ($options->getDsn() !== null && $options->getDsn()->getPublicKey() !== null) {
173173
$samplingContext->set('public_key', $options->getDsn()->getPublicKey());
174174
}
175+
if ($options->getDsn() !== null && $options->getDsn()->getOrgId() !== null) {
176+
$samplingContext->set('org_id', (string) $options->getDsn()->getOrgId());
177+
}
175178

176179
if ($options->getRelease() !== null) {
177180
$samplingContext->set('release', $options->getRelease());
@@ -209,6 +212,10 @@ public static function fromOptions(Options $options, Scope $scope): self
209212
$samplingContext->set('public_key', $options->getDsn()->getPublicKey());
210213
}
211214

215+
if ($options->getDsn() !== null && $options->getDsn()->getOrgId() !== null) {
216+
$samplingContext->set('org_id', (string) $options->getDsn()->getOrgId());
217+
}
218+
212219
if ($options->getRelease() !== null) {
213220
$samplingContext->set('release', $options->getRelease());
214221
}

src/functions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* max_breadcrumbs?: int,
4747
* max_request_body_size?: "none"|"never"|"small"|"medium"|"always",
4848
* max_value_length?: int,
49+
* org_id?: int|null,
4950
* prefixes?: array<string>,
5051
* profiles_sample_rate?: int|float|null,
5152
* release?: string|null,

tests/DsnTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public function testCreateFromString(
2222
int $expectedPort,
2323
string $expectedPublicKey,
2424
string $expectedProjectId,
25-
string $expectedPath
25+
string $expectedPath,
26+
?int $expectedOrgId
2627
): void {
2728
$dsn = Dsn::createFromString($value);
2829

@@ -32,6 +33,7 @@ public function testCreateFromString(
3233
$this->assertSame($expectedPublicKey, $dsn->getPublicKey());
3334
$this->assertSame($expectedProjectId, $dsn->getProjectId(true));
3435
$this->assertSame($expectedPath, $dsn->getPath());
36+
$this->assertSame($expectedOrgId, $dsn->getOrgId());
3537
}
3638

3739
public static function createFromStringDataProvider(): \Generator
@@ -44,6 +46,7 @@ public static function createFromStringDataProvider(): \Generator
4446
'public',
4547
'1',
4648
'/sentry',
49+
null,
4750
];
4851

4952
yield [
@@ -54,6 +57,18 @@ public static function createFromStringDataProvider(): \Generator
5457
'public',
5558
'1',
5659
'',
60+
null,
61+
];
62+
63+
yield [
64+
'http://public@o1.example.com/1',
65+
'http',
66+
'o1.example.com',
67+
80,
68+
'public',
69+
'1',
70+
'',
71+
1,
5772
];
5873

5974
yield [
@@ -64,6 +79,7 @@ public static function createFromStringDataProvider(): \Generator
6479
'public',
6580
'1',
6681
'',
82+
null,
6783
];
6884

6985
yield [
@@ -74,6 +90,7 @@ public static function createFromStringDataProvider(): \Generator
7490
'public',
7591
'1',
7692
'',
93+
null,
7794
];
7895

7996
yield [
@@ -84,6 +101,7 @@ public static function createFromStringDataProvider(): \Generator
84101
'public',
85102
'1',
86103
'',
104+
null,
87105
];
88106

89107
yield [
@@ -94,6 +112,7 @@ public static function createFromStringDataProvider(): \Generator
94112
'public',
95113
'1',
96114
'',
115+
null,
97116
];
98117

99118
yield [
@@ -104,6 +123,7 @@ public static function createFromStringDataProvider(): \Generator
104123
'public',
105124
'1',
106125
'',
126+
null,
107127
];
108128

109129
yield [
@@ -114,6 +134,7 @@ public static function createFromStringDataProvider(): \Generator
114134
'public',
115135
'1',
116136
'',
137+
null,
117138
];
118139
}
119140

@@ -240,6 +261,7 @@ public static function toStringDataProvider(): array
240261
return [
241262
['http://public@example.com/sentry/1'],
242263
['http://public@example.com/1'],
264+
['http://public@o1.example.com/1'],
243265
['http://public@example.com:8080/sentry/1'],
244266
['https://public@example.com/sentry/1'],
245267
['https://public@example.com:4343/sentry/1'],

tests/OptionsTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ public function testGettersAndSetters(
7575

7676
public static function optionsDataProvider(): \Generator
7777
{
78+
yield [
79+
'org_id',
80+
1,
81+
'getOrgId',
82+
'setOrgId',
83+
];
84+
7885
yield [
7986
'prefixes',
8087
['foo', 'bar'],

0 commit comments

Comments
 (0)