Skip to content

Commit 2714b2d

Browse files
cleptricstayallive
andauthored
feat: Add support for profiling (#1477)
Co-authored-by: Alex Bouma <[email protected]>
1 parent 841ab48 commit 2714b2d

23 files changed

+1686
-9
lines changed

phpstan.neon

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ parameters:
1212
- tests/Fixtures
1313
dynamicConstantNames:
1414
- Monolog\Logger::API
15+
bootstrapFiles:
16+
- stubs/autoload.php

psalm-baseline.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="4.27.0@faf106e717c37b8c81721845dba9de3d8deed8ff">
2+
<files psalm-version="4.30.0@d0bc6e25d89f649e4f36a534f330f8bb4643dd69">
33
<file src="src/Dsn.php">
44
<PossiblyUndefinedArrayOffset occurrences="4">
55
<code>$parsedDsn['host']</code>
@@ -59,6 +59,12 @@
5959
<code>$record['context']</code>
6060
</PossiblyUndefinedMethod>
6161
</file>
62+
<file src="src/Profiling/Profile.php">
63+
<LessSpecificReturnStatement occurrences="1"/>
64+
<MoreSpecificReturnType occurrences="1">
65+
<code>SentryProfile|null</code>
66+
</MoreSpecificReturnType>
67+
</file>
6268
<file src="src/Serializer/RepresentationSerializer.php">
6369
<InvalidReturnStatement occurrences="1">
6470
<code>$value</code>

psalm.xml.dist

+8
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@
6464
</errorLevel>
6565
</UndefinedClass>
6666
</issueHandlers>
67+
68+
<stubs>
69+
<file name="stubs/ExcimerLog.stub" />
70+
<file name="stubs/ExcimerLogEntry.stub" />
71+
<file name="stubs/ExcimerProfiler.stub" />
72+
<file name="stubs/ExcimerTimer.stub" />
73+
<file name="stubs/globals.stub" />
74+
</stubs>
6775
</psalm>

src/Context/OsContext.php

+27-2
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,27 @@ final class OsContext
3131
*/
3232
private $kernelVersion;
3333

34+
/**
35+
* @var string|null
36+
*/
37+
private $machineType;
38+
3439
/**
3540
* Constructor.
3641
*
3742
* @param string $name The name of the operating system
3843
* @param string|null $version The version of the operating system
3944
* @param string|null $build The internal build revision of the operating system
4045
* @param string|null $kernelVersion An independent kernel version string
46+
* @param string|null $machineType The machine type
4147
*/
42-
public function __construct(string $name, ?string $version = null, ?string $build = null, ?string $kernelVersion = null)
43-
{
48+
public function __construct(
49+
string $name,
50+
?string $version = null,
51+
?string $build = null,
52+
?string $kernelVersion = null,
53+
?string $machineType = null
54+
) {
4455
if ('' === trim($name)) {
4556
throw new \InvalidArgumentException('The $name argument cannot be an empty string.');
4657
}
@@ -49,6 +60,7 @@ public function __construct(string $name, ?string $version = null, ?string $buil
4960
$this->version = $version;
5061
$this->build = $build;
5162
$this->kernelVersion = $kernelVersion;
63+
$this->machineType = $machineType;
5264
}
5365

5466
/**
@@ -126,4 +138,17 @@ public function setKernelVersion(?string $kernelVersion): void
126138
{
127139
$this->kernelVersion = $kernelVersion;
128140
}
141+
142+
public function getMachineType(): ?string
143+
{
144+
return $this->machineType;
145+
}
146+
147+
/**
148+
* @param string|null $machineType The machine type
149+
*/
150+
public function setMachineType(?string $machineType): void
151+
{
152+
$this->machineType = $machineType;
153+
}
129154
}

src/Event.php

+27
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Sentry\Context\OsContext;
88
use Sentry\Context\RuntimeContext;
9+
use Sentry\Profiling\Profile;
910
use Sentry\Tracing\Span;
1011

1112
/**
@@ -167,6 +168,11 @@ final class Event
167168
*/
168169
private $type;
169170

171+
/**
172+
* @var Profile|null The profile data
173+
*/
174+
private $profile;
175+
170176
private function __construct(?EventId $eventId, EventType $eventType)
171177
{
172178
$this->id = $eventId ?? EventId::generate();
@@ -747,4 +753,25 @@ public function setSpans(array $spans): void
747753
{
748754
$this->spans = $spans;
749755
}
756+
757+
public function setProfile(?Profile $profile): void
758+
{
759+
$this->profile = $profile;
760+
}
761+
762+
public function getProfile(): ?Profile
763+
{
764+
return $this->profile;
765+
}
766+
767+
public function getTraceId(): ?string
768+
{
769+
$traceId = $this->getContexts()['trace']['trace_id'];
770+
771+
if (\is_string($traceId) && !empty($traceId)) {
772+
return $traceId;
773+
}
774+
775+
return null;
776+
}
750777
}

src/Integration/EnvironmentIntegration.php

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ private function updateServerOsContext(?OsContext $osContext): ?OsContext
7070
$osContext->setKernelVersion(php_uname('a'));
7171
}
7272

73+
if (null === $osContext->getMachineType()) {
74+
$osContext->setMachineType(php_uname('m'));
75+
}
76+
7377
return $osContext;
7478
}
7579
}

src/Options.php

+17
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,21 @@ public function setTracesSampleRate(?float $sampleRate): void
183183
$this->options = $this->resolver->resolve($options);
184184
}
185185

186+
public function getProfilesSampleRate(): ?float
187+
{
188+
/** @var int|float|null $value */
189+
$value = $this->options['profiles_sample_rate'] ?? null;
190+
191+
return $value ?? null;
192+
}
193+
194+
public function setProfilesSampleRate(?float $sampleRate): void
195+
{
196+
$options = array_merge($this->options, ['profiles_sample_rate' => $sampleRate]);
197+
198+
$this->options = $this->resolver->resolve($options);
199+
}
200+
186201
/**
187202
* Gets whether tracing is enabled or not. The feature is enabled when at
188203
* least one of the `traces_sample_rate` and `traces_sampler` options is
@@ -848,6 +863,7 @@ private function configureOptions(OptionsResolver $resolver): void
848863
'enable_tracing' => null,
849864
'traces_sample_rate' => null,
850865
'traces_sampler' => null,
866+
'profiles_sample_rate' => null,
851867
'attach_stacktrace' => false,
852868
'context_lines' => 5,
853869
'enable_compression' => true,
@@ -887,6 +903,7 @@ private function configureOptions(OptionsResolver $resolver): void
887903
$resolver->setAllowedTypes('enable_tracing', ['null', 'bool']);
888904
$resolver->setAllowedTypes('traces_sample_rate', ['null', 'int', 'float']);
889905
$resolver->setAllowedTypes('traces_sampler', ['null', 'callable']);
906+
$resolver->setAllowedTypes('profiles_sample_rate', ['null', 'int', 'float']);
890907
$resolver->setAllowedTypes('attach_stacktrace', 'bool');
891908
$resolver->setAllowedTypes('context_lines', ['null', 'int']);
892909
$resolver->setAllowedTypes('enable_compression', 'bool');

0 commit comments

Comments
 (0)