-
Notifications
You must be signed in to change notification settings - Fork 692
[querier] Support external mode for traceID lookups #6185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
electron0zero
merged 9 commits into
main
from
logiraptor/support-external-traceid-lookup
Jan 13, 2026
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1b9b3ba
[querier] Support `external` mode for traceIDv2
Logiraptor 120f7fc
Update config docs
Logiraptor 22ce578
Update CHANGELOG
Logiraptor 1119f31
Update test
Logiraptor d04c471
Update manifest
Logiraptor dfe6646
Add metrics and traces
Logiraptor 3f65cfb
Update modules/querier/external/client.go
Logiraptor 7d1c116
Address PR feedback
Logiraptor 728796f
make generate-manifest
Logiraptor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package external | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/hex" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/grafana/dskit/user" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/promauto" | ||
| "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" | ||
|
|
||
| "github.com/grafana/tempo/pkg/api" | ||
| "github.com/grafana/tempo/pkg/tempopb" | ||
| ) | ||
|
|
||
| var metricExternalRequestDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{ | ||
| Namespace: "tempo", | ||
| Name: "querier_external_endpoint_request_duration_seconds", | ||
| Help: "Duration of requests to the external endpoint in seconds.", | ||
| Buckets: prometheus.DefBuckets, | ||
| NativeHistogramBucketFactor: 1.1, | ||
| NativeHistogramMaxBucketNumber: 100, | ||
| NativeHistogramMinResetDuration: 1 * time.Hour, | ||
| }, []string{"status_code"}) | ||
|
|
||
| type Client struct { | ||
| httpClient *http.Client | ||
| externalURL *url.URL | ||
| } | ||
|
|
||
| func NewClient(endpoint string, timeout time.Duration) (*Client, error) { | ||
| externalURL, err := url.Parse(endpoint) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid external endpoint URL: %w", err) | ||
| } | ||
|
|
||
| return &Client{ | ||
| httpClient: &http.Client{ | ||
| Timeout: timeout, | ||
| Transport: otelhttp.NewTransport(http.DefaultTransport), | ||
| }, | ||
| externalURL: externalURL, | ||
| }, nil | ||
| } | ||
|
|
||
| // TraceByID forwards a trace-by-ID request to the external endpoint | ||
|
Logiraptor marked this conversation as resolved.
Outdated
|
||
| // traceID is the trace ID to query | ||
| // startTime and endTime are Unix timestamps in seconds (0 means not specified) | ||
| func (c *Client) TraceByID(ctx context.Context, userID string, traceID []byte, startTime, endTime int64) (*tempopb.TraceByIDResponse, error) { | ||
| start := time.Now() | ||
| statusCode := "error" | ||
| defer func() { | ||
| metricExternalRequestDuration.WithLabelValues(statusCode).Observe(time.Since(start).Seconds()) | ||
| }() | ||
|
|
||
| path := c.externalURL.JoinPath(strings.Replace(api.PathTracesV2, "{traceID}", hex.EncodeToString(traceID), 1)) | ||
|
|
||
| // Add query parameters for start/end times | ||
| q := path.Query() | ||
| if startTime != 0 { | ||
| q.Set("start", strconv.FormatInt(startTime, 10)) | ||
| } | ||
| if endTime != 0 { | ||
| q.Set("end", strconv.FormatInt(endTime, 10)) | ||
| } | ||
| path.RawQuery = q.Encode() | ||
|
|
||
| httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create external request: %w", err) | ||
| } | ||
|
|
||
| httpReq.Header.Set(api.HeaderAccept, api.HeaderAcceptProtobuf) | ||
| httpReq.Header.Set(user.OrgIDHeaderName, userID) | ||
|
|
||
| resp, err := c.httpClient.Do(httpReq) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("external endpoint request failed: %w", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| // Set the status code for the metric tracking in defer | ||
| statusCode = strconv.Itoa(resp.StatusCode) | ||
|
|
||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read external response body: %w", err) | ||
| } | ||
|
|
||
| if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotFound { | ||
| return nil, fmt.Errorf("external endpoint returned status %d: %s", resp.StatusCode, string(body)) | ||
| } | ||
|
|
||
| var trace tempopb.Trace | ||
|
Logiraptor marked this conversation as resolved.
|
||
| err = trace.Unmarshal(body) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to unmarshal external response: %w", err) | ||
| } | ||
|
|
||
| return &tempopb.TraceByIDResponse{ | ||
| Trace: &trace, | ||
| }, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package external | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/hex" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/grafana/dskit/user" | ||
| "github.com/grafana/tempo/pkg/api" | ||
| "github.com/grafana/tempo/pkg/tempopb" | ||
| v1_trace "github.com/grafana/tempo/pkg/tempopb/trace/v1" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestClient_TraceByID(t *testing.T) { | ||
| traceID := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10} | ||
| userID := "test-tenant" | ||
| expectedPath := "/api/v2/traces/" + hex.EncodeToString(traceID) | ||
|
|
||
| // Create a test trace to return | ||
| testTrace := &tempopb.Trace{ | ||
| ResourceSpans: []*v1_trace.ResourceSpans{ | ||
| { | ||
| ScopeSpans: []*v1_trace.ScopeSpans{ | ||
| { | ||
| Spans: []*v1_trace.Span{ | ||
| { | ||
| TraceId: traceID, | ||
| SpanId: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, | ||
| Name: "test-span", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // Create httptest server that validates the request | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| // Validate path | ||
| require.Equal(t, expectedPath, r.URL.Path, "path should match expected trace path") | ||
|
|
||
| // Validate headers | ||
| require.Equal(t, api.HeaderAcceptProtobuf, r.Header.Get(api.HeaderAccept), "Accept header should be protobuf") | ||
| require.Equal(t, userID, r.Header.Get(user.OrgIDHeaderName), "X-Scope-OrgID header should match userID") | ||
|
|
||
| // Validate method | ||
| require.Equal(t, http.MethodGet, r.Method, "method should be GET") | ||
|
|
||
| // Validate query parameters | ||
| require.Equal(t, "123", r.URL.Query().Get("start"), "start query parameter should be 123") | ||
| require.Equal(t, "456", r.URL.Query().Get("end"), "end query parameter should be 456") | ||
|
|
||
| // Marshal and return the trace | ||
| traceBytes, err := testTrace.Marshal() | ||
| require.NoError(t, err) | ||
|
|
||
| w.Header().Set("Content-Type", api.HeaderAcceptProtobuf) | ||
| w.WriteHeader(http.StatusOK) | ||
| _, err = w.Write(traceBytes) | ||
| require.NoError(t, err) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| // Create client | ||
| client, err := NewClient(server.URL, 10*time.Second) | ||
| require.NoError(t, err) | ||
|
|
||
| // Call TraceByID | ||
| ctx := context.Background() | ||
| resp, err := client.TraceByID(ctx, userID, traceID, 123, 456) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, resp) | ||
| require.NotNil(t, resp.Trace) | ||
| require.Len(t, resp.Trace.ResourceSpans, 1) | ||
| require.Len(t, resp.Trace.ResourceSpans[0].ScopeSpans, 1) | ||
| require.Len(t, resp.Trace.ResourceSpans[0].ScopeSpans[0].Spans, 1) | ||
| require.Equal(t, "test-span", resp.Trace.ResourceSpans[0].ScopeSpans[0].Spans[0].Name) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need the enabled flag in here as well? I see that we have
external_enabledconfig under the frontend section as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not strictly necessary. Is this what you had in mind? 7d1c116