@@ -10,6 +10,9 @@ import (
1010
1111 "github.com/go-kit/log"
1212 "github.com/grafana/dskit/tenant"
13+ "github.com/opentracing/opentracing-go"
14+ "github.com/opentracing/opentracing-go/ext"
15+ otlog "github.com/opentracing/opentracing-go/log"
1316 "github.com/prometheus/prometheus/model/labels"
1417 "github.com/prometheus/prometheus/promql/parser"
1518 "google.golang.org/grpc/codes"
@@ -73,18 +76,26 @@ func (q *QueryFrontend) Query(
7376 ctx context.Context ,
7477 req * queryv1.QueryRequest ,
7578) (* queryv1.QueryResponse , error ) {
79+ span , ctx := opentracing .StartSpanFromContext (ctx , "QueryFrontend.Query" )
80+ defer span .Finish ()
81+ span .SetTag ("start_time" , req .StartTime )
82+ span .SetTag ("end_time" , req .EndTime )
83+ span .SetTag ("label_selector" , req .LabelSelector )
84+
7685 // This method is supposed to be the entry point of the read path
7786 // in the future versions. Therefore, validation, overrides, and
7887 // rest of the request handling should be moved here.
7988 tenants , err := tenant .TenantIDs (ctx )
8089 if err != nil {
8190 return nil , status .Error (codes .InvalidArgument , err .Error ())
8291 }
92+ span .SetTag ("tenant_ids" , tenants )
8393
8494 blocks , err := q .QueryMetadata (ctx , req )
8595 if err != nil {
8696 return nil , err
8797 }
98+ span .SetTag ("block_count" , len (blocks ))
8899 if len (blocks ) == 0 {
89100 return new (queryv1.QueryResponse ), nil
90101 }
@@ -98,7 +109,8 @@ func (q *QueryFrontend) Query(
98109 p := queryplan .Build (blocks , 4 , 20 )
99110
100111 // Only check for symbolization if all tenants have it enabled
101- shouldSymbolize := q .shouldSymbolize (tenants , blocks )
112+ shouldSymbolize := q .shouldSymbolize (ctx , tenants , blocks )
113+ span .SetTag ("should_symbolize" , shouldSymbolize )
102114
103115 modifiedQueries := make ([]* queryv1.Query , len (req .Query ))
104116 for i , originalQuery := range req .Query {
@@ -148,15 +160,29 @@ func (q *QueryFrontend) Query(
148160func (q * QueryFrontend ) QueryMetadata (
149161 ctx context.Context ,
150162 req * queryv1.QueryRequest ,
151- ) ([]* metastorev1.BlockMeta , error ) {
163+ ) (blocks []* metastorev1.BlockMeta , err error ) {
164+ span , ctx := opentracing .StartSpanFromContext (ctx , "QueryFrontend.QueryMetadata" )
165+ defer func () {
166+ if err != nil {
167+ ext .LogError (span , err )
168+ }
169+ span .Finish ()
170+ }()
171+ span .SetTag ("start_time" , req .StartTime )
172+ span .SetTag ("end_time" , req .EndTime )
173+ span .SetTag ("label_selector" , req .LabelSelector )
174+
152175 tenants , err := tenant .TenantIDs (ctx )
153176 if err != nil {
154177 return nil , status .Error (codes .InvalidArgument , err .Error ())
155178 }
179+ span .SetTag ("tenant_ids" , tenants )
180+
156181 matchers , err := parser .ParseMetricSelector (req .LabelSelector )
157182 if err != nil {
158183 return nil , status .Error (codes .InvalidArgument , err .Error ())
159184 }
185+
160186 query := & metastorev1.QueryMetadataRequest {
161187 TenantId : tenants ,
162188 StartTime : req .StartTime ,
@@ -187,6 +213,7 @@ func (q *QueryFrontend) QueryMetadata(
187213 if err != nil {
188214 return nil , err
189215 }
216+ span .SetTag ("blocks_count" , len (md .Blocks ))
190217
191218 return md .Blocks , nil
192219}
@@ -202,7 +229,12 @@ func (q *QueryFrontend) hasUnsymbolizedProfiles(block *metastorev1.BlockMeta) bo
202229}
203230
204231// shouldSymbolize determines if we should symbolize profiles based on tenant settings
205- func (q * QueryFrontend ) shouldSymbolize (tenants []string , blocks []* metastorev1.BlockMeta ) bool {
232+ func (q * QueryFrontend ) shouldSymbolize (ctx context.Context , tenants []string , blocks []* metastorev1.BlockMeta ) bool {
233+ span := opentracing .SpanFromContext (ctx )
234+ if span != nil {
235+ span .LogFields (otlog .String ("event" , "shouldSymbolize" ))
236+ }
237+
206238 if q .symbolizer == nil {
207239 return false
208240 }
@@ -213,21 +245,39 @@ func (q *QueryFrontend) shouldSymbolize(tenants []string, blocks []*metastorev1.
213245 }
214246 }
215247
248+ blocksWithUnsymbolized := 0
216249 for _ , block := range blocks {
217250 if q .hasUnsymbolizedProfiles (block ) {
218- return true
251+ blocksWithUnsymbolized ++
219252 }
220253 }
221254
222- return false
255+ if span != nil {
256+ span .LogFields (
257+ otlog .Int ("blocks_with_unsymbolized" , blocksWithUnsymbolized ),
258+ otlog .Int ("total_blocks" , len (blocks )),
259+ )
260+ }
261+
262+ return blocksWithUnsymbolized > 0
223263}
224264
225265// processAndSymbolizeProfiles handles the symbolization of profiles from the response
226266func (q * QueryFrontend ) processAndSymbolizeProfiles (
227267 ctx context.Context ,
228268 resp * queryv1.InvokeResponse ,
229269 originalQueries []* queryv1.Query ,
230- ) error {
270+ ) (err error ) {
271+ span , ctx := opentracing .StartSpanFromContext (ctx , "QueryFrontend.processAndSymbolizeProfiles" )
272+ defer func () {
273+ if err != nil {
274+ ext .LogError (span , err )
275+ }
276+ span .Finish ()
277+ }()
278+ span .SetTag ("query_count" , len (originalQueries ))
279+ span .SetTag ("report_count" , len (resp .Reports ))
280+
231281 if len (originalQueries ) != len (resp .Reports ) {
232282 return fmt .Errorf ("query/report count mismatch: %d queries but %d reports" ,
233283 len (originalQueries ), len (resp .Reports ))
0 commit comments