Return empty objects when no spans are passed in OTLP/GRPC exporter implementation (#412)

* Return empty object if no spans are passed for OTLPGrpc

* Return empty object if no spans are passed for OtlpHttp

* Remove trailing whitespace

* Remove extra whitespace
This commit is contained in:
Grunet 2021-09-15 14:05:23 -05:00 committed by GitHub
parent 500ff88cdb
commit 1cfd72accf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 4 deletions

View File

@ -176,11 +176,12 @@ class SpanConverter
public function as_otlp_resource_span(iterable $spans): ResourceSpans
{
// TODO: Should return an empty ResourceSpans when $spans is empty
// At the minute it returns an semi populated ResourceSpan
$isSpansEmpty = true; //Waiting for the loop to prove otherwise
$ils = $convertedSpans = [];
foreach ($spans as $span) {
$isSpansEmpty = false;
/** @var \OpenTelemetry\Sdk\InstrumentationLibrary $il */
$il = $span->getInstrumentationLibrary();
$ilKey = sprintf('%s@%s', $il->getName(), $il->getVersion()??'');
@ -191,6 +192,10 @@ class SpanConverter
$convertedSpans[$ilKey][] = $this->as_otlp_span($span);
}
if ($isSpansEmpty == true) {
return new Proto\Trace\V1\ResourceSpans();
}
$ilSpans = [];
foreach ($ils as $ilKey => $il) {
$ilSpans[] = new InstrumentationLibrarySpans([

View File

@ -174,11 +174,12 @@ class SpanConverter
public function as_otlp_resource_span(iterable $spans): ResourceSpans
{
// TODO: Should return an empty ResourceSpans when $spans is empty
// At the minute it returns an semi populated ResourceSpan
$isSpansEmpty = true; //Waiting for the loop to prove otherwise
$ils = $convertedSpans = [];
foreach ($spans as $span) {
$isSpansEmpty = false;
/** @var \OpenTelemetry\Sdk\InstrumentationLibrary $il */
$il = $span->getInstrumentationLibrary();
$ilKey = sprintf('%s@%s', $il->getName(), $il->getVersion()??'');
@ -189,6 +190,10 @@ class SpanConverter
$convertedSpans[$ilKey][] = $this->as_otlp_span($span);
}
if ($isSpansEmpty == true) {
return new Proto\Trace\V1\ResourceSpans();
}
$ilSpans = [];
foreach ($ils as $ilKey => $il) {
$ilSpans[] = new InstrumentationLibrarySpans([

View File

@ -264,4 +264,13 @@ class OTLPGrpcSpanConverterTest extends TestCase
$this->assertEquals($expected, $otlpspan);
}
public function testOtlpNoSpans()
{
$spans = [];
$otlpspan = (new SpanConverter())->as_otlp_resource_span($spans);
$this->assertEquals(new ResourceSpans(), $otlpspan);
}
}

View File

@ -252,4 +252,13 @@ class OTLPHttpSpanConverterTest extends TestCase
$this->assertEquals($expected, $otlpspan);
}
public function testOtlpNoSpans()
{
$spans = [];
$otlpspan = (new SpanConverter())->as_otlp_resource_span($spans);
$this->assertEquals(new ResourceSpans(), $otlpspan);
}
}