Skip to content

Commit 25fd6cb

Browse files
fix: use context.attach instead of _context parameter for logfire spans (#57)
1 parent e8c3d33 commit 25fd6cb

2 files changed

Lines changed: 20 additions & 21 deletions

File tree

src/policyengine_api/agent_sandbox.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def configure_logfire(traceparent: str | None = None):
2626

2727
token = os.environ.get("LOGFIRE_TOKEN", "")
2828
if not token:
29-
return None
29+
return
3030

3131
logfire.configure(
3232
service_name="policyengine-agent",
@@ -37,15 +37,15 @@ def configure_logfire(traceparent: str | None = None):
3737

3838
# If traceparent provided, attach to the current context
3939
if traceparent:
40+
from opentelemetry import context
4041
from opentelemetry.trace.propagation.tracecontext import (
4142
TraceContextTextMapPropagator,
4243
)
4344

4445
propagator = TraceContextTextMapPropagator()
4546
ctx = propagator.extract(carrier={"traceparent": traceparent})
46-
return ctx
47+
context.attach(ctx)
4748

48-
return None
4949

5050
SYSTEM_PROMPT = """You are a PolicyEngine assistant that helps users understand tax and benefit policies.
5151
@@ -524,9 +524,9 @@ def run_agent(
524524
"""Run agentic loop to answer a policy question (Modal wrapper)."""
525525
import logfire
526526

527-
ctx = configure_logfire(traceparent)
527+
configure_logfire(traceparent)
528528

529-
with logfire.span("run_agent", call_id=call_id, question=question[:200], _context=ctx):
529+
with logfire.span("run_agent", call_id=call_id, question=question[:200]):
530530
return _run_agent_impl(
531531
question,
532532
api_base_url,

src/policyengine_api/modal_app.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def configure_logfire(service_name: str, traceparent: str | None = None):
7878

7979
token = os.environ.get("LOGFIRE_TOKEN", "")
8080
if not token:
81-
return None
81+
return
8282

8383
logfire.configure(
8484
service_name=service_name,
@@ -89,15 +89,14 @@ def configure_logfire(service_name: str, traceparent: str | None = None):
8989

9090
# If traceparent provided, attach to the current context
9191
if traceparent:
92+
from opentelemetry import context
9293
from opentelemetry.trace.propagation.tracecontext import (
9394
TraceContextTextMapPropagator,
9495
)
9596

9697
propagator = TraceContextTextMapPropagator()
9798
ctx = propagator.extract(carrier={"traceparent": traceparent})
98-
return ctx
99-
100-
return None
99+
context.attach(ctx)
101100

102101

103102
def get_database_url() -> str:
@@ -167,9 +166,9 @@ def simulate_household_uk(
167166
import logfire
168167
from sqlmodel import Session, create_engine
169168

170-
ctx = configure_logfire("policyengine-modal-uk", traceparent)
169+
configure_logfire("policyengine-modal-uk", traceparent)
171170

172-
with logfire.span("simulate_household_uk", job_id=job_id, _context=ctx):
171+
with logfire.span("simulate_household_uk", job_id=job_id):
173172
logfire.info("Starting UK household calculation", job_id=job_id)
174173

175174
database_url = get_database_url()
@@ -298,9 +297,9 @@ def simulate_household_us(
298297
import logfire
299298
from sqlmodel import Session, create_engine
300299

301-
ctx = configure_logfire("policyengine-modal-us", traceparent)
300+
configure_logfire("policyengine-modal-us", traceparent)
302301

303-
with logfire.span("simulate_household_us", job_id=job_id, _context=ctx):
302+
with logfire.span("simulate_household_us", job_id=job_id):
304303
logfire.info("Starting US household calculation", job_id=job_id)
305304

306305
database_url = get_database_url()
@@ -424,9 +423,9 @@ def simulate_economy_uk(simulation_id: str, traceparent: str | None = None) -> N
424423
import logfire
425424
from sqlmodel import Session, create_engine
426425

427-
ctx = configure_logfire("policyengine-modal-uk", traceparent)
426+
configure_logfire("policyengine-modal-uk", traceparent)
428427

429-
with logfire.span("simulate_economy_uk", simulation_id=simulation_id, _context=ctx):
428+
with logfire.span("simulate_economy_uk", simulation_id=simulation_id):
430429
logfire.info("Starting UK economy simulation", simulation_id=simulation_id)
431430

432431
database_url = get_database_url()
@@ -540,9 +539,9 @@ def simulate_economy_us(simulation_id: str, traceparent: str | None = None) -> N
540539
import logfire
541540
from sqlmodel import Session, create_engine
542541

543-
ctx = configure_logfire("policyengine-modal-us", traceparent)
542+
configure_logfire("policyengine-modal-us", traceparent)
544543

545-
with logfire.span("simulate_economy_us", simulation_id=simulation_id, _context=ctx):
544+
with logfire.span("simulate_economy_us", simulation_id=simulation_id):
546545
logfire.info("Starting US economy simulation", simulation_id=simulation_id)
547546

548547
database_url = get_database_url()
@@ -656,9 +655,9 @@ def economy_comparison_uk(job_id: str, traceparent: str | None = None) -> None:
656655
import logfire
657656
from sqlmodel import Session, create_engine
658657

659-
ctx = configure_logfire("policyengine-modal-uk", traceparent)
658+
configure_logfire("policyengine-modal-uk", traceparent)
660659

661-
with logfire.span("economy_comparison_uk", job_id=job_id, _context=ctx):
660+
with logfire.span("economy_comparison_uk", job_id=job_id):
662661
logfire.info("Starting UK economy comparison", job_id=job_id)
663662

664663
database_url = get_database_url()
@@ -888,9 +887,9 @@ def economy_comparison_us(job_id: str, traceparent: str | None = None) -> None:
888887
import logfire
889888
from sqlmodel import Session, create_engine
890889

891-
ctx = configure_logfire("policyengine-modal-us", traceparent)
890+
configure_logfire("policyengine-modal-us", traceparent)
892891

893-
with logfire.span("economy_comparison_us", job_id=job_id, _context=ctx):
892+
with logfire.span("economy_comparison_us", job_id=job_id):
894893
logfire.info("Starting US economy comparison", job_id=job_id)
895894

896895
database_url = get_database_url()

0 commit comments

Comments
 (0)