diff --git a/tests/test_otel.py b/tests/test_otel.py index 10ce2fe8..11caa854 100644 --- a/tests/test_otel.py +++ b/tests/test_otel.py @@ -28,7 +28,7 @@ } otel_trace on; - otel_service_name test_service; + {{ resource_attrs }} server { listen 127.0.0.1:18443 ssl; @@ -240,7 +240,7 @@ def test_context(client, trace_service, parent, path): @pytest.mark.parametrize( "nginx_config", - [({"interval": "200ms", "scheme": "http://"})], + [{"interval": "200ms", "scheme": "http://"}], indirect=True, ) @pytest.mark.parametrize("batch_count", [1, 3]) @@ -257,8 +257,34 @@ def test_batches(client, trace_service, batch_count): assert len(trace_service.batches) == batch_count for batch in trace_service.batches: - assert get_attr(batch[0].resource, "service.name") == "test_service" + assert ( + get_attr(batch[0].resource, "service.name") + == "unknown_service:nginx" + ) assert len(batch[0].scope_spans[0].spans) == batch_size time.sleep(0.3) # wait for +1 request to be flushed trace_service.batches.clear() + + +@pytest.mark.parametrize( + "nginx_config", + [ + { + "resource_attrs": """ + otel_service_name "test_service"; + otel_resource_attr my.name "my name"; + otel_resource_attr my.service "my service"; + """, + } + ], + indirect=True, +) +def test_custom_resource_attributes(client, trace_service): + assert client.get("http://127.0.0.1:18080/ok").status_code == 200 + + batch = trace_service.get_batch() + + assert get_attr(batch.resource, "service.name") == "test_service" + assert get_attr(batch.resource, "my.name") == "my name" + assert get_attr(batch.resource, "my.service") == "my service" diff --git a/tests/trace_service.py b/tests/trace_service.py index 9f094f91..3b191a1d 100644 --- a/tests/trace_service.py +++ b/tests/trace_service.py @@ -14,16 +14,20 @@ def Export(self, request, context): self.batches.append(request.resource_spans) return trace_service_pb2.ExportTracePartialSuccess() - def get_span(self): + def get_batch(self): for _ in range(10): if len(self.batches): break time.sleep(0.001) + assert len(self.batches) == 1 + assert len(self.batches[0]) == 1 + return self.batches.pop()[0] - assert len(self.batches) == 1, "No spans received" - span = self.batches[0][0].scope_spans[0].spans.pop() - self.batches.clear() - return span + def get_span(self): + batch = self.get_batch() + assert len(batch.scope_spans) == 1 + assert len(batch.scope_spans[0].spans) == 1 + return batch.scope_spans[0].spans.pop() @pytest.fixture(scope="module")