This repository was archived by the owner on Jul 31, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed
Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 2424use Google \Cloud \Trace \Trace ;
2525use OpenCensus \Trace \SpanData ;
2626use OpenCensus \Trace \Exporter \Stackdriver \SpanConverter ;
27+ use OpenCensus \Version ;
2728
2829/**
2930 * This implementation of the ExporterInterface use the BatchRunner to provide
7172 */
7273class StackdriverExporter implements ExporterInterface
7374{
75+ const AGENT = 'g.co/agent ' ;
76+
7477 use BatchTrait;
7578
7679 /**
@@ -144,8 +147,13 @@ public function export(array $spans)
144147
145148 // Pull the traceId from the first span
146149 $ spans = array_map ([SpanConverter::class, 'convertSpan ' ], $ spans );
150+ $ rootSpan = $ spans [0 ];
147151 $ trace = self ::$ client ->trace (
148- $ spans [0 ]->traceId ()
152+ $ rootSpan ->traceId ()
153+ );
154+ $ rootSpan ->addAttribute (
155+ self ::AGENT ,
156+ sprintf ('opencensus-php [%s] ' , Version::VERSION )
149157 );
150158
151159 // build a Trace object and assign Spans
Original file line number Diff line number Diff line change @@ -83,4 +83,28 @@ public function testEmptyTrace()
8383 $ exporter = new StackdriverExporter (['client ' => $ this ->client ->reveal ()]);
8484 $ this ->assertFalse ($ exporter ->export ([]));
8585 }
86+
87+ public function testReportsVersionAttribute ()
88+ {
89+ $ trace = $ this ->prophesize (Trace::class);
90+ $ trace ->setSpans (Argument::that (function ($ spans ) {
91+ $ this ->assertCount (1 , $ spans );
92+ $ attributes = $ spans [0 ]->jsonSerialize ()['attributes ' ];
93+ $ this ->assertArrayHasKey ('g.co/agent ' , $ attributes );
94+ $ this ->assertRegexp ('/\d+\.\d+\.\d+/ ' , $ attributes ['g.co/agent ' ]);
95+ return true ;
96+ }))->shouldBeCalled ();
97+ $ this ->client ->trace ('aaa ' )->willReturn ($ trace ->reveal ());
98+ $ this ->client ->insert (Argument::type (Trace::class))
99+ ->willReturn (true )->shouldBeCalled ();
100+
101+ $ span = new OCSpan ([
102+ 'traceId ' => 'aaa '
103+ ]);
104+ $ span ->setStartTime ();
105+ $ span ->setEndTime ();
106+
107+ $ exporter = new StackdriverExporter (['client ' => $ this ->client ->reveal ()]);
108+ $ this ->assertTrue ($ exporter ->export ([$ span ->spanData ()]));
109+ }
86110}
You can’t perform that action at this time.
0 commit comments