open-telemetry/gen-otlp-protobuf
Generated OpenTelemetry OTLP protobuf classes for PHP. Requires google/protobuf and can use the PECL protobuf extension for much faster production performance. Read-only split from the OpenTelemetry PHP monorepo.
Installation Add the package via Composer:
composer require open-telemetry/gen-otlp-protobuf
No additional configuration is required—this is a protobuf schema generator for OpenTelemetry Protocol (OTLP) in PHP.
First Use Case: Generating OTLP Protobuf Classes
Use the gen-otlp-protobuf package to generate PHP classes from .proto files. This is typically done via a build script or CI pipeline:
vendor/bin/protoc --php_out=. --plugin=protoc-gen-php=vendor/bin/protoc-gen-php \
-I=vendor/open-telemetry/gen-otlp-protobuf/proto/otlp \
vendor/open-telemetry/gen-otlp-protobuf/proto/otlp/*.proto
This generates PHP classes (e.g., Span, Trace, Metric) in the current directory.
Where to Look First
proto/otlp directory.protoc, inspect the auto-generated classes (e.g., SpanProto, ResourceProto) in your project.open-telemetry/sdk for actual instrumentation.Generating Protobuf Classes for OTLP Exports
Integrate the protobuf generator into your build process (e.g., composer.json scripts):
{
"scripts": {
"generate:protobuf": "vendor/bin/protoc --php_out=. --plugin=protoc-gen-php=vendor/bin/protoc-gen-php -I=vendor/open-telemetry/gen-otlp-protobuf/proto/otlp vendor/open-telemetry/gen-otlp-protobuf/proto/otlp/*.proto"
}
}
Run with:
composer generate:protobuf
Serializing Data for OTLP Collectors Use the generated classes to construct OTLP payloads (e.g., for exporting traces/metrics):
use OpenTelemetry\Proto\Trace\V1\SpanProto;
$span = new SpanProto();
$span->setName("http.request");
$span->setStartTimeUnixNano(1_000_000_000); // 1 second in nanoseconds
$serialized = $span->serializeToString();
Integration with OpenTelemetry PHP SDK Combine with the OpenTelemetry PHP SDK to export data to OTLP endpoints:
use OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessor;
use OpenTelemetry\SDK\Trace\SpanExporter\OtlpSpanExporter;
$exporter = new OtlpSpanExporter(
host: 'otel-collector:4317',
insecure: true // For testing only!
);
$processor = new BatchSpanProcessor($exporter);
$provider->addSpanProcessor($processor);
Custom Protobuf Extensions Extend the generated classes for domain-specific fields:
// After generating protobuf classes, extend them:
class CustomSpanProto extends SpanProto {
public function setCustomAttribute(string $key, string $value) {
$this->attributes[$key] = ['stringValue' => $value];
}
}
post-install script to regenerate protobuf classes when dependencies update.Protobuf Schema Changes
gen-otlp-protobuf package may break existing code if protobuf schemas change.Missing protoc Plugin
protoc-gen-php plugin is required but not installed by default.composer require google/protobuf
Nanosecond Precision
microtime(true) returns seconds with microseconds.$nanoseconds = (int) ($seconds * 1_000_000_000 + $microseconds * 1_000);
Circular Dependencies
SpanProto ↔ ResourceProto).protoc --decode_raw to validate serialized data.\OpenTelemetry\SDK\Common\Instrumentation\Logger::setLogLevel(\Psr\Log\LogLevel::DEBUG);
v1 vs. v0.17.0).gzip). Configure the exporter to handle it:
$exporter = new OtlpSpanExporter(host: 'otel-collector:4317', compression: 'gzip');
Custom Protobuf Fields
Define new .proto files in your project and generate additional classes alongside the OTLP schemas.
Protobuf Plugins
Extend the protoc pipeline with custom plugins (e.g., for validation or transformation):
vendor/bin/protoc --php_out=. --plugin=protoc-gen-custom=./custom-plugin ...
OpenTelemetry SDK Hooks
Use the SDK’s SpanProcessor or MetricProcessor to intercept and modify protobuf payloads before export.
How can I help you explore Laravel packages today?