-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from worldia/feat/opentelemetry-meter-interface
feat: implement MeterInterface from OpenTelemetry
- Loading branch information
Showing
4 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the worldia/instrumentation-bundle package. | ||
* (c) Worldia <developers@worldia.com> | ||
*/ | ||
|
||
namespace Instrumentation\Metrics; | ||
|
||
use OpenTelemetry\API\Metrics\CounterInterface; | ||
use Prometheus\CollectorRegistry; | ||
|
||
class CounterAdapter implements CounterInterface | ||
{ | ||
public function __construct( | ||
private string $name, | ||
private string $description, | ||
private CollectorRegistry $collectorRegistry, | ||
) { | ||
} | ||
|
||
/** | ||
* @param int $amount | ||
* @param array{labels: array<string,mixed>} $attributes | ||
*/ | ||
public function add($amount, iterable $attributes = [], $context = null): void | ||
{ | ||
/** @var array<string> $labelNames */ | ||
$labelNames = array_keys($attributes['labels'] ?? []); | ||
$labelValues = array_values($attributes['labels'] ?? []); | ||
|
||
$counter = $this->collectorRegistry->getOrRegisterCounter( | ||
'', | ||
$this->name, | ||
$this->description, | ||
$labelNames, | ||
); | ||
|
||
$counter->incBy($amount, $labelValues); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the worldia/instrumentation-bundle package. | ||
* (c) Worldia <developers@worldia.com> | ||
*/ | ||
|
||
namespace Instrumentation\Metrics; | ||
|
||
use OpenTelemetry\API\Metrics\HistogramInterface; | ||
use Prometheus\CollectorRegistry; | ||
use Prometheus\Histogram; | ||
|
||
class HistogramAdapter implements HistogramInterface | ||
{ | ||
public function __construct( | ||
private string $name, | ||
private string $description, | ||
private CollectorRegistry $collectorRegistry, | ||
) { | ||
} | ||
|
||
/** | ||
* @param array{labels: array<string,mixed>, buckets?: array<int>} $attributes | ||
*/ | ||
public function record($amount, iterable $attributes = [], $context = null): void | ||
{ | ||
/** @var array<string> $labelNames */ | ||
$labelNames = array_keys($attributes['labels'] ?? []); | ||
$labelValues = array_values($attributes['labels'] ?? []); | ||
|
||
$histogram = $this->collectorRegistry->getOrRegisterHistogram( | ||
'', | ||
$this->name, | ||
$this->description, | ||
$labelNames, | ||
$attributes['buckets'] ?? Histogram::getDefaultBuckets(), | ||
); | ||
$histogram->observe($amount, $labelValues); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the worldia/instrumentation-bundle package. | ||
* (c) Worldia <developers@worldia.com> | ||
*/ | ||
|
||
namespace Instrumentation\Metrics; | ||
|
||
use OpenTelemetry\API\Metrics\CounterInterface; | ||
use OpenTelemetry\API\Metrics\HistogramInterface; | ||
use OpenTelemetry\API\Metrics\MeterInterface; | ||
use OpenTelemetry\API\Metrics\ObservableCounterInterface; | ||
use OpenTelemetry\API\Metrics\ObservableGaugeInterface; | ||
use OpenTelemetry\API\Metrics\ObservableUpDownCounterInterface; | ||
use OpenTelemetry\API\Metrics\UpDownCounterInterface; | ||
use Prometheus\CollectorRegistry; | ||
|
||
class Meter implements MeterInterface | ||
{ | ||
public function __construct( | ||
private CollectorRegistry $collectorRegistry, | ||
) { | ||
} | ||
|
||
public function createCounter(string $name, string $unit = null, string $description = null): CounterInterface | ||
{ | ||
return new CounterAdapter($name, $description ?: '', $this->collectorRegistry); | ||
} | ||
|
||
public function createObservableCounter(string $name, string $unit = null, string $description = null, callable ...$callbacks): ObservableCounterInterface | ||
{ | ||
throw new \LogicException(sprintf('Method %s is not implemented', __METHOD__)); | ||
} | ||
|
||
public function createHistogram(string $name, string $unit = null, string $description = null): HistogramInterface | ||
{ | ||
return new HistogramAdapter($name, $description ?: '', $this->collectorRegistry); | ||
} | ||
|
||
public function createObservableGauge(string $name, string $unit = null, string $description = null, callable ...$callbacks): ObservableGaugeInterface | ||
{ | ||
throw new \LogicException(sprintf('Method %s is not implemented', __METHOD__)); | ||
} | ||
|
||
public function createUpDownCounter(string $name, string $unit = null, string $description = null): UpDownCounterInterface | ||
{ | ||
return new UpDownCounterAdapter($name, $description ?: '', $this->collectorRegistry); | ||
} | ||
|
||
public function createObservableUpDownCounter(string $name, string $unit = null, string $description = null, callable ...$callbacks): ObservableUpDownCounterInterface | ||
{ | ||
throw new \LogicException(sprintf('Method %s is not implemented', __METHOD__)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the worldia/instrumentation-bundle package. | ||
* (c) Worldia <developers@worldia.com> | ||
*/ | ||
|
||
namespace Instrumentation\Metrics; | ||
|
||
use OpenTelemetry\API\Metrics\UpDownCounterInterface; | ||
use Prometheus\CollectorRegistry; | ||
|
||
class UpDownCounterAdapter implements UpDownCounterInterface | ||
{ | ||
public function __construct( | ||
private string $name, | ||
private string $description, | ||
private CollectorRegistry $collectorRegistry, | ||
) { | ||
} | ||
|
||
/** | ||
* @param int $amount | ||
* @param array{labels: array<string,mixed>} $attributes | ||
*/ | ||
public function add($amount, iterable $attributes = [], $context = null): void | ||
{ | ||
/** @var array<string> $labelNames */ | ||
$labelNames = array_keys($attributes['labels'] ?? []); | ||
$labelValues = array_values($attributes['labels'] ?? []); | ||
|
||
$gauge = $this->collectorRegistry->getOrRegisterGauge( | ||
'', | ||
$this->name, | ||
$this->description, | ||
$labelNames, | ||
); | ||
|
||
if ($amount > 0) { | ||
$gauge->incBy($amount, $labelValues); | ||
} else { | ||
$gauge->decBy(abs($amount), $labelValues); | ||
} | ||
} | ||
} |