Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\API\Instrumentation\AutoInstrumentation;

class AttributeTrackerById
{
/**
* @var array<non-empty-string, mixed>
*/
protected array $attributes = [];

public function has(string|int $id): bool
{
return isset($this->attributes[$id]);
}

/**
* @param array<non-empty-string, mixed> $attributes
*/
public function set(string|int $id, array $attributes): self
{
$this->attributes[$id] = $attributes;

Check failure on line 24 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.3, false)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:24:9: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<non-empty-string, mixed>|mixed>' provided (see https://psalm.dev/198)

Check failure on line 24 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.4, false, --ignore-platform-reqs)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:24:9: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<non-empty-string, mixed>|mixed>' provided (see https://psalm.dev/198)

Check failure on line 24 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.1, false)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:24:9: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<non-empty-string, mixed>|mixed>' provided (see https://psalm.dev/198)

Check failure on line 24 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.2, false)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:24:9: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<non-empty-string, mixed>|mixed>' provided (see https://psalm.dev/198)

return $this;
}

/**
* @param array<non-empty-string, mixed> $attributes
* @return array<non-empty-string, mixed>
*/
public function add(string|int $id, array $attributes): array
{
if (!isset($this->attributes[$id])) {
return $this->attributes[$id] = $attributes;

Check failure on line 36 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.3, false)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:36:20: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<non-empty-string, mixed>|mixed>' provided (see https://psalm.dev/198)

Check failure on line 36 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.4, false, --ignore-platform-reqs)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:36:20: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<non-empty-string, mixed>|mixed>' provided (see https://psalm.dev/198)

Check failure on line 36 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.1, false)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:36:20: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<non-empty-string, mixed>|mixed>' provided (see https://psalm.dev/198)

Check failure on line 36 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.2, false)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:36:20: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<non-empty-string, mixed>|mixed>' provided (see https://psalm.dev/198)
}

return $this->attributes[$id] = [...$this->attributes[$id], ...$attributes];

Check failure on line 39 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.3, false)

InvalidOperand

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:39:45: InvalidOperand: Cannot use spread operator on non-iterable type mixed (see https://psalm.dev/058)

Check failure on line 39 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.3, false)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:39:16: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<array-key, mixed>|mixed>' provided (see https://psalm.dev/198)

Check failure on line 39 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.4, false, --ignore-platform-reqs)

InvalidOperand

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:39:45: InvalidOperand: Cannot use spread operator on non-iterable type mixed (see https://psalm.dev/058)

Check failure on line 39 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.4, false, --ignore-platform-reqs)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:39:16: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<array-key, mixed>|mixed>' provided (see https://psalm.dev/198)

Check failure on line 39 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.1, false)

InvalidOperand

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:39:45: InvalidOperand: Cannot use spread operator on non-iterable type mixed (see https://psalm.dev/058)

Check failure on line 39 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.1, false)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:39:16: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<array-key, mixed>|mixed>' provided (see https://psalm.dev/198)

Check failure on line 39 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.2, false)

InvalidOperand

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:39:45: InvalidOperand: Cannot use spread operator on non-iterable type mixed (see https://psalm.dev/058)

Check failure on line 39 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.2, false)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:39:16: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<array-key, mixed>|mixed>' provided (see https://psalm.dev/198)
}

/**
* @return array<non-empty-string, mixed>
*/
public function get(string|int $id): array
{
if (!isset($this->attributes[$id])) {
return [];
}

return $this->attributes[$id];
}

/**
* @return array<non-empty-string, mixed>
*/
public function append(string|int $id, string|int $key, mixed $value): array
{
$this->attributes[$id] ??= [];

Check failure on line 59 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.3, false)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:59:9: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<never, never>|mixed>' provided (see https://psalm.dev/198)

Check failure on line 59 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.4, false, --ignore-platform-reqs)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:59:9: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<never, never>|mixed>' provided (see https://psalm.dev/198)

Check failure on line 59 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.1, false)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:59:9: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<never, never>|mixed>' provided (see https://psalm.dev/198)

Check failure on line 59 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.2, false)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:59:9: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<never, never>|mixed>' provided (see https://psalm.dev/198)
$this->attributes[$id][$key] = $value;

Check failure on line 60 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.3, false)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:60:9: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<int|string, mixed>|mixed>' provided (see https://psalm.dev/198)

Check failure on line 60 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.4, false, --ignore-platform-reqs)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:60:9: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<int|string, mixed>|mixed>' provided (see https://psalm.dev/198)

Check failure on line 60 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.1, false)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:60:9: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<int|string, mixed>|mixed>' provided (see https://psalm.dev/198)

Check failure on line 60 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.2, false)

