Skip to content

Commit fae7737

Browse files
authored
Use OTEL scopes better, especially instead of tags (#420)
1 parent 6f1a847 commit fae7737

File tree

8 files changed

+15
-45
lines changed

8 files changed

+15
-45
lines changed

logfire/_internal/async_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def log_slow_callbacks(logfire: Logfire, slow_duration: float) -> ContextManager
2626
Inspired by https://gitlab.com/quantlane/libs/aiodebug.
2727
"""
2828
original_run = asyncio.events.Handle._run
29-
logfire = logfire.with_tags('slow-async')
29+
logfire = logfire.with_settings(custom_scope_suffix='asyncio')
3030
timer = logfire.config.ns_timestamp_generator
3131
slow_duration *= ONE_SECOND_IN_NANOSECONDS
3232

logfire/_internal/auto_trace/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def install_auto_tracing(
5656
)
5757

5858
min_duration = int(min_duration * ONE_SECOND_IN_NANOSECONDS)
59+
logfire = logfire.with_settings(custom_scope_suffix='auto_tracing')
5960
finder = LogfireFinder(logfire, modules, min_duration)
6061
sys.meta_path.insert(0, finder)
6162

logfire/_internal/auto_trace/rewrite_ast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def rewrite_ast(
5353
min_duration: int,
5454
) -> ast.AST:
5555
tree = ast.parse(source)
56-
logfire_args = LogfireArgs(logfire_instance._tags + ('auto-trace',), logfire_instance._sample_rate) # type: ignore
56+
logfire_args = LogfireArgs(logfire_instance._tags, logfire_instance._sample_rate) # type: ignore
5757
transformer = AutoTraceTransformer(
5858
logfire_args, logfire_name, filename, module_name, logfire_instance, context_factories, min_duration
5959
)

logfire/_internal/integrations/fastapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def __init__(
158158
],
159159
excluded_urls: str | None,
160160
):
161-
self.logfire_instance = logfire_instance.with_tags('fastapi')
161+
self.logfire_instance = logfire_instance.with_settings(custom_scope_suffix='fastapi')
162162
self.request_attributes_mapper = request_attributes_mapper
163163

164164
# These lines, as well as the `excluded_urls_list.url_disabled` call below, are copied from OTEL.

logfire/integrations/pydantic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from pydantic_core import CoreConfig, CoreSchema
2727

2828

29-
METER = GLOBAL_CONFIG._meter_provider.get_meter('pydantic-plugin-meter') # type: ignore
29+
METER = GLOBAL_CONFIG._meter_provider.get_meter('logfire.pydantic') # type: ignore
3030
validation_counter = METER.create_counter('pydantic.validations')
3131

3232

tests/otel_integrations/test_fastapi.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ def test_path_param(client: TestClient, exporter: TestExporter) -> None:
187187
assert response.status_code == 200
188188
assert response.json() == {'param': 'param_val'}
189189

190+
assert exporter.exported_spans[1].instrumentation_scope.name == 'logfire.fastapi' # type: ignore
191+
190192
span_dicts = exporter.exported_spans_as_dict(_include_pending_spans=True)
191193

192194
# Highlights of the below mega-assert:
@@ -242,7 +244,6 @@ def test_path_param(client: TestClient, exporter: TestExporter) -> None:
242244
'logfire.msg': 'FastAPI arguments',
243245
'logfire.span_type': 'pending_span',
244246
'logfire.pending_parent_id': '0000000000000001',
245-
'logfire.tags': ('fastapi',),
246247
},
247248
},
248249
{
@@ -255,7 +256,6 @@ def test_path_param(client: TestClient, exporter: TestExporter) -> None:
255256
'logfire.msg_template': 'FastAPI arguments',
256257
'logfire.msg': 'FastAPI arguments',
257258
'logfire.span_type': 'span',
258-
'logfire.tags': ('fastapi',),
259259
'logfire.level_num': 5,
260260
},
261261
},
@@ -274,7 +274,6 @@ def test_path_param(client: TestClient, exporter: TestExporter) -> None:
274274
'logfire.msg_template': '{method} {http.route} ({code.function})',
275275
'logfire.span_type': 'pending_span',
276276
'logfire.json_schema': '{"type":"object","properties":{"method":{},"http.route":{}}}',
277-
'logfire.tags': ('fastapi',),
278277
'logfire.msg': 'GET /with_path_param/{param} (with_path_param)',
279278
'logfire.level_num': 5,
280279
'logfire.pending_parent_id': '0000000000000001',
@@ -295,7 +294,6 @@ def test_path_param(client: TestClient, exporter: TestExporter) -> None:
295294
'logfire.msg_template': '{method} {http.route} ({code.function})',
296295
'logfire.span_type': 'span',
297296
'logfire.json_schema': '{"type":"object","properties":{"method":{},"http.route":{}}}',
298-
'logfire.tags': ('fastapi',),
299297
'logfire.msg': 'GET /with_path_param/{param} (with_path_param)',
300298
'logfire.level_num': 5,
301299
},
@@ -461,7 +459,6 @@ def test_fastapi_instrumentation(client: TestClient, exporter: TestExporter) ->
461459
'logfire.msg': 'FastAPI arguments',
462460
'logfire.span_type': 'pending_span',
463461
'logfire.pending_parent_id': '0000000000000003',
464-
'logfire.tags': ('fastapi',),
465462
},
466463
},
467464
{
@@ -472,7 +469,6 @@ def test_fastapi_instrumentation(client: TestClient, exporter: TestExporter) ->
472469
'end_time': 4000000000,
473470
'attributes': {
474471
'logfire.span_type': 'span',
475-
'logfire.tags': ('fastapi',),
476472
'logfire.msg_template': 'FastAPI arguments',
477473
'logfire.msg': 'FastAPI arguments',
478474
'logfire.level_num': 5,
@@ -494,7 +490,6 @@ def test_fastapi_instrumentation(client: TestClient, exporter: TestExporter) ->
494490
'logfire.json_schema': '{"type":"object","properties":{"method":{},"http.route":{}}}',
495491
'logfire.span_type': 'pending_span',
496492
'logfire.msg': 'GET / (homepage)',
497-
'logfire.tags': ('fastapi',),
498493
'logfire.level_num': 5,
499494
'logfire.pending_parent_id': '0000000000000003',
500495
},
@@ -530,7 +525,6 @@ def test_fastapi_instrumentation(client: TestClient, exporter: TestExporter) ->
530525
'logfire.msg_template': '{method} {http.route} ({code.function})',
531526
'logfire.span_type': 'span',
532527
'logfire.json_schema': '{"type":"object","properties":{"method":{},"http.route":{}}}',
533-
'logfire.tags': ('fastapi',),
534528
'logfire.msg': 'GET / (homepage)',
535529
'logfire.level_num': 5,
536530
},
@@ -684,7 +678,6 @@ def test_fastapi_arguments(client: TestClient, exporter: TestExporter) -> None:
684678
},
685679
}
686680
),
687-
'logfire.tags': ('fastapi',),
688681
},
689682
},
690683
{
@@ -791,7 +784,6 @@ def test_get_fastapi_arguments(client: TestClient, exporter: TestExporter) -> No
791784
},
792785
}
793786
),
794-
'logfire.tags': ('fastapi',),
795787
},
796788
},
797789
{
@@ -809,7 +801,6 @@ def test_get_fastapi_arguments(client: TestClient, exporter: TestExporter) -> No
809801
'logfire.msg_template': '{method} {http.route} ({code.function})',
810802
'logfire.msg': 'GET /other (other_route)',
811803
'logfire.json_schema': '{"type":"object","properties":{"method":{},"http.route":{}}}',
812-
'logfire.tags': ('fastapi',),
813804
'logfire.level_num': 5,
814805
'logfire.span_type': 'span',
815806
},
@@ -918,7 +909,6 @@ def test_first_lvl_subapp_fastapi_arguments(client: TestClient, exporter: TestEx
918909
},
919910
}
920911
),
921-
'logfire.tags': ('fastapi',),
922912
},
923913
},
924914
{
@@ -936,7 +926,6 @@ def test_first_lvl_subapp_fastapi_arguments(client: TestClient, exporter: TestEx
936926
'logfire.msg_template': '{method} {http.route} ({code.function})',
937927
'logfire.msg': 'GET /other (other_route)',
938928
'logfire.json_schema': '{"type":"object","properties":{"method":{},"http.route":{}}}',
939-
'logfire.tags': ('fastapi',),
940929
'logfire.level_num': 5,
941930
'logfire.span_type': 'span',
942931
},
@@ -1045,7 +1034,6 @@ def test_second_lvl_subapp_fastapi_arguments(client: TestClient, exporter: TestE
10451034
},
10461035
}
10471036
),
1048-
'logfire.tags': ('fastapi',),
10491037
},
10501038
},
10511039
{
@@ -1063,7 +1051,6 @@ def test_second_lvl_subapp_fastapi_arguments(client: TestClient, exporter: TestE
10631051
'logfire.msg_template': '{method} {http.route} ({code.function})',
10641052
'logfire.msg': 'GET /other (other_route)',
10651053
'logfire.json_schema': '{"type":"object","properties":{"method":{},"http.route":{}}}',
1066-
'logfire.tags': ('fastapi',),
10671054
'logfire.level_num': 5,
10681055
'logfire.span_type': 'span',
10691056
},
@@ -1149,7 +1136,6 @@ def test_fastapi_unhandled_exception(client: TestClient, exporter: TestExporter)
11491136
'attributes': {
11501137
'logfire.msg_template': 'FastAPI arguments',
11511138
'logfire.msg': 'FastAPI arguments',
1152-
'logfire.tags': ('fastapi',),
11531139
'logfire.span_type': 'span',
11541140
'logfire.level_num': 5,
11551141
},
@@ -1169,7 +1155,6 @@ def test_fastapi_unhandled_exception(client: TestClient, exporter: TestExporter)
11691155
'logfire.msg_template': '{method} {http.route} ({code.function})',
11701156
'logfire.msg': 'GET /exception (exception)',
11711157
'logfire.json_schema': '{"type":"object","properties":{"method":{},"http.route":{}}}',
1172-
'logfire.tags': ('fastapi',),
11731158
'logfire.span_type': 'span',
11741159
'logfire.level_num': 17,
11751160
},
@@ -1251,7 +1236,6 @@ def test_fastapi_handled_exception(client: TestClient, exporter: TestExporter) -
12511236
'attributes': {
12521237
'logfire.msg_template': 'FastAPI arguments',
12531238
'logfire.msg': 'FastAPI arguments',
1254-
'logfire.tags': ('fastapi',),
12551239
'logfire.span_type': 'span',
12561240
'logfire.level_num': 5,
12571241
},
@@ -1271,7 +1255,6 @@ def test_fastapi_handled_exception(client: TestClient, exporter: TestExporter) -
12711255
'logfire.msg_template': '{method} {http.route} ({code.function})',
12721256
'logfire.msg': 'GET /validation_error (validation_error)',
12731257
'logfire.json_schema': '{"type":"object","properties":{"method":{},"http.route":{}}}',
1274-
'logfire.tags': ('fastapi',),
12751258
'logfire.span_type': 'span',
12761259
'logfire.level_num': 17,
12771260
},
@@ -1390,7 +1373,6 @@ def test_scrubbing(client: TestClient, exporter: TestExporter) -> None:
13901373
'fastapi.route.name': 'secret',
13911374
'logfire.null_args': ('fastapi.route.operation_id',),
13921375
'logfire.json_schema': '{"type":"object","properties":{"values":{"type":"object"},"errors":{"type":"array"},"custom_attr":{},"http.method":{},"http.route":{},"fastapi.route.name":{},"fastapi.route.operation_id":{}}}',
1393-
'logfire.tags': ('fastapi',),
13941376
'logfire.scrubbed': IsJson(
13951377
[
13961378
{'path': ['attributes', 'values', 'path_param'], 'matched_substring': 'auth'},
@@ -1415,7 +1397,6 @@ def test_scrubbing(client: TestClient, exporter: TestExporter) -> None:
14151397
'logfire.msg_template': '{method} {http.route} ({code.function})',
14161398
'logfire.msg': 'GET /secret/{path_param} (get_secret)',
14171399
'logfire.json_schema': '{"type":"object","properties":{"method":{},"http.route":{}}}',
1418-
'logfire.tags': ('fastapi',),
14191400
'logfire.level_num': 5,
14201401
'logfire.span_type': 'span',
14211402
},
@@ -1541,7 +1522,6 @@ def test_request_hooks_without_send_receiev_spans(exporter: TestExporter):
15411522
'attributes': {
15421523
'logfire.msg_template': 'FastAPI arguments',
15431524
'logfire.msg': 'FastAPI arguments',
1544-
'logfire.tags': ('fastapi',),
15451525
'logfire.span_type': 'span',
15461526
'values': '{}',
15471527
'errors': '[]',
@@ -1583,7 +1563,6 @@ def test_request_hooks_without_send_receiev_spans(exporter: TestExporter):
15831563
'logfire.msg_template': '{method} {http.route} ({code.function})',
15841564
'logfire.msg': 'POST /echo_body (echo_body)',
15851565
'logfire.json_schema': '{"type":"object","properties":{"method":{},"http.route":{}}}',
1586-
'logfire.tags': ('fastapi',),
15871566
'logfire.level_num': 5,
15881567
'logfire.span_type': 'span',
15891568
},
@@ -1686,7 +1665,6 @@ def test_request_hooks_with_send_receive_spans(exporter: TestExporter):
16861665
'attributes': {
16871666
'logfire.msg_template': 'FastAPI arguments',
16881667
'logfire.msg': 'FastAPI arguments',
1689-
'logfire.tags': ('fastapi',),
16901668
'logfire.span_type': 'span',
16911669
'values': '{}',
16921670
'errors': '[]',
@@ -1741,7 +1719,6 @@ def test_request_hooks_with_send_receive_spans(exporter: TestExporter):
17411719
'logfire.msg_template': '{method} {http.route} ({code.function})',
17421720
'logfire.msg': 'POST /echo_body (echo_body)',
17431721
'logfire.json_schema': '{"type":"object","properties":{"method":{},"http.route":{}}}',
1744-
'logfire.tags': ('fastapi',),
17451722
'logfire.level_num': 5,
17461723
'logfire.span_type': 'span',
17471724
},

tests/test_auto_trace.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pytest
88
from inline_snapshot import snapshot
99

10+
import logfire
1011
from logfire import DEFAULT_LOGFIRE_INSTANCE, AutoTraceModule, install_auto_tracing
1112
from logfire._internal.auto_trace import (
1213
AutoTraceModuleAlreadyImportedException,
@@ -21,7 +22,7 @@
2122
def test_auto_trace_sample(exporter: TestExporter) -> None:
2223
meta_path = sys.meta_path.copy()
2324

24-
install_auto_tracing('tests.auto_trace_samples')
25+
logfire.with_tags('testing', 'auto-tracing').install_auto_tracing('tests.auto_trace_samples')
2526
# Check that having multiple LogfireFinders doesn't break things
2627
install_auto_tracing('tests.blablabla')
2728

@@ -50,6 +51,8 @@ def test_auto_trace_sample(exporter: TestExporter) -> None:
5051
with pytest.raises(IndexError): # foo.bar intentionally raises an error to test that it's recorded below
5152
asyncio.run(foo.bar())
5253

54+
assert exporter.exported_spans[0].instrumentation_scope.name == 'logfire.auto_tracing' # type: ignore
55+
5356
assert exporter.exported_spans_as_dict(_include_pending_spans=True) == snapshot(
5457
[
5558
{
@@ -63,9 +66,9 @@ def test_auto_trace_sample(exporter: TestExporter) -> None:
6366
'code.lineno': 123,
6467
'code.function': 'bar',
6568
'logfire.msg_template': 'Calling tests.auto_trace_samples.foo.bar',
69+
'logfire.tags': ('testing', 'auto-tracing'),
6670
'logfire.msg': 'Calling tests.auto_trace_samples.foo.bar',
6771
'logfire.span_type': 'pending_span',
68-
'logfire.tags': ('auto-trace',),
6972
'logfire.pending_parent_id': '0000000000000000',
7073
},
7174
},
@@ -111,8 +114,8 @@ def test_auto_trace_sample(exporter: TestExporter) -> None:
111114
'code.lineno': 123,
112115
'code.function': 'bar',
113116
'logfire.msg_template': 'Calling tests.auto_trace_samples.foo.bar',
117+
'logfire.tags': ('testing', 'auto-tracing'),
114118
'logfire.span_type': 'span',
115-
'logfire.tags': ('auto-trace',),
116119
'logfire.msg': 'Calling tests.auto_trace_samples.foo.bar',
117120
'logfire.level_num': 17,
118121
},
@@ -256,7 +259,6 @@ def method4(self):
256259
'code.lineno': 8,
257260
'code.function': 'func.<locals>.Class.method',
258261
'logfire.msg_template': 'Calling module.name.func.<locals>.Class.method',
259-
'logfire.tags': ('auto-trace',),
260262
},
261263
),
262264
(
@@ -266,7 +268,6 @@ def method4(self):
266268
'code.lineno': 16,
267269
'code.function': 'func.<locals>.Class.method2.<locals>.Class2.method3',
268270
'logfire.msg_template': 'Calling module.name.func.<locals>.Class.method2.<locals>.Class2.method3',
269-
'logfire.tags': ('auto-trace',),
270271
},
271272
),
272273
(
@@ -276,7 +277,6 @@ def method4(self):
276277
'code.lineno': 12,
277278
'code.function': 'func.<locals>.Class.method2',
278279
'logfire.msg_template': 'Calling module.name.func.<locals>.Class.method2',
279-
'logfire.tags': ('auto-trace',),
280280
},
281281
),
282282
(
@@ -286,7 +286,6 @@ def method4(self):
286286
'code.lineno': 2,
287287
'code.function': 'func',
288288
'logfire.msg_template': 'Calling module.name.func',
289-
'logfire.tags': ('auto-trace',),
290289
},
291290
),
292291
(
@@ -296,7 +295,6 @@ def method4(self):
296295
'code.lineno': 26,
297296
'code.function': 'Class3.method4',
298297
'logfire.msg_template': 'Calling module.name.Class3.method4',
299-
'logfire.tags': ('auto-trace',),
300298
},
301299
),
302300
]
@@ -468,7 +466,6 @@ def test_min_duration(exporter: TestExporter):
468466
'code.lineno': 123,
469467
'code.function': 'func2',
470468
'logfire.msg_template': 'Calling tests.auto_trace_samples.simple_nesting.func2',
471-
'logfire.tags': ('auto-trace',),
472469
'logfire.span_type': 'span',
473470
'logfire.msg': 'Calling tests.auto_trace_samples.simple_nesting.func2',
474471
},
@@ -484,7 +481,6 @@ def test_min_duration(exporter: TestExporter):
484481
'code.lineno': 123,
485482
'code.function': 'func1',
486483
'logfire.msg_template': 'Calling tests.auto_trace_samples.simple_nesting.func1',
487-
'logfire.tags': ('auto-trace',),
488484
'logfire.span_type': 'span',
489485
'logfire.msg': 'Calling tests.auto_trace_samples.simple_nesting.func1',
490486
},

0 commit comments

Comments
 (0)