Skip to content

Push over OTLP

To send the metrics to an OTLP/HTTP endpoint, set the endpoint and schedule metrics:push.

Set the base URL of the endpoint. The package appends /v1/metrics, so leave it out.

.env
OTEL_EXPORTER_OTLP_ENDPOINT=http://collector:4318

To send JSON instead of protobuf:

.env
OTEL_EXPORTER_OTLP_PROTOCOL=http/json

The package reads these through config/metrics.php. After a change, run php artisan config:cache again if the configuration is cached.

Terminal window
php artisan metrics:push

The command prints Sent N metrics. on success. It prints The store holds no metrics. when there is nothing to send yet. Exit code 1 means the store cannot be read or the endpoint refused the push. See Troubleshoot.

routes/console.php
use Illuminate\Support\Facades\Schedule;
Schedule::command('metrics:push')->everyMinute();

When several servers run the scheduler, push from one only:

routes/console.php
Schedule::command('metrics:push')->everyMinute()->onOneServer();

onOneServer() needs a cache store that every server shares, such as Redis or the database.

Push more often than the backend forgets a series. The Collector’s Prometheus exporter drops a series after metric_expiration, 5 minutes by default, without a push.

A minimal Collector configuration that accepts the push:

otel-collector.yaml
receivers:
otlp:
protocols:
http:
endpoint: 0.0.0.0:4318
exporters:
debug:
verbosity: basic
service:
pipelines:
metrics:
receivers: [otlp]
exporters: [debug]

Replace the debug exporter with the exporter of your backend.

Put the headers in push.headers. Publish the configuration first:

Terminal window
php artisan vendor:publish --tag=metrics-config

For a backend that takes a bearer token:

config/metrics.php
'push' => [
// ...
'headers' => [
'Authorization' => 'Bearer '.env('OTLP_TOKEN'),
],
],

For Grafana Cloud, the endpoint is the OTLP gateway of your stack, and the header is Basic authentication with the instance ID and an access policy token:

.env
OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp-gateway-prod-eu-west-2.grafana.net/otlp
GRAFANA_INSTANCE_ID=123456
GRAFANA_OTLP_TOKEN=glc_...
config/metrics.php
'headers' => [
'Authorization' => 'Basic '.base64_encode(env('GRAFANA_INSTANCE_ID').':'.env('GRAFANA_OTLP_TOKEN')),
],

Copy the gateway URL and the instance ID from the OpenTelemetry tile of your Grafana Cloud stack. The region in the URL differs per stack.

Only string header values are sent.

Each push carries service.name and deployment.environment.name:

  • service.name is OTEL_SERVICE_NAME, else APP_NAME, else laravel.
  • deployment.environment.name is APP_ENV.

To add your own attributes:

config/metrics.php
'resource' => [
'service.name' => env('OTEL_SERVICE_NAME', env('APP_NAME', 'laravel')),
'deployment.environment.name' => env('APP_ENV'),
'service.namespace' => 'shop',
],

Do not add host or process attributes. The store holds the totals of every process and host.

The push times out after 10 seconds. To change it:

config/metrics.php
'timeout' => 5,