This repository was archived by the owner on Oct 3, 2023. It is now read-only.
File tree Expand file tree Collapse file tree
tests/unit/Trace/Reporter Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -177,6 +177,7 @@ public function report(TracerInterface $tracer)
177177 return self ::$ client ->insert ($ trace );
178178 }
179179 } catch (\Exception $ e ) {
180+ error_log ('Reporting the Trace data failed: ' . $ e ->getMessage ());
180181 return false ;
181182 }
182183 }
Original file line number Diff line number Diff line change @@ -61,6 +61,7 @@ public function report(TracerInterface $tracer)
6161 try {
6262 $ this ->logger ->log ($ this ->level , json_encode ($ tracer ->spans ()));
6363 } catch (\Exception $ e ) {
64+ error_log ('Reporting the Trace data failed: ' . $ e ->getMessage ());
6465 return false ;
6566 }
6667 return true ;
Original file line number Diff line number Diff line change 1717
1818namespace OpenCensus \Tests \Unit \Trace \Reporter ;
1919
20+ require_once __DIR__ . '/mock_error_log.php ' ;
21+
2022use OpenCensus \Trace \Reporter \GoogleCloudReporter ;
2123use OpenCensus \Trace \TraceContext ;
2224use OpenCensus \Trace \Tracer \TracerInterface ;
@@ -61,6 +63,25 @@ public function testFormatsTrace()
6163 }
6264 }
6365
66+ public function testReportWithAnExceptionErrorLog ()
67+ {
68+ $ tracer = new ContextTracer (new TraceContext ('testtraceid ' ));
69+ $ tracer ->inSpan (['name ' => 'main ' ], function () {});
70+ $ this ->client ->insert (Argument::any ())->willThrow (
71+ new \Exception ('error_log test ' )
72+ );
73+ $ trace = $ this ->prophesize (Trace::class);
74+ $ trace ->setSpans (Argument::any ())->shouldBeCalled ();
75+ $ this ->client ->trace (Argument::any ())->willReturn ($ trace ->reveal ());
76+ $ reporter = new GoogleCloudReporter (
77+ ['client ' => $ this ->client ->reveal ()]
78+ );
79+ $ this ->expectOutputString (
80+ 'Reporting the Trace data failed: error_log test '
81+ );
82+ $ this ->assertFalse ($ reporter ->report ($ tracer ));
83+ }
84+
6485 public function testHandlesKind ()
6586 {
6687 $ tracer = new ContextTracer (new TraceContext ('testtraceid ' ));
Original file line number Diff line number Diff line change 1717
1818namespace OpenCensus \Tests \Unit \Trace \Reporter ;
1919
20+ require_once __DIR__ . '/mock_error_log.php ' ;
21+
2022use Psr \Log \LoggerInterface ;
2123use OpenCensus \Trace \Reporter \LoggerReporter ;
2224use OpenCensus \Trace \TraceContext ;
@@ -38,6 +40,28 @@ public function setUp()
3840 $ this ->tracer = $ this ->prophesize (TracerInterface::class);
3941 }
4042
43+ public function testReportWithAnExceptionErrorLog ()
44+ {
45+ $ spans = [
46+ new TraceSpan ([
47+ 'name ' => 'span ' ,
48+ 'startTime ' => microtime (true ),
49+ 'endTime ' => microtime (true ) + 10
50+ ])
51+ ];
52+ $ this ->tracer ->context ()->willReturn (new TraceContext ('testtraceid ' ));
53+ $ this ->tracer ->spans ()->willReturn ($ spans );
54+ $ this ->logger ->log ('some-level ' , Argument::type ('string ' ))->willThrow (
55+ new \Exception ('error_log test ' )
56+ );
57+
58+ $ reporter = new LoggerReporter ($ this ->logger ->reveal (), 'some-level ' );
59+ $ this ->expectOutputString (
60+ 'Reporting the Trace data failed: error_log test '
61+ );
62+ $ this ->assertFalse ($ reporter ->report ($ this ->tracer ->reveal ()));
63+ }
64+
4165 public function testLogsTrace ()
4266 {
4367 $ spans = [
Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * Copyright 2017 OpenCensus Authors
4+ *
5+ * Licensed under the Apache License, Version 2.0 (the "License");
6+ * you may not use this file except in compliance with the License.
7+ * You may obtain a copy of the License at
8+ *
9+ * http://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ */
17+
18+ namespace OpenCensus \Trace \Reporter ;
19+
20+ /**
21+ * A mock function for testing error_log function.
22+ */
23+
24+ function error_log ($ msg )
25+ {
26+ echo $ msg ;
27+ }
You can’t perform that action at this time.
0 commit comments