Skip to content

Commit

Permalink
feat(monitoring): don't set environment variables and allow use of GR…
Browse files Browse the repository at this point in the history
…PC `OTLPLogExporter`
  • Loading branch information
judahrand committed Jun 14, 2024
1 parent ddb653d commit 0f5f3e5
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions src/bentoml/_internal/monitoring/otlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import datetime
import logging
import logging.config
import os
import random
import typing as t

Expand All @@ -13,12 +12,6 @@
from opentelemetry.sdk._logs import LoggerProvider
from opentelemetry.sdk._logs import LoggingHandler
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
from opentelemetry.sdk.environment_variables import OTEL_EXPORTER_OTLP_CERTIFICATE
from opentelemetry.sdk.environment_variables import OTEL_EXPORTER_OTLP_COMPRESSION
from opentelemetry.sdk.environment_variables import OTEL_EXPORTER_OTLP_ENDPOINT
from opentelemetry.sdk.environment_variables import OTEL_EXPORTER_OTLP_HEADERS
from opentelemetry.sdk.environment_variables import OTEL_EXPORTER_OTLP_INSECURE
from opentelemetry.sdk.environment_variables import OTEL_EXPORTER_OTLP_TIMEOUT
from opentelemetry.sdk.resources import Resource

from ...exceptions import MissingDependencyException
Expand All @@ -27,7 +20,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 +104,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 +118,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 +135,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 All @@ -156,20 +158,26 @@ def _init_logger(self) -> None:
)
set_logger_provider(self.logger_provider)

if self.endpoint is not None:
os.environ[OTEL_EXPORTER_OTLP_ENDPOINT] = self.endpoint
if self.insecure is not None:
os.environ[OTEL_EXPORTER_OTLP_INSECURE] = str(self.insecure)
if self.credentials is not None:
os.environ[OTEL_EXPORTER_OTLP_CERTIFICATE] = self.credentials
if self.headers is not None:
os.environ[OTEL_EXPORTER_OTLP_HEADERS] = self.headers
if self.timeout is not None:
os.environ[OTEL_EXPORTER_OTLP_TIMEOUT] = str(self.timeout)
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,
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 0f5f3e5

Please sign in to comment.