Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export otlp InstrumentationScope attributes #825

Merged
merged 2 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Contrib/Otlp/MetricConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ private function convertScopeMetrics(SDK\Common\Instrumentation\InstrumentationS
$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());

Expand Down Expand Up @@ -230,7 +231,7 @@ private function convertExemplar(SDK\Metrics\Data\Exemplar $exemplar): Exemplar
}

/**
* @param Resource_|NumberDataPoint|HistogramDataPoint $pElement
* @param Resource_|NumberDataPoint|HistogramDataPoint|InstrumentationScope $pElement
*/
private function setAttributes($pElement, SDK\Common\Attribute\AttributesInterface $attributes): void
{
Expand Down
3 changes: 2 additions & 1 deletion src/Contrib/Otlp/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,15 @@ private function convertScopeSpans(InstrumentationScopeInterface $instrumentatio
$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());

return $pScopeSpans;
}

/**
* @param Resource_|Span|Event|Link $pElement
* @param Resource_|Span|Event|Link|InstrumentationScope $pElement
*/
private function setAttributes($pElement, AttributesInterface $attributes): void
{
Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/Contrib/Otlp/MetricConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,28 @@ public function test_multiple_resources_result_in_multiple_resource_metrics(): v
])->getResourceMetrics(),
);
}

public function test_instrumentation_scope_is_converted(): void
{
$this->assertJsonStringEqualsJsonString(
<<<JSON
{"resourceMetrics": [{"resource": {},"scopeMetrics": [{
"metrics": [{"description": "description-1","name": "name-1","unit": "unit-1"}],
"schemaUrl": "http://schema.url",
"scope": {"attributes": [{"key": "foo","value": {"stringValue": "bar"}}],"name": "scope-name","version": "scope-version"}
}
]}]}
JSON,
(new MetricConverter())->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(),
);
}
}
8 changes: 7 additions & 1 deletion tests/Unit/Contrib/Otlp/OTLPSpanConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function test_otlp_happy_path_span(): void
->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)
Expand Down Expand Up @@ -202,6 +202,12 @@ public function test_otlp_happy_path_span(): void
'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([
Expand Down