summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/sdk/Logs/ReadableLogRecord.php
blob: 5c6531477c761f879ffb276d51cdd5f721805aff (plain)
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
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Logs;

use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\API\Trace\SpanContextInterface;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Context\ContextInterface;
use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;
use OpenTelemetry\SDK\Common\Attribute\LogRecordAttributeValidator;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeInterface;
use OpenTelemetry\SDK\Resource\ResourceInfo;

/**
 * @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#log-and-event-record-definition
 * "Note: Typically this will be implemented with a new interface or (immutable) value type."
 */
class ReadableLogRecord extends LogRecord
{
    private InstrumentationScopeInterface $scope;
    private LoggerSharedState $loggerSharedState;
    protected AttributesInterface $convertedAttributes;
    protected SpanContextInterface $spanContext;

    public function __construct(InstrumentationScopeInterface $scope, LoggerSharedState $loggerSharedState, LogRecord $logRecord)
    {
        $this->scope = $scope;
        $this->loggerSharedState = $loggerSharedState;

        parent::__construct($logRecord->body);
        $this->timestamp = $logRecord->timestamp;
        $this->observedTimestamp = $logRecord->observedTimestamp
            ?? (int) (microtime(true) * LogRecord::NANOS_PER_SECOND);
        $this->context = $logRecord->context;
        $context = $this->context ?? Context::getCurrent();
        $this->spanContext = Span::fromContext($context)->getContext();
        $this->severityNumber = $logRecord->severityNumber;
        $this->severityText = $logRecord->severityText;

        //convert attributes now so that excess data is not sent to processors
        $this->convertedAttributes = $this->loggerSharedState
            ->getLogRecordLimits()
            ->getAttributeFactory()
            ->builder($logRecord->attributes, new LogRecordAttributeValidator())
            ->build();
    }

    public function getInstrumentationScope(): InstrumentationScopeInterface
    {
        return $this->scope;
    }

    public function getResource(): ResourceInfo
    {
        return $this->loggerSharedState->getResource();
    }

    public function getTimestamp(): ?int
    {
        return $this->timestamp;
    }

    public function getObservedTimestamp(): ?int
    {
        return $this->observedTimestamp;
    }

    public function getContext(): ?ContextInterface
    {
        return $this->context;
    }

    public function getSpanContext(): ?SpanContextInterface
    {
        return $this->spanContext;
    }

    public function getSeverityNumber(): ?int
    {
        return $this->severityNumber;
    }

    public function getSeverityText(): ?string
    {
        return $this->severityText;
    }

    /**
     * @return mixed|null
     */
    public function getBody()
    {
        return $this->body;
    }

    public function getAttributes(): AttributesInterface
    {
        return $this->convertedAttributes;
    }
}