Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit cd0b41e

Browse files
bpotchingor13
authored andcommitted
Allow Tracer usage in code when there is no active tracer (#117)
For the most part this makes a Tracer without an $instance act like it has a NullTracer. Fixes #116
1 parent 27be25d commit cd0b41e

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

src/Trace/Integrations/Grpc.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ public static function load()
113113
*/
114114
public static function updateMetadata($metadata, $jwtAuthUri)
115115
{
116-
if ($context = Tracer::spanContext()) {
116+
$context = Tracer::spanContext();
117+
if ($context->enabled()) {
117118
$propagator = new GrpcMetadataPropagator();
118119
$metadata += [
119120
$propagator->key() => [$propagator->formatter()->serialize($context)]

src/Trace/Integrations/Guzzle/EventSubscriber.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ public function getEvents()
8585
public function onBefore(BeforeEvent $event)
8686
{
8787
$request = $event->getRequest();
88-
if ($context = Tracer::spanContext()) {
88+
$context = Tracer::spanContext();
89+
if ($context->enabled()) {
8990
$request->setHeader($this->propagator->key(), $this->propagator->formatter()->serialize($context));
9091
}
9192
$span = Tracer::startSpan([

src/Trace/Integrations/Guzzle/Middleware.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public function __construct(PropagatorInterface $propagator = null)
6767
public function __invoke(callable $handler)
6868
{
6969
return function (RequestInterface $request, $options) use ($handler) {
70-
if ($context = Tracer::spanContext()) {
70+
$context = Tracer::spanContext();
71+
if ($context->enabled()) {
7172
$request = $request->withHeader(
7273
$this->propagator->key(),
7374
$this->propagator->formatter()->serialize($context)

src/Trace/Tracer.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ public static function start(ExporterInterface $reporter, array $options = [])
161161
*/
162162
public static function inSpan(array $spanOptions, callable $callable, array $arguments = [])
163163
{
164+
if (!isset(self::$instance)) {
165+
return call_user_func_array($callable, $arguments);
166+
}
164167
return self::$instance->inSpan($spanOptions, $callable, $arguments);
165168
}
166169

@@ -186,6 +189,9 @@ public static function inSpan(array $spanOptions, callable $callable, array $arg
186189
*/
187190
public static function startSpan(array $spanOptions = [])
188191
{
192+
if (!isset(self::$instance)) {
193+
return new Span();
194+
}
189195
return self::$instance->startSpan($spanOptions);
190196
}
191197

@@ -195,8 +201,8 @@ public static function startSpan(array $spanOptions = [])
195201
*
196202
* Example:
197203
* ```
198-
* $span = RequestTracer::startSpan(['name' => 'expensive-operation']);
199-
* $scope = RequestTracer::withSpan($span);
204+
* $span = Tracer::startSpan(['name' => 'expensive-operation']);
205+
* $scope = Tracer::withSpan($span);
200206
* try {
201207
* // do something expensive
202208
* } finally {
@@ -209,6 +215,10 @@ public static function startSpan(array $spanOptions = [])
209215
*/
210216
public static function withSpan(Span $span)
211217
{
218+
if (!isset(self::$instance)) {
219+
return new Scope(function () {
220+
});
221+
}
212222
return self::$instance->withSpan($span);
213223
}
214224

@@ -223,6 +233,9 @@ public static function withSpan(Span $span)
223233
*/
224234
public static function addAttribute($attribute, $value, $options = [])
225235
{
236+
if (!isset(self::$instance)) {
237+
return;
238+
}
226239
return self::$instance->addAttribute($attribute, $value, $options);
227240
}
228241

@@ -233,6 +246,9 @@ public static function addAttribute($attribute, $value, $options = [])
233246
*/
234247
public static function spanContext()
235248
{
249+
if (!isset(self::$instance)) {
250+
return new SpanContext(null, null, false);
251+
}
236252
return self::$instance->tracer()->spanContext();
237253
}
238254
}

0 commit comments

Comments
 (0)