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
10 changes: 8 additions & 2 deletions src/OpenTelemetryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
use OpenTelemetry\API\Globals;
use OpenTelemetry\API\Metrics\MeterInterface;
use OpenTelemetry\API\Trace\TracerInterface;
use OpenTelemetry\SDK\Metrics\MeterProviderFactory;
use OpenTelemetry\SDK\Resource\Detectors\Sdk;
use Overtrue\LaravelOpenTelemetry\Console\Commands\TestCommand;
use Overtrue\LaravelOpenTelemetry\Facades\Measure;
use Overtrue\LaravelOpenTelemetry\Http\Middleware\AddTraceId;
use Overtrue\LaravelOpenTelemetry\Http\Middleware\TraceRequest;
use Overtrue\LaravelOpenTelemetry\Support\Metric;

class OpenTelemetryServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -62,9 +65,12 @@ public function register(): void
});
$this->app->alias(Support\Metric::class, 'opentelemetry.metric');

// register custom meter
$this->app->singleton(MeterInterface::class, function () {
return Globals::meterProvider()
->getMeter(config('otel.meter_name', 'overtrue.laravel-open-telemetry'));
$resourceInfo = (new Sdk)->getResource();
Metric::$meterProvider = (new MeterProviderFactory)->create($resourceInfo);
Copy link
Owner

Choose a reason for hiding this comment

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

可以考虑给 Metric 增加一个 Setter 和 Getter 方法,把属性私有,这么直接改感觉不太好

Copy link
Author

Choose a reason for hiding this comment

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

1


return Metric::$meterProvider->getMeter(config('otel.meter_name', 'overtrue.laravel-open-telemetry'));
});

$this->app->alias(MeterInterface::class, 'opentelemetry.meter');
Expand Down
6 changes: 4 additions & 2 deletions src/Support/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Facades\Log;
use OpenTelemetry\API\Globals;
use OpenTelemetry\API\Metrics\CounterInterface;
use OpenTelemetry\API\Metrics\GaugeInterface;
use OpenTelemetry\API\Metrics\HistogramInterface;
use OpenTelemetry\API\Metrics\MeterInterface;
use OpenTelemetry\API\Metrics\MeterProviderInterface;
use OpenTelemetry\API\Metrics\Noop\NoopMeter;
use OpenTelemetry\API\Metrics\ObservableGaugeInterface;
use Throwable;
Expand All @@ -17,6 +17,8 @@ class Metric
{
private static ?bool $enabled = null;

public static ?MeterProviderInterface $meterProvider = null;

public function __construct(protected Application $app) {}

// ======================= Enable/Disable Management =======================
Expand Down Expand Up @@ -45,7 +47,7 @@ public function isEnabled(): bool
*/
public function flush(): void
{
Globals::meterProvider()?->forceFlush();
self::$meterProvider?->forceFlush();
}

// ======================= Core OpenTelemetry API =======================
Expand Down