-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix an inconsistency in wal search (#1548)
* Move flatbuffer search extract methods from distributor to reuseable location * Move trace test suite to reusable location * Run trace search tests against wal and backend search blocks. Fix wal header tag check to be substring * lint * changelog * review feedback
- Loading branch information
Showing
10 changed files
with
427 additions
and
329 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,113 +1,14 @@ | ||
package distributor | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/grafana/tempo/pkg/model/trace" | ||
"github.com/grafana/tempo/pkg/tempofb" | ||
"github.com/grafana/tempo/pkg/tempopb" | ||
common_v1 "github.com/grafana/tempo/pkg/tempopb/common/v1" | ||
) | ||
|
||
type extractTagFunc func(tag string) bool | ||
import "github.com/grafana/tempo/pkg/model/trace" | ||
|
||
// extractSearchDataAll returns flatbuffer search data for every trace. | ||
func extractSearchDataAll(traces []*rebatchedTrace, extractTag extractTagFunc) [][]byte { | ||
func extractSearchDataAll(traces []*rebatchedTrace, extractTag trace.ExtractTagFunc) [][]byte { | ||
headers := make([][]byte, len(traces)) | ||
|
||
for i, t := range traces { | ||
headers[i] = extractSearchData(t.trace, t.id, extractTag) | ||
headers[i] = trace.ExtractSearchData(t.trace, t.id, extractTag) | ||
} | ||
|
||
return headers | ||
} | ||
|
||
// extractSearchData returns the flatbuffer search data for the given trace. It is extracted here | ||
// in the distributor because this is the only place on the ingest path where the trace is available | ||
// in object form. | ||
func extractSearchData(tr *tempopb.Trace, id []byte, extractTag extractTagFunc) []byte { | ||
data := &tempofb.SearchEntryMutable{} | ||
|
||
data.TraceID = id | ||
|
||
for _, b := range tr.Batches { | ||
// Batch attrs | ||
if b.Resource != nil { | ||
for _, a := range b.Resource.Attributes { | ||
if !extractTag(a.Key) { | ||
continue | ||
} | ||
if s, ok := extractValueAsString(a.Value); ok { | ||
data.AddTag(a.Key, s) | ||
} | ||
} | ||
} | ||
|
||
for _, ils := range b.InstrumentationLibrarySpans { | ||
for _, s := range ils.Spans { | ||
|
||
// Root span | ||
if len(s.ParentSpanId) == 0 { | ||
|
||
// Collect root.name | ||
data.AddTag(trace.RootSpanNameTag, s.Name) | ||
|
||
// Collect root.service.name | ||
if b.Resource != nil { | ||
for _, a := range b.Resource.Attributes { | ||
if a.Key == trace.ServiceNameTag { | ||
if s, ok := extractValueAsString(a.Value); ok { | ||
data.AddTag(trace.RootServiceNameTag, s) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Collect for any spans | ||
data.AddTag(trace.SpanNameTag, s.Name) | ||
if s.Status != nil { | ||
data.AddTag(trace.StatusCodeTag, strconv.Itoa(int(s.Status.Code))) | ||
} | ||
data.SetStartTimeUnixNano(s.StartTimeUnixNano) | ||
data.SetEndTimeUnixNano(s.EndTimeUnixNano) | ||
|
||
for _, a := range s.Attributes { | ||
if !extractTag(a.Key) { | ||
continue | ||
} | ||
if s, ok := extractValueAsString(a.Value); ok { | ||
data.AddTag(a.Key, s) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return data.ToBytes() | ||
} | ||
|
||
func extractValueAsString(v *common_v1.AnyValue) (s string, ok bool) { | ||
vv := v.GetValue() | ||
if vv == nil { | ||
return "", false | ||
} | ||
|
||
if s, ok := vv.(*common_v1.AnyValue_StringValue); ok { | ||
return s.StringValue, true | ||
} | ||
|
||
if b, ok := vv.(*common_v1.AnyValue_BoolValue); ok { | ||
return strconv.FormatBool(b.BoolValue), true | ||
} | ||
|
||
if i, ok := vv.(*common_v1.AnyValue_IntValue); ok { | ||
return strconv.FormatInt(i.IntValue, 10), true | ||
} | ||
|
||
if d, ok := vv.(*common_v1.AnyValue_DoubleValue); ok { | ||
return strconv.FormatFloat(d.DoubleValue, 'g', -1, 64), true | ||
} | ||
|
||
return "", false | ||
} |
This file contains 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,101 @@ | ||
package trace | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/grafana/tempo/pkg/tempofb" | ||
"github.com/grafana/tempo/pkg/tempopb" | ||
common_v1 "github.com/grafana/tempo/pkg/tempopb/common/v1" | ||
) | ||
|
||
type ExtractTagFunc func(tag string) bool | ||
|
||
// ExtractSearchData returns the flatbuffer search data for the given trace. It is extracted here | ||
// in the distributor because this is the only place on the ingest path where the trace is available | ||
// in object form. | ||
func ExtractSearchData(tr *tempopb.Trace, id []byte, extractTag ExtractTagFunc) []byte { | ||
data := &tempofb.SearchEntryMutable{} | ||
|
||
data.TraceID = id | ||
|
||
for _, b := range tr.Batches { | ||
// Batch attrs | ||
if b.Resource != nil { | ||
for _, a := range b.Resource.Attributes { | ||
if !extractTag(a.Key) { | ||
continue | ||
} | ||
if s, ok := extractValueAsString(a.Value); ok { | ||
data.AddTag(a.Key, s) | ||
} | ||
} | ||
} | ||
|
||
for _, ils := range b.InstrumentationLibrarySpans { | ||
for _, s := range ils.Spans { | ||
|
||
// Root span | ||
if len(s.ParentSpanId) == 0 { | ||
|
||
// Collect root.name | ||
data.AddTag(RootSpanNameTag, s.Name) | ||
|
||
// Collect root.service.name | ||
if b.Resource != nil { | ||
for _, a := range b.Resource.Attributes { | ||
if a.Key == ServiceNameTag { | ||
if s, ok := extractValueAsString(a.Value); ok { | ||
data.AddTag(RootServiceNameTag, s) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Collect for any spans | ||
data.AddTag(SpanNameTag, s.Name) | ||
if s.Status != nil { | ||
data.AddTag(StatusCodeTag, strconv.Itoa(int(s.Status.Code))) | ||
} | ||
data.SetStartTimeUnixNano(s.StartTimeUnixNano) | ||
data.SetEndTimeUnixNano(s.EndTimeUnixNano) | ||
|
||
for _, a := range s.Attributes { | ||
if !extractTag(a.Key) { | ||
continue | ||
} | ||
if s, ok := extractValueAsString(a.Value); ok { | ||
data.AddTag(a.Key, s) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return data.ToBytes() | ||
} | ||
|
||
func extractValueAsString(v *common_v1.AnyValue) (s string, ok bool) { | ||
vv := v.GetValue() | ||
if vv == nil { | ||
return "", false | ||
} | ||
|
||
if s, ok := vv.(*common_v1.AnyValue_StringValue); ok { | ||
return s.StringValue, true | ||
} | ||
|
||
if b, ok := vv.(*common_v1.AnyValue_BoolValue); ok { | ||
return strconv.FormatBool(b.BoolValue), true | ||
} | ||
|
||
if i, ok := vv.(*common_v1.AnyValue_IntValue); ok { | ||
return strconv.FormatInt(i.IntValue, 10), true | ||
} | ||
|
||
if d, ok := vv.(*common_v1.AnyValue_DoubleValue); ok { | ||
return strconv.FormatFloat(d.DoubleValue, 'g', -1, 64), true | ||
} | ||
|
||
return "", false | ||
} |
This file contains 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.