Skip to content

Commit 52cd14a

Browse files
Split sampling rules into typed subclasses
1 parent 9319f00 commit 52cd14a

33 files changed

Lines changed: 462 additions & 292 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"minimum-stability": "dev",
6262
"extra": {
6363
"branch-alias": {
64-
"dev-main": "1.3.x-dev"
64+
"dev-main": "3.x-dev"
6565
}
6666
}
6767
}

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,6 @@ parameters:
9595
count: 1
9696
path: src/Recorders/SpanEventsRecorder.php
9797

98-
-
99-
message: "#^Unsafe usage of new static\\(\\)\\.$#"
100-
count: 8
101-
path: src/Sampling/SamplingRule.php
102-
10398
-
10499
message: "#^Instanceof between \\*NEVER\\* and Spatie\\\\FlareClient\\\\Spans\\\\SpanEvent will always evaluate to false\\.$#"
105100
count: 1

src/AttributesProviders/PhpCommandAttributesProvider.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,19 @@ public function commandClass(): ?string
3030
{
3131
return $this->commandClass;
3232
}
33+
34+
public function entryPointHandlerIdentifier(): ?string
35+
{
36+
return $this->command;
37+
}
38+
39+
public function entryPointHandlerName(): ?string
40+
{
41+
return $this->commandClass;
42+
}
43+
44+
public function entryPointHandlerType(): ?string
45+
{
46+
return 'php_command';
47+
}
3348
}

src/AttributesProviders/PhpJobAttributesProvider.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,19 @@ public function jobClass(): ?string
2626
{
2727
return $this->jobClass;
2828
}
29+
30+
public function entryPointHandlerIdentifier(): ?string
31+
{
32+
return $this->jobName;
33+
}
34+
35+
public function entryPointHandlerName(): ?string
36+
{
37+
return $this->jobClass;
38+
}
39+
40+
public function entryPointHandlerType(): ?string
41+
{
42+
return 'php_job';
43+
}
2944
}

src/AttributesProviders/SymfonyInputCommandAttributesProvider.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
namespace Spatie\FlareClient\AttributesProviders;
44

55
use Spatie\FlareClient\Contracts\CommandAttributesProvider;
6-
use Spatie\FlareClient\Contracts\EntryPointHandlerProvider;
76
use Symfony\Component\Console\Input\InputInterface;
87

9-
class SymfonyInputCommandAttributesProvider implements CommandAttributesProvider, EntryPointHandlerProvider
8+
class SymfonyInputCommandAttributesProvider implements CommandAttributesProvider
109
{
1110
public function __construct(
1211
protected InputInterface $input,

src/Contracts/CommandAttributesProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Spatie\FlareClient\Contracts;
44

5-
interface CommandAttributesProvider extends AttributesProvider
5+
interface CommandAttributesProvider extends AttributesProvider, EntryPointHandlerProvider
66
{
77
public function command(): string;
88

src/Contracts/JobAttributesProvider.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace Spatie\FlareClient\Contracts;
44

5-
interface JobAttributesProvider extends AttributesProvider
5+
interface JobAttributesProvider extends QueuedJobAttributesProvider, EntryPointHandlerProvider
66
{
7-
public function jobName(): string;
8-
9-
public function jobClass(): ?string;
107
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Spatie\FlareClient\Contracts;
4+
5+
interface QueuedJobAttributesProvider extends AttributesProvider
6+
{
7+
public function jobName(): string;
8+
9+
public function jobClass(): ?string;
10+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Spatie\FlareClient\Contracts;
4+
5+
/**
6+
* Implemented by attribute providers (route, command, job, ...) that want to
7+
* expose a small map of attributes for sampling rules to match against,
8+
* separately from the full attribute payload produced by toArray(). Keep the
9+
* returned values cheap to compute — this runs on every entry-point handler
10+
* resolution, including for traces that may still get dropped.
11+
*/
12+
interface SamplingAttributesProvider
13+
{
14+
/** @return array<string, mixed> */
15+
public function samplingAttributes(): array;
16+
}

src/EntryPoint/EntryPoint.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Spatie\FlareClient\EntryPoint;
44

5+
use Spatie\FlareClient\Contracts\AttributesProvider;
6+
use Spatie\FlareClient\Contracts\EntryPointHandlerProvider;
7+
use Spatie\FlareClient\Contracts\SamplingAttributesProvider;
58
use Spatie\FlareClient\Enums\EntryPointType;
69

710
class EntryPoint
@@ -14,6 +17,9 @@ class EntryPoint
1417

1518
public ?string $handlerType;
1619

20+
/** @var array<string, mixed> */
21+
public array $samplingAttributes = [];
22+
1723
public function __construct(
1824
public EntryPointType $type,
1925
public string $value,
@@ -25,17 +31,33 @@ public function updateValue(string $value): void
2531
$this->value = $value;
2632
}
2733

34+
/** @param array<string, mixed> $samplingAttributes */
2835
public function setHandler(
2936
string $handlerIdentifier,
3037
?string $handlerName,
3138
?string $handlerType,
39+
array $samplingAttributes = [],
3240
): void {
3341
$this->handlerIdentifier = $handlerIdentifier;
3442
$this->handlerName = $handlerName;
3543
$this->handlerType = $handlerType;
44+
$this->samplingAttributes = $samplingAttributes;
3645
$this->handlerResolved = true;
3746
}
3847

48+
public function setHandlerFromAttributesProvider(
49+
AttributesProvider&EntryPointHandlerProvider $provider,
50+
): void {
51+
$this->setHandler(
52+
handlerIdentifier: $provider->entryPointHandlerIdentifier() ?? 'unknown',
53+
handlerName: $provider->entryPointHandlerName(),
54+
handlerType: $provider->entryPointHandlerType() ?? 'unknown',
55+
samplingAttributes: $provider instanceof SamplingAttributesProvider
56+
? $provider->samplingAttributes()
57+
: [],
58+
);
59+
}
60+
3961
/** @return array<string, string|null> */
4062
public function toAttributes(): array
4163
{

0 commit comments

Comments
 (0)