Skip to content

Commit da7b2d7

Browse files
committed
pr comments: Address comments for tracing module
- Make newrelic import explicit with clear error message for missing dependency - Remove tracing re-exports from agave.core to avoid forcing newrelic installation - Use Optional and Union for Python 3.9 compatibility instead of PEP 604 syntax and future module - Add specific type hints dict[str, str] for trace headers - Add trailing newline to requirements-test.txt
1 parent ceff470 commit da7b2d7

4 files changed

Lines changed: 25 additions & 40 deletions

File tree

agave/core/__init__.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
1-
from .tracing import (
2-
TRACE_HEADERS_KEY,
3-
accept_trace_from_queue,
4-
accept_trace_headers,
5-
add_custom_attribute,
6-
background_task,
7-
get_trace_headers,
8-
inject_trace_headers,
9-
trace_attributes,
10-
)
11-
12-
__all__ = [
13-
'TRACE_HEADERS_KEY',
14-
'accept_trace_from_queue',
15-
'accept_trace_headers',
16-
'add_custom_attribute',
17-
'background_task',
18-
'get_trace_headers',
19-
'inject_trace_headers',
20-
'trace_attributes',
21-
]
1+
# Tracing utilities live in agave.core.tracing.
2+
# We don't export them here because we didn't want to force everyone to install newrelic.
3+
# Just import directly when needed from agave.core.tracing import ...

agave/core/tracing.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
from __future__ import annotations
2-
31
import inspect
42
from contextlib import contextmanager
53
from functools import wraps
6-
from typing import Any, Callable, Optional
7-
8-
import newrelic.agent
4+
from typing import Any, Callable, Optional, Union
5+
6+
try:
7+
import newrelic.agent
8+
except ImportError: # pragma: no cover
9+
raise ImportError(
10+
"You must install agave with [tracing] option.\n"
11+
"You can install it with: pip install agave[tracing]"
12+
)
913

1014
# trace headers key
1115
TRACE_HEADERS_KEY = "_nr_trace_headers"
@@ -15,7 +19,7 @@
1519
def background_task(
1620
name: str,
1721
group: str = "Task",
18-
trace_headers: Optional[dict] = None,
22+
trace_headers: Optional[dict[str, str]] = None,
1923
):
2024
with newrelic.agent.BackgroundTask(
2125
application=newrelic.agent.application(),
@@ -27,14 +31,14 @@ def background_task(
2731
yield
2832

2933

30-
def get_trace_headers() -> dict:
34+
def get_trace_headers() -> dict[str, str]:
3135
headers_list: list = []
3236
newrelic.agent.insert_distributed_trace_headers(headers_list)
3337
return dict(headers_list)
3438

3539

3640
def accept_trace_headers(
37-
headers: dict | None, transport_type: str = "HTTP"
41+
headers: Optional[dict[str, str]], transport_type: str = "HTTP"
3842
) -> None:
3943
"""
4044
Accept incoming trace headers to continue a distributed trace.
@@ -136,7 +140,7 @@ def sync_wrapper(*args, **kwargs):
136140
return decorator
137141

138142

139-
def trace_attributes(**extractors: Callable | str):
143+
def trace_attributes(**extractors: Union[Callable, str]):
140144
"""
141145
Decorator to add custom attributes to New Relic traces.
142146

requirements-test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ moto[server]==5.0.26
1212
pytest-vcr==1.0.2
1313
pytest-asyncio==0.18.*
1414
typing_extensions==4.12.2
15-
newrelic==11.2.0
15+
newrelic==11.2.0

tests/core/test_tracing.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from __future__ import annotations
2-
31
import asyncio
2+
from typing import Optional
43
from unittest.mock import patch
54

65
from agave.core.tracing import (
@@ -131,7 +130,7 @@ async def my_task(data: dict):
131130

132131
def test_inject_trace_headers_async_keyword_arg():
133132
@inject_trace_headers()
134-
async def my_func(_url: str, trace_headers: dict | None = None):
133+
async def my_func(_url: str, trace_headers: Optional[dict] = None):
135134
return trace_headers
136135

137136
with patch(
@@ -144,7 +143,7 @@ async def my_func(_url: str, trace_headers: dict | None = None):
144143

145144
def test_inject_trace_headers_merges_existing_headers_async():
146145
@inject_trace_headers()
147-
async def my_func(_url: str, trace_headers: dict | None = None):
146+
async def my_func(_url: str, trace_headers: Optional[dict] = None):
148147
return trace_headers
149148

150149
with patch(
@@ -161,7 +160,7 @@ def test_inject_trace_headers_handles_positional_headers_async():
161160
"""Test that positional args don't cause 'multiple values' error."""
162161

163162
@inject_trace_headers("headers")
164-
async def my_func(_url: str, headers: dict | None = None):
163+
async def my_func(_url: str, headers: Optional[dict] = None):
165164
return headers
166165

167166
with patch(
@@ -177,7 +176,7 @@ async def my_func(_url: str, headers: dict | None = None):
177176

178177
def test_inject_trace_headers_sync_keyword_arg():
179178
@inject_trace_headers()
180-
def my_func(_url: str, trace_headers: dict | None = None):
179+
def my_func(_url: str, trace_headers: Optional[dict] = None):
181180
return trace_headers
182181

183182
with patch(
@@ -190,7 +189,7 @@ def my_func(_url: str, trace_headers: dict | None = None):
190189

191190
def test_inject_trace_headers_merges_existing_headers_sync():
192191
@inject_trace_headers()
193-
def my_func(_url: str, trace_headers: dict | None = None):
192+
def my_func(_url: str, trace_headers: Optional[dict] = None):
194193
return trace_headers
195194

196195
with patch(
@@ -207,7 +206,7 @@ def test_inject_trace_headers_handles_positional_headers_sync():
207206
"""Test that positional args don't cause 'multiple values' error."""
208207

209208
@inject_trace_headers("headers")
210-
def my_func(_url: str, headers: dict | None = None):
209+
def my_func(_url: str, headers: Optional[dict] = None):
211210
return headers
212211

213212
with patch(
@@ -221,7 +220,7 @@ def my_func(_url: str, headers: dict | None = None):
221220

222221
def test_inject_trace_headers_custom_param_name():
223222
@inject_trace_headers("custom_headers")
224-
def my_func(_url: str, custom_headers: dict | None = None):
223+
def my_func(_url: str, custom_headers: Optional[dict] = None):
225224
return custom_headers
226225

227226
with patch(

0 commit comments

Comments
 (0)