-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathConfiguration.php
629 lines (575 loc) · 21.9 KB
/
Configuration.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
<?php
declare(strict_types=1);
namespace OpenTelemetry\Symfony\OtelSdkBundle\DependencyInjection;
use OpenTelemetry\SDK\Trace\SpanExporterInterface;
use OpenTelemetry\Symfony\OtelSdkBundle\Util\ExporterDsnParser;
use Psr\Log\LoggerInterface;
use ReflectionClass;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* @codeCoverageIgnore
*/
class Configuration implements ConfigurationInterface
{
// PUBLIC CONSTANTS
public const ROOT_KEY = 'otel_sdk';
public const CUSTOM_TYPE = 'custom';
public const DEFAULT_TYPE = 'default';
public const CLASS_NODE = 'class';
public const ID_NODE = 'id';
public const RESOURCE_NODE = 'resource';
public const LIMITS_NODE = 'limits';
public const LIMITS_COUNT_DEFAULT = 128;
public const ATTRIBUTES_NODE = 'attributes';
public const SERVICE_NAME_ATTR = 'service.name';
public const SERVICE_NAME_NODE = 'service_name';
public const REQUIRED_SOURCE_ATTRS = [
self::SERVICE_NAME_ATTR,
];
public const TRACE_NODE = 'trace';
public const SAMPLER_NODE = 'sampler';
public const ROOT_NODE = 'root';
public const REMOTE_NODE = 'remote';
public const LOCAL_NODE = 'local';
public const SAMPLED_NODE = 'sampled';
public const NOT_SAMPLED_NODE = 'not_sampled';
public const ALWAYS_ON_SAMPLER = 'always_on';
public const ALWAYS_OFF_SAMPLER = 'always_off';
public const TRACE_ID_RATIO_SAMPLER = 'trace_id_ratio_based';
public const PARENT_BASED_SAMPLER = 'parent_based';
public const SAMPLER_NODE_DEFAULT = self::ALWAYS_ON_SAMPLER;
public const SAMPLER_NODE_VALUES = [
self::ALWAYS_ON_SAMPLER,
self::ALWAYS_OFF_SAMPLER,
self::TRACE_ID_RATIO_SAMPLER,
];
public const PROBABILITY = 'probability';
public const PROBABILITY_DEFAULT = 1.0;
public const SPAN_NODE = 'span';
public const ATTR_COUNT_NODE = 'attribute_count';
public const ATTR_VALUE_LENGTH_NODE = 'attribute_value_length';
public const EVENT_COUNT_NODE = 'event_count';
public const LINK_COUNT_NODE = 'link_count';
public const ATTRS_EVENT_NODE = 'attribute_per_event';
public const ATTRS_LINK_NODE = 'attribute_per_link';
public const SPAN_LIMIT_ATTRS = [
self::ATTR_COUNT_NODE,
self::ATTR_VALUE_LENGTH_NODE,
self::EVENT_COUNT_NODE,
self::LINK_COUNT_NODE,
self::ATTRS_EVENT_NODE,
self::ATTRS_LINK_NODE,
];
public const PROCESSORS_NODE = 'processors';
public const SIMPLE_PROCESSOR = 'simple';
public const BATCH_PROCESSOR = 'batch';
public const NOOP_PROCESSOR = 'noop';
public const MULTI_PROCESSOR = 'multi';
public const PROCESSOR_DEFAULT = self::BATCH_PROCESSOR;
public const PROCESSOR_NODE_VALUES = [
self::SIMPLE_PROCESSOR,
self::BATCH_PROCESSOR,
self::NOOP_PROCESSOR,
];
public const EXPORTERS_NODE = 'exporters';
public const PROCESSOR_NODE = 'processor';
public const DSN_NODE = 'dsn';
public const TYPE_NODE = 'type';
public const URL_NODE = 'url';
public const OPTIONS_NODE = 'options';
public const NAME_KEY = 'name';
public const ENV_TYPE = 'env';
public const JAEGER_EXPORTER = 'jaeger';
public const ZIPKIN_EXPORTER = 'zipkin';
public const NEWRELIC_EXPORTER = 'newrelic';
public const OTLP_HTTP_EXPORTER = 'otlphttp';
public const OTLP_GRPC_EXPORTER = 'otlpgrpc';
public const ZIPKIN_TO_NEWRELIC_EXPORTER = 'zipkintonewrelic';
public const EXPORTERS_NODE_VALUES = [
self::JAEGER_EXPORTER,
self::ZIPKIN_EXPORTER,
self::NEWRELIC_EXPORTER,
self::OTLP_HTTP_EXPORTER,
self::OTLP_GRPC_EXPORTER,
self::ZIPKIN_TO_NEWRELIC_EXPORTER,
];
// PRIVATE CONSTANTS
private const ARRAY_NODE_TYPE = 'array';
private const SCALAR_NODE_TYPE = 'scalar';
private const RESOURCE_XML = 'resource';
private const PROCESSORS_XML = 'processor';
private const EXPORTERS_XML = 'exporter';
private const OPTIONS_XML = 'option';
private const ENV_PREFIX = 'env_';
private const SERVICE_PREFIX = '@';
private const EXPORTER_HR = 'span exporter';
private const PROCESSOR_HR = 'span processor';
// PRIVATE PROPERTIES
private ?LoggerInterface $logger = null;
private bool $debug;
/**
* @param bool $debug The kernel.debug value
*/
public function __construct(bool $debug = false, ?LoggerInterface $logger = null)
{
$this->debug = $debug;
$this->logger = $logger;
}
public function getConfigTreeBuilder(): TreeBuilder
{
$this->debug('Start building config tree in ' . __CLASS__);
$treeBuilder = self::createTreeBuilder(static::ROOT_KEY);
/** @phpstan-ignore-next-line */
$rootNode = $treeBuilder->getRootNode()
/** @psalm-suppress PossiblyUndefinedMethod **/
->canBeDisabled()
;
self::addResourceSection($rootNode);
self::addTraceSection($rootNode);
$this->debug('Finished building config tree in ' . __CLASS__);
return $treeBuilder;
}
private static function createTreeBuilder(
string $name,
string $type = self::ARRAY_NODE_TYPE,
NodeBuilder $builder = null
): TreeBuilder {
return new TreeBuilder($name, $type, $builder);
}
private static function addResourceSection(ArrayNodeDefinition $node): void
{
$node
->children()
->arrayNode(self::RESOURCE_NODE)
->children()
->append(self::createResourceLimitsNode())
->append(self::createResourceAttributesNode())
->end();
}
private static function addTraceSection(ArrayNodeDefinition $node): void
{
$node
->children()
->arrayNode(self::TRACE_NODE)
->canBeDisabled()
->children()
->append(self::createSamplerSectionNode())
->append(self::createSpanSectionNode())
->append(self::createExporters())
->end();
}
private static function createResourceLimitsNode(): NodeDefinition
{
/** @phpstan-ignore-next-line */
return self::createTreeBuilder(self::LIMITS_NODE)
->getRootNode()
/** @psalm-suppress PossiblyUndefinedMethod **/
->children()
->integerNode(self::ATTR_COUNT_NODE)
->defaultValue(self::LIMITS_COUNT_DEFAULT)
->end()
->integerNode(self::ATTR_VALUE_LENGTH_NODE)
->defaultValue(PHP_INT_MAX)
->end()
->end()
;
}
private static function createResourceAttributesNode(): NodeDefinition
{
/** @phpstan-ignore-next-line */
return self::createTreeBuilder(self::ATTRIBUTES_NODE)
->getRootNode()
->beforeNormalization()
->ifTrue(static function ($v) {
foreach (self::REQUIRED_SOURCE_ATTRS as $attr) {
if (!isset($v[$attr])) {
return true;
}
}
return false;
})
->then(static function ($v) {
throw new ConfigurationException(
sprintf(
'Opentelemetry configuration must provide following resource attributes: %s ',
implode(', ', self::REQUIRED_SOURCE_ATTRS)
)
);
})
->end()
/** @psalm-suppress PossiblyUndefinedMethod **/
->fixXmlConfig(self::RESOURCE_XML)
->useAttributeAsKey(self::NAME_KEY)
->prototype(self::SCALAR_NODE_TYPE)
->end()
;
}
private static function createSamplerSectionNode(): NodeDefinition
{
/** @phpstan-ignore-next-line */
return self::createTreeBuilder(self::SAMPLER_NODE)
->getRootNode()
->beforeNormalization()
->ifString()
->then(static function ($v) {
return [self::ROOT_NODE => $v];
})
->end()
/** @psalm-suppress PossiblyUndefinedMethod **/
->append(self::createSamplerNode(self::ROOT_NODE))
->children()
->arrayNode(self::REMOTE_NODE)
->append(self::createSamplerNode(self::SAMPLED_NODE))
->append(self::createSamplerNode(self::NOT_SAMPLED_NODE, self::ALWAYS_OFF_SAMPLER))
->end()
->arrayNode(self::LOCAL_NODE)
->append(self::createSamplerNode(self::SAMPLED_NODE))
->append(self::createSamplerNode(self::NOT_SAMPLED_NODE, self::ALWAYS_OFF_SAMPLER))
->end()
->end()
;
}
private static function createSamplerNode(string $name, string $default = self::SAMPLER_NODE_DEFAULT): NodeDefinition
{
/** @phpstan-ignore-next-line */
return self::createTreeBuilder($name)
->getRootNode()
->beforeNormalization()
->ifString()
->then(static function ($v) {
return [self::TYPE_NODE => $v];
})
->end()
->beforeNormalization()
->ifTrue(static function ($v) {
return $v[self::TYPE_NODE] === self::CUSTOM_TYPE;
})
->then(static function ($v) {
self::validateCustomService($v);
return $v;
})
->end()
->beforeNormalization()
->ifTrue(static function ($v) {
return $v[self::TYPE_NODE] !== self::CUSTOM_TYPE;
})
->then(static function ($v) {
if (!in_array($v[self::TYPE_NODE], self::SAMPLER_NODE_VALUES)) {
throw new ConfigurationException(
sprintf(
'sampler type must be either "custom" or one of : %s. Given: %s',
implode(', ', self::SAMPLER_NODE_VALUES),
$v[self::TYPE_NODE]
)
);
}
return $v;
})
->end()
/** @psalm-suppress PossiblyUndefinedMethod **/
->children()
->append(self::createTypeNode($default))
->append(self::createClassNode())
->append(self::createIdNode())
->append(self::createOptionsNodes())
->end()
;
}
private static function createSpanSectionNode(): NodeDefinition
{
/** @phpstan-ignore-next-line */
return self::createTreeBuilder(self::SPAN_NODE)
->getRootNode()
/** @psalm-suppress PossiblyUndefinedMethod **/
->children()
->append(self::createSpanLimitsNode())
->append(self::createSpanProcessors())
->end()
;
}
private static function createSpanLimitsNode(): NodeDefinition
{
/** @phpstan-ignore-next-line */
return self::createTreeBuilder(self::LIMITS_NODE)
->getRootNode()
/** @psalm-suppress PossiblyUndefinedMethod **/
->children()
->integerNode(self::ATTR_COUNT_NODE)->defaultValue(self::LIMITS_COUNT_DEFAULT)->end()
->integerNode(self::ATTR_VALUE_LENGTH_NODE)->defaultValue(PHP_INT_MAX)->end()
->integerNode(self::EVENT_COUNT_NODE)->defaultValue(self::LIMITS_COUNT_DEFAULT)->end()
->integerNode(self::LINK_COUNT_NODE)->defaultValue(self::LIMITS_COUNT_DEFAULT)->end()
->integerNode(self::ATTRS_EVENT_NODE)->defaultValue(self::LIMITS_COUNT_DEFAULT)->end()
->integerNode(self::ATTRS_LINK_NODE)->defaultValue(self::LIMITS_COUNT_DEFAULT)->end()
->end()
;
}
private static function createSpanProcessors(): NodeDefinition
{
/** @phpstan-ignore-next-line */
return self::createTreeBuilder(self::PROCESSORS_NODE)
->getRootNode()
->beforeNormalization()
->ifString()
->castToArray()
->end()
/** @psalm-suppress PossiblyUndefinedMethod **/
->fixXmlConfig(self::PROCESSORS_XML)
->arrayPrototype()
->beforeNormalization()
->ifString()
->then(static function ($v) {
return [self::TYPE_NODE => $v];
})
->end()
->beforeNormalization()
->ifTrue(static function ($v) {
return $v[self::TYPE_NODE] === self::CUSTOM_TYPE;
})
->then(static function ($v) {
self::validateCustomService($v, self::PROCESSOR_HR);
return $v;
})
->end()
->beforeNormalization()
->ifTrue(static function ($v) {
return $v[self::TYPE_NODE] !== self::CUSTOM_TYPE;
})
->then(static function ($v) {
if (!in_array($v[self::TYPE_NODE], self::PROCESSOR_NODE_VALUES)) {
throw new ConfigurationException(
sprintf(
'span processor type must be either "custom" or one of : %s. Given: %s',
implode(', ', self::PROCESSOR_NODE_VALUES),
$v[self::TYPE_NODE]
)
);
}
return $v;
})
->end()
->children()
->append(self::createTypeNode(self::PROCESSOR_DEFAULT))
->append(self::createClassNode())
->append(self::createIdNode())
->append(self::createOptionsNodes())
->end()
->end()
;
}
private static function createExporters(): NodeDefinition
{
/** @phpstan-ignore-next-line */
return self::createTreeBuilder(self::EXPORTERS_NODE)
->getRootNode()
/** @phpstan-ignore-next-line */
->requiresAtLeastOneElement()
->beforeNormalization()
->ifString()
->castToArray()
->end()
->fixXmlConfig(self::EXPORTERS_XML)
->arrayPrototype()
->beforeNormalization()
->always()
->then(static function ($v) {
return self::normalizeExporterConfig($v);
})
->end()
->children()
->append(self::createTypeNode())
->scalarNode(self::PROCESSOR_NODE)
->defaultValue(self::DEFAULT_TYPE)
->end()
->scalarNode(self::URL_NODE)->end()
->append(self::createClassNode())
->append(self::createIdNode())
->append(self::createOptionsNodes())
->end()
->end()
;
}
private static function createClassNode(): NodeDefinition
{
return self::createTreeBuilder(self::CLASS_NODE, self::SCALAR_NODE_TYPE)
->getRootNode();
}
private static function createIdNode(): NodeDefinition
{
return self::createTreeBuilder(self::ID_NODE, self::SCALAR_NODE_TYPE)
->getRootNode();
}
private static function createOptionsNodes(): NodeDefinition
{
/** @phpstan-ignore-next-line */
return self::createTreeBuilder(self::OPTIONS_NODE, self::ARRAY_NODE_TYPE)
->getRootNode()
->fixXmlConfig(self::OPTIONS_XML)
->useAttributeAsKey(self::NAME_KEY)
->prototype(self::SCALAR_NODE_TYPE)->end();
}
private static function createTypeNode(?string $default = null): NodeDefinition
{
$node = self::createTreeBuilder(self::TYPE_NODE, self::SCALAR_NODE_TYPE)
->getRootNode()
->isRequired();
if ($default !== null) {
$node->defaultValue($default);
}
return $node;
}
/**
* @param mixed $config
* @return array|string[]
*/
private static function normalizeExporterConfig($config): array
{
if (is_array($config)) {
// exporter is set via - dsn: type+scheme://host:123/path
if (isset($config[self::DSN_NODE])) {
$dsn = $config[self::DSN_NODE];
if (is_string($dsn)) {
return self::normalizeExporterConfig($dsn);
}
throw new ConfigurationException(
'Exporter configuration "dsn" value must be a string'
);
}
if (isset($config[self::TYPE_NODE])) {
self::validateTypedExporterConfig($config);
return $config;
}
throw new ConfigurationException(
'Exporter configuration must either have a key "dsn", keys "type"+"url" or type=custom and keys "class" or "id"'
);
}
if (is_string($config)) {
if (self::isEnvVarReference($config)) {
return [
self::TYPE_NODE => self::ENV_TYPE,
self::URL_NODE => $config,
];
}
return self::exporterDsnToArray($config);
}
throw new ConfigurationException(
'Exporter configuration must be either a dsn or an array'
);
}
private static function exporterDsnToArray(string $config): array
{
return ExporterDsnParser::parse($config)->asConfigArray();
}
private static function isEnvVarReference(string $value): bool
{
return stripos($value, self::ENV_PREFIX) === 0;
}
private static function isServiceReference(string $value): bool
{
return stripos($value, self::SERVICE_PREFIX) === 0;
}
private static function validateTypedExporterConfig(array $config)
{
// custom exporter
if ($config[self::TYPE_NODE] === self::CUSTOM_TYPE) {
self::validateCustomExporterConfig($config);
}
// exporter is set via eg. - [type: jaeger, url: scheme://host:123/path]
if (isset($config[self::URL_NODE])) {
//return $config;
}
}
private static function validateCustomExporterConfig(array $config)
{
// custom exporters need class or id provided.
self::validateCustomService(
$config,
self::EXPORTER_HR
);
if (isset($config[self::CLASS_NODE])) {
// custom exporters classes need to be a valid FQCN
self::validateCustomClass(
$config[self::CLASS_NODE],
self::EXPORTER_HR
);
// custom span exporters need to implement OpenTelemetry\SDK\Trace\SpanExporterInterface
self::validateCustomClassImplements(
$config[self::CLASS_NODE],
SpanExporterInterface::class,
self::EXPORTER_HR
);
}
}
private static function validateCustomService(array $config, string $type = '')
{
if (!isset($config[self::CLASS_NODE]) && !isset($config[self::ID_NODE])) {
throw new ConfigurationException(
sprintf(
'Custom %s service needs a "class" or "id" option to be configured',
$type
)
);
}
if (isset($config[self::CLASS_NODE]) && isset($config[self::ID_NODE])) {
throw new ConfigurationException(
sprintf(
'Custom %s service needs either a "class" or "id" option to be configured, not both',
$type
)
);
}
if (isset($config[self::CLASS_NODE])) {
self::validateCustomClass($config[self::CLASS_NODE]);
}
}
private static function validateCustomClass(string $fqcn, string $type = '')
{
if (!class_exists($fqcn)) {
throw new ConfigurationException(
sprintf(
'Could not find configured custom %s class. given: %s',
$type,
$fqcn
)
);
}
}
private static function validateCustomClassImplements(string $fqcn, string $interface, string $type = '')
{
if (!self::classImplemets($fqcn, $interface)) {
throw new ConfigurationException(
sprintf(
'Custom %s class need to implement %s',
$type,
SpanExporterInterface::class
)
);
}
}
private static function classImplemets(string $fqcn, string $interface): bool
{
try {
return in_array($interface, (new ReflectionClass($fqcn))->getInterfaceNames());
} catch (\Throwable $t) {
return false;
}
}
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
private function debug(string $message, array $context = [])
{
if (!$this->isDebug() || !$this->logger instanceof LoggerInterface) {
return;
}
$this->logger->debug($message, $context);
}
private function isDebug(): bool
{
return $this->debug;
}
}