Skip to content

Commit

Permalink
Add support for SPLUNK_REALM (#569)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcollins authored Dec 10, 2024
1 parent ddca52b commit 38cca2a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
25 changes: 23 additions & 2 deletions src/splunk_otel/distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -30,6 +33,7 @@
SPLUNK_ACCESS_TOKEN,
SPLUNK_PROFILER_ENABLED,
SPLUNK_PROFILER_LOGS_ENDPOINT,
SPLUNK_REALM,
SPLUNK_TRACE_RESPONSE_HEADER_ENABLED,
Env,
)
Expand Down Expand Up @@ -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)
Expand All @@ -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}")
Expand Down
2 changes: 1 addition & 1 deletion src/splunk_otel/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
Expand All @@ -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__)

Expand Down
8 changes: 8 additions & 0 deletions tests/test_distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 38cca2a

Please sign in to comment.