Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

[Feat]: Enable gRPC export for OpenTelemetry traces and metrics (#508)

Patcher294fba56d991
diff --git a/sdk/python/pyproject.toml b/sdk/python/pyproject.toml
index 01069e2a..5da5a844 100644
--- a/sdk/python/pyproject.toml
+++ b/sdk/python/pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "openlit"
-version = "1.30.1"
+version = "1.30.2"
description = "OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications and GPUs, facilitating the integration of observability into your GenAI-driven projects"
authors = ["OpenLIT"]
repository = "https://github.com/openlit/openlit/tree/main/openlit/python"
diff --git a/sdk/python/src/openlit/__init__.py b/sdk/python/src/openlit/__init__.py
index 224e058a..427984cb 100644
--- a/sdk/python/src/openlit/__init__.py
+++ b/sdk/python/src/openlit/__init__.py
@@ -379,7 +379,7 @@ def get_prompt(url=None, name=None, api_key=None, prompt_id=None,
# Return the JSON response
return response.json()
except requests.RequestException as error:
- print(f"Error fetching prompt: {error}")
+ logger.error("Error fetching prompt: '%s'", error)
return None
def get_secrets(url=None, api_key=None, key=None, tags=None, should_set_env=None):
@@ -437,7 +437,7 @@ def get_secrets(url=None, api_key=None, key=None, tags=None, should_set_env=None
os.environ[token] = str(value)
return vault_response
except requests.RequestException as error:
- print(f"Error fetching secrets: {error}")
+ logger.error("Error fetching secrets: '%s'", error)
return None
def trace(wrapped):
diff --git a/sdk/python/src/openlit/evals/utils.py b/sdk/python/src/openlit/evals/utils.py
index fa4918bb..3cbd6d51 100644
--- a/sdk/python/src/openlit/evals/utils.py
+++ b/sdk/python/src/openlit/evals/utils.py
@@ -3,15 +3,18 @@
import json
import os
+import logging
from typing import Optional, Tuple, List
from pydantic import BaseModel
-
from opentelemetry.metrics import get_meter
from opentelemetry.sdk.resources import TELEMETRY_SDK_NAME
from anthropic import Anthropic
from openai import OpenAI
from openlit.semcov import SemanticConvetion
+# Initialize logger for logging potential issues and operations
+logger = logging.getLogger(__name__)
+
class JsonOutput(BaseModel):
"""
A model representing the structure of JSON output for prompt injection detection.
@@ -216,7 +219,7 @@ def parse_llm_response(response) -> JsonOutput:
return JsonOutput(**data)
except (json.JSONDecodeError, TypeError) as e:
- print(f"Error parsing LLM response: {e}")
+ logger.error("Error parsing LLM response: '%s'", e)
return JsonOutput(score=0, classification="none", explanation="none",
verdict="no", evaluation="none")
diff --git a/sdk/python/src/openlit/guard/utils.py b/sdk/python/src/openlit/guard/utils.py
index f3ba9447..0335099f 100644
--- a/sdk/python/src/openlit/guard/utils.py
+++ b/sdk/python/src/openlit/guard/utils.py
@@ -4,6 +4,7 @@
import re
import json
import os
+import logging
from typing import Optional, Tuple
from pydantic import BaseModel
from opentelemetry.metrics import get_meter
@@ -12,6 +13,9 @@ from anthropic import Anthropic
from openai import OpenAI
from openlit.semcov import SemanticConvetion
+# Initialize logger for logging potential issues and operations
+logger = logging.getLogger(__name__)
+
class JsonOutput(BaseModel):
"""
A model representing the structure of JSON output for prompt injection detection.
@@ -158,7 +162,7 @@ def parse_llm_response(response) -> JsonOutput:
return JsonOutput(**data)
except (json.JSONDecodeError, TypeError) as e:
- print(f"Error parsing LLM response: {e}")
+ logger.error("Error parsing LLM response: '%s'", e)
return JsonOutput(score=0, classification="none", explanation="none",
verdict="none", guard="none")
diff --git a/sdk/python/src/openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py b/sdk/python/src/openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py
index 0c60be52..7a4f2124 100644
--- a/sdk/python/src/openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py
+++ b/sdk/python/src/openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py
@@ -195,7 +195,6 @@ def async_complete(gen_ai_endpoint, version, environment, application_name,
with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
response = await wrapped(*args, **kwargs)
- # print(instance._system_instruction.__dict__["_pb"].parts[0].text)
try:
# Format 'messages' into a single string
message_prompt = kwargs.get("messages", "")
diff --git a/sdk/python/src/openlit/instrumentation/azure_ai_inference/azure_ai_inference.py b/sdk/python/src/openlit/instrumentation/azure_ai_inference/azure_ai_inference.py
index 77c9e8b3..17b2ebda 100644
--- a/sdk/python/src/openlit/instrumentation/azure_ai_inference/azure_ai_inference.py
+++ b/sdk/python/src/openlit/instrumentation/azure_ai_inference/azure_ai_inference.py
@@ -195,7 +195,6 @@ def complete(gen_ai_endpoint, version, environment, application_name,
with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
response = wrapped(*args, **kwargs)
- # print(instance._system_instruction.__dict__["_pb"].parts[0].text)
try:
# Format 'messages' into a single string
message_prompt = kwargs.get("messages", "")
diff --git a/sdk/python/src/openlit/instrumentation/google_ai_studio/google_ai_studio.py b/sdk/python/src/openlit/instrumentation/google_ai_studio/google_ai_studio.py
index ac02e7e0..e531d24b 100644
--- a/sdk/python/src/openlit/instrumentation/google_ai_studio/google_ai_studio.py
+++ b/sdk/python/src/openlit/instrumentation/google_ai_studio/google_ai_studio.py
@@ -164,7 +164,6 @@ def generate(gen_ai_endpoint, version, environment, application_name,
with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
response = wrapped(*args, **kwargs)
- # print(instance._system_instruction.__dict__["_pb"].parts[0].text)
try:
prompt = ""
for arg in args:
diff --git a/sdk/python/src/openlit/otel/metrics.py b/sdk/python/src/openlit/otel/metrics.py
index eeb3ef53..0a4d94d0 100644
--- a/sdk/python/src/openlit/otel/metrics.py
+++ b/sdk/python/src/openlit/otel/metrics.py
@@ -1,4 +1,4 @@
-# pylint: disable=duplicate-code, line-too-long
+# pylint: disable=duplicate-code, line-too-long, ungrouped-imports
"""
Setups up OpenTelemetry Meter
"""
@@ -8,10 +8,13 @@ from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader, ConsoleMetricExporter
from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
from opentelemetry.sdk.resources import Resource
-from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter
-
from openlit.semcov import SemanticConvetion
+if os.environ.get("OTEL_EXPORTER_OTLP_PROTOCOL") == "grpc":
+ from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter
+else:
+ from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter
+
# Global flag to check if the meter provider initialization is complete.
METER_SET = False
diff --git a/sdk/python/src/openlit/otel/tracing.py b/sdk/python/src/openlit/otel/tracing.py
index cb619f7a..d49017e8 100644
--- a/sdk/python/src/openlit/otel/tracing.py
+++ b/sdk/python/src/openlit/otel/tracing.py
@@ -1,4 +1,4 @@
-# pylint: disable=duplicate-code, line-too-long
+# pylint: disable=duplicate-code, line-too-long, ungrouped-imports
"""
Setups up OpenTelemetry tracer
"""
@@ -10,8 +10,11 @@ from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, SimpleSpanProcessor
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
-from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
+if os.environ.get("OTEL_EXPORTER_OTLP_PROTOCOL") == "grpc":
+ from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
+else:
+ from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
# Global flag to check if the tracer provider initialization is complete.
TRACER_SET = False