From 0a11f30f4fe77394cfe4c6082d8544c39dd4e9bf Mon Sep 17 00:00:00 2001 From: Pablo Collins Date: Tue, 10 Dec 2024 14:48:48 -0500 Subject: [PATCH] Add support for SPLUNK_REALM --- src/splunk_otel/distro.py | 25 +++++++++++++++++++++++-- src/splunk_otel/env.py | 2 +- tests/test_distro.py | 8 ++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/splunk_otel/distro.py b/src/splunk_otel/distro.py index 6f9ec42..0d8254e 100644 --- a/src/splunk_otel/distro.py +++ b/src/splunk_otel/distro.py @@ -19,6 +19,9 @@ from opentelemetry.sdk.environment_variables import ( OTEL_EXPORTER_OTLP_HEADERS, OTEL_EXPORTER_OTLP_LOGS_ENDPOINT, + OTEL_EXPORTER_OTLP_METRICS_ENDPOINT, + OTEL_EXPORTER_OTLP_PROTOCOL, + OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, OTEL_RESOURCE_ATTRIBUTES, OTEL_SERVICE_NAME, ) @@ -30,6 +33,7 @@ SPLUNK_ACCESS_TOKEN, SPLUNK_PROFILER_ENABLED, SPLUNK_PROFILER_LOGS_ENDPOINT, + SPLUNK_REALM, SPLUNK_TRACE_RESPONSE_HEADER_ENABLED, Env, ) @@ -61,9 +65,26 @@ def _configure(self, **kwargs): self.check_service_name() self.set_profiling_env() self.set_resource_attributes() - self.configure_headers() + self.handle_realm() + self.configure_token_headers() self.set_server_timing_propagator() + def handle_realm(self): + realm = self.env.getval(SPLUNK_REALM) + if len(realm): + ingest_url = f"https://ingest.{realm}.signalfx.com" + self.env.setdefault( + OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, + f"{ingest_url}/v2/trace/otlp", + ) + self.env.setdefault( + OTEL_EXPORTER_OTLP_METRICS_ENDPOINT, + f"{ingest_url}/v2/datapoint/otlp", + ) + + # if realm is set, we assume direct ingest and set the protocol to `http/proto` + self.env.setdefault(OTEL_EXPORTER_OTLP_PROTOCOL, "http/protobuf") + def check_service_name(self): if not len(self.env.getval(OTEL_SERVICE_NAME)): _pylogger.warning(_NO_SERVICE_NAME_WARNING) @@ -84,7 +105,7 @@ def set_resource_attributes(self): self.env.list_append(OTEL_RESOURCE_ATTRIBUTES, f"telemetry.distro.name={_DISTRO_NAME}") self.env.list_append(OTEL_RESOURCE_ATTRIBUTES, f"telemetry.distro.version={version}") - def configure_headers(self): + def configure_token_headers(self): tok = self.env.getval(SPLUNK_ACCESS_TOKEN).strip() if tok: self.env.list_append(OTEL_EXPORTER_OTLP_HEADERS, f"{_X_SF_TOKEN}={tok}") diff --git a/src/splunk_otel/env.py b/src/splunk_otel/env.py index ee2541f..81be7cf 100644 --- a/src/splunk_otel/env.py +++ b/src/splunk_otel/env.py @@ -19,7 +19,6 @@ "OTEL_TRACES_EXPORTER": "otlp", "OTEL_METRICS_EXPORTER": "otlp", "OTEL_LOGS_EXPORTER": "otlp", - "OTEL_EXPORTER_OTLP_PROTOCOL": "grpc", # FIXME: revisit "OTEL_ATTRIBUTE_COUNT_LIMIT": "", "OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT": "", "OTEL_SPAN_EVENT_COUNT_LIMIT": "", @@ -39,6 +38,7 @@ SPLUNK_PROFILER_ENABLED = "SPLUNK_PROFILER_ENABLED" SPLUNK_PROFILER_CALL_STACK_INTERVAL = "SPLUNK_PROFILER_CALL_STACK_INTERVAL" SPLUNK_PROFILER_LOGS_ENDPOINT = "SPLUNK_PROFILER_LOGS_ENDPOINT" +SPLUNK_REALM = "SPLUNK_REALM" _pylogger = logging.getLogger(__name__) diff --git a/tests/test_distro.py b/tests/test_distro.py index 07cc15a..e0598a3 100644 --- a/tests/test_distro.py +++ b/tests/test_distro.py @@ -120,6 +120,14 @@ def test_service_name(caplog): assert "service.name attribute is not set" in caplog.text +def test_realm(): + env_store = {"SPLUNK_REALM": "us2"} + configure_distro(env_store) + assert env_store["OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"] == "https://ingest.us2.signalfx.com/v2/trace/otlp" + assert env_store["OTEL_EXPORTER_OTLP_METRICS_ENDPOINT"] == "https://ingest.us2.signalfx.com/v2/datapoint/otlp" + assert env_store["OTEL_EXPORTER_OTLP_PROTOCOL"] == "http/protobuf" + + def configure_distro(env_store): sd = SplunkDistro() sd.env = Env(env_store)