Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Licensed under the MIT License.

import logging
Comment thread
rodrigobr-msft marked this conversation as resolved.
import os
import threading
from collections.abc import Callable
from typing import Any, Optional
Expand All @@ -10,6 +11,7 @@
from opentelemetry.sdk.resources import SERVICE_NAME, SERVICE_NAMESPACE, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
Comment thread
rodrigobr-msft marked this conversation as resolved.
Outdated

from .exporters.agent365_exporter import _Agent365Exporter
from .exporters.agent365_exporter_options import Agent365ExporterOptions
Expand Down Expand Up @@ -161,10 +163,11 @@ def _configure_internal(
use_s2s_endpoint=exporter_options.use_s2s_endpoint,
suppress_invoke_agent_input=suppress_invoke_agent_input,
)

else:
exporter = ConsoleSpanExporter()
self._logger.warning(
"is_agent365_exporter_enabled() not enabled or token_resolver not set.Falling back to console exporter."
"is_agent365_exporter_enabled() not enabled or token_resolver not set. Falling back to console exporter."
)
Comment thread
rodrigobr-msft marked this conversation as resolved.

# Add span processors
Expand All @@ -181,6 +184,13 @@ def _configure_internal(
self._span_processors["batch"] = batch_processor
self._span_processors["agent"] = agent_processor

if os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT"):
Comment thread
rodrigobr-msft marked this conversation as resolved.
Outdated
# The OTLPSpanExporter is auto configured from the environment variables
otlp_exporter = OTLPSpanExporter()
tracer_provider.add_span_processor(
_EnrichingBatchSpanProcessor(otlp_exporter, **batch_processor_kwargs)
)

Comment thread
rodrigobr-msft marked this conversation as resolved.
Outdated
Comment thread
rodrigobr-msft marked this conversation as resolved.
Outdated
Comment thread
rodrigobr-msft marked this conversation as resolved.
Outdated
# Configure logging if logger_name is provided
if logger_name:
target_logger = logging.getLogger(logger_name)
Expand Down
42 changes: 42 additions & 0 deletions tests/observability/core/test_agent365.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,47 @@ def test_configure_uses_existing_tracer_provider(self, mock_get_provider, mock_i
self.assertIn("SpanProcessor", processor_types)


class TestOTLPExporterConfiguration(unittest.TestCase):
"""Test suite for OTLP exporter configuration based on environment variables."""

@patch("microsoft_agents_a365.observability.core.config.OTLPSpanExporter")
@patch(
"microsoft_agents_a365.observability.core.config.is_agent365_exporter_enabled",
return_value=False,
)
@patch("microsoft_agents_a365.observability.core.config.TelemetryManager._instance", None)
@patch.dict("os.environ", {"OTEL_EXPORTER_OTLP_ENDPOINT": "http://localhost:4318"}, clear=True)
def test_otlp_exporter_initialized_when_env_var_set(self, mock_is_enabled, mock_otlp_exporter):
Comment thread
rodrigobr-msft marked this conversation as resolved.
Comment thread
rodrigobr-msft marked this conversation as resolved.
"""Test that OTLPSpanExporter is initialized when OTEL_EXPORTER_OTLP_ENDPOINT is set."""
Comment thread
rodrigobr-msft marked this conversation as resolved.
Outdated

result = configure(
service_name="test-service",
service_namespace="test-namespace",
)

self.assertTrue(result, "configure() should return True")
mock_otlp_exporter.assert_called_once()

@patch("microsoft_agents_a365.observability.core.config.OTLPSpanExporter")
@patch(
"microsoft_agents_a365.observability.core.config.is_agent365_exporter_enabled",
return_value=False,
)
@patch("microsoft_agents_a365.observability.core.config.TelemetryManager._instance", None)
@patch.dict("os.environ", {}, clear=True)
def test_otlp_exporter_not_initialized_when_env_var_not_set(
Comment thread
rodrigobr-msft marked this conversation as resolved.
self, mock_is_enabled, mock_otlp_exporter
):
"""Test that OTLPSpanExporter is NOT initialized when OTEL_EXPORTER_OTLP_ENDPOINT is not set."""

result = configure(
service_name="test-service",
service_namespace="test-namespace",
)

self.assertTrue(result, "configure() should return True")
mock_otlp_exporter.assert_not_called()


if __name__ == "__main__":
unittest.main()