Skip to content

Commit

Permalink
Add instrumentation scope attributes (#735)
Browse files Browse the repository at this point in the history
* Add attributes to instrumentation scope

* Remove `::getDefaultTracer()`

Instrumentation should use dedicated tracers.

* Remove SDK `::getTracer()` default name

Instrumentation should use API `TracerProvider` which requires providing a name.
  • Loading branch information
Nevay authored Jun 30, 2022
1 parent 409dd52 commit 739f212
Show file tree
Hide file tree
Showing 41 changed files with 209 additions and 239 deletions.
2 changes: 1 addition & 1 deletion examples/AirGappedTraceDebugging.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
new SimpleSpanProcessor($exporter),
new AlwaysOnSampler()
);
$tracer = $tracerProvider->getTracer();
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');
/**
* Create some tracing data
*/
Expand Down
2 changes: 1 addition & 1 deletion examples/AlwaysOnJaegerExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

echo 'Starting AlwaysOnJaegerExample';

$tracer = $tracerProvider->getTracer();
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');

$rootSpan = $tracer->spanBuilder('root')->startSpan();
$rootSpan->activate();
Expand Down
2 changes: 1 addition & 1 deletion examples/AlwaysOnJaegerThriftExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

echo 'Starting JaegerThriftExample';

$tracer = $tracerProvider->getTracer();
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');

$rootSpan = $tracer->spanBuilder('root')->startSpan();
$rootSpan->activate();
Expand Down
2 changes: 1 addition & 1 deletion examples/AlwaysOnOTLPExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
$exporter
)
);
$tracer = $tracerProvider->getTracer();
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');

$root = $span = $tracer->spanBuilder('root')->startSpan();
$span->activate();
Expand Down
2 changes: 1 addition & 1 deletion examples/AlwaysOnOTLPGrpcExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
$exporter
)
);
$tracer = $tracerProvider->getTracer();
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');

$root = $span = $tracer->spanBuilder('root')->startSpan();
$span->activate();
Expand Down
2 changes: 1 addition & 1 deletion examples/AlwaysOnZipkinExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
$zipkinExporter
)
);
$tracer = $tracerProvider->getTracer();
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');

echo 'Starting AlwaysOnZipkinExample';

Expand Down
2 changes: 1 addition & 1 deletion examples/BatchExporting.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
)
);

$tracer = $tracerProvider->getTracer();
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');

$rootSpan = $tracer->spanBuilder('root')->startSpan();
$scope = $rootSpan->activate();
Expand Down
2 changes: 1 addition & 1 deletion examples/ConcurrentSpans.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
)
);

$tracer = $tracerProvider->getTracer();
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');

$rootSpan = $tracer->spanBuilder('root')->startSpan();
$scope = $rootSpan->activate();
Expand Down
2 changes: 1 addition & 1 deletion examples/ConfigurationFromEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

$tracerProvider = (new TracerProviderFactory('example'))->create();

$tracer = $tracerProvider->getTracer();
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');

echo 'Starting Tracer' . PHP_EOL;

Expand Down
2 changes: 1 addition & 1 deletion examples/CreatingANewTraceInTheSameProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
new SimpleSpanProcessor(
new ConsoleSpanExporter()
)
))->getTracer();
))->getTracer('io.opentelemetry.contrib.php');

// This creates a span and sets it as the current parent (and root) span
$rootSpan = $tracer->spanBuilder('foo')->startSpan();
Expand Down
26 changes: 0 additions & 26 deletions examples/DefaultTracer.php

This file was deleted.

2 changes: 1 addition & 1 deletion examples/GettingStarted.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)
);

$tracer = $tracerProvider->getTracer();
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');

//start a root span
$rootSpan = $tracer->spanBuilder('root')->startSpan();
Expand Down
2 changes: 1 addition & 1 deletion examples/LoggingOfSpanData.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
new BatchSpanProcessor($decorator),
new AlwaysOnSampler()
);
$tracer = $tracerProvider->getTracer();
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');
/**
* Create some tracing data
*/
Expand Down
2 changes: 1 addition & 1 deletion examples/ResourceDetectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

$tracerProvider = (new TracerProviderFactory('example'))->create();

$tracer = $tracerProvider->getTracer();
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');

echo 'Starting Tracer' . PHP_EOL;

Expand Down
2 changes: 1 addition & 1 deletion examples/SettingUpLogging.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
new OtlpGrpcExporter(), //default endpoint unavailable, so exporting will fail
)
);
$tracer = $tracerProvider->getTracer();
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');
$span = $tracer->spanBuilder('root-span')->startSpan();
$span->end();
2 changes: 1 addition & 1 deletion examples/SpanResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
$resource
);

