Resource
Entity metadata attached to all telemetry signals.
A Resource represents the entity producing telemetry data, such as a service, container, or host. Resources are immutable and provide context that is attached to all traces, logs, and metrics.
Common resource attributes include:
- service.name: The logical name of the service
- service.version: The version of the service
- host.name: The hostname of the machine
- process.pid: The process ID
Example usage:
$resource = Resource::create([
'service.name' => 'my-service',
'service.version' => '1.0.0',
]);
Properties
Methods
- __construct() : mixed
- all() : array<string, array<string|int, bool|float|int|string>|bool|float|int|string>
- Get all attributes.
- count() : int
- Get the number of attributes.
- create() : self
- Create a new Resource with the given attributes.
- empty() : self
- Create an empty Resource with no attributes.
- fromArray() : self
- Create a Resource from a normalized array representation.
- get() : null|array<string|int, bool|DateTimeInterface|float|int|string|Throwable>|bool|DateTimeInterface|float|int|string|Throwable
- Get the value of an attribute.
- has() : bool
- Check if an attribute exists.
- isEmpty() : bool
- Check if the resource has no attributes.
- merge() : self
- Merge this Resource with another, with the other's attributes taking precedence.
-
normalize()
: array{attributes: array
} - Normalize the Resource to an array representation for serialization.
- with() : self
- Create a new Resource with an additional attribute.
Properties
$attributes
public
Attributes
$attributes
= new Attributes()
Methods
__construct()
public
__construct([Attributes $attributes = new Attributes() ]) : mixed
Parameters
- $attributes : Attributes = new Attributes()
all()
Get all attributes.
public
all() : array<string, array<string|int, bool|float|int|string>|bool|float|int|string>
Return values
array<string, array<string|int, bool|float|int|string>|bool|float|int|string>count()
Get the number of attributes.
public
count() : int
Return values
intcreate()
Create a new Resource with the given attributes.
public
static create([array<string, array<string|int, bool|float|int|string>|bool|float|int|string> $attributes = [] ]) : self
Parameters
- $attributes : array<string, array<string|int, bool|float|int|string>|bool|float|int|string> = []
Return values
selfempty()
Create an empty Resource with no attributes.
public
static empty() : self
Return values
selffromArray()
Create a Resource from a normalized array representation.
public
static fromArray(array{attributes: array} $data) : self
Parameters
-
$data
: array{attributes: array
} -
Normalized Resource data
Return values
selfget()
Get the value of an attribute.
public
get(string $key) : null|array<string|int, bool|DateTimeInterface|float|int|string|Throwable>|bool|DateTimeInterface|float|int|string|Throwable
Parameters
- $key : string
-
Attribute key
Return values
null|array<string|int, bool|DateTimeInterface|float|int|string|Throwable>|bool|DateTimeInterface|float|int|string|Throwable —The attribute value, or null if not found
has()
Check if an attribute exists.
public
has(string $key) : bool
Parameters
- $key : string
-
Attribute key
Return values
boolisEmpty()
Check if the resource has no attributes.
public
isEmpty() : bool
Return values
boolmerge()
Merge this Resource with another, with the other's attributes taking precedence.
public
merge(self $other) : self
Parameters
- $other : self
-
The Resource to merge with
Return values
self —New Resource with merged attributes
normalize()
Normalize the Resource to an array representation for serialization.
public
normalize() : array{attributes: array}
Return values
array{attributes: arraywith()
Create a new Resource with an additional attribute.
public
with(string $key, array<string|int, bool|float|int|string>|bool|float|int|string $value) : self
If the key already exists, its value will be replaced.
Parameters
- $key : string
-
Attribute key
- $value : array<string|int, bool|float|int|string>|bool|float|int|string
-
Attribute value
Return values
self —New Resource with the added attribute