-
Notifications
You must be signed in to change notification settings - Fork 0
/
Env.php
126 lines (111 loc) · 6.3 KB
/
Env.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php declare(strict_types=1);
namespace Nevay\OTelSDK\Configuration;
use Nevay\OTelSDK\Common\Provider\MultiProvider;
use Nevay\OTelSDK\Common\Provider\NoopProvider;
use Nevay\OTelSDK\Common\Resource;
use Nevay\OTelSDK\Configuration\Env\EnvResolver;
use Nevay\OTelSDK\Configuration\Env\Loader;
use Nevay\OTelSDK\Configuration\Env\LoaderRegistry;
use Nevay\OTelSDK\Configuration\Env\MutableLoaderRegistry;
use Nevay\OTelSDK\Configuration\Environment\EnvSourceReader;
use Nevay\OTelSDK\Configuration\Environment\PhpIniEnvSource;
use Nevay\OTelSDK\Configuration\Environment\ServerEnvSource;
use Nevay\OTelSDK\Logs\LoggerProviderBuilder;
use Nevay\OTelSDK\Logs\LogRecordProcessor;
use Nevay\OTelSDK\Metrics\ExemplarFilter;
use Nevay\OTelSDK\Metrics\MeterProviderBuilder;
use Nevay\OTelSDK\Metrics\MetricReader;
use Nevay\OTelSDK\Trace\Sampler;
use Nevay\OTelSDK\Trace\SpanProcessor;
use Nevay\OTelSDK\Trace\TracerProviderBuilder;
use Nevay\SPI\ServiceLoader;
use OpenTelemetry\API\Logs\NoopEventLoggerProvider;
use OpenTelemetry\API\Logs\NoopLoggerProvider;
use OpenTelemetry\API\Metrics\Noop\NoopMeterProvider;
use OpenTelemetry\API\Trace\NoopTracerProvider;
use OpenTelemetry\Context\Propagation\MultiTextMapPropagator;
use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface;
use function array_unique;
final class Env {
public static function load(
Context $context = new Context(),
): ConfigurationResult {
$registry = new MutableLoaderRegistry();
foreach (ServiceLoader::load(Loader::class) as $loader) {
$registry->register($loader);
}
$env = new EnvResolver(new EnvSourceReader([
new ServerEnvSource(),
new PhpIniEnvSource(),
]), $context->logger);
$propagators = [];
foreach (array_unique($env->list('OTEL_PROPAGATORS') ?? ['tracecontext', 'baggage']) as $name) {
$propagators[] = $registry->load(TextMapPropagatorInterface::class, $name, $env, $context);
}
$textMapPropagator = new MultiTextMapPropagator($propagators);
if ($env->bool('OTEL_SDK_DISABLED') ?? false) {
return new ConfigurationResult(
$textMapPropagator,
new NoopTracerProvider(),
new NoopMeterProvider(),
new NoopLoggerProvider(),
new NoopEventLoggerProvider(),
new NoopProvider(),
);
}
$tracerProviderBuilder = new TracerProviderBuilder();
$meterProviderBuilder = new MeterProviderBuilder();
$loggerProviderBuilder = new LoggerProviderBuilder();
$attributes = $env->map('OTEL_RESOURCE_ATTRIBUTES') ?? [];
if (($serviceName = $env->string('OTEL_SERVICE_NAME')) !== null) {
$attributes['service.name'] = $serviceName;
}
$resource = Resource::create($attributes);
$tracerProviderBuilder->addResource($resource);
$meterProviderBuilder->addResource($resource);
$loggerProviderBuilder->addResource($resource);
$attributeCountLimit = $env->int('OTEL_ATTRIBUTE_COUNT_LIMIT');
$attributeValueLengthLimit = $env->int('OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT');
$tracerProviderBuilder->setAttributeLimits($attributeCountLimit, $attributeValueLengthLimit);
$loggerProviderBuilder->setAttributeLimits($attributeCountLimit, $attributeValueLengthLimit);
self::tracerProvider($tracerProviderBuilder, $env, $registry, $context);
self::meterProvider($meterProviderBuilder, $env, $registry, $context);
self::loggerProvider($loggerProviderBuilder, $env, $registry, $context);
$resource = Resource::detect();
$tracerProviderBuilder->addResource($resource);
$meterProviderBuilder->addResource($resource);
$loggerProviderBuilder->addResource($resource);
$tracerProvider = $tracerProviderBuilder->build($context->logger);
$meterProvider = $meterProviderBuilder->build($context->logger);
$loggerProvider = $loggerProviderBuilder->build($context->logger);
return new ConfigurationResult(
$textMapPropagator,
$tracerProvider,
$meterProvider,
$loggerProvider,
$loggerProvider,
new MultiProvider([
$tracerProvider,
$meterProvider,
$loggerProvider,
]),
);
}
private static function tracerProvider(TracerProviderBuilder $tracerProviderBuilder, EnvResolver $env, LoaderRegistry $registry, Context $context): void {
$tracerProviderBuilder->setSpanAttributeLimits($env->int('OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT'), $env->int('OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT'));
$tracerProviderBuilder->setEventCountLimit($env->int('OTEL_SPAN_EVENT_COUNT_LIMIT'));
$tracerProviderBuilder->setLinkCountLimit($env->int('OTEL_SPAN_LINK_COUNT_LIMIT'));
$tracerProviderBuilder->setEventAttributeLimits($env->int('OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT'));
$tracerProviderBuilder->setLinkAttributeLimits($env->int('OTEL_LINK_ATTRIBUTE_COUNT_LIMIT'));
$tracerProviderBuilder->setSampler($registry->load(Sampler::class, $env->string('OTEL_TRACES_SAMPLER') ?? 'parentbased_always_on', $env, $context));
$tracerProviderBuilder->addSpanProcessor($registry->load(SpanProcessor::class, $env->string('OTEL_TRACES_EXPORTER') ?? 'otlp', $env, $context));
}
private static function meterProvider(MeterProviderBuilder $meterProviderBuilder, EnvResolver $env, LoaderRegistry $registry, Context $context): void {
$meterProviderBuilder->setExemplarFilter($registry->load(ExemplarFilter::class, $env->string('OTEL_METRICS_EXEMPLAR_FILTER') ?? 'trace_based', $env, $context));
$meterProviderBuilder->addMetricReader($registry->load(MetricReader::class, $env->string('OTEL_METRICS_EXPORTER') ?? 'otlp', $env, $context));
}
private static function loggerProvider(LoggerProviderBuilder $loggerProviderBuilder, EnvResolver $env, LoaderRegistry $registry, Context $context): void {
$loggerProviderBuilder->setLogRecordAttributeLimits($env->int('OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT'), $env->int('OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT'));
$loggerProviderBuilder->addLogRecordProcessor($registry->load(LogRecordProcessor::class, $env->string('OTEL_LOGS_EXPORTER') ?? 'otlp', $env, $context));
}
}