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

Commit 77e75c8

Browse files
authored
Allow collecting time events/links (#171)
* Add tests for ability to add annotations, links, message events * Tracers return SpanData * Add setters to RequestHandler * Implement SpanEventHandler to keep extension spans in sync * Remove extension config file that should be ignored * Add documentation
1 parent 4e6ca09 commit 77e75c8

16 files changed

+1000
-61
lines changed

ext/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ config.status
1515
config.sub
1616
configure
1717
configure.in
18+
configure.ac
1819
install-sh
1920
libtool
2021
ltmain.sh

ext/opencensus_trace_span.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
#include "opencensus_trace_link.h"
9898
#include "opencensus_trace_message_event.h"
9999
#include "Zend/zend_alloc.h"
100+
#include "Zend/zend_variables.h"
100101

101102
zend_class_entry* opencensus_trace_span_ce = NULL;
102103

@@ -498,6 +499,9 @@ int opencensus_trace_span_add_annotation(opencensus_trace_span_t *span, zend_str
498499
opencensus_trace_annotation_t *annotation = opencensus_trace_annotation_alloc();
499500
annotation->time_event.time = opencensus_now();
500501
annotation->description = zend_string_copy(description);
502+
if (options != NULL) {
503+
zend_hash_merge(Z_ARR(annotation->options), Z_ARR_P(options), zval_add_ref, 1);
504+
}
501505

502506
zend_hash_next_index_insert_ptr(span->time_events, annotation);
503507
return SUCCESS;
@@ -509,6 +513,9 @@ int opencensus_trace_span_add_link(opencensus_trace_span_t *span, zend_string *t
509513
opencensus_trace_link_t *link = opencensus_trace_link_alloc();
510514
link->trace_id = zend_string_copy(trace_id);
511515
link->span_id = zend_string_copy(span_id);
516+
if (options != NULL) {
517+
zend_hash_merge(Z_ARR(link->options), Z_ARR_P(options), zval_add_ref, 1);
518+
}
512519

513520
zend_hash_next_index_insert_ptr(span->links, link);
514521
return FAILURE;
@@ -521,6 +528,9 @@ int opencensus_trace_span_add_message_event(opencensus_trace_span_t *span, zend_
521528
message_event->time_event.time = opencensus_now();
522529
message_event->type = zend_string_copy(type);
523530
message_event->id = zend_string_copy(id);
531+
if (options != NULL) {
532+
zend_hash_merge(Z_ARR(message_event->options), Z_ARR_P(options), zval_add_ref, 1);
533+
}
524534

525535
zend_hash_next_index_insert_ptr(span->time_events, message_event);
526536
return SUCCESS;
@@ -569,6 +579,8 @@ int opencensus_trace_span_apply_span_options(opencensus_trace_span_t *span, zval
569579
if (Z_TYPE_P(v) == IS_FALSE) {
570580
span->same_process_as_parent_span = 0;
571581
}
582+
} else if (strcmp(ZSTR_VAL(k), "stackTrace") == 0) {
583+
ZVAL_COPY(&span->stackTrace, v);
572584
}
573585
} ZEND_HASH_FOREACH_END();
574586
return SUCCESS;

ext/tests/annotations.phpt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ OpenCensus Trace: Test setting annotations
44
<?php
55

66
opencensus_trace_begin('root', ['spanId' => '1234']);
7-
opencensus_trace_add_annotation('foo');
7+
opencensus_trace_add_annotation('foo', [
8+
'attributes' => [
9+
'foo' => 'bar'
10+
]
11+
]);
812
opencensus_trace_begin('inner', []);
913
opencensus_trace_add_annotation('asdf', ['spanId' => '1234']);
1014
opencensus_trace_add_annotation('abc');
@@ -29,6 +33,11 @@ Array
2933
[time:protected] => %d.%d
3034
[options:protected] => Array
3135
(
36+
[attributes] => Array
37+
(
38+
[foo] => bar
39+
)
40+
3241
)
3342

3443
)
@@ -39,6 +48,7 @@ Array
3948
[time:protected] => %d.%d
4049
[options:protected] => Array
4150
(
51+
[spanId] => 1234
4252
)
4353

4454
)

ext/tests/links.phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ OpenCensus Trace: Test setting links
44
<?php
55

66
opencensus_trace_begin('root', ['spanId' => '1234']);
7-
opencensus_trace_add_link('traceId', 'spanId');
7+
opencensus_trace_add_link('traceId', 'spanId', [
8+
'type' => 'CHILD_LINKED_SPAN'
9+
]);
810
opencensus_trace_begin('inner', []);
911
opencensus_trace_add_link('traceId', 'spanId', ['spanId' => '1234']);
1012
opencensus_trace_add_link('traceId', 'spanId');
@@ -29,6 +31,7 @@ Array
2931
[spanId:protected] => spanId
3032
[options:protected] => Array
3133
(
34+
[type] => CHILD_LINKED_SPAN
3235
)
3336

3437
)
@@ -39,6 +42,7 @@ Array
3942
[spanId:protected] => spanId
4043
[options:protected] => Array
4144
(
45+
[spanId] => 1234
4246
)
4347

4448
)

ext/tests/message_events.phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ OpenCensus Trace: Test setting message events
44
<?php
55

66
opencensus_trace_begin('root', ['spanId' => '1234']);
7-
opencensus_trace_add_message_event('TYPE_UNSPECIFIED', 'some id');
7+
opencensus_trace_add_message_event('TYPE_UNSPECIFIED', 'some id', [
8+
'compressedSize' => 123
9+
]);
810
opencensus_trace_begin('inner', []);
911
opencensus_trace_add_message_event('TYPE_UNSPECIFIED', 'some id', ['spanId' => '1234']);
1012
opencensus_trace_add_message_event('TYPE_UNSPECIFIED', 'some id');
@@ -30,6 +32,7 @@ Array
3032
[time:protected] => %d.%d
3133
[options:protected] => Array
3234
(
35+
[compressedSize] => 123
3336
)
3437

