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

Commit 5432014

Browse files
authored
Add static methods to global tracer for annotations, links, message events (#178)
* Add static methods to global tracer * Add setters to global tracer for annotations, links, message events
1 parent cd2b4d5 commit 5432014

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

src/Trace/Tracer.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,68 @@ public static function addAttribute($attribute, $value, $options = [])
239239
return self::$instance->addAttribute($attribute, $value, $options);
240240
}
241241

242+
/**
243+
* Add an annotation to the provided Span
244+
*
245+
* @param string $description
246+
* @param array $options [optional] Configuration options.
247+
*
248+
* @type Span $span The span to add the annotation to.
249+
* @type array $attributes Attributes for this annotation.
250+
* @type \DateTimeInterface|int|float $time The time of this event.
251+
*/
252+
public static function addAnnotation($description, $options = [])
253+
{
254+
if (!isset(self::$instance)) {
255+
return;
256+
}
257+
return self::$instance->addAnnotation($description, $options);
258+
}
259+
260+
/**
261+
* Add a link to the provided Span
262+
*
263+
* @param string $traceId
264+
* @param string $spanId
265+
* @param array $options [optional] Configuration options.
266+
*
267+
* @type Span $span The span to add the link to.
268+
* @type string $type The relationship of the current span relative to
269+
* the linked span: child, parent, or unspecified.
270+
* @type array $attributes Attributes for this annotation.
271+
* @type \DateTimeInterface|int|float $time The time of this event.
272+
*/
273+
public static function addLink($traceId, $spanId, $options = [])
274+
{
275+
if (!isset(self::$instance)) {
276+
return;
277+
}
278+
return self::$instance->addLink($traceId, $spanId, $options);
279+
}
280+
281+
/**
282+
* Add an message event to the provided Span
283+
*
284+
* @param string $type
285+
* @param string $id
286+
* @param array $options [optional] Configuration options.
287+
*
288+
* @type Span $span The span to add the message event to.
289+
* @type int $uncompressedSize The number of uncompressed bytes sent or
290+
* received.
291+
* @type int $compressedSize The number of compressed bytes sent or
292+
* received. If missing assumed to be the same size as
293+
* uncompressed.
294+
* @type \DateTimeInterface|int|float $time The time of this event.
295+
*/
296+
public static function addMessageEvent($type, $id, $options = [])
297+
{
298+
if (!isset(self::$instance)) {
299+
return;
300+
}
301+
return self::$instance->addMessageEvent($type, $id, $options);
302+
}
303+
242304
/**
243305
* Returns the current span context.
244306
*

tests/unit/Trace/TracerTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,68 @@ public function testForceEnabled()
5858

5959
$this->assertTrue($tracer->spanContext()->enabled());
6060
}
61+
62+
public function testGlobalAttributes()
63+
{
64+
$rt = Tracer::start($this->reporter->reveal(), [
65+
'sampler' => new AlwaysSampleSampler(),
66+
'skipReporting' => true
67+
]);
68+
Tracer::addAttribute('foo', 'bar');
69+
$spans = $rt->tracer()->spans();
70+
$span = $spans[count($spans) - 1];
71+
$this->assertEquals(['foo' => 'bar'], $span->attributes());
72+
}
73+
74+
public function testGlobalAnnotation()
75+
{
76+
$rt = Tracer::start($this->reporter->reveal(), [
77+
'sampler' => new AlwaysSampleSampler(),
78+
'skipReporting' => true
79+
]);
80+
Tracer::addAnnotation('some description', [
81+
'attributes' => [
82+
'foo' => 'bar'
83+
]
84+
]);
85+
$spans = $rt->tracer()->spans();
86+
$span = $spans[count($spans) - 1];
87+
88+
$this->assertCount(1, $span->timeEvents());
89+
$annotation = $span->timeEvents()[0];
90+
$this->assertEquals('some description', $annotation->description());
91+
$this->assertEquals(['foo' => 'bar'], $annotation->attributes());
92+
}
93+
94+
public function testGlobalMessageEvent()
95+
{
96+
$rt = Tracer::start($this->reporter->reveal(), [
97+
'sampler' => new AlwaysSampleSampler(),
98+
'skipReporting' => true
99+
]);
100+
Tracer::addMessageEvent('SENT', 'message-id');
101+
$spans = $rt->tracer()->spans();
102+
$span = $spans[count($spans) - 1];
103+
104+
$this->assertCount(1, $span->timeEvents());
105+
$messageEvent = $span->timeEvents()[0];
106+
$this->assertEquals('SENT', $messageEvent->type());
107+
$this->assertEquals('message-id', $messageEvent->id());
108+
}
109+
110+
public function testGlobalLink()
111+
{
112+
$rt = Tracer::start($this->reporter->reveal(), [
113+
'sampler' => new AlwaysSampleSampler(),
114+
'skipReporting' => true
115+
]);
116+
Tracer::addLink('trace-id', 'span-id');
117+
$spans = $rt->tracer()->spans();
118+
$span = $spans[count($spans) - 1];
119+
120+
$this->assertCount(1, $span->links());
121+
$link = $span->links()[0];
122+
$this->assertEquals('trace-id', $link->traceId());
123+
$this->assertEquals('span-id', $link->spanId());
124+
}
61125
}

0 commit comments

Comments
 (0)