github.com/thanos-io/thanos@v0.32.5/pkg/queryfrontend/request.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package queryfrontend 5 6 import ( 7 "time" 8 9 "github.com/thanos-io/thanos/pkg/store/storepb" 10 11 "github.com/opentracing/opentracing-go" 12 otlog "github.com/opentracing/opentracing-go/log" 13 "github.com/prometheus/prometheus/model/labels" 14 "github.com/prometheus/prometheus/model/timestamp" 15 16 "github.com/thanos-io/thanos/internal/cortex/querier/queryrange" 17 ) 18 19 // ThanosRequestStoreMatcherGetter is a an interface for store matching that all request share. 20 // TODO(yeya24): Add partial result when needed. 21 type ThanosRequestStoreMatcherGetter interface { 22 GetStoreMatchers() [][]*labels.Matcher 23 } 24 25 // ShardedRequest interface represents a query request that can be sharded vertically. 26 type ShardedRequest interface { 27 WithShardInfo(info *storepb.ShardInfo) queryrange.Request 28 } 29 30 type RequestHeader struct { 31 Name string 32 Values []string 33 } 34 35 // ThanosRequestDedup is a an interface for all requests that share setting deduplication. 36 type ThanosRequestDedup interface { 37 IsDedupEnabled() bool 38 } 39 40 type ThanosQueryRangeRequest struct { 41 Path string 42 Start int64 43 End int64 44 Step int64 45 Timeout time.Duration 46 Query string 47 Dedup bool 48 PartialResponse bool 49 AutoDownsampling bool 50 MaxSourceResolution int64 51 ReplicaLabels []string 52 StoreMatchers [][]*labels.Matcher 53 CachingOptions queryrange.CachingOptions 54 Headers []*RequestHeader 55 Stats string 56 ShardInfo *storepb.ShardInfo 57 LookbackDelta int64 58 Explain string 59 Engine string 60 } 61 62 // IsDedupEnabled returns true if deduplication is enabled. 63 func (r *ThanosQueryRangeRequest) IsDedupEnabled() bool { return r.Dedup } 64 65 // GetStoreMatchers returns store matches. 66 func (r *ThanosQueryRangeRequest) GetStoreMatchers() [][]*labels.Matcher { return r.StoreMatchers } 67 68 // GetStart returns the start timestamp of the request in milliseconds. 69 func (r *ThanosQueryRangeRequest) GetStart() int64 { return r.Start } 70 71 // GetEnd returns the end timestamp of the request in milliseconds. 72 func (r *ThanosQueryRangeRequest) GetEnd() int64 { return r.End } 73 74 // GetStep returns the step of the request in milliseconds. 75 func (r *ThanosQueryRangeRequest) GetStep() int64 { return r.Step } 76 77 // GetQuery returns the query of the request. 78 func (r *ThanosQueryRangeRequest) GetQuery() string { return r.Query } 79 80 func (r *ThanosQueryRangeRequest) GetCachingOptions() queryrange.CachingOptions { 81 return r.CachingOptions 82 } 83 84 func (r *ThanosQueryRangeRequest) GetStats() string { return r.Stats } 85 86 func (r *ThanosQueryRangeRequest) WithStats(stats string) queryrange.Request { 87 q := *r 88 q.Stats = stats 89 return &q 90 } 91 92 // WithStartEnd clone the current request with different start and end timestamp. 93 func (r *ThanosQueryRangeRequest) WithStartEnd(start, end int64) queryrange.Request { 94 q := *r 95 q.Start = start 96 q.End = end 97 return &q 98 } 99 100 // WithQuery clone the current request with a different query. 101 func (r *ThanosQueryRangeRequest) WithQuery(query string) queryrange.Request { 102 q := *r 103 q.Query = query 104 return &q 105 } 106 107 // WithShardInfo clones the current request with a different shard info. 108 func (r *ThanosQueryRangeRequest) WithShardInfo(info *storepb.ShardInfo) queryrange.Request { 109 q := *r 110 q.ShardInfo = info 111 return &q 112 } 113 114 // LogToSpan writes information about this request to an OpenTracing span. 115 func (r *ThanosQueryRangeRequest) LogToSpan(sp opentracing.Span) { 116 fields := []otlog.Field{ 117 otlog.String("query", r.GetQuery()), 118 otlog.String("start", timestamp.Time(r.GetStart()).String()), 119 otlog.String("end", timestamp.Time(r.GetEnd()).String()), 120 otlog.Int64("step (ms)", r.GetStep()), 121 otlog.Bool("dedup", r.Dedup), 122 otlog.Bool("partial_response", r.PartialResponse), 123 otlog.Object("replicaLabels", r.ReplicaLabels), 124 otlog.Object("storeMatchers", r.StoreMatchers), 125 otlog.Bool("auto-downsampling", r.AutoDownsampling), 126 otlog.Int64("max_source_resolution (ms)", r.MaxSourceResolution), 127 } 128 129 sp.LogFields(fields...) 130 } 131 132 // Reset implements proto.Message interface required by queryrange.Request, 133 // which is not used in thanos. 134 func (r *ThanosQueryRangeRequest) Reset() {} 135 136 // String implements proto.Message interface required by queryrange.Request, 137 // which is not used in thanos. 138 func (r *ThanosQueryRangeRequest) String() string { return "" } 139 140 // ProtoMessage implements proto.Message interface required by queryrange.Request, 141 // which is not used in thanos. 142 func (r *ThanosQueryRangeRequest) ProtoMessage() {} 143 144 type ThanosQueryInstantRequest struct { 145 Path string 146 Time int64 147 Timeout time.Duration 148 Query string 149 Dedup bool 150 PartialResponse bool 151 AutoDownsampling bool 152 MaxSourceResolution int64 153 ReplicaLabels []string 154 StoreMatchers [][]*labels.Matcher 155 Headers []*RequestHeader 156 Stats string 157 ShardInfo *storepb.ShardInfo 158 LookbackDelta int64 // in milliseconds. 159 Explain string 160 Engine string 161 } 162 163 // IsDedupEnabled returns true if deduplication is enabled. 164 func (r *ThanosQueryInstantRequest) IsDedupEnabled() bool { return r.Dedup } 165 166 // GetStoreMatchers returns store matches. 167 func (r *ThanosQueryInstantRequest) GetStoreMatchers() [][]*labels.Matcher { return r.StoreMatchers } 168 169 // GetStart returns the start timestamp of the request in milliseconds. 170 func (r *ThanosQueryInstantRequest) GetStart() int64 { return 0 } 171 172 // GetEnd returns the end timestamp of the request in milliseconds. 173 func (r *ThanosQueryInstantRequest) GetEnd() int64 { return 0 } 174 175 // GetStep returns the step of the request in milliseconds. 176 func (r *ThanosQueryInstantRequest) GetStep() int64 { return 0 } 177 178 // GetQuery returns the query of the request. 179 func (r *ThanosQueryInstantRequest) GetQuery() string { return r.Query } 180 181 func (r *ThanosQueryInstantRequest) GetCachingOptions() queryrange.CachingOptions { 182 return queryrange.CachingOptions{} 183 } 184 185 func (r *ThanosQueryInstantRequest) GetStats() string { return r.Stats } 186 187 func (r *ThanosQueryInstantRequest) WithStats(stats string) queryrange.Request { 188 q := *r 189 q.Stats = stats 190 return &q 191 } 192 193 // WithStartEnd clone the current request with different start and end timestamp. 194 func (r *ThanosQueryInstantRequest) WithStartEnd(_, _ int64) queryrange.Request { return nil } 195 196 // WithQuery clone the current request with a different query. 197 func (r *ThanosQueryInstantRequest) WithQuery(query string) queryrange.Request { 198 q := *r 199 q.Query = query 200 return &q 201 } 202 203 // WithShardInfo clones the current request with a different shard info. 204 func (r *ThanosQueryInstantRequest) WithShardInfo(info *storepb.ShardInfo) queryrange.Request { 205 q := *r 206 q.ShardInfo = info 207 return &q 208 } 209 210 // LogToSpan writes information about this request to an OpenTracing span. 211 func (r *ThanosQueryInstantRequest) LogToSpan(sp opentracing.Span) { 212 fields := []otlog.Field{ 213 otlog.String("query", r.GetQuery()), 214 otlog.Int64("time", r.Time), 215 otlog.Bool("dedup", r.Dedup), 216 otlog.Bool("partial_response", r.PartialResponse), 217 otlog.Object("replicaLabels", r.ReplicaLabels), 218 otlog.Object("storeMatchers", r.StoreMatchers), 219 otlog.Bool("auto-downsampling", r.AutoDownsampling), 220 otlog.Int64("max_source_resolution (ms)", r.MaxSourceResolution), 221 } 222 223 sp.LogFields(fields...) 224 } 225 226 // Reset implements proto.Message interface required by queryrange.Request, 227 // which is not used in thanos. 228 func (r *ThanosQueryInstantRequest) Reset() {} 229 230 // String implements proto.Message interface required by queryrange.Request, 231 // which is not used in thanos. 232 func (r *ThanosQueryInstantRequest) String() string { return "" } 233 234 // ProtoMessage implements proto.Message interface required by queryrange.Request, 235 // which is not used in thanos. 236 func (r *ThanosQueryInstantRequest) ProtoMessage() {} 237 238 type ThanosLabelsRequest struct { 239 Start int64 240 End int64 241 Label string 242 Path string 243 Matchers [][]*labels.Matcher 244 StoreMatchers [][]*labels.Matcher 245 PartialResponse bool 246 CachingOptions queryrange.CachingOptions 247 Headers []*RequestHeader 248 Stats string 249 } 250 251 // GetStoreMatchers returns store matches. 252 func (r *ThanosLabelsRequest) GetStoreMatchers() [][]*labels.Matcher { return r.StoreMatchers } 253 254 // GetStart returns the start timestamp of the request in milliseconds. 255 func (r *ThanosLabelsRequest) GetStart() int64 { return r.Start } 256 257 // GetEnd returns the end timestamp of the request in milliseconds. 258 func (r *ThanosLabelsRequest) GetEnd() int64 { return r.End } 259 260 // GetStep returns the step of the request in milliseconds. Returns 1 is a trick to avoid panic in 261 // https://github.com/cortexproject/cortex/blob/master/pkg/querier/queryrange/results_cache.go#L447. 262 func (r *ThanosLabelsRequest) GetStep() int64 { return 1 } 263 264 // GetQuery returns the query of the request. 265 func (r *ThanosLabelsRequest) GetQuery() string { return "" } 266 267 func (r *ThanosLabelsRequest) GetCachingOptions() queryrange.CachingOptions { return r.CachingOptions } 268 269 func (r *ThanosLabelsRequest) GetStats() string { return r.Stats } 270 271 func (r *ThanosLabelsRequest) WithStats(stats string) queryrange.Request { 272 q := *r 273 q.Stats = stats 274 return &q 275 } 276 277 // WithStartEnd clone the current request with different start and end timestamp. 278 func (r *ThanosLabelsRequest) WithStartEnd(start, end int64) queryrange.Request { 279 q := *r 280 q.Start = start 281 q.End = end 282 return &q 283 } 284 285 // WithQuery clone the current request with a different query. 286 func (r *ThanosLabelsRequest) WithQuery(_ string) queryrange.Request { 287 q := *r 288 return &q 289 } 290 291 // LogToSpan writes information about this request to an OpenTracing span. 292 func (r *ThanosLabelsRequest) LogToSpan(sp opentracing.Span) { 293 fields := []otlog.Field{ 294 otlog.String("start", timestamp.Time(r.GetStart()).String()), 295 otlog.String("end", timestamp.Time(r.GetEnd()).String()), 296 otlog.Bool("partial_response", r.PartialResponse), 297 otlog.Object("matchers", r.Matchers), 298 otlog.Object("storeMatchers", r.StoreMatchers), 299 } 300 if r.Label != "" { 301 fields = append(fields, otlog.Object("label", r.Label)) 302 } 303 304 sp.LogFields(fields...) 305 } 306 307 // Reset implements proto.Message interface required by queryrange.Request, 308 // which is not used in thanos. 309 func (r *ThanosLabelsRequest) Reset() {} 310 311 // String implements proto.Message interface required by queryrange.Request, 312 // which is not used in thanos. 313 func (r *ThanosLabelsRequest) String() string { return "" } 314 315 // ProtoMessage implements proto.Message interface required by queryrange.Request, 316 // which is not used in thanos. 317 func (r *ThanosLabelsRequest) ProtoMessage() {} 318 319 type ThanosSeriesRequest struct { 320 Path string 321 Start int64 322 End int64 323 Dedup bool 324 PartialResponse bool 325 ReplicaLabels []string 326 Matchers [][]*labels.Matcher 327 StoreMatchers [][]*labels.Matcher 328 CachingOptions queryrange.CachingOptions 329 Headers []*RequestHeader 330 Stats string 331 } 332 333 // IsDedupEnabled returns true if deduplication is enabled. 334 func (r *ThanosSeriesRequest) IsDedupEnabled() bool { return r.Dedup } 335 336 // GetStoreMatchers returns store matches. 337 func (r *ThanosSeriesRequest) GetStoreMatchers() [][]*labels.Matcher { return r.StoreMatchers } 338 339 // GetStart returns the start timestamp of the request in milliseconds. 340 func (r *ThanosSeriesRequest) GetStart() int64 { return r.Start } 341 342 // GetEnd returns the end timestamp of the request in milliseconds. 343 func (r *ThanosSeriesRequest) GetEnd() int64 { return r.End } 344 345 // GetStep returns the step of the request in milliseconds. Returns 1 is a trick to avoid panic in 346 // https://github.com/cortexproject/cortex/blob/master/pkg/querier/queryrange/results_cache.go#L447. 347 func (r *ThanosSeriesRequest) GetStep() int64 { return 1 } 348 349 // GetQuery returns the query of the request. 350 func (r *ThanosSeriesRequest) GetQuery() string { return "" } 351 352 func (r *ThanosSeriesRequest) GetCachingOptions() queryrange.CachingOptions { return r.CachingOptions } 353 354 func (r *ThanosSeriesRequest) GetStats() string { return r.Stats } 355 356 func (r *ThanosSeriesRequest) WithStats(stats string) queryrange.Request { 357 q := *r 358 q.Stats = stats 359 return &q 360 } 361 362 // WithStartEnd clone the current request with different start and end timestamp. 363 func (r *ThanosSeriesRequest) WithStartEnd(start, end int64) queryrange.Request { 364 q := *r 365 q.Start = start 366 q.End = end 367 return &q 368 } 369 370 // WithQuery clone the current request with a different query. 371 func (r *ThanosSeriesRequest) WithQuery(_ string) queryrange.Request { 372 q := *r 373 return &q 374 } 375 376 // LogToSpan writes information about this request to an OpenTracing span. 377 func (r *ThanosSeriesRequest) LogToSpan(sp opentracing.Span) { 378 fields := []otlog.Field{ 379 otlog.String("start", timestamp.Time(r.GetStart()).String()), 380 otlog.String("end", timestamp.Time(r.GetEnd()).String()), 381 otlog.Bool("dedup", r.Dedup), 382 otlog.Bool("partial_response", r.PartialResponse), 383 otlog.Object("replicaLabels", r.ReplicaLabels), 384 otlog.Object("matchers", r.Matchers), 385 otlog.Object("storeMatchers", r.StoreMatchers), 386 } 387 388 sp.LogFields(fields...) 389 } 390 391 // Reset implements proto.Message interface required by queryrange.Request, 392 // which is not used in thanos. 393 func (r *ThanosSeriesRequest) Reset() {} 394 395 // String implements proto.Message interface required by queryrange.Request, 396 // which is not used in thanos. 397 func (r *ThanosSeriesRequest) String() string { return "" } 398 399 // ProtoMessage implements proto.Message interface required by queryrange.Request, 400 // which is not used in thanos. 401 func (r *ThanosSeriesRequest) ProtoMessage() {}