Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new org & strictTraceContinuation options #1794

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/Dsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
final class Dsn implements \Stringable
{

/**
* @var string Regex to match the organization ID in the host.
* This only applies to Sentry SaaS DSNs that contain the organization ID.
*/
private const SENTRY_ORG_REGEX = '/^o(\d+)\./';

/**
* @var string The protocol to be used to access the resource
*/
Expand Down Expand Up @@ -42,6 +49,11 @@
*/
private $path;

/**
* @var string
*/
private $org;

/**
* Class constructor.
*
Expand All @@ -51,15 +63,17 @@
* @param string $projectId The ID of the resource to access
* @param string $path The specific resource that the web client wants to access
* @param string $publicKey The public key to authenticate the SDK
* @param string $org
*/
private function __construct(string $scheme, string $host, int $port, string $projectId, string $path, string $publicKey)
private function __construct(string $scheme, string $host, int $port, string $projectId, string $path, string $publicKey, ?string $org = null)
{
$this->scheme = $scheme;
$this->host = $host;
$this->port = $port;
$this->publicKey = $publicKey;
$this->path = $path;
$this->projectId = $projectId;
$this->path = $path;
$this->publicKey = $publicKey;
$this->org = $org;

Check failure on line 76 in src/Dsn.php

View workflow job for this annotation

GitHub Actions / PHPStan

Property Sentry\Dsn::$org (string) does not accept string|null.
}

/**
Expand Down Expand Up @@ -94,13 +108,19 @@
$path = substr($parsedDsn['path'], 0, $lastSlashPosition);
}

$org = null;
if (preg_match(self::SENTRY_ORG_REGEX, $parsedDsn['host'], $matches) == 1) {

Check failure on line 112 in src/Dsn.php

View workflow job for this annotation

GitHub Actions / PHPStan

Ignored error pattern #^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\}\.$# in path /home/runner/work/sentry-php/sentry-php/src/Dsn.php is expected to occur 1 time, but occurred 2 times.
$org = $matches[1];
}

return new self(
$parsedDsn['scheme'],
$parsedDsn['host'],

Check failure on line 118 in src/Dsn.php

View workflow job for this annotation

GitHub Actions / PHPStan

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}.
$parsedDsn['port'] ?? ($parsedDsn['scheme'] === 'http' ? 80 : 443),
$projectId,
$path,
$parsedDsn['user']
$parsedDsn['user'],
$org
);
}

Expand Down Expand Up @@ -152,6 +172,11 @@
return $this->publicKey;
}

public function getOrg(): ?string
{
return $this->org;
}

/**
* Returns the URL of the API for the envelope endpoint.
*/
Expand Down
32 changes: 32 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,20 @@
return $this->options['dsn'];
}

public function getOrg(): ?string
{
return $this->options['org'];

Check failure on line 429 in src/Options.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Sentry\Options::getOrg() should return string|null but returns mixed.
}

public function setOrg(string $org): self
{
$options = array_merge($this->options, ['org' => $org]);

$this->options = $this->resolver->resolve($options);

return $this;
}

/**
* Gets the name of the server the SDK is running on (e.g. the hostname).
*/
Expand Down Expand Up @@ -636,6 +650,20 @@
return $this;
}

public function isStrictTracePropagationEnabled(): bool
{
return $this->options['strict_trace_propagation'];

Check failure on line 655 in src/Options.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Sentry\Options::isStrictTracePropagationEnabled() should return bool but returns mixed.
}

public function setStrictTracePropagationEnabled(bool $enabled): self
{
$options = array_merge($this->options, ['strict_trace_propagation' => $enabled]);

$this->options = $this->resolver->resolve($options);

return $this;
}

