Skip to content

Commit

Permalink
maint: Don’t append OTLP signal paths if already present (#156)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?
If an HTTP endpoint for traces or metrics already includes the signal
path (eg /v1/traces or /v1/metrics), the path is added a second time (eg
`http://somewhere.com/v1/traces` becomes
`http://somewhere.com/v1/traces/v1/traces`).

The path should only be added if it doesn't already exist.

- Closes #155 

## Short description of the changes
- Update options.get_traces_endpoint and options.get_metrics_endpoint to
check if the endpoint already ends with the signal path before adding it
- Add tests to verify behaviour

## How to verify that this has the expected result
You can now use an endpoint with a signal path and the signal path won't
be added twice.
  • Loading branch information
MikeGoldsmith authored Sep 22, 2023
1 parent 9f8466f commit 9bc3f9a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/honeycomb/opentelemetry/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
EXPORTER_PROTOCOL_GRPC = "grpc"
EXPORTER_PROTOCOL_HTTP_PROTO = "http/protobuf"

TRACES_HTTP_PATH = "v1/traces"
METRICS_HTTP_PATH = "v1/metrics"

exporter_protocols = {
EXPORTER_PROTOCOL_GRPC,
EXPORTER_PROTOCOL_HTTP_PROTO
Expand Down Expand Up @@ -155,26 +158,28 @@ def parse_int(environment_variable: str,
def _append_traces_path(protocol: str, endpoint: str) -> str:
"""
Appends the OTLP traces HTTP path '/v1/traces' to the endpoint if the
protocol is http/protobuf.
protocol is http/protobuf and it doesn't already exist.
Returns:
string: the endpoint, optionally appended with traces path
"""
if endpoint and protocol == "http/protobuf":
return "/".join([endpoint.strip("/"), "v1/traces"])
if endpoint and protocol == "http/protobuf" \
and not endpoint.strip("/").endswith(TRACES_HTTP_PATH):
return "/".join([endpoint.strip("/"), TRACES_HTTP_PATH])
return endpoint


def _append_metrics_path(protocol: str, endpoint: str) -> str:
"""
Appends the OTLP metrics HTTP path '/v1/metrics' to the endpoint if the
protocol is http/protobuf.
protocol is http/protobuf and it doesn't already exist.
Returns:
string: the endpoint, optionally appended with metrics path
"""
if endpoint and protocol == "http/protobuf":
return "/".join([endpoint.strip("/"), "v1/metrics"])
if endpoint and protocol == "http/protobuf" \
and not endpoint.strip("/").endswith(METRICS_HTTP_PATH):
return "/".join([endpoint.strip("/"), METRICS_HTTP_PATH])
return endpoint


Expand Down
20 changes: 20 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,16 @@ def test_get_traces_endpoint_with_http_proto_protocol_returns_correctly_formatte
options = HoneycombOptions(exporter_protocol=protocol)
assert options.get_traces_endpoint() == EXPECTED_ENDPOINT

def test_get_traces_endpoint_with_traces_path_and_http_proto_returns_corretly_formatted_endpoint(monkeypatch):
# http
protocol = EXPORTER_PROTOCOL_HTTP_PROTO

# endpoint already has /v1/traces
endpoint = DEFAULT_API_ENDPOINT + "/v1/traces"

# set endpoint in options
options = HoneycombOptions(exporter_protocol=protocol, endpoint=endpoint)
assert options.get_traces_endpoint() == endpoint

def test_get_metrics_endpoint_with_grpc_protocol_returns_correctly_formatted_endpoint(monkeypatch):
# grpc
Expand Down Expand Up @@ -546,6 +556,16 @@ def test_get_metrics_endpoint_with_http_proto_protocol_returns_correctly_formatt
options = HoneycombOptions(exporter_protocol=protocol)
assert options.get_metrics_endpoint() == EXPECTED_ENDPOINT

def test_get_metrics_endpoint_with_metrics_path_and_http_proto_returns_corretly_formatted_endpoint(monkeypatch):
# http
protocol = EXPORTER_PROTOCOL_HTTP_PROTO

# endpoint already has /v1/metrics
endpoint = DEFAULT_API_ENDPOINT + "/v1/metrics"

# set endpoint in options
options = HoneycombOptions(exporter_protocol=protocol, endpoint=endpoint)
assert options.get_metrics_endpoint() == endpoint

def test_debug_sets_log_level_to_debug():
options = HoneycombOptions(debug=True)
Expand Down

0 comments on commit 9bc3f9a

Please sign in to comment.