PropertyTypeCoercion

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:60:9: PropertyTypeCoercion: $this->attributes expects 'array<non-empty-string, mixed>', parent type 'non-empty-array<int|string, array<int|string, mixed>|mixed>' provided (see https://psalm.dev/198)

return $this->attributes[$id];

Check failure on line 62 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.3, false)

InvalidReturnStatement

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:62:16: InvalidReturnStatement: The inferred type 'array<int|string, mixed>|mixed' does not match the declared return type 'array<non-empty-string, mixed>' for OpenTelemetry\API\Instrumentation\AutoInstrumentation\AttributeTrackerById::append (see https://psalm.dev/128)

Check failure on line 62 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.4, false, --ignore-platform-reqs)

InvalidReturnStatement

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:62:16: InvalidReturnStatement: The inferred type 'array<int|string, mixed>|mixed' does not match the declared return type 'array<non-empty-string, mixed>' for OpenTelemetry\API\Instrumentation\AutoInstrumentation\AttributeTrackerById::append (see https://psalm.dev/128)

Check failure on line 62 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.1, false)

InvalidReturnStatement

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:62:16: InvalidReturnStatement: The inferred type 'array<int|string, mixed>|mixed' does not match the declared return type 'array<non-empty-string, mixed>' for OpenTelemetry\API\Instrumentation\AutoInstrumentation\AttributeTrackerById::append (see https://psalm.dev/128)

Check failure on line 62 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php

View workflow job for this annotation

GitHub Actions / php (8.2, false)

InvalidReturnStatement

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerById.php:62:16: InvalidReturnStatement: The inferred type 'array<int|string, mixed>|mixed' does not match the declared return type 'array<non-empty-string, mixed>' for OpenTelemetry\API\Instrumentation\AutoInstrumentation\AttributeTrackerById::append (see https://psalm.dev/128)
}

public function clear(string|int $id): void
{
unset($this->attributes[$id]);
}

public function reset(): void
{
$this->attributes = [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\API\Instrumentation\AutoInstrumentation;

use WeakMap;

class AttributeTrackerByObject
{
/**
* @var WeakMap<object, array<non-empty-string, mixed>>
*/
protected WeakMap $attributes;

public function __construct()
{
/** @psalm-suppress PropertyTypeCoercion */
$this->attributes = new WeakMap();
}

public function has(object $id): bool
{
return $this->attributes->offsetExists($id);
}

/**
* @param array<non-empty-string, mixed> $attributes
*/
public function set(object $id, array $attributes): self
{
$this->attributes[$id] = $attributes;

return $this;
}

/**
* @param array<non-empty-string, mixed> $attributes
* @return array<non-empty-string, mixed>
*/
public function add(object $id, array $attributes): array
{
if ($this->attributes->offsetExists($id) === false) {
return $this->attributes[$id] = $attributes;
}

return $this->attributes[$id] = [...$this->attributes[$id], ...$attributes];

Check failure on line 47 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php

View workflow job for this annotation

GitHub Actions / php (8.3, false)

InvalidOperand

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php:47:45: InvalidOperand: Cannot use spread operator on non-iterable type array<non-empty-string, mixed>|null (see https://psalm.dev/058)

Check failure on line 47 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php

View workflow job for this annotation

GitHub Actions / php (8.4, false, --ignore-platform-reqs)

InvalidOperand

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php:47:45: InvalidOperand: Cannot use spread operator on non-iterable type array<non-empty-string, mixed>|null (see https://psalm.dev/058)

Check failure on line 47 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php

View workflow job for this annotation

GitHub Actions / php (8.1, false)

InvalidOperand

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php:47:45: InvalidOperand: Cannot use spread operator on non-iterable type array<non-empty-string, mixed>|null (see https://psalm.dev/058)

Check failure on line 47 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php

View workflow job for this annotation

GitHub Actions / php (8.2, false)

InvalidOperand

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php:47:45: InvalidOperand: Cannot use spread operator on non-iterable type array<non-empty-string, mixed>|null (see https://psalm.dev/058)
}

/**
* @return array<non-empty-string, mixed>

Check failure on line 51 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php

View workflow job for this annotation

GitHub Actions / php (8.3, false)

InvalidNullableReturnType

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php:51:16: InvalidNullableReturnType: The declared return type 'array<string, mixed>' for OpenTelemetry\API\Instrumentation\AutoInstrumentation\AttributeTrackerByObject::get is not nullable, but 'array<string, mixed>|null' contains null (see https://psalm.dev/144)

Check failure on line 51 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php

View workflow job for this annotation

GitHub Actions / php (8.4, false, --ignore-platform-reqs)

InvalidNullableReturnType

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php:51:16: InvalidNullableReturnType: The declared return type 'array<string, mixed>' for OpenTelemetry\API\Instrumentation\AutoInstrumentation\AttributeTrackerByObject::get is not nullable, but 'array<string, mixed>|null' contains null (see https://psalm.dev/144)

Check failure on line 51 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php

View workflow job for this annotation

GitHub Actions / php (8.1, false)

InvalidNullableReturnType

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php:51:16: InvalidNullableReturnType: The declared return type 'array<string, mixed>' for OpenTelemetry\API\Instrumentation\AutoInstrumentation\AttributeTrackerByObject::get is not nullable, but 'array<string, mixed>|null' contains null (see https://psalm.dev/144)

Check failure on line 51 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php

View workflow job for this annotation

GitHub Actions / php (8.2, false)

InvalidNullableReturnType

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php:51:16: InvalidNullableReturnType: The declared return type 'array<string, mixed>' for OpenTelemetry\API\Instrumentation\AutoInstrumentation\AttributeTrackerByObject::get is not nullable, but 'array<string, mixed>|null' contains null (see https://psalm.dev/144)
*/
public function get(object $id): array
{
if ($this->attributes->offsetExists($id) === false) {
return [];
}

return $this->attributes[$id];

Check failure on line 59 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php

View workflow job for this annotation

GitHub Actions / php (8.3, false)

NullableReturnStatement

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php:59:16: NullableReturnStatement: The declared return type 'array<non-empty-string, mixed>' for OpenTelemetry\API\Instrumentation\AutoInstrumentation\AttributeTrackerByObject::get is not nullable, but the function returns 'array<non-empty-string, mixed>|null' (see https://psalm.dev/139)

Check failure on line 59 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php

View workflow job for this annotation

GitHub Actions / php (8.4, false, --ignore-platform-reqs)

NullableReturnStatement

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php:59:16: NullableReturnStatement: The declared return type 'array<non-empty-string, mixed>' for OpenTelemetry\API\Instrumentation\AutoInstrumentation\AttributeTrackerByObject::get is not nullable, but the function returns 'array<non-empty-string, mixed>|null' (see https://psalm.dev/139)

Check failure on line 59 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php

View workflow job for this annotation

GitHub Actions / php (8.1, false)

NullableReturnStatement

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php:59:16: NullableReturnStatement: The declared return type 'array<non-empty-string, mixed>' for OpenTelemetry\API\Instrumentation\AutoInstrumentation\AttributeTrackerByObject::get is not nullable, but the function returns 'array<non-empty-string, mixed>|null' (see https://psalm.dev/139)

Check failure on line 59 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php

View workflow job for this annotation

GitHub Actions / php (8.2, false)

NullableReturnStatement

src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php:59:16: NullableReturnStatement: The declared return type 'array<non-empty-string, mixed>' for OpenTelemetry\API\Instrumentation\AutoInstrumentation\AttributeTrackerByObject::get is not nullable, but the function returns 'array<non-empty-string, mixed>|null' (see https://psalm.dev/139)
}

/**
* @return array<non-empty-string, mixed>
*/
public function append(object $id, string|int $key, mixed $value): array
{
/** @var array<non-empty-string, mixed> $attributes */
$attributes = $this->attributes[$id] ?? [];
$attributes[$key] = $value;

return $this->attributes[$id] = $attributes;

Check failure on line 71 in src/API/Instrumentation/AutoInstrumentation/AttributeTrackerByObject.php

View workflow job for this annotation

GitHub Actions / php (8.5, true, --ignore-platform-reqs)

WeakMap<object, array<non-empty-string, mixed>> does not accept array<int|string, mixed>.
}

public function clear(object $id): void
{
unset($this->attributes[$id]);
}

public function reset(): void
{
$this->attributes = new WeakMap();
}
}
46 changes: 46 additions & 0 deletions src/API/Metrics/TimerTrackerById.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\API\Metrics;

use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Common\Time\ClockInterface;

class TimerTrackerById
{
/**
* @var array <string|int, int|float>
*/
protected array $timers = [];
protected ClockInterface $clock;

public function __construct(?ClockInterface $clock = null)
{
$this->clock = $clock ?? Clock::getDefault();
}

public function start(string|int $id): void
{
$this->timers[$id] = $this->clock->now();
}

public function durationNanos(string|int $id): int|float
{
if (!isset($this->timers[$id])) {
return 0;
}

return ($this->clock->now() - $this->timers[$id]);
}

public function durationMs(string|int $id): float
{
return $this->durationNanos($id) / ClockInterface::NANOS_PER_MILLISECOND;
}

public function durationS(string|int $id): float
{
return $this->durationNanos($id) / ClockInterface::NANOS_PER_SECOND;
}
}
48 changes: 48 additions & 0 deletions src/API/Metrics/TimerTrackerByObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\API\Metrics;

use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Common\Time\ClockInterface;
use WeakMap;

class TimerTrackerByObject
{
/**
* @var WeakMap <object, int|float>
*/
protected WeakMap $timers;
protected ClockInterface $clock;

public function __construct(?ClockInterface $clock = null)
{
$this->timers = new WeakMap();
$this->clock = $clock ?? Clock::getDefault();
}

public function start(object $id): void
{
$this->timers[$id] = $this->clock->now();
}

public function durationNanos(object $id): int|float
{
if ($this->timers->offsetExists($id) === false) {
return 0;
}

return $this->clock->now() - $this->timers[$id];
}

public function durationMs(object $id): float
{
return $this->durationNanos($id) / ClockInterface::NANOS_PER_MILLISECOND;
}

public function durationS(object $id): float
{
return $this->durationNanos($id) / ClockInterface::NANOS_PER_SECOND;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

declare(strict_types=1);

namespace Unit\API\Instrumentation\AutoInstrumentation;

use OpenTelemetry\API\Instrumentation\AutoInstrumentation\AttributeTrackerById;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(AttributeTrackerById::class)]
class AttributeTrackerByIdTest extends TestCase
{
private AttributeTrackerById $tracker;

protected function setUp(): void
{
parent::setUp();
$this->tracker = new AttributeTrackerById();
}

public function test_set(): void
{
$id = 'test_id_1';
$attributes = ['key1' => 'value1', 'key2' => 123];
$this->tracker->set($id, $attributes);
$this->assertTrue($this->tracker->has($id));
$this->assertEquals($attributes, $this->tracker->get($id));

$newAttributes = ['key3' => true];
$this->tracker->set($id, $newAttributes); // Overwrite
$this->assertEquals($newAttributes, $this->tracker->get($id));
}

public function test_get(): void
{
$id = 'test_id_2';
$this->assertEmpty($this->tracker->get($id)); // Should return empty array if not set

$attributes = ['name' => 'John', 'age' => 30];
$this->tracker->set($id, $attributes);
$this->assertEquals($attributes, $this->tracker->get($id));
}

public function test_add(): void
{
$id = 'test_id_3';
$initialAttributes = ['color' => 'red'];
$this->tracker->add($id, $initialAttributes);
$this->assertEquals($initialAttributes, $this->tracker->get($id));

$additionalAttributes = ['size' => 'M', 'color' => 'blue']; // 'color' should be overwritten
$result = $this->tracker->add($id, $additionalAttributes);
$expected = ['color' => 'blue', 'size' => 'M']; // Note: order might vary but keys/values should match
$this->assertEquals($expected, $this->tracker->get($id));
$this->assertEquals($expected, $result); // 'add' method returns the merged array

$id2 = 'test_id_4';
$result2 = $this->tracker->add($id2, ['item' => 'book']);
$this->assertEquals(['item' => 'book'], $this->tracker->get($id2));
$this->assertEquals(['item' => 'book'], $result2);
}

public function test_reset(): void
{
$this->tracker->set('id1', ['a' => 1]);
$this->tracker->set('id2', ['b' => 2]);
$this->assertTrue($this->tracker->has('id1'));
$this->assertTrue($this->tracker->has('id2'));

$this->tracker->reset();
$this->assertFalse($this->tracker->has('id1'));
$this->assertFalse($this->tracker->has('id2'));
$this->assertEmpty($this->tracker->get('id1'));
}

public function test_has(): void
{
$id = 'test_id_5';
$this->assertFalse($this->tracker->has($id));

$this->tracker->set($id, ['data' => 'some_data']);
$this->assertTrue($this->tracker->has($id));

$this->tracker->clear($id);
$this->assertFalse($this->tracker->has($id));
}

public function test_append(): void
{
$id = 'test_id_6';
$this->tracker->append($id, 'first_key', 'first_value');
$this->assertEquals(['first_key' => 'first_value'], $this->tracker->get($id));

$this->tracker->append($id, 'second_key', 456);
$this->assertEquals(['first_key' => 'first_value', 'second_key' => 456], $this->tracker->get($id));

$this->tracker->append($id, 'first_key', 'new_value'); // Overwrite
$this->assertEquals(['first_key' => 'new_value', 'second_key' => 456], $this->tracker->get($id));

// Test with a numeric key
$this->tracker->append($id, 0, 'numeric_value');
$this->assertEquals(['first_key' => 'new_value', 'second_key' => 456, 0 => 'numeric_value'], $this->tracker->get($id));
}

public function test_clear(): void
{
$id = 'test_id_7';
$this->tracker->set($id, ['foo' => 'bar']);
$this->assertTrue($this->tracker->has($id));
$this->assertEquals(['foo' => 'bar'], $this->tracker->get($id));

$this->tracker->clear($id);
$this->assertFalse($this->tracker->has($id));
$this->assertEmpty($this->tracker->get($id));

// Clearing a non-existent ID should not cause an error
$this->tracker->clear('non_existent_id');
$this->assertFalse($this->tracker->has('non_existent_id'));
}
}
Loading
Loading