Skip to content

Commit 1206934

Browse files
authored
adding resource detectors to registry (#999)
To enable registering non-SDK resource detectors within the SDK, extend the SDK Registry. Detectors will be looked up by name and applied as part of 'all', or as part of a list of detectors
1 parent 1e38d2e commit 1206934

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

Registry.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use OpenTelemetry\SDK\Common\Export\TransportFactoryInterface;
99
use OpenTelemetry\SDK\Logs\LogRecordExporterFactoryInterface;
1010
use OpenTelemetry\SDK\Metrics\MetricExporterFactoryInterface;
11+
use OpenTelemetry\SDK\Resource\ResourceDetectorInterface;
1112
use OpenTelemetry\SDK\Trace\SpanExporter\SpanExporterFactoryInterface;
1213
use RuntimeException;
1314

@@ -22,6 +23,7 @@ class Registry
2223
private static array $metricExporterFactories = [];
2324
private static array $textMapPropagators = [];
2425
private static array $logRecordExporterFactories = [];
26+
private static array $resourceDetectors = [];
2527

2628
/**
2729
* @param TransportFactoryInterface|class-string<TransportFactoryInterface> $factory
@@ -91,6 +93,7 @@ public static function registerMetricExporterFactory(string $exporter, $factory,
9193
}
9294
self::$metricExporterFactories[$exporter] = $factory;
9395
}
96+
9497
public static function registerLogRecordExporterFactory(string $exporter, $factory, bool $clobber = false): void
9598
{
9699
if (!$clobber && array_key_exists($exporter, self::$logRecordExporterFactories)) {
@@ -119,6 +122,11 @@ public static function registerTextMapPropagator(string $name, TextMapPropagator
119122
self::$textMapPropagators[$name] = $propagator;
120123
}
121124

125+
public static function registerResourceDetector(string $name, ResourceDetectorInterface $detector): void
126+
{
127+
self::$resourceDetectors[$name] = $detector;
128+
}
129+
122130
public static function spanExporterFactory(string $exporter): SpanExporterFactoryInterface
123131
{
124132
if (!array_key_exists($exporter, self::$spanExporterFactories)) {
@@ -180,4 +188,21 @@ public static function textMapPropagator(string $name): TextMapPropagatorInterfa
180188

181189
return self::$textMapPropagators[$name];
182190
}
191+
192+
public static function resourceDetector(string $name): ResourceDetectorInterface
193+
{
194+
if (!array_key_exists($name, self::$resourceDetectors)) {
195+
throw new RuntimeException('Resource detector not registered for: ' . $name);
196+
}
197+
198+
return self::$resourceDetectors[$name];
199+
}
200+
201+
/**
202+
* @return array<int, ResourceDetectorInterface>
203+
*/
204+
public static function resourceDetectors(): array
205+
{
206+
return array_values(self::$resourceDetectors);
207+
}
183208
}

Resource/ResourceInfoFactory.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
namespace OpenTelemetry\SDK\Resource;
66

77
use function in_array;
8+
use OpenTelemetry\API\Behavior\LogsMessagesTrait;
89
use OpenTelemetry\SDK\Common\Attribute\Attributes;
910
use OpenTelemetry\SDK\Common\Configuration\Configuration;
1011
use OpenTelemetry\SDK\Common\Configuration\KnownValues as Values;
1112
use OpenTelemetry\SDK\Common\Configuration\Variables as Env;
13+
use OpenTelemetry\SDK\Registry;
14+
use RuntimeException;
1215

1316
class ResourceInfoFactory
1417
{
18+
use LogsMessagesTrait;
19+
1520
/**
1621
* Merges resources into a new one.
1722
*
@@ -43,10 +48,10 @@ public static function defaultResource(): ResourceInfo
4348
new Detectors\OperatingSystem(),
4449
new Detectors\Process(),
4550
new Detectors\ProcessRuntime(),
46-
new Detectors\Container(),
4751
new Detectors\Sdk(),
4852
new Detectors\SdkProvided(),
4953
new Detectors\Composer(),
54+
...Registry::resourceDetectors(),
5055
new Detectors\Environment(),
5156
]))->getResource();
5257
}
@@ -82,17 +87,21 @@ public static function defaultResource(): ResourceInfo
8287
case Values::VALUE_DETECTORS_SDK_PROVIDED:
8388
$resourceDetectors[] = new Detectors\SdkProvided();
8489

85-
break;
86-
case Values::VALUE_DETECTORS_CONTAINER:
87-
$resourceDetectors[] = new Detectors\Container();
88-
8990
break;
9091

9192
case Values::VALUE_DETECTORS_COMPOSER:
9293
$resourceDetectors[] = new Detectors\Composer();
9394

95+
break;
96+
case Values::VALUE_NONE:
97+
9498
break;
9599
default:
100+
try {
101+
$resourceDetectors[] = Registry::resourceDetector($detector);
102+
} catch (RuntimeException $e) {
103+
self::logWarning($e->getMessage());
104+
}
96105
}
97106
}
98107

0 commit comments

Comments
 (0)