• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FriendsOfOpenTelemetry / opentelemetry-bundle / 7738304608

01 Feb 2024 08:26AM UTC coverage: 37.885% (-26.6%) from 64.527%
7738304608

Pull #38

github

gaelreyrol
wip
Pull Request #38: Refactor services injection

100 of 459 new or added lines in 44 files covered. (21.79%)

255 existing lines in 18 files now uncovered.

799 of 2109 relevant lines covered (37.89%)

8.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/DependencyInjection/OpenTelemetryTracesExtension.php
1
<?php
2

3
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection;
4

5
use FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Exporter\ExporterOptionsInterface;
6
use FriendsOfOpenTelemetry\OpenTelemetryBundle\OpenTelemetry\Trace\TraceSamplerEnum;
7
use Symfony\Component\DependencyInjection\ChildDefinition;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\DependencyInjection\Reference;
11

12
/**
13
 * @phpstan-import-type ExporterOptions from ExporterOptionsInterface
14
 */
15
final class OpenTelemetryTracesExtension
16
{
17
    /**
18
     * @var array<string, mixed>
19
     */
20
    private array $config;
21
    private ContainerBuilder $container;
22

23
    /**
24
     * @param array{
25
     *     default_tracer?: string,
26
     *     tracers: array<string, mixed>,
27
     *     exporters: array<string, mixed>,
28
     *     processors: array<string, mixed>,
29
     *     providers: array<string, mixed>
30
     * }|array<string, mixed> $config
31
     */
32
    public function __invoke(array $config, ContainerBuilder $container): void
33
    {
UNCOV
34
        $this->config = $config;
×
UNCOV
35
        $this->container = $container;
×
36

UNCOV
37
        foreach ($this->config['exporters'] as $name => $exporter) {
×
UNCOV
38
            $this->loadTraceExporter($name, $exporter);
×
39
        }
40

UNCOV
41
        foreach ($this->config['processors'] as $name => $processor) {
×
UNCOV
42
            $this->loadTraceProcessor($name, $processor);
×
43
        }
44

UNCOV
45
        foreach ($this->config['providers'] as $name => $provider) {
×
UNCOV
46
            $this->loadTraceProvider($name, $provider);
×
47
        }
48

UNCOV
49
        foreach ($this->config['tracers'] as $name => $tracer) {
×
UNCOV
50
            $this->loadTraceTracer($name, $tracer);
×
51
        }
52

UNCOV
53
        $defaultTracer = null;
×
UNCOV
54
        if (0 < count($this->config['tracers'])) {
×
UNCOV
55
            $defaultTracer = array_key_first($this->config['tracers']);
×
56
        }
57

UNCOV
58
        if (null !== $defaultTracer) {
×
UNCOV
59
            $this->container->setAlias('open_telemetry.traces.default_tracer', new Reference(sprintf('open_telemetry.traces.tracers.%s', $defaultTracer)));
×
60
        }
61
    }
62

63
    /**
64
     * @param array{
65
     *      dsn: string,
66
     *      options?: ExporterOptions
67
     *  } $options
68
     */
69
    private function loadTraceExporter(string $name, array $options): void
70
    {
NEW
71
        $dsn = $this->container->getDefinition('open_telemetry.exporter_dsn')->setArguments([$options['dsn']]);
×
NEW
72
        $exporterOptions = $this->container->getDefinition('open_telemetry.otlp_exporter_options')->setArguments([$options['options'] ?? []]);
×
73

UNCOV
74
        $this->container
×
NEW
75
            ->setDefinition(
×
NEW
76
                sprintf('open_telemetry.traces.exporters.%s', $name),
×
NEW
77
                new ChildDefinition('open_telemetry.traces.exporter_interface')
×
NEW
78
            )
×
NEW
79
            ->setArguments([$dsn, $exporterOptions]);
×
80
    }
81

82
    /**
83
     * @param array{
84
     *      type: string,
85
     *      processors?: string[],
86
     *      exporter?: string
87
     *  } $processor
88
     */
89
    private function loadTraceProcessor(string $name, array $processor): void
90
    {
UNCOV
91
        $this->container
×
NEW
92
            ->setDefinition(
×
NEW
93
                sprintf('open_telemetry.traces.processors.%s', $name),
×
NEW
94
                new ChildDefinition('open_telemetry.traces.processor_interface'),
×
NEW
95
            )
×
NEW
96
            ->setFactory([new Reference(sprintf('open_telemetry.traces.processor_factory.%s', $processor['type'])), 'createProcessor'])
×
UNCOV
97
            ->setArguments([
×
NEW
98
                array_map(fn (string $processor) => new Reference($processor), $processor['processors'] ?? []),
×
NEW
99
                new Reference($processor['exporter'], ContainerInterface::NULL_ON_INVALID_REFERENCE),
×
UNCOV
100
            ]);
×
101
    }
102

103
    /**
104
     * @param array{
105
     *     type: string,
106
     *     sampler?: array{type: string, probability?: float},
107
     *     processors?: string[]
108
     * } $provider
109
     */
110
    private function loadTraceProvider(string $name, array $provider): void
111
    {
NEW
112
        $sampler = $this->container->getDefinition('open_telemetry.traces.sampler_factory')->setArguments([
×
NEW
113
            $provider['sampler']['type'] ?? TraceSamplerEnum::AlwaysOn->value,
×
NEW
114
            $provider['sampler']['probability'] ?? null,
×
NEW
115
        ]);
×
116

UNCOV
117
        $this->container
×
NEW
118
            ->setDefinition(
×
NEW
119
                sprintf('open_telemetry.traces.providers.%s', $name),
×
NEW
120
                new ChildDefinition('open_telemetry.traces.provider_interface'),
×
NEW
121
            )
×
NEW
122
            ->setFactory([new Reference(sprintf('open_telemetry.traces.provider_factory.%s', $provider['type'])), 'createProvider'])
×
UNCOV
123
            ->setArguments([
×
NEW
124
                $sampler,
×
NEW
125
                array_map(fn (string $processor) => new Reference($processor), $provider['processors'] ?? []),
×
UNCOV
126
            ]);
×
127
    }
128

129
    /**
130
     * @param array{
131
     *     name?: string,
132
     *     version?: string,
133
     *     provider: string
134
     * } $tracer
135
     */
136
    private function loadTraceTracer(string $name, array $tracer): void
137
    {
UNCOV
138
        $this->container
×
NEW
139
            ->setDefinition(
×
NEW
140
                sprintf('open_telemetry.traces.tracers.%s', $name),
×
NEW
141
                new ChildDefinition('open_telemetry.traces.tracer'),
×
NEW
142
            )
×
UNCOV
143
            ->setPublic(true)
×
NEW
144
            ->setFactory([new Reference($tracer['provider']), 'getTracer'])
×
UNCOV
145
            ->setArguments([
×
UNCOV
146
                $tracer['name'] ?? $this->container->getParameter('open_telemetry.bundle.name'),
×
UNCOV
147
                $tracer['version'] ?? $this->container->getParameter('open_telemetry.bundle.version'),
×
UNCOV
148
            ]);
×
149
    }
150
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc