MCPcopy
hub / github.com/grafana/tempo / TraceByID

Method TraceByID

modules/querier/external/client.go:56–112  ·  view source on GitHub ↗

TraceByID forwards a trace-by-ID v2 request to the external endpoint traceID is the trace ID to query startTime and endTime are Unix timestamps in seconds (0 means not specified)

(ctx context.Context, userID string, traceID []byte, startTime, endTime int64)

Source from the content-addressed store, hash-verified

54// traceID is the trace ID to query
55// startTime and endTime are Unix timestamps in seconds (0 means not specified)
56func (c *Client) TraceByID(ctx context.Context, userID string, traceID []byte, startTime, endTime int64) (*tempopb.TraceByIDResponse, error) {
57 start := time.Now()
58 statusCode := "error"
59 defer func() {
60 metricExternalRequestDuration.WithLabelValues(statusCode).Observe(time.Since(start).Seconds())
61 }()
62
63 path := c.externalURL.JoinPath(strings.Replace(api.PathTraces, "{traceID}", hex.EncodeToString(traceID), 1))
64
65 // Add query parameters for start/end times
66 q := path.Query()
67 if startTime != 0 {
68 q.Set("start", strconv.FormatInt(startTime, 10))
69 }
70 if endTime != 0 {
71 q.Set("end", strconv.FormatInt(endTime, 10))
72 }
73 path.RawQuery = q.Encode()
74
75 httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil)
76 if err != nil {
77 return nil, fmt.Errorf("failed to create external request: %w", err)
78 }
79
80 httpReq.Header.Set(api.HeaderAccept, api.HeaderAcceptProtobuf)
81 httpReq.Header.Set(user.OrgIDHeaderName, userID)
82
83 resp, err := c.httpClient.Do(httpReq)
84 if err != nil {
85 return nil, fmt.Errorf("external endpoint request failed: %w", err)
86 }
87 defer resp.Body.Close()
88
89 // Set the status code for the metric tracking in defer
90 statusCode = strconv.Itoa(resp.StatusCode)
91
92 body, err := io.ReadAll(resp.Body)
93 if err != nil {
94 return nil, fmt.Errorf("failed to read external response body: %w", err)
95 }
96
97 if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotFound {
98 return nil, fmt.Errorf("external endpoint returned status %d: %s", resp.StatusCode, string(body))
99 }
100
101 // The external endpoint is expected to return an OTEL trace protobuf (ptrace.Traces).
102 // This is wire-compatible with tempopb.Trace, so we can unmarshal it directly.
103 var trace tempopb.Trace
104 err = trace.Unmarshal(body)
105 if err != nil {
106 return nil, fmt.Errorf("failed to unmarshal external response: %w", err)
107 }
108
109 return &tempopb.TraceByIDResponse{
110 Trace: &trace,
111 }, nil
112}

Callers 2

TestClient_TraceByIDFunction · 0.95
FindTraceByIDMethod · 0.80

Calls 11

UnmarshalMethod · 0.95
EncodeToStringMethod · 0.80
QueryMethod · 0.80
NowMethod · 0.65
ObserveMethod · 0.65
SetMethod · 0.65
EncodeMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.65
ReadAllMethod · 0.65
StringMethod · 0.45

Tested by 1

TestClient_TraceByIDFunction · 0.76