MCPcopy
hub / github.com/grafana/tempo / hashForSearchRequest

Function hashForSearchRequest

modules/frontend/search_sharder.go:354–376  ·  modules/frontend/search_sharder.go::hashForSearchRequest

hashForSearchRequest returns a uint64 hash of the query. if the query is invalid it returns a 0 hash. before hashing the query is forced into a canonical form so equivalent queries will hash to the same value.

(searchRequest *tempopb.SearchRequest)

Source from the content-addressed store, hash-verified

352// hashForSearchRequest returns a uint64 hash of the query. if the query is invalid it returns a 0 hash.
353// before hashing the query is forced into a canonical form so equivalent queries will hash to the same value.
354func hashForSearchRequest(searchRequest *tempopb.SearchRequest) uint64 {
355 if searchRequest.Query == "" {
356 return 0
357 }
358
359 ast, err := traceql.ParseNoOptimizations(searchRequest.Query)
360 if err != nil { // this should never occur. if we've made this far we've already validated the query can parse. however, for sanity, just fail to cache if we can't parse
361 return 0
362 }
363
364 // forces the query into a canonical form
365 query := ast.String()
366
367 // add the query, limit and spss to the hash
368 hash := fnv1a.HashString64(query)
369 hash = fnv1a.AddUint64(hash, uint64(searchRequest.Limit))
370 hash = fnv1a.AddUint64(hash, uint64(searchRequest.SpansPerSpanSet))
371 for _, name := range searchRequest.SkipASTTransformations {
372 hash = fnv1a.AddString64(hash, name)
373 }
374
375 return hash
376}
377
378// pagesPerRequest returns an integer value that indicates the number of pages
379// that should be searched per query. This value is based on the target number of bytes

Callers 4

TestHashTraceQLQueryFunction · 0.85
buildBackendRequestsFunction · 0.85
TestSearchAccessesCacheFunction · 0.85
TestSearchCachedMetricsFunction · 0.85

Calls 2

ParseNoOptimizationsFunction · 0.92
StringMethod · 0.45

Tested by 3

TestHashTraceQLQueryFunction · 0.68
TestSearchAccessesCacheFunction · 0.68
TestSearchCachedMetricsFunction · 0.68