Implementation:Bentoml BentoML OTLPMonitor
| Knowledge Sources | |
|---|---|
| Domains | Monitoring, OpenTelemetry, Observability |
| Last Updated | 2026-02-13 15:00 GMT |
Overview
Provides an OTLP (OpenTelemetry Protocol) based monitoring implementation that exports inference data and schema to an OpenTelemetry collector endpoint.
Description
The OTLPMonitor class extends MonitorBase and implements monitoring data export via the OpenTelemetry Protocol. It supports both HTTP and gRPC transport protocols for sending log records to an OTLP endpoint. The monitor configures an OpenTelemetry LoggerProvider with a BatchLogRecordProcessor and the appropriate exporter (OTLPHttpLogExporter or OTLPGrpcLogExporter). Configuration can be provided through constructor arguments or standard OTLP environment variables (endpoint, insecure, credentials, headers, timeout, compression). Schema metadata is embedded within data records at a configurable sample rate (meta_sample_rate), and always included when schema changes. The monitor attaches BentoML service resource attributes (service name, instance ID) to the OpenTelemetry resource.
Usage
Use this class when you want to export BentoML monitoring data to an OpenTelemetry-compatible backend such as Jaeger, Grafana Tempo, or any OTLP collector. Requires the opentelemetry-exporter-otlp package to be installed.
Code Reference
Source Location
- Repository: Bentoml_BentoML
- File: src/bentoml/_internal/monitoring/otlp.py
- Lines: 1-267
Signature
class OTLPMonitor(MonitorBase["JSONSerializable"]):
PRESERVED_COLUMNS = (COLUMN_TIME, COLUMN_RID, COLUMN_TID, COLUMN_META) = (
"timestamp", "request_id", "trace_id", "bento_meta",
)
def __init__(
self,
name: str,
endpoint: str | None = None,
insecure: bool | str | None = None,
credentials: str | None = None,
headers: str | None = None,
timeout: int | str | None = None,
compression: str | None = None,
meta_sample_rate: float = 1.0,
protocol: t.Literal["http", "grpc"] = "http",
**_: t.Any,
) -> None: ...
def _init_logger(self) -> None: ...
def __del__(self) -> None: ...
def export_schema(self, columns_schema: dict[str, dict[str, str]]) -> None: ...
def export_data(self, datas: t.Dict[str, collections.deque[JSONSerializable]]) -> None: ...
Import
from bentoml._internal.monitoring.otlp import OTLPMonitor
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| name | str | Yes | Name of the monitor instance |
| endpoint | str or None | No | OTLP endpoint URL; falls back to OTEL_EXPORTER_OTLP_ENDPOINT env var |
| insecure | bool or str or None | No | Whether to use insecure (non-TLS) connection |
| credentials | str or None | No | Path to TLS certificate file |
| headers | str or None | No | Headers to send with OTLP requests |
| timeout | int or str or None | No | Request timeout in seconds |
| compression | str or None | No | Compression algorithm to use (e.g., gzip) |
| meta_sample_rate | float | No | Rate (0.0-1.0) at which schema metadata is included in data records; defaults to 1.0 |
| protocol | Literal["http", "grpc"] | No | Transport protocol; defaults to "http" |
Outputs
| Name | Type | Description |
|---|---|---|
| (side effect) | OTLP log records | Monitoring data and schema exported as OpenTelemetry log records to the configured endpoint |
Usage Examples
import collections
from bentoml._internal.monitoring.otlp import OTLPMonitor
# Create OTLP monitor with HTTP transport
monitor = OTLPMonitor(
name="iris_classifier_prediction",
endpoint="http://localhost:4318",
protocol="http",
meta_sample_rate=0.5,
)
# Export schema
schema = {
"sepal_length": {"name": "sepal_length", "role": "feature", "type": "numerical"},
}
monitor.export_schema(schema)
# Export data
data = {
"sepal_length": collections.deque([5.1, 4.9]),
}
monitor.export_data(data)