Skip to content

Commit 9e60a40

Browse files
committed
Add lazy factories for remaining instruments
1 parent 48e97de commit 9e60a40

3 files changed

Lines changed: 124 additions & 0 deletions

File tree

src/Telemetry/Histogram.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,48 @@
44

55
abstract class Histogram
66
{
7+
/**
8+
* @param array<string, mixed> $advisory
9+
*/
10+
public static function lazy(
11+
Adapter $telemetry,
12+
string $name,
13+
?string $unit = null,
14+
?string $description = null,
15+
array $advisory = [],
16+
): self {
17+
return new class ($telemetry, $name, $unit, $description, $advisory) extends Histogram {
18+
private ?Histogram $inner = null;
19+
20+
/**
21+
* @param array<string, mixed> $advisory
22+
*/
23+
public function __construct(
24+
private Adapter $telemetry,
25+
private string $name,
26+
private ?string $unit,
27+
private ?string $description,
28+
private array $advisory,
29+
) {
30+
}
31+
32+
/**
33+
* @param iterable<non-empty-string, array<mixed>|bool|float|int|string|null> $attributes
34+
*/
35+
public function record(float|int $amount, iterable $attributes = []): void
36+
{
37+
$this->inner ??= $this->telemetry->createHistogram(
38+
$this->name,
39+
$this->unit,
40+
$this->description,
41+
$this->advisory,
42+
);
43+
44+
$this->inner->record($amount, $attributes);
45+
}
46+
};
47+
}
48+
749
/**
850
* @param iterable<non-empty-string, array<mixed>|bool|float|int|string|null> $attributes
951
*/

src/Telemetry/UpDownCounter.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,48 @@
44

55
abstract class UpDownCounter
66
{
7+
/**
8+
* @param array<string, mixed> $advisory
9+
*/
10+
public static function lazy(
11+
Adapter $telemetry,
12+
string $name,
13+
?string $unit = null,
14+
?string $description = null,
15+
array $advisory = [],
16+
): self {
17+
return new class ($telemetry, $name, $unit, $description, $advisory) extends UpDownCounter {
18+
private ?UpDownCounter $inner = null;
19+
20+
/**
21+
* @param array<string, mixed> $advisory
22+
*/
23+
public function __construct(
24+
private Adapter $telemetry,
25+
private string $name,
26+
private ?string $unit,
27+
private ?string $description,
28+
private array $advisory,
29+
) {
30+
}
31+
32+
/**
33+
* @param iterable<non-empty-string, array<mixed>|bool|float|int|string|null> $attributes
34+
*/
35+
public function add(float|int $amount, iterable $attributes = []): void
36+
{
37+
$this->inner ??= $this->telemetry->createUpDownCounter(
38+
$this->name,
39+
$this->unit,
40+
$this->description,
41+
$this->advisory,
42+
);
43+
44+
$this->inner->add($amount, $attributes);
45+
}
46+
};
47+
}
48+
749
/**
850
* @param iterable<non-empty-string, array<mixed>|bool|float|int|string|null> $attributes
951
*/

tests/Telemetry/LazyInstrumentTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Utopia\Telemetry\Adapter\Test;
77
use Utopia\Telemetry\Counter;
88
use Utopia\Telemetry\Gauge;
9+
use Utopia\Telemetry\Histogram;
10+
use Utopia\Telemetry\UpDownCounter;
911

1012
class LazyInstrumentTest extends TestCase
1113
{
@@ -46,4 +48,42 @@ public function testLazyGaugeCreatesInnerGaugeOnFirstRecord(): void
4648
$this->assertSame($inner, $telemetry->gauges['event.timestamp']);
4749
$this->assertSame([123.45, 456.78], $telemetry->gauges['event.timestamp']->values);
4850
}
51+
52+
public function testLazyHistogramCreatesInnerHistogramOnFirstRecord(): void
53+
{
54+
$telemetry = new Test();
55+
$histogram = Histogram::lazy($telemetry, 'request.duration', 'ms', 'Request duration');
56+
57+
$this->assertSame([], $telemetry->histograms);
58+
59+
$histogram->record(12.3, ['route' => '/v1/health']);
60+
61+
$this->assertArrayHasKey('request.duration', $telemetry->histograms);
62+
$this->assertSame([12.3], $telemetry->histograms['request.duration']->values);
63+
64+
$inner = $telemetry->histograms['request.duration'];
65+
$histogram->record(45.6);
66+
67+
$this->assertSame($inner, $telemetry->histograms['request.duration']);
68+
$this->assertSame([12.3, 45.6], $telemetry->histograms['request.duration']->values);
69+
}
70+
71+
public function testLazyUpDownCounterCreatesInnerCounterOnFirstAdd(): void
72+
{
73+
$telemetry = new Test();
74+
$counter = UpDownCounter::lazy($telemetry, 'active.requests', '{request}', 'Active requests');
75+
76+
$this->assertSame([], $telemetry->upDownCounters);
77+
78+
$counter->add(1, ['route' => '/v1/health']);
79+
80+
$this->assertArrayHasKey('active.requests', $telemetry->upDownCounters);
81+
$this->assertSame([1], $telemetry->upDownCounters['active.requests']->values);
82+
83+
$inner = $telemetry->upDownCounters['active.requests'];
84+
$counter->add(-1);
85+
86+
$this->assertSame($inner, $telemetry->upDownCounters['active.requests']);
87+
$this->assertSame([1, -1], $telemetry->upDownCounters['active.requests']->values);
88+
}
4989
}

0 commit comments

Comments
 (0)