Skip to content

Commit 590ddcc

Browse files
committed
chore: review feedback
1 parent 654896e commit 590ddcc

File tree

8 files changed

+102
-31
lines changed

8 files changed

+102
-31
lines changed

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Utopia Telemetry
2+
3+
![Total Downloads](https://img.shields.io/packagist/dt/utopia-php/telemetry.svg)
4+
[![Discord](https://img.shields.io/discord/564160730845151244?label=discord)](https://appwrite.io/discord)
5+
6+
Utopia Telemetry is a powerful Telemtry library. This library is aiming to be as simple and easy to learn and use. This library is maintained by the [Appwrite team](https://appwrite.io).
7+
8+
Although this library is part of the [Utopia System](https://github.com/utopia-php) project it is dependency free and can be used as standalone with any other PHP project or framework.
9+
10+
## Getting Started
11+
12+
Install using composer:
13+
14+
```bash
15+
composer require utopia-php/telemetry
16+
```
17+
18+
Init in your application:
19+
20+
```php
21+
<?php
22+
23+
require_once __DIR__ . '/vendor/autoload.php';
24+
25+
// Create a Telemetry instance using OpenTelemetry adapter.
26+
use Utopia\Telemetry\Adapter\OpenTelemetry;
27+
28+
$telemetry = new OpenTelemetry('http://localhost:4138', 'namespace', 'app', 'unique-instance-name');
29+
$counter = $telemetry->createUpDownCounter('http.server.active_requests', '{request}');
30+
31+
$counter->add(1);
32+
$counter->add(2);
33+
34+
// Periodically collect metrics and send them to the configured OpenTelemetry endpoint.
35+
$telemetry->collect();
36+
37+
// Example using Swoole
38+
\Swoole\Timer::tick(60_000, fn () => $telemetry->collect());
39+
```
40+
41+
## System Requirements
42+
43+
Utopia Framework requires PHP 8.0 or later. We recommend using the latest PHP version whenever possible.
44+
45+
## Copyright and license
46+
47+
The MIT License (MIT) [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php)

phpstan.neon

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
parameters:
22
level: 5
33
paths:
4-
- src
5-
- tests
4+
- src
Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,42 @@
88
use Utopia\Telemetry\Histogram;
99
use Utopia\Telemetry\UpDownCounter;
1010

11-
class Noop implements Adapter
11+
class None implements Adapter
1212
{
1313
public function createCounter(string $name, ?string $unit = null, ?string $description = null, array $advisory = []): Counter
1414
{
15-
return new Counter();
15+
return new class () extends Counter {
16+
public function add(float|int $amount, iterable $attributes = []): void
17+
{
18+
}
19+
};
1620
}
1721

1822
public function createHistogram(string $name, ?string $unit = null, ?string $description = null, array $advisory = []): Histogram
1923
{
20-
return new Histogram();
24+
return new class () extends Histogram {
25+
public function record(float|int $amount, iterable $attributes = []): void
26+
{
27+
}
28+
};
2129
}
2230

2331
public function createGauge(string $name, ?string $unit = null, ?string $description = null, array $advisory = []): Gauge
2432
{
25-
return new Gauge();
33+
return new class () extends Gauge {
34+
public function record(float|int $amount, iterable $attributes = []): void
35+
{
36+
}
37+
};
2638
}
2739

2840
public function createUpDownCounter(string $name, ?string $unit = null, ?string $description = null, array $advisory = []): UpDownCounter
2941
{
30-
return new UpDownCounter();
42+
return new class () extends UpDownCounter {
43+
public function add(float|int $amount, iterable $attributes = []): void
44+
{
45+
}
46+
};
3147
}
3248

3349
public function collect(): bool

src/Telemetry/Adapter/OpenTelemetry.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use OpenTelemetry\Contrib\Otlp\MetricExporter;
1212
use OpenTelemetry\Contrib\Otlp\OtlpHttpTransportFactory;
1313
use OpenTelemetry\SDK\Common\Attribute\Attributes;
14+
use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;
15+
use OpenTelemetry\SDK\Metrics\Data\Temporality;
1416
use OpenTelemetry\SDK\Metrics\MeterProvider;
1517
use OpenTelemetry\SDK\Metrics\MetricExporterInterface;
1618
use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader;
@@ -28,6 +30,12 @@ class OpenTelemetry implements Adapter
2830
{
2931
private MetricReaderInterface $reader;
3032
private MeterInterface $meter;
33+
private array $meterStorage = [
34+
Counter::class => [],
35+
UpDownCounter::class => [],
36+
Histogram::class => [],
37+
Gauge::class => [],
38+
];
3139

3240
public function __construct(string $endpoint, string $serviceNamespace, string $serviceName, string $serviceInstanceId)
3341
{
@@ -40,7 +48,7 @@ public function __construct(string $endpoint, string $serviceNamespace, string $
4048
$this->meter = $this->initMeter($exporter, $attributes);
4149
}
4250

43-
protected function initMeter(MetricExporterInterface $exporter, Attributes $attributes): MeterInterface
51+
protected function initMeter(MetricExporterInterface $exporter, AttributesInterface $attributes): MeterInterface
4452
{
4553
$this->reader = new ExportingReader($exporter);
4654
$meterProvider = MeterProvider::builder()
@@ -56,12 +64,21 @@ protected function initMeter(MetricExporterInterface $exporter, Attributes $attr
5664
protected function createExporter(string $endpoint): MetricExporterInterface
5765
{
5866
$transport = (new OtlpHttpTransportFactory())->create($endpoint, ContentTypes::PROTOBUF);
59-
return new MetricExporter($transport);
67+
return new MetricExporter($transport, Temporality::CUMULATIVE);
68+
}
69+
70+
private function createMeter(string $type, string $name, callable $creator): mixed
71+
{
72+
if (!isset($this->meterStorage[$type][$name])) {
73+
$this->meterStorage[$type][$name] = $creator();
74+
}
75+
76+
return $this->meterStorage[$type][$name];
6077
}
6178

6279
public function createCounter(string $name, ?string $unit = null, ?string $description = null, array $advisory = []): Counter
6380
{
64-
$counter = $this->meter->createCounter($name, $unit, $description, $advisory);
81+
$counter = $this->createMeter(Counter::class, $name, fn () => $this->meter->createCounter($name, $unit, $description, $advisory));
6582
return new class ($counter) extends Counter {
6683
public function __construct(private CounterInterface $counter)
6784
{
@@ -75,12 +92,12 @@ public function add(float|int $amount, iterable $attributes = []): void
7592

7693
public function createHistogram(string $name, ?string $unit = null, ?string $description = null, array $advisory = []): Histogram
7794
{
78-
$histogram = $this->meter->createHistogram($name, $unit, $description, $advisory);
95+
$histogram = $this->createMeter(Histogram::class, $name, fn () => $this->meter->createHistogram($name, $unit, $description, $advisory));
7996
return new class ($histogram) extends Histogram {
8097
public function __construct(private HistogramInterface $histogram)
8198
{
8299
}
83-
public function add(float|int $amount, iterable $attributes = []): void
100+
public function record(float|int $amount, iterable $attributes = []): void
84101
{
85102
$this->histogram->record($amount, $attributes);
86103
}
@@ -89,12 +106,12 @@ public function add(float|int $amount, iterable $attributes = []): void
89106

90107
public function createGauge(string $name, ?string $unit = null, ?string $description = null, array $advisory = []): Gauge
91108
{
92-
$gauge = $this->meter->createGauge($name, $unit, $description, $advisory);
109+
$gauge = $this->createMeter(Gauge::class, $name, fn () => $this->meter->createGauge($name, $unit, $description, $advisory));
93110
return new class ($gauge) extends Gauge {
94111
public function __construct(private GaugeInterface $gauge)
95112
{
96113
}
97-
public function add(float|int $amount, iterable $attributes = []): void
114+
public function record(float|int $amount, iterable $attributes = []): void
98115
{
99116
$this->gauge->record($amount, $attributes);
100117
}
@@ -103,7 +120,7 @@ public function add(float|int $amount, iterable $attributes = []): void
103120

104121
public function createUpDownCounter(string $name, ?string $unit = null, ?string $description = null, array $advisory = []): UpDownCounter
105122
{
106-
$upDownCounter = $this->meter->createUpDownCounter($name, $unit, $description, $advisory);
123+
$upDownCounter = $this->createMeter(UpDownCounter::class, $name, fn () => $this->meter->createUpDownCounter($name, $unit, $description, $advisory));
107124
return new class ($upDownCounter) extends UpDownCounter {
108125
public function __construct(private UpDownCounterInterface $upDownCounter)
109126
{

src/Telemetry/Counter.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Utopia\Telemetry;
44

5-
class Counter
5+
abstract class Counter
66
{
7-
public function add(float|int $amount, iterable $attributes = []): void
8-
{
9-
}
7+
abstract public function add(float|int $amount, iterable $attributes = []): void;
108
}

src/Telemetry/Gauge.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Utopia\Telemetry;
44

5-
class Gauge
5+
abstract class Gauge
66
{
7-
public function record(float|int $amount, iterable $attributes = []): void
8-
{
9-
}
7+
abstract public function record(float|int $amount, iterable $attributes = []): void;
108
}

src/Telemetry/Histogram.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Utopia\Telemetry;
44

5-
class Histogram
5+
abstract class Histogram
66
{
7-
public function record(float|int $amount, iterable $attributes = []): void
8-
{
9-
}
7+
abstract public function record(float|int $amount, iterable $attributes = []): void;
108
}

src/Telemetry/UpDownCounter.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Utopia\Telemetry;
44

5-
class UpDownCounter
5+
abstract class UpDownCounter
66
{
7-
public function add(float|int $amount, iterable $attributes = []): void
8-
{
9-
}
7+
abstract public function add(float|int $amount, iterable $attributes = []): void;
108
}

0 commit comments

Comments
 (0)