Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Facades/Metric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

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()
* @method static void disable()
* @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)
*
* @see \Overtrue\LaravelOpenTelemetry\Support\Metric
*/
class Metric extends Facade
{
protected static function getFacadeAccessor(): string
{
return \Overtrue\LaravelOpenTelemetry\Support\Metric::class;
}
}
16 changes: 16 additions & 0 deletions src/OpenTelemetryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
use Illuminate\Support\ServiceProvider;
use Laravel\Octane\Events;
use OpenTelemetry\API\Globals;
use OpenTelemetry\API\Metrics\MeterInterface;
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;

Expand Down Expand Up @@ -41,6 +43,7 @@ public function register(): void
__DIR__.'/../config/otel.php', 'otel',
);

// Register Tracer
$this->app->singleton(Support\Measure::class, function ($app) {
return new Support\Measure($app);
});
Expand All @@ -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');
}

Expand Down
95 changes: 95 additions & 0 deletions src/Support/Metric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Overtrue\LaravelOpenTelemetry\Support;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Facades\Log;
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;

class Metric
{
private static ?bool $enabled = null;

public function __construct(protected Application $app)
{
}


// ======================= Enable/Disable Management =======================

public function enable(): void
{
self::$enabled = true;
}

public function disable(): void
{
self::$enabled = false;
}

public function isEnabled(): bool
{
if (self::$enabled === null) {
return config('otel.enabled', true);
}

return self::$enabled;
}


// ======================= Core OpenTelemetry API =======================

/**
* Get the meter instance
*/
public function meter(): MeterInterface
{
if (!$this->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,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这几个 createXXX 我希望改成 laravel style, 就是直接 Metric::counter() Metric::histogram

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

?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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是不是应该叫 advisories ?

{
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);
}


}
Loading