$tracer = $tracerProvider->getTracer();
$tracer = $tracerProvider->getTracer('io.opentelemetry.contrib.php');

$span = $tracer->spanBuilder('root')->startSpan();
$span->end();
18 changes: 9 additions & 9 deletions examples/distributed-tracing/src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
use Slim\Routing\RouteContext;

//create default tracer from environment variables, now available as TracerProvider::getDefaultTracer
$tracer = (new TracerProviderFactory('example'))->create()->getTracer();
$tracer = (new TracerProviderFactory('example'))->create()->getTracer('io.opentelemetry.contrib.php');

$cb = new ContainerBuilder();
$container = $cb->addDefinitions([
Tracer::class => $tracer,
Client::class => function () {
Client::class => function () use ($tracer) {
$stack = HandlerStack::create();
//a guzzle middleware to wrap http calls in a span, and inject trace headers
$stack->push(function (callable $handler) {
return function (RequestInterface $request, array $options) use ($handler): PromiseInterface {
$span = TracerProvider::getDefaultTracer()
$stack->push(function (callable $handler) use ($tracer) {
return function (RequestInterface $request, array $options) use ($handler, $tracer): PromiseInterface {
$span = $tracer
->spanBuilder(sprintf('%s %s', $request->getMethod(), $request->getUri()))
->setSpanKind(SpanKind::KIND_CLIENT)
->setAttribute('http.method', $request->getMethod())
Expand Down Expand Up @@ -70,11 +70,11 @@
$app = Bridge::create($container);

//middleware starts root span based on route pattern, sets status from http code
$app->add(function (Request $request, RequestHandler $handler) {
$app->add(function (Request $request, RequestHandler $handler) use ($tracer) {
$carrier = TraceContextPropagator::getInstance()->extract($request->getHeaders());
$routeContext = RouteContext::fromRequest($request);
$route = $routeContext->getRoute();
$root = TracerProvider::getDefaultTracer()->spanBuilder($route->getPattern())
$root = $tracer->spanBuilder($route->getPattern())
->setStartTimestamp((int) ($request->getServerParams()['REQUEST_TIME_FLOAT'] * 1e9))
->setParent($carrier)
->setSpanKind(SpanKind::KIND_SERVER)
Expand Down Expand Up @@ -105,8 +105,8 @@
});

//route for service-two
$app->get('/two/{name}', function (Response $response, $name) {
$span = TracerProvider::getDefaultTracer()
$app->get('/two/{name}', function (Response $response, $name) use ($tracer) {
$span = $tracer
->spanBuilder('get-user')
->setAttribute('db.system', 'mysql')
->setAttribute('db.name', 'users')
Expand Down
8 changes: 6 additions & 2 deletions src/API/Trace/NoopTracerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@

class NoopTracerProvider implements TracerProviderInterface
{
public function getTracer(string $name, ?string $version = null, ?string $schemaUrl = null): TracerInterface
{
public function getTracer(
string $name,
?string $version = null,
?string $schemaUrl = null,
iterable $attributes = []
): TracerInterface {
return NoopTracer::getInstance();
}
}
7 changes: 6 additions & 1 deletion src/API/Trace/TracerProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ interface TracerProviderInterface
/**
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/specification/trace/api.md#get-a-tracer
*/
public function getTracer(string $name, ?string $version = null, ?string $schemaUrl = null): TracerInterface;
public function getTracer(
string $name,
?string $version = null,
?string $schemaUrl = null,
iterable $attributes = []
): TracerInterface;
}
3 changes: 3 additions & 0 deletions src/Contrib/Jaeger/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ private static function convertOtelSpanDataToJaegerTags(SpanDataInterface $span)
foreach ($span->getResource()->getAttributes() as $k => $v) {
$tags[$k] = $v;
}
foreach ($span->getInstrumentationScope()->getAttributes() as $k => $v) {
$tags[$k] = $v;
}

$tags = self::buildTags($tags);

Expand Down
3 changes: 3 additions & 0 deletions src/Contrib/Newrelic/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ private function convertSpan(SpanDataInterface $span): array
foreach ($span->getResource()->getAttributes() as $k => $v) {
$row['attributes'][$k] = $v;
}
foreach ($span->getInstrumentationScope()->getAttributes() as $k => $v) {
$row['attributes'][$k] = $v;
}

/*
foreach ($span->getEvents() as $event) {
Expand Down
4 changes: 3 additions & 1 deletion src/Contrib/Otlp/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use OpenTelemetry\SDK\Common\Instrumentation\KeyGenerator;
use OpenTelemetry\SDK\Trace\SpanConverterInterface;
use OpenTelemetry\SDK\Trace\SpanDataInterface;
use function spl_object_id;

class SpanConverter implements SpanConverterInterface
{
Expand Down Expand Up @@ -186,12 +187,13 @@ private function as_otlp_resource_span(iterable $spans): ResourceSpans
{
$isSpansEmpty = true; //Waiting for the loop to prove otherwise

$scopeKeyCache = [];
$instrumentationScopes = $convertedSpans = $schemas = [];
foreach ($spans as /** @var SpanDataInterface $span */ $span) {
$isSpansEmpty = false;

$scope = $span->getInstrumentationScope();
$isKey = KeyGenerator::generateInstanceKey($scope->getName(), $scope->getVersion(), $scope->getSchemaUrl());
$isKey = $scopeKeyCache[spl_object_id($scope)] ??= KeyGenerator::generateInstanceKey($scope);
if (!isset($instrumentationScopes[$isKey])) {
$convertedSpans[$isKey] = [];
$instrumentationScopes[$isKey] = new InstrumentationScope(['name' => $scope->getName(), 'version' => $scope->getVersion() ?? '']);
Expand Down
3 changes: 3 additions & 0 deletions src/Contrib/Zipkin/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ private function convertSpan(SpanDataInterface $span): array
foreach ($span->getResource()->getAttributes() as $k => $v) {
$row['tags'][$k] = $this->sanitiseTagValue($v);
}
foreach ($span->getInstrumentationScope()->getAttributes() as $k => $v) {
$row['tags'][$k] = $this->sanitiseTagValue($v);
}

foreach ($span->getEvents() as $event) {
$row['annotations'][] = SpanConverter::toAnnotation($event);
Expand Down
3 changes: 3 additions & 0 deletions src/Contrib/ZipkinToNewrelic/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ private function convertSpan(SpanDataInterface $span): array
foreach ($span->getResource()->getAttributes() as $k => $v) {
$row['tags'][$k] = $this->sanitiseTagValue($v);
}
foreach ($span->getInstrumentationScope()->getAttributes() as $k => $v) {
$row['tags'][$k] = $this->sanitiseTagValue($v);
}

foreach ($span->getEvents() as $event) {
if (!array_key_exists('annotations', $row)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace OpenTelemetry\SDK\Common\Dev\Compatibility\BC;

use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;
use OpenTelemetry\SDK\Common\Dev\Compatibility\Util;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScope as Moved;

Expand All @@ -18,7 +20,7 @@ final class InstrumentationLibrary implements InstrumentationLibraryInterface

public function __construct(string $name, ?string $version = null, ?string $schemaUrl = null)
{
$this->adapted = new Moved($name, $version, $schemaUrl);
$this->adapted = new Moved($name, $version, $schemaUrl, Attributes::create([]));

Util::triggerClassDeprecationNotice(
OpenTelemetry_SDK_InstrumentationLibrary,
Expand All @@ -40,6 +42,11 @@ public function getSchemaUrl(): ?string
{
return $this->adapted->getSchemaUrl();
}

public function getAttributes(): AttributesInterface
{
return $this->adapted->getAttributes();
}
}

class_alias(InstrumentationLibrary::class, OpenTelemetry_SDK_InstrumentationLibrary);
Expand Down
22 changes: 10 additions & 12 deletions src/SDK/Common/Instrumentation/InstrumentationScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,24 @@

namespace OpenTelemetry\SDK\Common\Instrumentation;

use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;

/**
* Represents the instrumentation scope information associated with the Tracer or Meter
*/
final class InstrumentationScope implements InstrumentationScopeInterface
{
private static ?self $empty = null;

private string $name;
private ?string $version;
private ?string $schemaUrl;
private AttributesInterface $attributes;

public function __construct(string $name, ?string $version = null, ?string $schemaUrl = null)
public function __construct(string $name, ?string $version, ?string $schemaUrl, AttributesInterface $attributes)
{
$this->name = $name;
$this->version = $version;
$this->schemaUrl = $schemaUrl;
}

/**
* @internal
* @psalm-internal OpenTelemetry
*/
public static function getEmpty(): InstrumentationScope
{
return self::$empty ?? self::$empty = new self('', null, null);
$this->attributes = $attributes;
}

public function getName(): string
Expand All @@ -45,4 +38,9 @@ public function getSchemaUrl(): ?string
{
return $this->schemaUrl;
}

public function getAttributes(): AttributesInterface
{
return $this->attributes;
}
}
Loading

0 comments on commit 739f212

Please sign in to comment.