Skip to content

Commit

Permalink
feat(monitoring): support GRPC LogExporter (#4808)
Browse files Browse the repository at this point in the history
* feat(monitoring): support GRPC LogExporter

* Add dependency to `monitor-otlp`

* Pass through credentials
  • Loading branch information
judahrand authored Jun 17, 2024
1 parent 9c61652 commit e1efdc6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
8 changes: 4 additions & 4 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ tracing = [
tracing-jaeger = ["opentelemetry-exporter-jaeger==1.20.0"]
tracing-zipkin = ["opentelemetry-exporter-zipkin==1.20.0"]
tracing-otlp = ["opentelemetry-exporter-otlp==1.20.0"]
monitor-otlp = ["opentelemetry-exporter-otlp-proto-http==1.20.0"]
monitor-otlp = [
"opentelemetry-exporter-otlp-proto-http==1.20.0",
"opentelemetry-exporter-otlp-proto-grpc==1.20.0",
]

[build-system]
requires = ['hatchling', "hatch-vcs>=0.3.0"]
Expand Down
33 changes: 31 additions & 2 deletions src/bentoml/_internal/monitoring/otlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
from .base import MonitorBase

try:
from opentelemetry.exporter.otlp.proto.http._log_exporter import OTLPLogExporter
from opentelemetry.exporter.otlp.proto.grpc._log_exporter import (
OTLPLogExporter as OTLPGrpcLogExporter,
)
from opentelemetry.exporter.otlp.proto.http._log_exporter import (
OTLPLogExporter as OTLPHttpLogExporter,
)
except ImportError:
raise MissingDependencyException(
"'opentelemetry-exporter-otlp' is required to use OTLP exporter. Make sure to install it with 'pip install \"bentoml[monitor-otlp]\""
Expand Down Expand Up @@ -106,6 +111,7 @@ def __init__(
timeout: int | str | None = None,
compression: str | None = None,
meta_sample_rate: float = 1.0,
protocol: t.Literal["http", "grpc"] = "http",
**_: t.Any,
) -> None:
"""
Expand All @@ -119,6 +125,7 @@ def __init__(
headers: The headers to use.
timeout: The timeout to use.
compression: The compression to use.
protocol: The protocol to use.
"""
super().__init__(name, **_)

Expand All @@ -135,6 +142,8 @@ def __init__(
self._schema: dict[str, dict[str, str]] = {}
self._will_export_schema = False

self.protocol = protocol

def _init_logger(self) -> None:
from opentelemetry.sdk.resources import SERVICE_INSTANCE_ID
from opentelemetry.sdk.resources import SERVICE_NAME
Expand Down Expand Up @@ -169,7 +178,27 @@ def _init_logger(self) -> None:
if self.compression is not None:
os.environ[OTEL_EXPORTER_OTLP_COMPRESSION] = self.compression

exporter = OTLPLogExporter()
exporter: OTLPHttpLogExporter | OTLPGrpcLogExporter
if self.protocol == "http":
exporter = OTLPHttpLogExporter(
endpoint=self.endpoint,
certificate_file=self.credentials,
headers=self.headers,
timeout=self.timeout,
compression=self.compression,
)
elif self.protocol == "grpc":
exporter = OTLPGrpcLogExporter(
endpoint=self.endpoint,
insecure=self.insecure,
credentials=self.credentials,
headers=self.headers,
timeout=self.timeout,
compression=self.compression,
)
else:
raise ValueError(f"Invalid protocol: {self.protocol}")

self.logger_provider.add_log_record_processor(BatchLogRecordProcessor(exporter))
handler = LoggingHandler(
level=logging.NOTSET, logger_provider=self.logger_provider
Expand Down

0 comments on commit e1efdc6

Please sign in to comment.