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

fix(monitoring): don't override user set environment variables #4807

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
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
Loading