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

[ES, C*] Check for the presence of a tag #2054

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
17 changes: 9 additions & 8 deletions cmd/query/app/grpc_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ func (g *GRPCHandler) ArchiveTrace(ctx context.Context, r *api_v2.ArchiveTraceRe
func (g *GRPCHandler) FindTraces(r *api_v2.FindTracesRequest, stream api_v2.QueryService_FindTracesServer) error {
query := r.GetQuery()
queryParams := spanstore.TraceQueryParameters{
ServiceName: query.ServiceName,
OperationName: query.OperationName,
Tags: query.Tags,
StartTimeMin: query.StartTimeMin,
StartTimeMax: query.StartTimeMax,
DurationMin: query.DurationMin,
DurationMax: query.DurationMax,
NumTraces: int(query.SearchDepth),
ServiceName: query.ServiceName,
OperationName: query.OperationName,
Tags: query.Tags,
StartTimeMin: query.StartTimeMin,
StartTimeMax: query.StartTimeMax,
DurationMin: query.DurationMin,
DurationMax: query.DurationMax,
NumTraces: int(query.SearchDepth),
CheckTagsPresent: query.CheckTagsPresent,
}
traces, err := g.queryService.FindTraces(stream.Context(), &queryParams)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions model/proto/api_v2/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ message TraceQueryParameters {
(gogoproto.nullable) = false
];
int32 search_depth = 8;
repeated string check_tags_present = 9;
}

message FindTracesRequest {
Expand Down
28 changes: 28 additions & 0 deletions plugin/storage/cassandra/spanstore/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package spanstore

import (
"context"
"strings"
"time"

"github.com/opentracing/opentracing-go"
Expand Down Expand Up @@ -45,6 +46,12 @@ const (
WHERE service_name = ? AND tag_key = ? AND tag_value = ? and start_time > ? and start_time < ?
ORDER BY start_time DESC
LIMIT ?`
queryForTagPresence = `
SELECT trace_id
FROM tag_index
WHERE service_name = ? AND tag_key IN ? and start_time > ? and start_time < ?
ORDER BY start_time DESC
LIMIT ?`
queryByServiceName = `
SELECT trace_id
FROM service_name_index
Expand Down Expand Up @@ -301,9 +308,30 @@ func (s *SpanReader) findTraceIDs(ctx context.Context, traceQuery *spanstore.Tra
if len(traceQuery.Tags) > 0 {
return s.queryByTagsAndLogs(ctx, traceQuery)
}
if len(traceQuery.CheckTagsPresent) > 0 {
s.queryCheckTagPresence(ctx, traceQuery)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Will fix.

}
return s.queryByService(ctx, traceQuery)
}

func (s *SpanReader) queryCheckTagPresence(ctx context.Context, tq *spanstore.TraceQueryParameters) (dbmodel.UniqueTraceIDs, error) {
span, _ := startSpanForQuery(ctx, "queryCheckTagPresence", queryByTag)
query := s.session.Query(
tq.ServiceName,
"("+strings.Join(tq.CheckTagsPresent, ",")+")",
model.TimeAsEpochMicroseconds(tq.StartTimeMin),
model.TimeAsEpochMicroseconds(tq.StartTimeMax),
tq.NumTraces*limitMultiple,
).PageSize(0)

t, err := s.executeQuery(span, query, s.metrics.queryTagIndex)
if err != nil {
return nil, err
}

return t, nil
}

func (s *SpanReader) queryByTagsAndLogs(ctx context.Context, tq *spanstore.TraceQueryParameters) (dbmodel.UniqueTraceIDs, error) {
span, ctx := startSpanForQuery(ctx, "queryByTagsAndLogs", queryByTag)
defer span.Finish()
Expand Down
14 changes: 14 additions & 0 deletions plugin/storage/es/spanstore/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

"github.com/olivere/elastic"
Expand Down Expand Up @@ -577,6 +578,12 @@ func (s *SpanReader) buildFindTraceIDsQuery(traceQuery *spanstore.TraceQueryPara
tagQuery := s.buildTagQuery(k, v)
boolQuery.Must(tagQuery)
}

for _, v := range traceQuery.CheckTagsPresent {
checkTagPresentQuery := s.buildCheckTagPresentQuery(v)
boolQuery.Must(checkTagPresentQuery)
}

return boolQuery
}

Expand Down Expand Up @@ -633,6 +640,13 @@ func (s *SpanReader) buildObjectQuery(field string, k string, v string) elastic.
return elastic.NewBoolQuery().Must(keyQuery)
}

func (s *SpanReader) buildCheckTagPresentQuery(field string) elastic.Query {
// Check in tags as well as process tags
checkTagQuery := elastic.NewExistsQuery(strings.Join([]string{"tag", field}, "."))
checkProcesTagQuery := elastic.NewExistsQuery(strings.Join([]string{"process", "tag", field}, "."))
return elastic.NewBoolQuery().Should(checkTagQuery, checkProcesTagQuery)
}

func logErrorToSpan(span opentracing.Span, err error) {
ottag.Error.Set(span, true)
span.LogFields(otlog.Error(err))
Expand Down
4 changes: 4 additions & 0 deletions plugin/storage/es/spanstore/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,9 @@ func TestSpanReader_buildFindTraceIDsQuery(t *testing.T) {
Tags: map[string]string{
"hello": "world",
},
CheckTagsPresent: []string{
"hello",
},
}

actualQuery := r.reader.buildFindTraceIDsQuery(traceQuery)
Expand All @@ -927,6 +930,7 @@ func TestSpanReader_buildFindTraceIDsQuery(t *testing.T) {
r.reader.buildServiceNameQuery("s"),
r.reader.buildOperationNameQuery("o"),
r.reader.buildTagQuery("hello", "world"),
r.reader.buildCheckTagPresentQuery("hello"),
)
expected, err := expectedQuery.Source()
require.NoError(t, err)
Expand Down
Loading