Skip to content

Commit

Permalink
Merge pull request #42 from worldia/feat/opentelemetry-meter-interface
Browse files Browse the repository at this point in the history
feat: implement MeterInterface from OpenTelemetry
  • Loading branch information
cyve authored Jun 7, 2023
2 parents 47d829f + 7b8ee8b commit f71d21a
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Metrics/CounterAdapter.php
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);
}
}
41 changes: 41 additions & 0 deletions src/Metrics/HistogramAdapter.php
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);
}
}
55 changes: 55 additions & 0 deletions src/Metrics/Meter.php
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__));
}
}
45 changes: 45 additions & 0 deletions src/Metrics/UpDownCounterAdapter.php
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);
}
}
}

0 comments on commit f71d21a

Please sign in to comment.