Skip to content

Commit

Permalink
fix: normalize telemetry url before setup (#3001)
Browse files Browse the repository at this point in the history
* fix: notmalize telemetry url before setup

* organize imports

* Fix

* Update src/phoenix/server/telemetry.py

Co-authored-by: Roger Yang <80478925+RogerHYang@users.noreply.github.com>

---------

Co-authored-by: Roger Yang <80478925+RogerHYang@users.noreply.github.com>
  • Loading branch information
mikeldking and RogerHYang authored Apr 26, 2024
1 parent 3c73f03 commit 28389e8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/phoenix/server/telemetry.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,51 @@
import os
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from opentelemetry.trace import TracerProvider

from phoenix.config import (
ENV_PHOENIX_SERVER_INSTRUMENTATION_OTLP_TRACE_COLLECTOR_GRPC_ENDPOINT,
ENV_PHOENIX_SERVER_INSTRUMENTATION_OTLP_TRACE_COLLECTOR_HTTP_ENDPOINT,
)

if TYPE_CHECKING:
from opentelemetry.trace import TracerProvider
from logging import getLogger

logger = getLogger(__name__)


def normalize_http_collector_endpoint(endpoint: str) -> str:
normalized_endpoint = endpoint
if not normalized_endpoint.startswith("http://") and not normalized_endpoint.startswith(
"https://"
):
logger.warning(
"HTTP collector endpoint should include the protocol (http:// or https://)."
"Assuming http."
)
# assume http if no protocol is provided
normalized_endpoint = f"http://{endpoint}"
if normalized_endpoint.endswith("/v1/traces"):
logger.warning(
"HTTP collector endpoint should not include the /v1/traces path. Removing it."
)
# remove the /v1/traces path
normalized_endpoint = normalized_endpoint[: -len("/v1/traces")]
# remove trailing slashes
normalized_endpoint = normalized_endpoint.rstrip("/")
return normalized_endpoint


def initialize_opentelemetry_tracer_provider() -> "TracerProvider":
logger.info("Initializing OpenTelemetry tracer provider")
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import BatchSpanProcessor

tracer_provider = trace_sdk.TracerProvider()
if http_endpoint := os.getenv(
ENV_PHOENIX_SERVER_INSTRUMENTATION_OTLP_TRACE_COLLECTOR_HTTP_ENDPOINT
):
logger.info(f"Using HTTP collector endpoint: {http_endpoint}")
http_endpoint = normalize_http_collector_endpoint(http_endpoint) + "/v1/traces"
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
OTLPSpanExporter as HttpExporter,
)
Expand All @@ -26,9 +54,11 @@ def initialize_opentelemetry_tracer_provider() -> "TracerProvider":
if grpc_endpoint := os.getenv(
ENV_PHOENIX_SERVER_INSTRUMENTATION_OTLP_TRACE_COLLECTOR_GRPC_ENDPOINT
):
logger.info(f"Using gRPC collector endpoint: {grpc_endpoint}")
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
OTLPSpanExporter as GrpcExporter,
)

tracer_provider.add_span_processor(BatchSpanProcessor(GrpcExporter(grpc_endpoint)))
logger.info("🔭 OpenTelemetry tracer provider initialized")
return tracer_provider
16 changes: 16 additions & 0 deletions tests/server/test_telemetry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from phoenix.server.telemetry import normalize_http_collector_endpoint


def test_normalize_http_collector_endpoint():
assert normalize_http_collector_endpoint("http://localhost:4317") == "http://localhost:4317"
assert normalize_http_collector_endpoint("https://localhost:4317") == "https://localhost:4317"
assert normalize_http_collector_endpoint("localhost:4317") == "http://localhost:4317"
assert (
normalize_http_collector_endpoint("http://localhost:4317/v1/traces")
== "http://localhost:4317"
)
assert (
normalize_http_collector_endpoint("https://localhost:4317/v1/traces")
== "https://localhost:4317"
)
assert normalize_http_collector_endpoint("localhost:4317/v1/traces") == "http://localhost:4317"

0 comments on commit 28389e8

Please sign in to comment.