/**
* Gets a list of default tags for events.
*
Expand Down Expand Up @@ -1131,6 +1159,7 @@
'spotlight_url' => 'http://localhost:8969',
'release' => $_SERVER['SENTRY_RELEASE'] ?? $_SERVER['AWS_LAMBDA_FUNCTION_VERSION'] ?? null,
'dsn' => $_SERVER['SENTRY_DSN'] ?? null,
'org' => null,
'server_name' => gethostname(),
'ignore_exceptions' => [],
'ignore_transactions' => [],
Expand All @@ -1150,6 +1179,7 @@
return null;
},
'trace_propagation_targets' => null,
'strict_trace_propagation' => false,
'tags' => [],
'error_types' => null,
'max_breadcrumbs' => self::DEFAULT_MAX_BREADCRUMBS,
Expand Down Expand Up @@ -1191,12 +1221,14 @@
$resolver->setAllowedTypes('spotlight_url', 'string');
$resolver->setAllowedTypes('release', ['null', 'string']);
$resolver->setAllowedTypes('dsn', ['null', 'string', 'bool', Dsn::class]);
$resolver->setAllowedTypes('org', ['null', 'string']);
$resolver->setAllowedTypes('server_name', 'string');
$resolver->setAllowedTypes('before_send', ['callable']);
$resolver->setAllowedTypes('before_send_transaction', ['callable']);
$resolver->setAllowedTypes('ignore_exceptions', 'string[]');
$resolver->setAllowedTypes('ignore_transactions', 'string[]');
$resolver->setAllowedTypes('trace_propagation_targets', ['null', 'string[]']);
$resolver->setAllowedTypes('strict_trace_propagation', ['null', 'bool']);
$resolver->setAllowedTypes('tags', 'string[]');
$resolver->setAllowedTypes('error_types', ['null', 'int']);
$resolver->setAllowedTypes('max_breadcrumbs', 'int');
Expand Down
7 changes: 7 additions & 0 deletions src/Tracing/DynamicSamplingContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ public static function fromTransaction(Transaction $transaction, HubInterface $h
if ($options->getDsn() !== null && $options->getDsn()->getPublicKey() !== null) {
$samplingContext->set('public_key', $options->getDsn()->getPublicKey());
}
if ($options->getDsn() !== null && $options->getDsn()->getOrg() !== null) {
$samplingContext->set('org', $options->getDsn()->getOrg());
}

if ($options->getRelease() !== null) {
$samplingContext->set('release', $options->getRelease());
Expand Down Expand Up @@ -204,6 +207,10 @@ public static function fromOptions(Options $options, Scope $scope): self
$samplingContext->set('public_key', $options->getDsn()->getPublicKey());
}

if ($options->getDsn() !== null && $options->getDsn()->getOrg() !== null) {
$samplingContext->set('org', $options->getDsn()->getOrg());
}

if ($options->getRelease() !== null) {
$samplingContext->set('release', $options->getRelease());
}
Expand Down
24 changes: 23 additions & 1 deletion tests/DsnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public function testCreateFromString(
int $expectedPort,
string $expectedPublicKey,
string $expectedProjectId,
string $expectedPath
string $expectedPath,
?string $expectedOrg,
): void {
$dsn = Dsn::createFromString($value);

Expand All @@ -32,6 +33,7 @@ public function testCreateFromString(
$this->assertSame($expectedPublicKey, $dsn->getPublicKey());
$this->assertSame($expectedProjectId, $dsn->getProjectId(true));
$this->assertSame($expectedPath, $dsn->getPath());
$this->assertSame($expectedOrg, $dsn->getOrg());
}

public static function createFromStringDataProvider(): \Generator
Expand All @@ -44,6 +46,7 @@ public static function createFromStringDataProvider(): \Generator
'public',
'1',
'/sentry',
null,
];

yield [
Expand All @@ -54,6 +57,18 @@ public static function createFromStringDataProvider(): \Generator
'public',
'1',
'',
null,
];

yield [
'http://[email protected]/1',
'http',
'o1.example.com',
80,
'public',
'1',
'',
'1',
];

yield [
Expand All @@ -64,6 +79,7 @@ public static function createFromStringDataProvider(): \Generator
'public',
'1',
'',
null,
];

yield [
Expand All @@ -74,6 +90,7 @@ public static function createFromStringDataProvider(): \Generator
'public',
'1',
'',
null,
];

yield [
Expand All @@ -84,6 +101,7 @@ public static function createFromStringDataProvider(): \Generator
'public',
'1',
'',
null,
];

yield [
Expand All @@ -94,6 +112,7 @@ public static function createFromStringDataProvider(): \Generator
'public',
'1',
'',
null,
];

yield [
Expand All @@ -104,6 +123,7 @@ public static function createFromStringDataProvider(): \Generator
'public',
'1',
'',
null,
];

yield [
Expand All @@ -114,6 +134,7 @@ public static function createFromStringDataProvider(): \Generator
'public',
'1',
'',
null,
];
}

Expand Down Expand Up @@ -240,6 +261,7 @@ public static function toStringDataProvider(): array
return [
['http://[email protected]/sentry/1'],
['http://[email protected]/1'],
['http://[email protected]/1'],
['http://[email protected]:8080/sentry/1'],
['https://[email protected]/sentry/1'],
['https://[email protected]:4343/sentry/1'],
Expand Down
Loading