3538
)
@@ -41,6 +44,7 @@ Array
4144
[time:protected] => %d.%d
4245
[options:protected] => Array
4346
(
47+
[spanId] => 1234
4448
)
4549

4650
)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace OpenCensus\Trace\EventHandler;
4+
5+
use OpenCensus\Trace\Annotation;
6+
use OpenCensus\Trace\Link;
7+
use OpenCensus\Trace\MessageEvent;
8+
use OpenCensus\Trace\Span;
9+
use OpenCensus\Trace\TimeEvent;
10+
11+
/**
12+
* This implementation of the SpanEventHandlerInterface does nothing.
13+
*/
14+
class NullEventHandler implements SpanEventHandlerInterface
15+
{
16+
/**
17+
* Triggers when an attribute is added to a span.
18+
*
19+
* @param Span $span The span the attribute was added to
20+
* @param string $attribute The name of the attribute added
21+
* @param string $value The attribute value
22+
*/
23+
public function attributeAdded(Span $span, $attribute, $value)
24+
{
25+
}
26+
27+
/**
28+
* Triggers when a link is added to a span.
29+
*
30+
* @param Span $span The span the link was added to
31+
* @param Link $link The link added to the span
32+
*/
33+
public function linkAdded(Span $span, Link $link)
34+
{
35+
}
36+
37+
/**
38+
* Triggers when a time event is added to a span.
39+
*
40+
* @param Span $span The span the time event was added to
41+
* @param TimeEvent $timeEvent The time event added to the span
42+
*/
43+
public function timeEventAdded(Span $span, TimeEvent $timeEvent)
44+
{
45+
}
46+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace OpenCensus\Trace\EventHandler;
4+
5+
use OpenCensus\Trace\Link;
6+
use OpenCensus\Trace\Span;
7+
use OpenCensus\Trace\TimeEvent;
8+
9+
/**
10+
* This interface defines events that are triggered when a Span is updated.
11+
*/
12+
interface SpanEventHandlerInterface
13+
{
14+
/**
15+
* Triggers when an attribute is added to a span.
16+
*
17+
* @param Span $span The span the attribute was added to
18+
* @param string $attribute The name of the attribute added
19+
* @param string $value The attribute value
20+
*/
21+
public function attributeAdded(Span $span, $attribute, $value);
22+
23+
/**
24+
* Triggers when a link is added to a span.
25+
*
26+
* @param Span $span The span the link was added to
27+
* @param Link $link The link added to the span
28+
*/
29+
public function linkAdded(Span $span, Link $link);
30+
31+
/**
32+
* Triggers when a time event is added to a span.
33+
*
34+
* @param Span $span The span the time event was added to
35+
* @param TimeEvent $timeEvent The time event added to the span
36+
*/
37+
public function timeEventAdded(Span $span, TimeEvent $timeEvent);
38+
}

src/Trace/RequestHandler.php

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,7 @@ public function onExit()
138138

139139
$this->scope->close();
140140

141-
// Fetch the SpanData of all the collected Spans
142-
$spans = array_map(function (Span $span) {
143-
return $span->spanData();
144-
}, $this->tracer->spans());
145-
$this->exporter->export($spans);
141+
$this->exporter->export($this->tracer->spans());
146142
}
147143

148144
/**
@@ -210,6 +206,59 @@ public function addAttribute($attribute, $value, $options = [])
210206
$this->tracer->addAttribute($attribute, $value, $options);
211207
}
212208

209+
/**
210+
* Add an annotation to the provided Span
211+
*
212+
* @param string $description
213+
* @param array $options [optional] Configuration options.
214+
*
215+
* @type Span $span The span to add the annotation to.
216+
* @type array $attributes Attributes for this annotation.
217+
* @type \DateTimeInterface|int|float $time The time of this event.
218+
*/
219+
public function addAnnotation($description, $options = [])
220+
{
221+
$this->tracer->addAnnotation($description, $options);
222+
}
223+
224+
/**
225+
* Add a link to the provided Span
226+
*
227+
* @param string $traceId
228+
* @param string $spanId
229+
* @param array $options [optional] Configuration options.
230+
*
231+
* @type Span $span The span to add the link to.
232+
* @type string $type The relationship of the current span relative to
233+
* the linked span: child, parent, or unspecified.
234+
* @type array $attributes Attributes for this annotation.
235+
* @type \DateTimeInterface|int|float $time The time of this event.
236+
*/
237+
public function addLink($traceId, $spanId, $options = [])
238+
{
239+
$this->tracer->addLink($traceId, $spanId, $options);
240+
}
241+
242+
/**
243+
* Add an message event to the provided Span
244+
*
245+
* @param string $type
246+
* @param string $id
247+
* @param array $options [optional] Configuration options.
248+
*
249+
* @type Span $span The span to add the message event to.
250+
* @type int $uncompressedSize The number of uncompressed bytes sent or
251+
* received.
252+
* @type int $compressedSize The number of compressed bytes sent or
253+
* received. If missing assumed to be the same size as
254+
* uncompressed.
255+
* @type \DateTimeInterface|int|float $time The time of this event.
256+
*/
257+
public function addMessageEvent($type, $id, $options)
258+
{
259+
$this->tracer->addMessageEvent($type, $id, $options);
260+
}
261+
213262
public function addCommonRequestAttributes(array $headers)
214263
{
215264
$responseCode = http_response_code();

0 commit comments

Comments
 (0)