From b551294d3ffd2f974723914af427fce80e913f5e Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Thu, 15 Sep 2022 10:59:51 +1000 Subject: [PATCH] export otlp InstrumentationScope attributes (#825) * export otlp InstrumentationScope attributes * remove redundant setting of dropped attributes count --- src/Contrib/Otlp/MetricConverter.php | 3 ++- src/Contrib/Otlp/SpanConverter.php | 3 ++- .../Unit/Contrib/Otlp/MetricConverterTest.php | 24 +++++++++++++++++++ .../Contrib/Otlp/OTLPSpanConverterTest.php | 8 ++++++- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/Contrib/Otlp/MetricConverter.php b/src/Contrib/Otlp/MetricConverter.php index 4a50f048..868276f5 100644 --- a/src/Contrib/Otlp/MetricConverter.php +++ b/src/Contrib/Otlp/MetricConverter.php @@ -95,6 +95,7 @@ final class MetricConverter $pInstrumentationScope = new InstrumentationScope(); $pInstrumentationScope->setName($instrumentationScope->getName()); $pInstrumentationScope->setVersion((string) $instrumentationScope->getVersion()); + $this->setAttributes($pInstrumentationScope, $instrumentationScope->getAttributes()); $pScopeMetrics->setScope($pInstrumentationScope); $pScopeMetrics->setSchemaUrl((string) $instrumentationScope->getSchemaUrl()); @@ -230,7 +231,7 @@ final class MetricConverter } /** - * @param Resource_|NumberDataPoint|HistogramDataPoint $pElement + * @param Resource_|NumberDataPoint|HistogramDataPoint|InstrumentationScope $pElement */ private function setAttributes($pElement, SDK\Common\Attribute\AttributesInterface $attributes): void { diff --git a/src/Contrib/Otlp/SpanConverter.php b/src/Contrib/Otlp/SpanConverter.php index 1aa65fab..0b8860c8 100644 --- a/src/Contrib/Otlp/SpanConverter.php +++ b/src/Contrib/Otlp/SpanConverter.php @@ -92,6 +92,7 @@ final class SpanConverter $pInstrumentationScope = new InstrumentationScope(); $pInstrumentationScope->setName($instrumentationScope->getName()); $pInstrumentationScope->setVersion((string) $instrumentationScope->getVersion()); + $this->setAttributes($pInstrumentationScope, $instrumentationScope->getAttributes()); $pScopeSpans->setScope($pInstrumentationScope); $pScopeSpans->setSchemaUrl((string) $instrumentationScope->getSchemaUrl()); @@ -99,7 +100,7 @@ final class SpanConverter } /** - * @param Resource_|Span|Event|Link $pElement + * @param Resource_|Span|Event|Link|InstrumentationScope $pElement */ private function setAttributes($pElement, AttributesInterface $attributes): void { diff --git a/tests/Unit/Contrib/Otlp/MetricConverterTest.php b/tests/Unit/Contrib/Otlp/MetricConverterTest.php index b5e4538b..07440c83 100644 --- a/tests/Unit/Contrib/Otlp/MetricConverterTest.php +++ b/tests/Unit/Contrib/Otlp/MetricConverterTest.php @@ -205,4 +205,28 @@ final class MetricConverterTest extends TestCase ])->getResourceMetrics(), ); } + + public function test_instrumentation_scope_is_converted(): void + { + $this->assertJsonStringEqualsJsonString( + <<convert([ + new Metric( + new InstrumentationScope('scope-name', 'scope-version', 'http://schema.url', Attributes::create(['foo' => 'bar'])), + ResourceInfoFactory::emptyResource(), + 'name-1', + 'unit-1', + 'description-1', + $this->createMock(DataInterface::class), + ), + ])->serializeToJsonString(), + ); + } } diff --git a/tests/Unit/Contrib/Otlp/OTLPSpanConverterTest.php b/tests/Unit/Contrib/Otlp/OTLPSpanConverterTest.php index be256588..97421911 100644 --- a/tests/Unit/Contrib/Otlp/OTLPSpanConverterTest.php +++ b/tests/Unit/Contrib/Otlp/OTLPSpanConverterTest.php @@ -166,7 +166,7 @@ class OTLPSpanConverterTest extends TestCase ->setStartEpochNanos($start_time) ->setEndEpochNanos($end_time) ->setName('http_get') - ->setInstrumentationScope(new InstrumentationScope('lib-test', 'v0.1.0', 'http://url', Attributes::create([]))) + ->setInstrumentationScope(new InstrumentationScope('lib-test', 'v0.1.0', 'http://url', Attributes::create(['foo' => 'bar']))) ->addAttribute('user', 'alice') ->addAttribute('authenticated', true) ->addEvent('Event1', Attributes::create(['success' => 'yes']), 1617313804325769955) @@ -204,6 +204,12 @@ class OTLPSpanConverterTest extends TestCase 'scope' => new \Opentelemetry\Proto\Common\V1\InstrumentationScope([ 'name' => 'lib-test', 'version' => 'v0.1.0', + 'attributes' => [ + new KeyValue([ + 'key' => 'foo', + 'value' => new AnyValue(['string_value' => 'bar']), + ]), + ], ]), 'spans' => [ new V1\Span([