-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
otel: fixes dep on grpc when not using it and honors opentelemetry-instrument #9972
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,28 +49,37 @@ class OpenTelemetryConfig: | |
exporter: Union[str, SpanExporter] = "console" | ||
endpoint: Optional[str] = None | ||
headers: Optional[str] = None | ||
debug: Optional[str] = None | ||
|
||
@classmethod | ||
def from_env(cls): | ||
""" | ||
OTEL_HEADERS=x-honeycomb-team=B85YgLm9**** | ||
OTEL_EXPORTER="otlp_http" | ||
OTEL_ENDPOINT="https://api.honeycomb.io/v1/traces" | ||
OTEL_HEADERS=x-honeycomb-team=B85YgLm9**** | ||
DEBUG_OTEL="true" | ||
|
||
OTEL_HEADERS gets sent as headers = {"x-honeycomb-team": "B85YgLm96******"} | ||
""" | ||
from opentelemetry.sdk.trace.export.in_memory_span_exporter import ( | ||
InMemorySpanExporter, | ||
) | ||
|
||
if os.getenv("OTEL_EXPORTER") == "in_memory": | ||
# Declare LiteLLM variables | ||
exporter = os.getenv("OTEL_EXPORTER", "console") | ||
endpoint = os.getenv("OTEL_ENDPOINT") | ||
headers = os.getenv("OTEL_HEADERS") | ||
debug = os.getenv("DEBUG_OTEL") | ||
|
||
if exporter == "in_memory": | ||
from opentelemetry.sdk.trace.export.in_memory_span_exporter import ( | ||
InMemorySpanExporter, | ||
) | ||
|
||
return cls(exporter=InMemorySpanExporter()) | ||
|
||
return cls( | ||
exporter=os.getenv("OTEL_EXPORTER", "console"), | ||
endpoint=os.getenv("OTEL_ENDPOINT"), | ||
headers=os.getenv( | ||
"OTEL_HEADERS" | ||
), # example: OTEL_HEADERS=x-honeycomb-team=B85YgLm96***" | ||
exporter=exporter, | ||
endpoint=endpoint, | ||
headers=headers, | ||
debug=str(debug).lower(), | ||
) | ||
|
||
|
||
|
@@ -82,29 +91,20 @@ def __init__( | |
**kwargs, | ||
): | ||
from opentelemetry import trace | ||
from opentelemetry.sdk.resources import Resource | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.trace import SpanKind | ||
|
||
if config is None: | ||
config = OpenTelemetryConfig.from_env() | ||
|
||
self.config = config | ||
self.callback_name = callback_name | ||
self.OTEL_EXPORTER = self.config.exporter | ||
self.OTEL_ENDPOINT = self.config.endpoint | ||
self.OTEL_HEADERS = self.config.headers | ||
provider = TracerProvider(resource=Resource(attributes=LITELLM_RESOURCE)) | ||
provider.add_span_processor(self._get_span_processor()) | ||
self.callback_name = callback_name | ||
|
||
trace.set_tracer_provider(provider) | ||
self.tracer = trace.get_tracer(LITELLM_TRACER_NAME) | ||
|
||
self.span_kind = SpanKind | ||
|
||
_debug_otel = str(os.getenv("DEBUG_OTEL", "False")).lower() | ||
|
||
if _debug_otel == "true": | ||
if self.config.debug == "true": | ||
# Set up logging | ||
import logging | ||
|
||
|
@@ -115,6 +115,16 @@ def __init__( | |
otel_exporter_logger = logging.getLogger("opentelemetry.sdk.trace.export") | ||
otel_exporter_logger.setLevel(logging.DEBUG) | ||
|
||
# Don't override the tracer provider set by `opentelemetry-instrument` | ||
if trace.get_tracer_provider() is None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this makes sure the opentelemetry-instrument supplied tracer is in use |
||
from opentelemetry.sdk.resources import Resource | ||
from opentelemetry.sdk.trace import TracerProvider | ||
|
||
provider = TracerProvider(resource=Resource(attributes=LITELLM_RESOURCE)) | ||
provider.add_span_processor(self._get_span_processor()) | ||
trace.set_tracer_provider(provider) | ||
self.tracer = trace.get_tracer(LITELLM_TRACER_NAME) | ||
|
||
# init CustomLogger params | ||
super().__init__(**kwargs) | ||
self._init_otel_logger_on_litellm_proxy() | ||
|
@@ -816,12 +826,6 @@ def _get_span_context(self, kwargs): | |
return TraceContextTextMapPropagator().extract(carrier=carrier), None | ||
|
||
def _get_span_processor(self, dynamic_headers: Optional[dict] = None): | ||
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( | ||
OTLPSpanExporter as OTLPSpanExporterGRPC, | ||
) | ||
from opentelemetry.exporter.otlp.proto.http.trace_exporter import ( | ||
OTLPSpanExporter as OTLPSpanExporterHTTP, | ||
) | ||
from opentelemetry.sdk.trace.export import ( | ||
BatchSpanProcessor, | ||
ConsoleSpanExporter, | ||
|
@@ -843,40 +847,48 @@ def _get_span_processor(self, dynamic_headers: Optional[dict] = None): | |
self.OTEL_EXPORTER, "export" | ||
): # Check if it has the export method that SpanExporter requires | ||
verbose_logger.debug( | ||
"OpenTelemetry: intiializing SpanExporter. Value of OTEL_EXPORTER: %s", | ||
"OpenTelemetry: initializing SpanExporter. Value of OTEL_EXPORTER: %s", | ||
self.OTEL_EXPORTER, | ||
) | ||
return SimpleSpanProcessor(cast(SpanExporter, self.OTEL_EXPORTER)) | ||
|
||
if self.OTEL_EXPORTER == "console": | ||
verbose_logger.debug( | ||
"OpenTelemetry: intiializing console exporter. Value of OTEL_EXPORTER: %s", | ||
"OpenTelemetry: initializing console exporter. Value of OTEL_EXPORTER: %s", | ||
self.OTEL_EXPORTER, | ||
) | ||
return BatchSpanProcessor(ConsoleSpanExporter()) | ||
elif self.OTEL_EXPORTER == "otlp_http": | ||
verbose_logger.debug( | ||
"OpenTelemetry: intiializing http exporter. Value of OTEL_EXPORTER: %s", | ||
"OpenTelemetry: initializing http exporter. Value of OTEL_EXPORTER: %s", | ||
self.OTEL_EXPORTER, | ||
) | ||
from opentelemetry.exporter.otlp.proto.http.trace_exporter import ( | ||
OTLPSpanExporter as OTLPSpanExporterHTTP, | ||
) | ||
|
||
return BatchSpanProcessor( | ||
OTLPSpanExporterHTTP( | ||
endpoint=self.OTEL_ENDPOINT, headers=_split_otel_headers | ||
), | ||
) | ||
elif self.OTEL_EXPORTER == "otlp_grpc": | ||
verbose_logger.debug( | ||
"OpenTelemetry: intiializing grpc exporter. Value of OTEL_EXPORTER: %s", | ||
"OpenTelemetry: initializing grpc exporter. Value of OTEL_EXPORTER: %s", | ||
self.OTEL_EXPORTER, | ||
) | ||
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( | ||
OTLPSpanExporter as OTLPSpanExporterGRPC, | ||
) | ||
|
||
return BatchSpanProcessor( | ||
OTLPSpanExporterGRPC( | ||
endpoint=self.OTEL_ENDPOINT, headers=_split_otel_headers | ||
), | ||
) | ||
else: | ||
verbose_logger.debug( | ||
"OpenTelemetry: intiializing console exporter. Value of OTEL_EXPORTER: %s", | ||
"OpenTelemetry: initializing console exporter. Value of OTEL_EXPORTER: %s", | ||
self.OTEL_EXPORTER, | ||
) | ||
return BatchSpanProcessor(ConsoleSpanExporter()) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.