Skip to content

Commit

Permalink
Merge pull request #476 from opensafely-core/trace-migration-fix
Browse files Browse the repository at this point in the history
fix: handle uninitialized traces gracefully
  • Loading branch information
bloodearnest authored Sep 29, 2022
2 parents 6213076 + 3b1f5c5 commit f2bf57d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion jobrunner/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ def start_new_state(job, timestamp_ns, error=None, **attrs):

def record_job_span(job, name, start_time, end_time, error, **attrs):
"""Record a span for a job."""
assert job.trace_context is not None, "job missing trace_context"
# handle migration gracefully
if not job.trace_context:
logger.info(f"not tracing job {job.id} as not initialised")
return

ctx = TraceContextTextMapPropagator().extract(carrier=job.trace_context)
tracer = trace.get_tracer("jobs")

Expand Down
10 changes: 10 additions & 0 deletions tests/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,13 @@ def test_start_new_state(db):
assert spans[-1].name == "ENTER PREPARING"
assert spans[-1].attributes["enter_state"] is True
assert spans[-1].end_time == int(ts + 1e9)


def test_record_job_span_skips_uninitialized_job(db):
job = job_factory()
ts = int(time.time() * 1e9)
job.trace_context = None

tracing.record_job_span(job, "name", ts, ts + 10000, error=None)

assert len(get_trace()) == 0

0 comments on commit f2bf57d

Please sign in to comment.