Skip to content

Commit 76834a1

Browse files
authored
Test Django metrics (#339)
1 parent 54d4f3f commit 76834a1

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

tests/otel_integrations/test_django.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,86 @@
22
from unittest import mock
33

44
import pytest
5+
from dirty_equals import IsInt, IsNumeric
56
from django.http import HttpResponse
67
from django.test import Client
78
from inline_snapshot import snapshot
9+
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
810

911
import logfire
1012
import logfire._internal
1113
import logfire._internal.integrations
1214
import logfire._internal.integrations.django
1315
from logfire.testing import TestExporter
16+
from tests.test_metrics import get_collected_metrics
1417

1518

16-
def test_good_route(client: Client, exporter: TestExporter):
19+
def test_good_route(client: Client, exporter: TestExporter, metrics_reader: InMemoryMetricReader):
1720
logfire.instrument_django()
1821
response: HttpResponse = client.get( # type: ignore
1922
'/django_test_app/123/?very_long_query_param_name=very+long+query+param+value&foo=1'
2023
)
2124
assert response.status_code == 200
2225
assert response.content == b'item_id: 123'
2326

27+
assert get_collected_metrics(metrics_reader) == snapshot(
28+
[
29+
{
30+
'name': 'http.server.active_requests',
31+
'description': 'Number of active HTTP server requests.',
32+
'unit': '{request}',
33+
'data': {
34+
'data_points': [
35+
{
36+
'attributes': {
37+
'http.method': 'GET',
38+
'http.server_name': 'testserver',
39+
'http.scheme': 'http',
40+
'http.flavor': '1.1',
41+
},
42+
'start_time_unix_nano': IsInt(),
43+
'time_unix_nano': IsInt(),
44+
'value': 0,
45+
}
46+
],
47+
'aggregation_temporality': 1,
48+
'is_monotonic': False,
49+
},
50+
},
51+
{
52+
'name': 'http.server.duration',
53+
'description': 'Duration of HTTP server requests.',
54+
'unit': 'ms',
55+
'data': {
56+
'data_points': [
57+
{
58+
'attributes': {
59+
'http.method': 'GET',
60+
'http.server_name': 'testserver',
61+
'http.scheme': 'http',
62+
'net.host.port': 80,
63+
'http.flavor': '1.1',
64+
'http.status_code': 200,
65+
},
66+
'start_time_unix_nano': IsInt(),
67+
'time_unix_nano': IsInt(),
68+
'count': 1,
69+
'sum': IsNumeric(),
70+
'scale': 20,
71+
'zero_count': 0,
72+
'positive': {'offset': IsInt(), 'bucket_counts': [1]},
73+
'negative': {'offset': 0, 'bucket_counts': [0]},
74+
'flags': 0,
75+
'min': IsNumeric(),
76+
'max': IsNumeric(),
77+
}
78+
],
79+
'aggregation_temporality': 1,
80+
},
81+
},
82+
]
83+
)
84+
2485
# TODO route and target should consistently start with /, including in the name/message
2586
assert exporter.exported_spans_as_dict() == snapshot(
2687
[

0 commit comments

Comments
 (0)