|
2 | 2 | from unittest import mock |
3 | 3 |
|
4 | 4 | import pytest |
| 5 | +from dirty_equals import IsInt, IsNumeric |
5 | 6 | from django.http import HttpResponse |
6 | 7 | from django.test import Client |
7 | 8 | from inline_snapshot import snapshot |
| 9 | +from opentelemetry.sdk.metrics.export import InMemoryMetricReader |
8 | 10 |
|
9 | 11 | import logfire |
10 | 12 | import logfire._internal |
11 | 13 | import logfire._internal.integrations |
12 | 14 | import logfire._internal.integrations.django |
13 | 15 | from logfire.testing import TestExporter |
| 16 | +from tests.test_metrics import get_collected_metrics |
14 | 17 |
|
15 | 18 |
|
16 | | -def test_good_route(client: Client, exporter: TestExporter): |
| 19 | +def test_good_route(client: Client, exporter: TestExporter, metrics_reader: InMemoryMetricReader): |
17 | 20 | logfire.instrument_django() |
18 | 21 | response: HttpResponse = client.get( # type: ignore |
19 | 22 | '/django_test_app/123/?very_long_query_param_name=very+long+query+param+value&foo=1' |
20 | 23 | ) |
21 | 24 | assert response.status_code == 200 |
22 | 25 | assert response.content == b'item_id: 123' |
23 | 26 |
|
| 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 | + |
24 | 85 | # TODO route and target should consistently start with /, including in the name/message |
25 | 86 | assert exporter.exported_spans_as_dict() == snapshot( |
26 | 87 | [ |
|
0 commit comments