SpanCreator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\Jaravel\Services\Span;
6
7
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator;
8
use OpenTelemetry\API\Trace\SpanInterface;
9
use OpenTelemetry\API\Trace\TracerInterface;
10
11
class SpanCreator
12
{
13
    private TracerInterface $tracer;
14
    private TraceContextPropagator $contextPropagator;
15
16
    public function __construct(TracerInterface $tracer, TraceContextPropagator $contextPropagator)
17
    {
18
        $this->tracer = $tracer;
19
        $this->contextPropagator = $contextPropagator;
20
    }
21
22
    public function create(
23
        string $operationName,
24
        ?string $traceIdHeader = null,
25
        ?string $traceStateHeader = null
26
    ): SpanInterface {
27
        $spanBuilder = $this->tracer->spanBuilder($operationName);
28
29
        if ($traceIdHeader) {
30
            $fields = [
31
                TraceContextPropagator::TRACEPARENT => $traceIdHeader,
32
                TraceContextPropagator::TRACESTATE => $traceStateHeader ?? null,
33
            ];
34
            $context = $this->contextPropagator->extract($fields);
35
            $spanBuilder->setParent($context);
36
        }
37
38
        return $spanBuilder->startSpan();
39
    }
40
}
41