From 16392ffd07587af06c857983f77c63536d6b956b Mon Sep 17 00:00:00 2001 From: billpyang Date: Fri, 5 Sep 2025 15:57:39 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=E6=96=B0=E5=A2=9E=E6=8C=87=E6=A0=87?= =?UTF-8?q?=E4=B8=8A=E6=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Facades/Metric.php | 30 +++++++++ src/OpenTelemetryServiceProvider.php | 16 +++++ src/Support/Metric.php | 95 ++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 src/Facades/Metric.php create mode 100644 src/Support/Metric.php diff --git a/src/Facades/Metric.php b/src/Facades/Metric.php new file mode 100644 index 0000000..8b1fb69 --- /dev/null +++ b/src/Facades/Metric.php @@ -0,0 +1,30 @@ +app->singleton(Support\Measure::class, function ($app) { return new Support\Measure($app); }); @@ -54,6 +57,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..78c0fc6 --- /dev/null +++ b/src/Support/Metric.php @@ -0,0 +1,95 @@ +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 createCounter(string $name, ?string $unit = null, + ?string $description = null, array $advisory = []): CounterInterface + { + return $this->meter()->createCounter($name, $unit, $description, $advisory); + } + + public function createHistogram(string $name, ?string $unit = null, + ?string $description = null, array $advisory = []): HistogramInterface + { + return $this->meter()->createHistogram($name, $unit, $description, $advisory); + } + + public function createGauge(string $name, ?string $unit = null, + ?string $description = null, array $advisory = []): GaugeInterface + { + return $this->meter()->createGauge($name, $unit, $description, $advisory); + } + + public function createObservableGauge(string $name, ?string $unit = null, + ?string $description = null, array|callable $advisory = [], callable ...$callbacks): ObservableGaugeInterface + { + return $this->meter()->createObservableGauge($name, $unit, $description, $advisory, $callbacks); + } + + +} \ No newline at end of file From 3e852def72269bd5e9e2a16e4c373c7f2caac530 Mon Sep 17 00:00:00 2001 From: billpyang Date: Mon, 8 Sep 2025 10:29:08 +0800 Subject: [PATCH 2/3] fix style --- src/Facades/Metric.php | 6 +----- src/Support/Metric.php | 30 ++++++++++++------------------ 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/Facades/Metric.php b/src/Facades/Metric.php index 8b1fb69..dcef943 100644 --- a/src/Facades/Metric.php +++ b/src/Facades/Metric.php @@ -3,10 +3,6 @@ namespace Overtrue\LaravelOpenTelemetry\Facades; use Illuminate\Support\Facades\Facade; -use OpenTelemetry\API\Metrics\CounterInterface; -use OpenTelemetry\API\Metrics\GaugeInterface; -use OpenTelemetry\API\Metrics\HistogramInterface; -use OpenTelemetry\API\Metrics\ObservableGaugeInterface; /** * @method static void enable() @@ -27,4 +23,4 @@ protected static function getFacadeAccessor(): string { return \Overtrue\LaravelOpenTelemetry\Support\Metric::class; } -} \ No newline at end of file +} diff --git a/src/Support/Metric.php b/src/Support/Metric.php index 78c0fc6..031f103 100644 --- a/src/Support/Metric.php +++ b/src/Support/Metric.php @@ -4,11 +4,11 @@ use Illuminate\Contracts\Foundation\Application; use Illuminate\Support\Facades\Log; +use OpenTelemetry\API\Metrics\CounterInterface; use OpenTelemetry\API\Metrics\GaugeInterface; use OpenTelemetry\API\Metrics\HistogramInterface; use OpenTelemetry\API\Metrics\MeterInterface; use OpenTelemetry\API\Metrics\Noop\NoopMeter; -use OpenTelemetry\API\Metrics\CounterInterface; use OpenTelemetry\API\Metrics\ObservableGaugeInterface; use Throwable; @@ -16,10 +16,7 @@ class Metric { private static ?bool $enabled = null; - public function __construct(protected Application $app) - { - } - + public function __construct(protected Application $app) {} // ======================= Enable/Disable Management ======================= @@ -42,7 +39,6 @@ public function isEnabled(): bool return self::$enabled; } - // ======================= Core OpenTelemetry API ======================= /** @@ -50,7 +46,7 @@ public function isEnabled(): bool */ public function meter(): MeterInterface { - if (!$this->isEnabled()) { + if (! $this->isEnabled()) { return new NoopMeter; } @@ -67,29 +63,27 @@ public function meter(): MeterInterface } } - public function createCounter(string $name, ?string $unit = null, - ?string $description = null, array $advisory = []): CounterInterface + public function createCounter(string $name, ?string $unit = null, + ?string $description = null, array $advisory = []): CounterInterface { return $this->meter()->createCounter($name, $unit, $description, $advisory); } - public function createHistogram(string $name, ?string $unit = null, - ?string $description = null, array $advisory = []): HistogramInterface + public function createHistogram(string $name, ?string $unit = null, + ?string $description = null, array $advisory = []): HistogramInterface { return $this->meter()->createHistogram($name, $unit, $description, $advisory); } - public function createGauge(string $name, ?string $unit = null, - ?string $description = null, array $advisory = []): GaugeInterface + public function createGauge(string $name, ?string $unit = null, + ?string $description = null, array $advisory = []): GaugeInterface { return $this->meter()->createGauge($name, $unit, $description, $advisory); } - public function createObservableGauge(string $name, ?string $unit = null, - ?string $description = null, array|callable $advisory = [], callable ...$callbacks): ObservableGaugeInterface + public function createObservableGauge(string $name, ?string $unit = null, + ?string $description = null, array|callable $advisory = [], callable ...$callbacks): ObservableGaugeInterface { return $this->meter()->createObservableGauge($name, $unit, $description, $advisory, $callbacks); } - - -} \ No newline at end of file +} From 2b18657c75cba76305ae5f6b68afb161265af38d Mon Sep 17 00:00:00 2001 From: billpyang Date: Mon, 8 Sep 2025 17:54:55 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=E5=AE=8C=E5=96=84=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Facades/Metric.php | 8 ++++---- src/OpenTelemetryServiceProvider.php | 1 - src/Support/Metric.php | 24 ++++++++++++------------ 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/Facades/Metric.php b/src/Facades/Metric.php index dcef943..feae3b6 100644 --- a/src/Facades/Metric.php +++ b/src/Facades/Metric.php @@ -10,10 +10,10 @@ * @method static bool isEnabled() * @method static void reset() * @method static \OpenTelemetry\API\Metrics\MeterInterface meter() - * @method static \OpenTelemetry\API\Metrics\CounterInterface createCounter(string $name, ?string $unit = null, ?string $description = null, array $advisory = []) - * @method static \OpenTelemetry\API\Metrics\HistogramInterface createHistogram(string $name, ?string $unit = null, ?string $description = null, array $advisory = []) - * @method static \OpenTelemetry\API\Metrics\GaugeInterface createGauge(string $name, ?string $unit = null, ?string $description = null, array $advisory = []) - * @method static \OpenTelemetry\API\Metrics\ObservableGaugeInterface createObservableGauge(string $name, ?string $unit = null, ?string $description = null, array|callable $advisory = [], callable ...$callbacks) + * @method static \OpenTelemetry\API\Metrics\CounterInterface counter(string $name, ?string $unit = null, ?string $description = null, array $advisories = []) + * @method static \OpenTelemetry\API\Metrics\HistogramInterface histogram(string $name, ?string $unit = null, ?string $description = null, array $advisories = []) + * @method static \OpenTelemetry\API\Metrics\GaugeInterface gauge(string $name, ?string $unit = null, ?string $description = null, array $advisories = []) + * @method static \OpenTelemetry\API\Metrics\ObservableGaugeInterface observableGauge(string $name, ?string $unit = null, ?string $description = null, array|callable $advisories = [], callable ...$callbacks) * * @see \Overtrue\LaravelOpenTelemetry\Support\Metric */ diff --git a/src/OpenTelemetryServiceProvider.php b/src/OpenTelemetryServiceProvider.php index 220e565..f51c377 100644 --- a/src/OpenTelemetryServiceProvider.php +++ b/src/OpenTelemetryServiceProvider.php @@ -14,7 +14,6 @@ use OpenTelemetry\API\Trace\TracerInterface; use Overtrue\LaravelOpenTelemetry\Console\Commands\TestCommand; use Overtrue\LaravelOpenTelemetry\Facades\Measure; -use Overtrue\LaravelOpenTelemetry\Facades\Metric; use Overtrue\LaravelOpenTelemetry\Http\Middleware\AddTraceId; use Overtrue\LaravelOpenTelemetry\Http\Middleware\TraceRequest; diff --git a/src/Support/Metric.php b/src/Support/Metric.php index 031f103..14e1ffe 100644 --- a/src/Support/Metric.php +++ b/src/Support/Metric.php @@ -63,27 +63,27 @@ public function meter(): MeterInterface } } - public function createCounter(string $name, ?string $unit = null, - ?string $description = null, array $advisory = []): CounterInterface + public function counter(string $name, ?string $unit = null, + ?string $description = null, array $advisories = []): CounterInterface { - return $this->meter()->createCounter($name, $unit, $description, $advisory); + return $this->meter()->createCounter($name, $unit, $description, $advisories); } - public function createHistogram(string $name, ?string $unit = null, - ?string $description = null, array $advisory = []): HistogramInterface + public function histogram(string $name, ?string $unit = null, + ?string $description = null, array $advisories = []): HistogramInterface { - return $this->meter()->createHistogram($name, $unit, $description, $advisory); + return $this->meter()->createHistogram($name, $unit, $description, $advisories); } - public function createGauge(string $name, ?string $unit = null, - ?string $description = null, array $advisory = []): GaugeInterface + public function gauge(string $name, ?string $unit = null, + ?string $description = null, array $advisories = []): GaugeInterface { - return $this->meter()->createGauge($name, $unit, $description, $advisory); + return $this->meter()->createGauge($name, $unit, $description, $advisories); } - public function createObservableGauge(string $name, ?string $unit = null, - ?string $description = null, array|callable $advisory = [], callable ...$callbacks): ObservableGaugeInterface + public function observableGauge(string $name, ?string $unit = null, + ?string $description = null, array|callable $advisories = [], callable ...$callbacks): ObservableGaugeInterface { - return $this->meter()->createObservableGauge($name, $unit, $description, $advisory, $callbacks); + return $this->meter()->createObservableGauge($name, $unit, $description, $advisories, ...$callbacks); } }