SpanCreator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 13
c 2
b 0
f 0
dl 0
loc 28
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 17 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