summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/sdk/Resource/ResourceInfo.php
blob: 4210a61428102de380582361666a07b0695c2eea (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Resource;

use OpenTelemetry\API\Behavior\LogsMessagesTrait;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;

use OpenTelemetry\SDK\Common\Dev\Compatibility\Util as BcUtil;

/**
 * A Resource is an immutable representation of the entity producing telemetry. For example, a process producing telemetry
 * that is running in a container on Kubernetes has a Pod name, it is in a namespace and possibly is part of a Deployment
 * which also has a name. All three of these attributes can be included in the Resource.
 *
 * The class named as ResourceInfo due to `resource` is the soft reserved word in PHP.
 */
class ResourceInfo
{
    use LogsMessagesTrait;

    private AttributesInterface $attributes;
    private ?string $schemaUrl;

    private function __construct(AttributesInterface $attributes, ?string $schemaUrl = null)
    {
        $this->attributes = $attributes;
        $this->schemaUrl = $schemaUrl;
    }

    public static function create(AttributesInterface $attributes, ?string $schemaUrl = null): self
    {
        return new ResourceInfo($attributes, $schemaUrl);
    }

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

    public function getSchemaUrl(): ?string
    {
        return $this->schemaUrl;
    }

    public function serialize(): string
    {
        $copyOfAttributesAsArray = array_slice($this->attributes->toArray(), 0); //This may be overly cautious (in trying to avoid mutating the source array)
        ksort($copyOfAttributesAsArray); //sort the associative array by keys since the serializer will consider equal arrays different otherwise

        //The exact return value doesn't matter, as long as it can distingusih between instances that represent the same/different resources
        return serialize([
            'schemaUrl' => $this->schemaUrl,
            'attributes' => $copyOfAttributesAsArray,
        ]);
    }

    /**
     * Merge current resource with an updating resource, combining all attributes. If a key exists on both the old and updating
     * resource, the value of the updating resource MUST be picked (even if the updated value is empty)
     *
     * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/resource/sdk.md#merge
     */
    public function merge(ResourceInfo $updating): ResourceInfo
    {
        $schemaUrl = self::mergeSchemaUrl($this->getSchemaUrl(), $updating->getSchemaUrl());
        $attributes = $updating->getAttributes()->toArray() + $this->getAttributes()->toArray();

        return ResourceInfo::create(Attributes::create($attributes), $schemaUrl);
    }

    /**
     * Merge the schema URLs from the old and updating resource.
     * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.20.0/specification/resource/sdk.md#merge
     */
    private static function mergeSchemaUrl(?string $old, ?string $updating): ?string
    {
        if (empty($old)) {
            return $updating;
        }
        if (empty($updating)) {
            return $old;
        }
        if ($old === $updating) {
            return $old;
        }

        self::logWarning('Merging resources with different schema URLs', [
            'old' => $old,
            'updating' => $updating,
        ]);

        return null;
    }

    /**
     * @codeCoverageIgnore
     */
    public static function defaultResource(): ResourceInfo
    {
        BcUtil::triggerMethodDeprecationNotice(
            __METHOD__,
            'defaultResource',
            ResourceInfoFactory::class
        );

        return ResourceInfoFactory::defaultResource();
    }

    /**
     * @codeCoverageIgnore
     */
    public static function emptyResource(): ResourceInfo
    {
        BcUtil::triggerMethodDeprecationNotice(
            __METHOD__,
            'emptyResource',
            ResourceInfoFactory::class
        );

        return ResourceInfoFactory::emptyResource();
    }
}