Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit 7dbe1ef

Browse files
authored
Merge pull request #2 from billyang-arch/feat/add-metrics
2 parents 747bc77 + 2b18657 commit 7dbe1ef

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

src/Facades/Metric.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Overtrue\LaravelOpenTelemetry\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* @method static void enable()
9+
* @method static void disable()
10+
* @method static bool isEnabled()
11+
* @method static void reset()
12+
* @method static \OpenTelemetry\API\Metrics\MeterInterface meter()
13+
* @method static \OpenTelemetry\API\Metrics\CounterInterface counter(string $name, ?string $unit = null, ?string $description = null, array $advisories = [])
14+
* @method static \OpenTelemetry\API\Metrics\HistogramInterface histogram(string $name, ?string $unit = null, ?string $description = null, array $advisories = [])
15+
* @method static \OpenTelemetry\API\Metrics\GaugeInterface gauge(string $name, ?string $unit = null, ?string $description = null, array $advisories = [])
16+
* @method static \OpenTelemetry\API\Metrics\ObservableGaugeInterface observableGauge(string $name, ?string $unit = null, ?string $description = null, array|callable $advisories = [], callable ...$callbacks)
17+
*
18+
* @see \Overtrue\LaravelOpenTelemetry\Support\Metric
19+
*/
20+
class Metric extends Facade
21+
{
22+
protected static function getFacadeAccessor(): string
23+
{
24+
return \Overtrue\LaravelOpenTelemetry\Support\Metric::class;
25+
}
26+
}

src/OpenTelemetryServiceProvider.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Support\ServiceProvider;
1111
use Laravel\Octane\Events;
1212
use OpenTelemetry\API\Globals;
13+
use OpenTelemetry\API\Metrics\MeterInterface;
1314
use OpenTelemetry\API\Trace\TracerInterface;
1415
use Overtrue\LaravelOpenTelemetry\Console\Commands\TestCommand;
1516
use Overtrue\LaravelOpenTelemetry\Facades\Measure;
@@ -41,6 +42,7 @@ public function register(): void
4142
__DIR__.'/../config/otel.php', 'otel',
4243
);
4344

45+
// Register Tracer
4446
$this->app->singleton(Support\Measure::class, function ($app) {
4547
return new Support\Measure($app);
4648
});
@@ -54,6 +56,19 @@ public function register(): void
5456

5557
$this->app->alias(TracerInterface::class, 'opentelemetry.tracer');
5658

59+
// Register metric
60+
$this->app->singleton(Support\Metric::class, function ($app) {
61+
return new Support\Metric($app);
62+
});
63+
$this->app->alias(Support\Metric::class, 'opentelemetry.metric');
64+
65+
$this->app->singleton(MeterInterface::class, function () {
66+
return Globals::meterProvider()
67+
->getMeter(config('otel.meter_name', 'overtrue.laravel-open-telemetry'));
68+
});
69+
70+
$this->app->alias(MeterInterface::class, 'opentelemetry.meter');
71+
5772
Log::debug('[laravel-open-telemetry] Service provider registered successfully');
5873
}
5974

src/Support/Metric.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)