resource_query_builder.go
Overview
File Name: resource_query_builder.go
This file provides functions to build ClickHouse queries for filtering resource attributes associated with logs. It includes functions to construct filter conditions based on different operators (equal, not equal, like, contains, regex, in, not in, exists, not exists), handle string escaping, and build subqueries for resource filtering.
Detailed Documentation
resourceLogOperators
var resourceLogOperators = map[v3.FilterOperator]string{
v3.FilterOperatorEqual: "=",
v3.FilterOperatorNotEqual: "!=",
v3.FilterOperatorLessThan: "<",
v3.FilterOperatorLessThanOrEq: "<=",
v3.FilterOperatorGreaterThan: ">",
v3.FilterOperatorGreaterThanOrEq: ">=",
v3.FilterOperatorLike: "LIKE",
v3.FilterOperatorNotLike: "NOT LIKE",
v3.FilterOperatorContains: "LIKE",
v3.FilterOperatorNotContains: "NOT LIKE",
v3.FilterOperatorRegex: "match(%s, %s)",
v3.FilterOperatorNotRegex: "NOT match(%s, %s)",
v3.FilterOperatorIn: "IN",
v3.FilterOperatorNotIn: "NOT IN",
v3.FilterOperatorExists: "mapContains(%s_%s, '%s')",
v3.FilterOperatorNotExists: "not mapContains(%s_%s, '%s')",
}
- Purpose: Defines a map of
FilterOperator
to its corresponding SQL operator string for ClickHouse. - Parameters: None
- Returns: None
buildResourceFilter
func buildResourceFilter(logsOp string, key string, op v3.FilterOperator, value interface{}) string
- Purpose: Builds a ClickHouse filter string for resource labels based on the provided operator and value.
- Parameters:
logsOp
(string): The SQL operator string.key
(string): The resource attribute key.op
(v3.FilterOperator): The filter operator.value
(interface{}): The filter value.
- Returns:
- (string): The generated filter string.
buildIndexFilterForInOperator
func buildIndexFilterForInOperator(key string, op v3.FilterOperator, value interface{}) string
- Purpose: Builds a ClickHouse filter string for the "IN" operator, optimized for index usage.
- Parameters:
key
(string): The resource attribute key.op
(v3.FilterOperator): The filter operator (IN or NOT IN).value
(interface{}): The filter value (either a string or a slice of strings).
- Returns:
- (string): The generated filter string.
buildResourceIndexFilter
func buildResourceIndexFilter(key string, op v3.FilterOperator, value interface{}) string
- Purpose: Builds a ClickHouse filter string for resource labels, utilizing indexes for optimized filtering.
- Parameters:
key
(string): The resource attribute key.op
(v3.FilterOperator): The filter operator.value
(interface{}): The filter value.
- Returns:
- (string): The generated filter string.
buildResourceFiltersFromFilterItems
func buildResourceFiltersFromFilterItems(fs *v3.FilterSet) ([]string, error)
- Purpose: Builds a list of ClickHouse filter strings for resource labels from a
FilterSet
. - Parameters:
fs
(*v3.FilterSet): The filter set containing filter items.
- Returns:
- ([]string): A slice of filter strings.
- (error): An error if any issue occurs during processing.
buildResourceFiltersFromGroupBy
func buildResourceFiltersFromGroupBy(groupBy []v3.AttributeKey) []string
- Purpose: Builds a list of ClickHouse filter strings from a slice of
AttributeKey
representing group-by attributes. - Parameters:
groupBy
([]v3.AttributeKey): A slice ofAttributeKey
representing the group-by attributes.
- Returns:
- ([]string): A slice of filter strings.
buildResourceFiltersFromAggregateAttribute
func buildResourceFiltersFromAggregateAttribute(aggregateAttribute v3.AttributeKey) string
- Purpose: Builds a ClickHouse filter string for an aggregate attribute.
- Parameters:
aggregateAttribute
(v3.AttributeKey): The aggregate attribute.
- Returns:
- (string): The generated filter string.
BuildResourceSubQuery
func BuildResourceSubQuery(dbName, tableName string, bucketStart, bucketEnd int64, fs *v3.FilterSet, groupBy []v3.AttributeKey, aggregateAttribute v3.AttributeKey, isLiveTail bool) (string, error)
- Purpose: Builds a complete ClickHouse subquery for filtering resource attributes.
- Parameters:
dbName
(string): The database name.tableName
(string): The table name.bucketStart
(int64): The start timestamp for the bucket.bucketEnd
(int64): The end timestamp for the bucket.fs
(*v3.FilterSet): The filter set.groupBy
([]v3.AttributeKey): Group by attributes.aggregateAttribute
(v3.AttributeKey): Aggregate attribute.isLiveTail
(bool): Indicates if it's a live tail query.
- Returns:
- (string): The generated subquery.
- (error): An error if any.
Code Examples
Not Applicable - The functions are designed to build query strings and don't have usage code examples.
Include in Getting Started: NO