15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/dspy/context.rb', line 15
def with_span(operation:, **attributes)
span_id = SecureRandom.uuid
parent_span_id = current[:span_stack].last
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
span_attributes = {
trace_id: current[:trace_id],
span_id: span_id,
parent_span_id: parent_span_id,
operation: operation,
**attributes
}
DSPy.log('span.start', **span_attributes)
otel_span = nil
if DSPy::Observability.enabled?
otel_span = DSPy::Observability.start_span(operation, span_attributes)
end
current[:span_stack].push(span_id)
begin
result = yield
ensure
current[:span_stack].pop
duration_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000).round(2)
DSPy.log('span.end',
trace_id: current[:trace_id],
span_id: span_id,
duration_ms: duration_ms
)
DSPy::Observability.finish_span(otel_span) if otel_span
end
result
end
|