query_builder.go
query_builder.go - Overview
-
Overview This file provides functionality to build ClickHouse queries for fetching and aggregating metric data based on the provided v3 query builder model. It supports various aggregation operators, grouping, filtering, and ordering options.
-
Detailed Documentation
aggregateOperatorToPercentile
var aggregateOperatorToPercentile = map[v3.AggregateOperator]float64{
v3.AggregateOperatorP05: 0.05,
v3.AggregateOperatorP10: 0.10,
v3.AggregateOperatorP20: 0.20,
v3.AggregateOperatorP25: 0.25,
v3.AggregateOperatorP50: 0.50,
v3.AggregateOperatorP75: 0.75,
v3.AggregateOperatorP90: 0.90,
v3.AggregateOperatorP95: 0.95,
v3.AggregateOperatorP99: 0.99,
v3.AggregateOperatorHistQuant50: 0.50,
v3.AggregateOperatorHistQuant75: 0.75,
v3.AggregateOperatorHistQuant90: 0.90,
v3.AggregateOperatorHistQuant95: 0.95,
v3.AggregateOperatorHistQuant99: 0.99,
}
- Purpose: Maps aggregate operators to their corresponding percentile values (e.g., P95 to 0.95).
aggregateOperatorToSQLFunc
var aggregateOperatorToSQLFunc = map[v3.AggregateOperator]string{
v3.AggregateOperatorAvg: "avg",
v3.AggregateOperatorMax: "max",
v3.AggregateOperatorMin: "min",
v3.AggregateOperatorSum: "sum",
v3.AggregateOperatorRateSum: "sum",
v3.AggregateOperatorRateAvg: "avg",
v3.AggregateOperatorRateMax: "max",
v3.AggregateOperatorRateMin: "min",
v3.AggregateOperatorSumRate: "sum",
v3.AggregateOperatorAvgRate: "avg",
v3.AggregateOperatorMaxRate: "max",
v3.AggregateOperatorMinRate: "min",
}
- Purpose: Maps aggregate operators to their corresponding SQL function names (e.g.,
AggregateOperatorAvg
to"avg"
).
rateWithoutNegative
var rateWithoutNegative = `If((value - lagInFrame(value, 1, 0) OVER rate_window) < 0, nan, If((ts - lagInFrame(ts, 1, toDate('1970-01-01')) OVER rate_window) >= 86400, nan, (value - lagInFrame(value, 1, 0) OVER rate_window) / (ts - lagInFrame(ts, 1, toDate('1970-01-01')) OVER rate_window))) `
- Purpose: Defines a ClickHouse expression for calculating the rate of change, handling negative rate values and large time gaps by returning NaN.
buildMetricQuery
func buildMetricQuery(start, end, step int64, mq *v3.BuilderQuery) (string, error) {
// ...
}
- Purpose: Constructs a ClickHouse query to fetch and aggregate metric data based on the provided parameters.
- Parameters:
start
(int64): Start timestamp in milliseconds.end
(int64): End timestamp in milliseconds.step
(int64): Step interval in seconds.mq
(*v3.BuilderQuery): Query parameters.
- Returns:
- (string): The generated ClickHouse query.
- (error): An error if any occurred during query construction.
- Parameters:
groupingSets
func groupingSets(tags ...string) string {
// ...
}
- Purpose: Creates a comma-separated string of tags for the
GROUP BY
clause, always includingts
.- Parameters:
tags
(...string): Variable number of tag names.
- Returns:
- (string): Comma-separated string of tags, including "ts".
- Parameters:
groupBy
func groupBy(tags ...string) string {
// ...
}
- Purpose: Creates a comma-separated string of tags for the
GROUP BY
clause, always includingts
.- Parameters:
tags
(...string): Variable number of tag names.
- Returns:
- (string): Comma-separated string of tags, including "ts".
- Parameters:
groupSelect
func groupSelect(tags ...string) string {
// ...
}
- Purpose: Creates a comma-separated string of tags for the
SELECT
clause.- Parameters:
tags
(...string): Variable number of tag names.
- Returns:
- (string): Comma-separated string of tags, with a trailing comma if tags are present.
- Parameters:
groupingSetsByAttributeKeyTags
func groupingSetsByAttributeKeyTags(tags ...v3.AttributeKey) string {
// ...
}
- Purpose: Extracts keys from
AttributeKey
and uses them to callgroupingSets
.- Parameters:
tags
(...v3.AttributeKey): Variable number ofAttributeKey
structs.
- Returns:
- (string): Comma-separated string of tags, including "ts".
- Parameters:
groupByAttributeKeyTags
func groupByAttributeKeyTags(tags ...v3.AttributeKey) string {
// ...
}
- Purpose: Extracts keys from
AttributeKey
and uses them to callgroupBy
.- Parameters:
tags
(...v3.AttributeKey): Variable number ofAttributeKey
structs.
- Returns:
- (string): Comma-separated string of tags, including "ts".
- Parameters:
groupSelectAttributeKeyTags
func groupSelectAttributeKeyTags(tags ...v3.AttributeKey) string {
// ...
}
- Purpose: Extracts keys from
AttributeKey
and uses them to callgroupSelect
.- Parameters:
tags
(...v3.AttributeKey): Variable number ofAttributeKey
structs.
- Returns:
- (string): Comma-separated string of tags, with a trailing comma if tags are present.
- Parameters:
orderBy
func orderBy(items []v3.OrderBy, tags []string) string {
// ...
}
- Purpose: Generates an
ORDER BY
clause string based on provided order configurations and tags.- Parameters:
items
([]v3.OrderBy): Slice ofOrderBy
structs specifying the order.tags
([]string): Slice of tag names to order by.
- Returns:
- (string):
ORDER BY
clause string.
- (string):
- Parameters:
orderByAttributeKeyTags
func orderByAttributeKeyTags(items []v3.OrderBy, tags []v3.AttributeKey) string {
// ...
}
- Purpose: Extracts keys from
AttributeKey
and uses them to callorderBy
.- Parameters:
items
([]v3.OrderBy): Slice ofOrderBy
structs specifying the order.tags
([]v3.AttributeKey): Slice ofAttributeKey
structs.
- Returns:
- (string):
ORDER BY
clause string.
- (string):
- Parameters:
having
func having(items []v3.Having) string {
// ...
}
- Purpose: Generates a
HAVING
clause string based on provided having conditions.- Parameters:
items
([]v3.Having): Slice ofHaving
structs specifying the conditions.
- Returns:
- (string):
HAVING
clause string.
- (string):
- Parameters:
reduceQuery
func reduceQuery(query string, reduceTo v3.ReduceToOperator, aggregateOperator v3.AggregateOperator) (string, error) {
// ...
}
- Purpose: Modifies the input query to reduce the results to a single value based on the specified
ReduceToOperator
.- Parameters:
query
(string): The original query.reduceTo
(v3.ReduceToOperator): The operator used to reduce the results (e.g.,Last
,Sum
,Avg
).aggregateOperator
(v3.AggregateOperator): The aggregate operator used in the original query.
- Returns:
- (string): The modified query with reduction logic.
- (error): An error if an unsupported
ReduceToOperator
is provided.
- Parameters:
PrepareMetricQuery
func PrepareMetricQuery(start, end int64, queryType v3.QueryType, panelType v3.PanelType, mq *v3.BuilderQuery, options Options) (string, error) {
// ...
}
- Purpose: Prepares the ClickHouse query for fetching metrics, applying filters, aggregations, and other transformations.
- Parameters:
start
(int64): Start timestamp in milliseconds.end
(int64): End timestamp in milliseconds.queryType
(v3.QueryType): Type of the query.panelType
(v3.PanelType): Type of the panel.mq
(*v3.BuilderQuery): Query parameters.options
(Options): Additional options.
- Returns:
- (string): The final ClickHouse query.
- (error): An error if any occurred during query preparation.
- Parameters:
BuildPromQuery
func BuildPromQuery(promQuery *v3.PromQuery, step, start, end int64) *model.QueryRangeParams {
// ...
}
- Purpose: Constructs a
QueryRangeParams
struct suitable for querying Prometheus.- Parameters:
promQuery
(*v3.PromQuery): The Prometheus query details.step
(int64): Step interval in seconds.start
(int64): Start timestamp in milliseconds.end
(int64): End timestamp in milliseconds.
- Returns:
- (*model.QueryRangeParams): A
QueryRangeParams
struct containing the query and time range.
- (*model.QueryRangeParams): A
- Parameters:
- Code Examples
No examples are necessary as the code is self-explanatory.
Include in Getting Started: NO