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

Commit f4bed32

Browse files
Takashi Matsuochingor13
authored andcommitted
Reports failure when reporting fails. (#36)
* Reports failure when reporting fails. fixes #35 * Also let the LoggerReporter reports error upon failure.
1 parent 3f7c093 commit f4bed32

5 files changed

Lines changed: 74 additions & 0 deletions

File tree

src/Trace/Reporter/GoogleCloudReporter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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
}

src/Trace/Reporter/LoggerReporter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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;

tests/unit/Trace/Reporter/GoogleCloudReporterTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace OpenCensus\Tests\Unit\Trace\Reporter;
1919

20+
require_once __DIR__ . '/mock_error_log.php';
21+
2022
use OpenCensus\Trace\Reporter\GoogleCloudReporter;
2123
use OpenCensus\Trace\TraceContext;
2224
use 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'));

tests/unit/Trace/Reporter/LoggerReporterTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace OpenCensus\Tests\Unit\Trace\Reporter;
1919

20+
require_once __DIR__ . '/mock_error_log.php';
21+
2022
use Psr\Log\LoggerInterface;
2123
use OpenCensus\Trace\Reporter\LoggerReporter;
2224
use 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 = [
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

0 commit comments

Comments
 (0)