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 all commits
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
26 changes: 26 additions & 0 deletions src/Facades/Metric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Overtrue\LaravelOpenTelemetry\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @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 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
*/
class Metric extends Facade
{
protected static function getFacadeAccessor(): string
{
return \Overtrue\LaravelOpenTelemetry\Support\Metric::class;
}
}
15 changes: 15 additions & 0 deletions src/OpenTelemetryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;
Expand Down Expand Up @@ -41,6 +42,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 +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');
}

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

namespace Overtrue\LaravelOpenTelemetry\Support;

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