query_builder.go
query_builder.go - Overview
This file implements the QueryBuilder
which is responsible for constructing database queries based on user-defined parameters. It supports building queries for traces, logs, and metrics, and includes features like expression evaluation and cache key generation.
Detailed Documentation
SupportedFunctions
- Overview: A list of functions that are supported in the query builder's expression evaluation.
- Type:
[]string
EvalFuncs
- Overview: A map of functions used in expression evaluation.
- Type:
map[string]govaluate.ExpressionFunction
QueryBuilder
- Purpose: Struct that contains the options and feature flags to prepare queries.
- Fields:
options QueryBuilderOptions
: Options for the query builder.featureFlags interfaces.FeatureLookup
: Feature flags for the query builder.
QueryBuilderOptions
- Purpose: Struct that contains the functions to build queries for traces, logs and metrics.
- Fields:
BuildTraceQuery prepareTracesQueryFunc
: Function to build trace queries.BuildLogQuery prepareLogsQueryFunc
: Function to build log queries.BuildMetricQuery prepareMetricQueryFunc
: Function to build metric queries.
NewQueryBuilder(options QueryBuilderOptions, featureFlags interfaces.FeatureLookup) *QueryBuilder
- Purpose: Creates a new
QueryBuilder
instance. - Parameters:
options QueryBuilderOptions
: The options for the query builder.featureFlags interfaces.FeatureLookup
: Feature flags.
- Returns: A pointer to the newly created
QueryBuilder
instance.
unique(slice []string) []string
- Purpose: Returns a slice containing only the unique values from the input slice.
- Parameters:
slice []string
: The input slice of strings.
- Returns: A slice of unique strings.
expressionToQuery(qp *v3.QueryRangeParamsV3, varToQuery map[string]string, expression *govaluate.EvaluableExpression, queryName string) (string, error)
- Purpose: Converts a parsed expression into a database query. It handles variable substitution and constructs the appropriate SQL.
- Parameters:
qp *v3.QueryRangeParamsV3
: Contains query parameters, including start and end times.varToQuery map[string]string
: A map of variable names to their corresponding queries.expression *govaluate.EvaluableExpression
: The parsed expression to convert.queryName string
: The name of the query.
- Returns: The generated SQL query string and an error, if any.
(qb *QueryBuilder) PrepareLiveTailQuery(params *v3.QueryRangeParamsV3) (string, error)
- Purpose: Prepares a query for live tailing of logs.
- Parameters:
params *v3.QueryRangeParamsV3
: Query parameters.
- Returns: The generated query string and an error, if any.
(qb *QueryBuilder) PrepareQueries(params *v3.QueryRangeParamsV3) (map[string]string, error)
- Purpose: Prepares a map of queries based on the provided query range parameters.
- Parameters:
params *v3.QueryRangeParamsV3
: The query parameters.
- Returns: A map of query names to query strings, and an error, if any.
cacheKeyGenerator
- Purpose: A struct that implements the
cache.KeyGenerator
interface for generating cache keys.
(c *cacheKeyGenerator) GenerateKeys(params *v3.QueryRangeParamsV3) map[string]string
- Purpose: Generates cache keys based on the query parameters.
- Parameters:
params *v3.QueryRangeParamsV3
: The query parameters.
- Returns: A map of query names to cache keys.
NewKeyGenerator() cache.KeyGenerator
- Purpose: Returns a new instance of the
cacheKeyGenerator
. - Returns: A
cache.KeyGenerator
instance.
Code Examples
None necessary.
Include in Getting Started: NO