Skip to content

Commit

Permalink
Clean up tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimf5 committed Dec 14, 2024
1 parent e363140 commit 05becf7
Showing 1 changed file with 13 additions and 45 deletions.
58 changes: 13 additions & 45 deletions tests/test_otel.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
server_name localhost;
location /ok {
otel_span_name ok_location;
return 200 "OK";
}
Expand All @@ -50,6 +49,7 @@
}
location /custom {
otel_span_name custom_location;
otel_span_attr http.request.completion
$request_completion;
otel_span_attr http.response.header.content.type
Expand Down Expand Up @@ -146,14 +146,14 @@ def test_response_http09(trace_service, nginx, logger):

span = trace_service.get_span()

assert span.name == "ok_location"
assert span.name == "/ok"


@pytest.mark.parametrize("http_ver", ["1.1", "2.0", "3.0"])
@pytest.mark.parametrize(
("path", "status"),
[("/ok", 200), ("/err", 500), ("", 403)],
ids=["ok", "500 error", "403 error"],
[("/ok", 200), ("/err", 500)],
ids=["ok", "500 error"],
)
def test_response_and_span_attributes(
http_ver, path, status, trace_service, client
Expand All @@ -164,14 +164,12 @@ def test_response_and_span_attributes(
r = client.get(f"{scheme}://127.0.0.1:{port}{path}", verify=False)

span = trace_service.get_span()

assert span.name == ("ok_location" if path == "/ok" else path)
assert span.name == path

# Default span attributes
assert get_attr(span, "http.method") == "GET"
# Some inconsistence between target and route metrics
assert get_attr(span, "http.target") == (path if path else "/")
assert get_attr(span, "http.route") == (path if path else None)
assert get_attr(span, "http.target") == path
assert get_attr(span, "http.route") == path
assert get_attr(span, "http.scheme") == scheme
assert get_attr(span, "http.flavor") == http_ver
assert get_attr(span, "http.user_agent") == (
Expand All @@ -186,34 +184,21 @@ def test_response_and_span_attributes(
assert get_attr(span, "net.sock.peer.port") in range(1024, 65536)


@pytest.mark.parametrize(
("path"),
[
("/custom"),
(""),
],
ids=["present", "absent"],
)
def test_custom_attributes(path, trace_service, client):
client.get(f"http://127.0.0.1:18080{path}")
def test_custom_attributes(trace_service, client):
assert client.get("http://127.0.0.1:18080/custom").status_code == 200

span = trace_service.get_span()
assert span.name == path
assert span.name == "custom_location"

# Custom span attributes
assert get_attr(span, "http.request.completion") == (
"OK" if path == "/custom" else None
)
assert get_attr(span, "http.request.completion") == "OK"
assert (
get_attr(span, "http.response.header.content.type")
.values[0]
.string_value
if path == "/custom"
else get_attr(span, "http.response.header.content.type")
) == ("text/plain" if path == "/custom" else None)
assert get_attr(span, "http.request") == (
"GET /custom HTTP/1.1" if path == "/custom" else None
== "text/plain"
)
assert get_attr(span, "http.request") == "GET /custom HTTP/1.1"


@pytest.mark.parametrize(
Expand Down Expand Up @@ -257,23 +242,6 @@ def test_variables(
assert r.headers["X-Otel-Parent-Sampled"] == ps[idx]


@pytest.mark.skip(reason="variables are present when trace is off")
@pytest.mark.parametrize(
"headers", [None, context], ids=["no context", "context"]
)
@pytest.mark.parametrize("path", ["/notrace"], ids=["trace off"])
def test_no_variables(headers, path, trace_service, client):
r = client.get(f"http://127.0.0.1:18080{path}", headers=headers)

time.sleep(0.01) # wait for spans
assert len(trace_service.batches) == 0

assert r.headers.get("X-Otel-Trace-Id") is None
assert r.headers.get("X-Otel-Span-Id") is None
assert r.headers.get("X-Otel-Parent-Id") is None
assert r.headers.get("X-Otel-Parent-Sampled") is None


@pytest.mark.parametrize(
("headers", "idx"),
[(None, 0), (context, 1)],
Expand Down

0 comments on commit 05becf7

Please sign in to comment.