Skip to content

Commit d3db7ec

Browse files
authored
Adds caller_event_id to spansummaries same as parent_span_id (#2215)
We need to add the `caller_event_id` from startSpan to populate spans represented in the spanSummaries to have a full picture of the flow of the trace. Context: CallerEventId We receive the CallerEventId as part of the SpanStart event. It represents the unique ID of the specific event in the parent span (e.g., the specific API call site) that triggered the creation of this new span. Capturing this allows the trace viewer to link the new span not just to a parent span, but to the exact causal event within that parent.
1 parent d888848 commit d3db7ec

File tree

7 files changed

+48
-29
lines changed

7 files changed

+48
-29
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE trace_span_index ADD COLUMN caller_event_id INTEGER NULL;

cli/daemon/engine/trace2/sqlite/read.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (s *Store) List(ctx context.Context, q *trace2.Query, iter trace2.ListEntry
4747
SELECT
4848
trace_id, span_id, started_at, span_type, is_root, service_name, endpoint_name,
4949
topic_name, subscription_name, message_id, is_error, test_skipped, duration_nanos,
50-
src_file, src_line, parent_span_id
50+
src_file, src_line, parent_span_id, caller_event_id
5151
FROM trace_span_index
5252
WHERE app_id = $1 AND has_response AND is_root AND span_type != $2 `+extraWhereClause+`
5353
ORDER BY started_at DESC
@@ -70,7 +70,8 @@ func (s *Store) List(ctx context.Context, q *trace2.Query, iter trace2.ListEntry
7070
err := rows.Scan(
7171
&t.TraceId, &t.SpanId, &startedAt, &t.Type, &t.IsRoot, &t.ServiceName, &t.EndpointName,
7272
&t.TopicName, &t.SubscriptionName, &t.MessageId, &t.IsError, &t.TestSkipped,
73-
&t.DurationNanos, &t.SrcFile, &t.SrcLine, &t.ParentSpanId)
73+
&t.TopicName, &t.SubscriptionName, &t.MessageId, &t.IsError, &t.TestSkipped,
74+
&t.DurationNanos, &t.SrcFile, &t.SrcLine, &t.ParentSpanId, &t.CallerEventId)
7475
if err != nil {
7576
return errors.Wrap(err, "scan trace")
7677
}
@@ -94,14 +95,16 @@ func (s *Store) emitCompleteSpanToListeners(ctx context.Context, appID, traceID,
9495
SELECT
9596
trace_id, span_id, started_at, span_type, is_root, service_name, endpoint_name,
9697
topic_name, subscription_name, message_id, is_error, test_skipped, duration_nanos,
97-
src_file, src_line, parent_span_id
98+
topic_name, subscription_name, message_id, is_error, test_skipped, duration_nanos,
99+
src_file, src_line, parent_span_id, caller_event_id
98100
FROM trace_span_index
99101
WHERE app_id = ? AND trace_id = ? AND span_id = ? AND has_response AND is_root AND span_type != ?
100102
ORDER BY started_at DESC
101103
`, appID, traceID, spanID, tracepb2.SpanSummary_AUTH).Scan(
102104
&t.TraceId, &t.SpanId, &startedAt, &t.Type, &t.IsRoot, &t.ServiceName, &t.EndpointName,
103105
&t.TopicName, &t.SubscriptionName, &t.MessageId, &t.IsError, &t.TestSkipped,
104-
&t.DurationNanos, &t.SrcFile, &t.SrcLine, &t.ParentSpanId)
106+
&t.TopicName, &t.SubscriptionName, &t.MessageId, &t.IsError, &t.TestSkipped,
107+
&t.DurationNanos, &t.SrcFile, &t.SrcLine, &t.ParentSpanId, &t.CallerEventId)
105108
if errors.Is(err, sql.ErrNoRows) {
106109
return
107110
} else if err != nil {

cli/daemon/engine/trace2/sqlite/write.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,19 @@ func (s *Store) updateSpanStartIndex(ctx context.Context, meta *trace2.Meta, ev
184184
}
185185
_, err := s.db.ExecContext(ctx, `
186186
INSERT INTO trace_span_index (
187-
app_id, trace_id, span_id, span_type, started_at, is_root, service_name, endpoint_name, external_request_id, parent_span_id,
187+
app_id, trace_id, span_id, span_type, started_at, is_root, service_name, endpoint_name, external_request_id, parent_span_id, caller_event_id,
188188
has_response, test_skipped
189-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, false, false)
189+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, false, false)
190190
ON CONFLICT (trace_id, span_id) DO UPDATE SET
191191
is_root = excluded.is_root,
192192
service_name = excluded.service_name,
193193
endpoint_name = excluded.endpoint_name,
194194
external_request_id = excluded.external_request_id,
195-
parent_span_id = excluded.parent_span_id
195+
parent_span_id = excluded.parent_span_id,
196+
caller_event_id = excluded.caller_event_id
196197
`, meta.AppID, encodeTraceID(ev.TraceId), encodeSpanID(ev.SpanId),
197198
tracepbcli.SpanSummary_REQUEST, ev.EventTime.AsTime().UnixNano(),
198-
isRoot, req.ServiceName, req.EndpointName, extRequestID, parentSpanID)
199+
isRoot, req.ServiceName, req.EndpointName, extRequestID, parentSpanID, start.CallerEventId)
199200
if err != nil {
200201
return errors.Wrap(err, "insert trace span event")
201202
}
@@ -210,17 +211,18 @@ func (s *Store) updateSpanStartIndex(ctx context.Context, meta *trace2.Meta, ev
210211
}
211212
_, err := s.db.ExecContext(ctx, `
212213
INSERT INTO trace_span_index (
213-
app_id, trace_id, span_id, span_type, started_at, is_root, service_name, endpoint_name, parent_span_id,
214+
app_id, trace_id, span_id, span_type, started_at, is_root, service_name, endpoint_name, parent_span_id, caller_event_id,
214215
has_response, test_skipped
215-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, false, false)
216+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, false, false)
216217
ON CONFLICT (trace_id, span_id) DO UPDATE SET
217218
is_root = excluded.is_root,
218219
service_name = excluded.service_name,
219220
endpoint_name = excluded.endpoint_name,
220-
parent_span_id = excluded.parent_span_id
221+
parent_span_id = excluded.parent_span_id,
222+
caller_event_id = excluded.caller_event_id
221223
`, meta.AppID, encodeTraceID(ev.TraceId), encodeSpanID(ev.SpanId),
222224
tracepbcli.SpanSummary_AUTH, ev.EventTime.AsTime().UnixNano(),
223-
isRoot, auth.ServiceName, auth.EndpointName, parentSpanID)
225+
isRoot, auth.ServiceName, auth.EndpointName, parentSpanID, start.CallerEventId)
224226
if err != nil {
225227
return errors.Wrap(err, "insert trace span event")
226228
}
@@ -236,19 +238,20 @@ func (s *Store) updateSpanStartIndex(ctx context.Context, meta *trace2.Meta, ev
236238
_, err := s.db.ExecContext(ctx, `
237239
INSERT INTO trace_span_index (
238240
app_id, trace_id, span_id, span_type, started_at, is_root, service_name,
239-
topic_name, subscription_name, message_id, parent_span_id,
241+
topic_name, subscription_name, message_id, parent_span_id, caller_event_id,
240242
has_response, test_skipped
241-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, false, false, ?)
243+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, false, false)
242244
ON CONFLICT (trace_id, span_id) DO UPDATE SET
243245
is_root = excluded.is_root,
244246
service_name = excluded.service_name,
245247
topic_name = excluded.topic_name,
246248
subscription_name = excluded.subscription_name,
247249
message_id = excluded.message_id,
248-
parent_span_id = excluded.parent_span_id
250+
parent_span_id = excluded.parent_span_id,
251+
caller_event_id = excluded.caller_event_id
249252
`, meta.AppID, encodeTraceID(ev.TraceId), encodeSpanID(ev.SpanId),
250253
tracepbcli.SpanSummary_PUBSUB_MESSAGE, ev.EventTime.AsTime().UnixNano(),
251-
isRoot, msg.ServiceName, msg.TopicName, msg.SubscriptionName, msg.MessageId, parentSpanID)
254+
isRoot, msg.ServiceName, msg.TopicName, msg.SubscriptionName, msg.MessageId, parentSpanID, start.CallerEventId)
252255
if err != nil {
253256
return errors.Wrap(err, "insert trace span event")
254257
}
@@ -263,17 +266,18 @@ func (s *Store) updateSpanStartIndex(ctx context.Context, meta *trace2.Meta, ev
263266
}
264267
_, err := s.db.ExecContext(ctx, `
265268
INSERT INTO trace_span_index (
266-
app_id, trace_id, span_id, span_type, started_at, is_root, service_name, endpoint_name, user_id, src_file, src_line, parent_span_id,
269+
app_id, trace_id, span_id, span_type, started_at, is_root, service_name, endpoint_name, user_id, src_file, src_line, parent_span_id, caller_event_id,
267270
has_response, test_skipped
268-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, false, false, ?)
271+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, false, false)
269272
ON CONFLICT (trace_id, span_id) DO UPDATE SET
270273
is_root = excluded.is_root,
271274
service_name = excluded.service_name,
272275
endpoint_name = excluded.endpoint_name,
273-
parent_span_id = excluded.parent_span_id
276+
parent_span_id = excluded.parent_span_id,
277+
caller_event_id = excluded.caller_event_id
274278
`, meta.AppID, encodeTraceID(ev.TraceId), encodeSpanID(ev.SpanId),
275279
tracepbcli.SpanSummary_TEST, ev.EventTime.AsTime().UnixNano(),
276-
isRoot, msg.ServiceName, msg.TestName, msg.Uid, msg.TestFile, msg.TestLine, parentSpanID)
280+
isRoot, msg.ServiceName, msg.TestName, msg.Uid, msg.TestFile, msg.TestLine, parentSpanID, start.CallerEventId)
277281
if err != nil {
278282
return errors.Wrap(err, "insert trace span event")
279283
}

proto/encore/engine/trace2/trace2.pb.go

Lines changed: 17 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/encore/engine/trace2/trace2.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ message SpanSummary {
2525
optional string src_file = 15; // the source file where the span was started (if available)
2626
optional uint32 src_line = 16; // the source line where the span was started (if available)
2727
optional string parent_span_id = 17; // parent of the span if it's a child, if this is not populated then it's a root
28+
optional uint64 caller_event_id = 18; // the event id of the call that started this span
2829

2930
enum SpanType {
3031
UNKNOWN = 0;

runtimes/go/appruntime/infrasdk/metrics/prometheus/prompb/remote.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

runtimes/go/appruntime/infrasdk/metrics/prometheus/prompb/types.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)