|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Overtrue\LaravelOpenTelemetry\Support; |
| 4 | + |
| 5 | +use Illuminate\Contracts\Foundation\Application; |
| 6 | +use Illuminate\Support\Facades\Log; |
| 7 | +use OpenTelemetry\API\Metrics\CounterInterface; |
| 8 | +use OpenTelemetry\API\Metrics\GaugeInterface; |
| 9 | +use OpenTelemetry\API\Metrics\HistogramInterface; |
| 10 | +use OpenTelemetry\API\Metrics\MeterInterface; |
| 11 | +use OpenTelemetry\API\Metrics\Noop\NoopMeter; |
| 12 | +use OpenTelemetry\API\Metrics\ObservableGaugeInterface; |
| 13 | +use Throwable; |
| 14 | + |
| 15 | +class Metric |
| 16 | +{ |
| 17 | + private static ?bool $enabled = null; |
| 18 | + |
| 19 | + public function __construct(protected Application $app) {} |
| 20 | + |
| 21 | + // ======================= Enable/Disable Management ======================= |
| 22 | + |
| 23 | + public function enable(): void |
| 24 | + { |
| 25 | + self::$enabled = true; |
| 26 | + } |
| 27 | + |
| 28 | + public function disable(): void |
| 29 | + { |
| 30 | + self::$enabled = false; |
| 31 | + } |
| 32 | + |
| 33 | + public function isEnabled(): bool |
| 34 | + { |
| 35 | + if (self::$enabled === null) { |
| 36 | + return config('otel.enabled', true); |
| 37 | + } |
| 38 | + |
| 39 | + return self::$enabled; |
| 40 | + } |
| 41 | + |
| 42 | + // ======================= Core OpenTelemetry API ======================= |
| 43 | + |
| 44 | + /** |
| 45 | + * Get the meter instance |
| 46 | + */ |
| 47 | + public function meter(): MeterInterface |
| 48 | + { |
| 49 | + if (! $this->isEnabled()) { |
| 50 | + return new NoopMeter; |
| 51 | + } |
| 52 | + |
| 53 | + try { |
| 54 | + return $this->app->get(MeterInterface::class); |
| 55 | + } catch (Throwable $e) { |
| 56 | + Log::error('[laravel-open-telemetry] Meter not found', [ |
| 57 | + 'error' => $e->getMessage(), |
| 58 | + 'line' => $e->getLine(), |
| 59 | + 'file' => $e->getFile(), |
| 60 | + ]); |
| 61 | + |
| 62 | + return new NoopMeter; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public function counter(string $name, ?string $unit = null, |
| 67 | + ?string $description = null, array $advisories = []): CounterInterface |
| 68 | + { |
| 69 | + return $this->meter()->createCounter($name, $unit, $description, $advisories); |
| 70 | + } |
| 71 | + |
| 72 | + public function histogram(string $name, ?string $unit = null, |
| 73 | + ?string $description = null, array $advisories = []): HistogramInterface |
| 74 | + { |
| 75 | + return $this->meter()->createHistogram($name, $unit, $description, $advisories); |
| 76 | + } |
| 77 | + |
| 78 | + public function gauge(string $name, ?string $unit = null, |
| 79 | + ?string $description = null, array $advisories = []): GaugeInterface |
| 80 | + { |
| 81 | + return $this->meter()->createGauge($name, $unit, $description, $advisories); |
| 82 | + } |
| 83 | + |
| 84 | + public function observableGauge(string $name, ?string $unit = null, |
| 85 | + ?string $description = null, array|callable $advisories = [], callable ...$callbacks): ObservableGaugeInterface |
| 86 | + { |
| 87 | + return $this->meter()->createObservableGauge($name, $unit, $description, $advisories, ...$callbacks); |
| 88 | + } |
| 89 | +} |
0 commit comments