Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(monitoring): support GRPC LogExporter #4808

Merged
merged 3 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 (
aarnphm marked this conversation as resolved.
Show resolved Hide resolved
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
Loading