Flow PHP

Attributes

Read onlyYes
FinalYes

Immutable container for telemetry attributes.

Attributes are key-value pairs attached to spans, logs, metrics, resources, and events. Values can be strings, integers, floats, booleans, DateTimeInterface, Throwable, or arrays of primitive types.

Example usage:

$attributes = Attributes::create([
    'user.id' => '12345',
    'user.roles' => ['admin', 'user'],
    'timestamp' => new \DateTimeImmutable(),
    'error' => $exception,
]);
Tags
phpstan-type

TAttributeValue = array<bool|\DateTimeInterface|float|int|string|\Throwable>|bool|\DateTimeInterface|float|int|string|\Throwable

phpstan-type

TAttributeValueMap = array<string, TAttributeValue>

Methods

__construct()  : mixed
count()  : int
Get attribute count.
create()  : self
Create Attributes from key-value pairs.
empty()  : self
Create empty Attributes.
fromArray()  : self
Create Attributes from normalized array.
get()  : null|TAttributeValue
Get a specific attribute value.
has()  : bool
Check if attribute exists.
id()  : string
Create a stable identity string from scalar attributes.
isEmpty()  : bool
Check if empty.
merge()  : self
Merge with another Attributes instance.
normalize()  : array<string, array<string|int, bool|float|int|string>|bool|float|int|string>
Normalize to array representation.
with()  : self
Create new Attributes with additional attribute.

Methods

__construct()

public __construct([array<string, null|TAttributeValue$values = [] ]) : mixed
Parameters
$values : array<string, null|TAttributeValue> = []

count()

Get attribute count.

public count() : int
Return values
int

create()

Create Attributes from key-value pairs.

public static create([array<string, null|TAttributeValue$values = [] ]) : self
Parameters
$values : array<string, null|TAttributeValue> = []
Return values
self

empty()

Create empty Attributes.

public static empty() : self
Return values
self

fromArray()

Create Attributes from normalized array.

public static fromArray(array<string, null|TAttributeValue$data) : self
Parameters
$data : array<string, null|TAttributeValue>
Return values
self

get()

Get a specific attribute value.

public get(string $key) : null|TAttributeValue
Parameters
$key : string
Return values
null|TAttributeValue

has()

Check if attribute exists.

public has(string $key) : bool
Parameters
$key : string
Return values
bool

id()

Create a stable identity string from scalar attributes.

public id() : string

Sorts attributes by key and joins them as key=value pairs separated by pipes. Useful for grouping/keying metrics by attribute set.

Returns empty string for empty attributes.

Return values
string

isEmpty()

Check if empty.

public isEmpty() : bool
Return values
bool

merge()

Merge with another Attributes instance.

public merge(self $other) : self

The other instance's attributes take precedence over this instance's attributes when keys overlap.

Parameters
$other : self
Return values
self

normalize()

Normalize to array representation.

public normalize() : array<string, array<string|int, bool|float|int|string>|bool|float|int|string>

DateTimeInterface values are converted to ISO 8601 strings. Throwable values are converted to structured arrays with type, message, and stacktrace. Null values are excluded from the result.

Return values
array<string, array<string|int, bool|float|int|string>|bool|float|int|string>

with()

Create new Attributes with additional attribute.

public with(string $key, TAttributeValue $value) : self

If the key already exists, its value will be replaced.

Parameters
$key : string
$value : TAttributeValue
Return values
self

        
On this page

Search results