Skip to content
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

SearchTagValuesV2 use protobuf internally instead of json to reduce latency #3731

Merged
merged 4 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* [FEATURE] TraceQL support for event scope and event:name intrinsic [#3708](https://github.com/grafana/tempo/pull/3708) (@stoewer)
* [FEATURE] Flush blocks to storage from the metrics-generator [#3628](https://github.com/grafana/tempo/pull/3628) [#3691](https://github.com/grafana/tempo/pull/3691) (@mapno)
* [ENHANCEMENT] Tag value lookup use protobuf internally for improved latency [#3731](https://github.com/grafana/tempo/pull/3731) (@mdisibio)
* [ENHANCEMENT] Improve use of OTEL semantic conventions on the service graph [#3711](https://github.com/grafana/tempo/pull/3711) (@zalegrala)

## v2.5.0-rc.1
Expand Down
17 changes: 15 additions & 2 deletions modules/frontend/combiner/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,21 @@ func (c *genericCombiner[T]) AddResponse(r PipelineResponse) error {
}

partial := c.new() // instantiating directly requires additional type constraints. this seemed cleaner: https://stackoverflow.com/questions/69573113/how-can-i-instantiate-a-non-nil-pointer-of-type-argument-with-generic-go
if err := jsonpb.Unmarshal(res.Body, partial); err != nil {
return fmt.Errorf("error unmarshalling response body: %w", err)

switch res.Header.Get(api.HeaderContentType) {
case api.HeaderAcceptProtobuf:
b, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("error reading response body")
}
if err := proto.Unmarshal(b, partial); err != nil {
return fmt.Errorf("error unmarshalling proto response body: %w", err)
}
default:
// Assume json
if err := jsonpb.Unmarshal(res.Body, partial); err != nil {
return fmt.Errorf("error unmarshalling response body: %w", err)
}
}

if err := c.combine(partial, c.current, r); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion modules/frontend/tag_sharder.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ func (s searchTagSharder) buildBackendRequests(ctx context.Context, tenantID str
errFn(err)
return
}
subR.Header.Set(api.HeaderAccept, api.HeaderAcceptProtobuf)
prepareRequestForQueriers(subR, tenantID, parent.URL.Path, subR.URL.Query())

key := cacheKey(keyPrefix, tenantID, hash, int64(searchReq.start()), int64(searchReq.end()), m, startPage, pages)
Expand Down Expand Up @@ -356,7 +357,7 @@ func (s searchTagSharder) buildIngesterRequest(ctx context.Context, tenantID str
if err != nil {
return nil, err
}

subR.Header.Set(api.HeaderAccept, api.HeaderAcceptProtobuf)
prepareRequestForQueriers(subR, tenantID, subR.URL.Path, subR.URL.Query())
return subR, nil
}
Expand Down
70 changes: 43 additions & 27 deletions modules/querier/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,44 +312,33 @@ func (q *Querier) SearchTagValuesV2Handler(w http.ResponseWriter, r *http.Reques
span, ctx := opentracing.StartSpanFromContext(ctx, "Querier.SearchTagValuesHandler")
defer span.Finish()

var resp *tempopb.SearchTagValuesV2Response
var err error

if !isSearchBlock {
req, err := api.ParseSearchTagValuesRequestV2(r)
var req *tempopb.SearchTagValuesRequest
req, err = api.ParseSearchTagValuesRequestV2(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

resp, err := q.SearchTagValuesV2(ctx, req)
if err != nil {
handleError(w, err)
return
}

marshaller := &jsonpb.Marshaler{}
err = marshaller.Marshal(w, resp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resp, err = q.SearchTagValuesV2(ctx, req)
} else {
req, err := api.ParseSearchTagValuesBlockRequestV2(r)
var req *tempopb.SearchTagValuesBlockRequest
req, err = api.ParseSearchTagValuesBlockRequestV2(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
resp, err := q.SearchTagValuesBlocksV2(ctx, req)
if err != nil {
handleError(w, err)
return
}
marshaller := &jsonpb.Marshaler{}
err = marshaller.Marshal(w, resp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resp, err = q.SearchTagValuesBlocksV2(ctx, req)
}
w.Header().Set(api.HeaderContentType, api.HeaderAcceptJSON)

if err != nil {
handleError(w, err)
return
}

writeFormattedContentForRequest(w, r, resp)
}

func (q *Querier) SpanMetricsSummaryHandler(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -482,3 +471,30 @@ func handleError(w http.ResponseWriter, err error) {

http.Error(w, err.Error(), http.StatusInternalServerError)
}

func writeFormattedContentForRequest(w http.ResponseWriter, req *http.Request, m proto.Message) {
switch req.Header.Get(api.HeaderAccept) {
case api.HeaderAcceptProtobuf:
b, err := proto.Marshal(m)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

_, err = w.Write(b)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set(api.HeaderContentType, api.HeaderAcceptProtobuf)
mapno marked this conversation as resolved.
Show resolved Hide resolved
default:
marshaller := &jsonpb.Marshaler{}
err := marshaller.Marshal(w, m)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set(api.HeaderContentType, api.HeaderAcceptJSON)
}
}
Loading