Skip to content

Commit c9136f2

Browse files
jimf5p-pautov
andauthored
Verify custom resource attributes support (#32).
Co-authored-by: p-pautov <37922380+p-pautov@users.noreply.github.com>
1 parent 1d25954 commit c9136f2

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

tests/test_otel.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
}
2929
3030
otel_trace on;
31-
otel_service_name test_service;
31+
{{ resource_attrs }}
3232
3333
server {
3434
listen 127.0.0.1:18443 ssl;
@@ -240,7 +240,7 @@ def test_context(client, trace_service, parent, path):
240240

241241
@pytest.mark.parametrize(
242242
"nginx_config",
243-
[({"interval": "200ms", "scheme": "http://"})],
243+
[{"interval": "200ms", "scheme": "http://"}],
244244
indirect=True,
245245
)
246246
@pytest.mark.parametrize("batch_count", [1, 3])
@@ -257,8 +257,34 @@ def test_batches(client, trace_service, batch_count):
257257
assert len(trace_service.batches) == batch_count
258258

259259
for batch in trace_service.batches:
260-
assert get_attr(batch[0].resource, "service.name") == "test_service"
260+
assert (
261+
get_attr(batch[0].resource, "service.name")
262+
== "unknown_service:nginx"
263+
)
261264
assert len(batch[0].scope_spans[0].spans) == batch_size
262265

263266
time.sleep(0.3) # wait for +1 request to be flushed
264267
trace_service.batches.clear()
268+
269+
270+
@pytest.mark.parametrize(
271+
"nginx_config",
272+
[
273+
{
274+
"resource_attrs": """
275+
otel_service_name "test_service";
276+
otel_resource_attr my.name "my name";
277+
otel_resource_attr my.service "my service";
278+
""",
279+
}
280+
],
281+
indirect=True,
282+
)
283+
def test_custom_resource_attributes(client, trace_service):
284+
assert client.get("http://127.0.0.1:18080/ok").status_code == 200
285+
286+
batch = trace_service.get_batch()
287+
288+
assert get_attr(batch.resource, "service.name") == "test_service"
289+
assert get_attr(batch.resource, "my.name") == "my name"
290+
assert get_attr(batch.resource, "my.service") == "my service"

tests/trace_service.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@ def Export(self, request, context):
1414
self.batches.append(request.resource_spans)
1515
return trace_service_pb2.ExportTracePartialSuccess()
1616

17-
def get_span(self):
17+
def get_batch(self):
1818
for _ in range(10):
1919
if len(self.batches):
2020
break
2121
time.sleep(0.001)
22+
assert len(self.batches) == 1
23+
assert len(self.batches[0]) == 1
24+
return self.batches.pop()[0]
2225

23-
assert len(self.batches) == 1, "No spans received"
24-
span = self.batches[0][0].scope_spans[0].spans.pop()
25-
self.batches.clear()
26-
return span
26+
def get_span(self):
27+
batch = self.get_batch()
28+
assert len(batch.scope_spans) == 1
29+
assert len(batch.scope_spans[0].spans) == 1
30+
return batch.scope_spans[0].spans.pop()
2731

2832

2933
@pytest.fixture(scope="module")

0 commit comments

Comments
 (0)