diff --git a/src/Facades/Metric.php b/src/Facades/Metric.php new file mode 100644 index 0000000..feae3b6 --- /dev/null +++ b/src/Facades/Metric.php @@ -0,0 +1,26 @@ +app->singleton(Support\Measure::class, function ($app) { return new Support\Measure($app); }); @@ -54,6 +56,19 @@ public function register(): void $this->app->alias(TracerInterface::class, 'opentelemetry.tracer'); + // Register metric + $this->app->singleton(Support\Metric::class, function ($app) { + return new Support\Metric($app); + }); + $this->app->alias(Support\Metric::class, 'opentelemetry.metric'); + + $this->app->singleton(MeterInterface::class, function () { + return Globals::meterProvider() + ->getMeter(config('otel.meter_name', 'overtrue.laravel-open-telemetry')); + }); + + $this->app->alias(MeterInterface::class, 'opentelemetry.meter'); + Log::debug('[laravel-open-telemetry] Service provider registered successfully'); } diff --git a/src/Support/Metric.php b/src/Support/Metric.php new file mode 100644 index 0000000..14e1ffe --- /dev/null +++ b/src/Support/Metric.php @@ -0,0 +1,89 @@ +isEnabled()) { + return new NoopMeter; + } + + try { + return $this->app->get(MeterInterface::class); + } catch (Throwable $e) { + Log::error('[laravel-open-telemetry] Meter not found', [ + 'error' => $e->getMessage(), + 'line' => $e->getLine(), + 'file' => $e->getFile(), + ]); + + return new NoopMeter; + } + } + + public function counter(string $name, ?string $unit = null, + ?string $description = null, array $advisories = []): CounterInterface + { + return $this->meter()->createCounter($name, $unit, $description, $advisories); + } + + public function histogram(string $name, ?string $unit = null, + ?string $description = null, array $advisories = []): HistogramInterface + { + return $this->meter()->createHistogram($name, $unit, $description, $advisories); + } + + public function gauge(string $name, ?string $unit = null, + ?string $description = null, array $advisories = []): GaugeInterface + { + return $this->meter()->createGauge($name, $unit, $description, $advisories); + } + + public function observableGauge(string $name, ?string $unit = null, + ?string $description = null, array|callable $advisories = [], callable ...$callbacks): ObservableGaugeInterface + { + return $this->meter()->createObservableGauge($name, $unit, $description, $advisories, ...$callbacks); + } +}