Skip to main content

threshold_rule.go

threshold_rule.go - Overview

  1. Overview This file defines the ThresholdRule struct and its associated methods for evaluating threshold-based alerting rules. It handles query execution, result processing, and alert state management.

  2. Detailed Documentation

Type: ThresholdRule

  • Purpose: Represents a threshold-based alerting rule. It contains the logic for querying data, evaluating conditions, and managing alert states.
  • Fields:
    • BaseRule: A pointer to the base rule struct, containing common rule properties.
    • version: string, Indicates the version of the metrics query builder used by the rule ("v3" or "v4").
    • querier: interfaces.Querier, The querier instance used for rules created before the introduction of the new metrics query builder.
    • querierV2: interfaces.Querier, The querier instance used for rules created after the introduction of the new metrics query builder.
    • logsKeys: map[string]v3.AttributeKey, Used for attribute metadata enrichment for logs.
    • spansKeys: map[string]v3.AttributeKey, Used for attribute metadata enrichment for traces.
    • useTraceNewSchema: bool, Indicates whether to use the new traces schema.

Function: NewThresholdRule

  • Purpose: Creates a new ThresholdRule instance.
  • Parameters:
    • id: string, The unique identifier for the rule.
    • p: *PostableRule, A pointer to the postable rule struct containing the rule configuration.
    • featureFlags: interfaces.FeatureLookup, Feature flags for the querier.
    • reader: interfaces.Reader, An interface for reading data.
    • useLogsNewSchema: bool, Indicates whether to use the new logs schema.
    • useTraceNewSchema: bool, Indicates whether to use the new traces schema.
    • opts: ...RuleOption, Optional rule configurations.
  • Returns:
    • *ThresholdRule: A pointer to the newly created ThresholdRule instance.
    • error: An error if the rule creation fails.

Function: Type

  • Purpose: Returns the type of the rule (RuleTypeThreshold).
  • Parameters: None
  • Returns:
    • RuleType: RuleTypeThreshold, the type of the rule.

Function: prepareQueryRange

  • Purpose: Prepares the query range parameters for the rule evaluation based on the provided timestamp.
  • Parameters:
    • ts: time.Time, The timestamp for which the query range is being prepared.
  • Returns:
    • *v3.QueryRangeParamsV3: A pointer to the prepared query range parameters.
    • error: An error if preparing the query range fails.

Function: prepareLinksToLogs

  • Purpose: Prepares links to logs explorer based on the query and labels.
  • Parameters:
    • ts: time.Time, The timestamp for the query.
    • lbls: labels.Labels, The labels associated with the data point.
  • Returns:
    • string: The URL for the logs explorer, or an empty string if preparation fails.

Function: prepareLinksToTraces

  • Purpose: Prepares links to traces explorer based on the query and labels.
  • Parameters:
    • ts: time.Time, The timestamp for the query.
    • lbls: labels.Labels, The labels associated with the data point.
  • Returns:
    • string: The URL for the traces explorer, or an empty string if preparation fails.

Function: GetSelectedQuery

  • Purpose: Returns the selected query name from the rule condition.
  • Parameters: None
  • Returns:
    • string: The selected query name.

Function: buildAndRunQuery

  • Purpose: Builds and runs the query to fetch data for rule evaluation.
  • Parameters:
    • ctx: context.Context, The context for the query execution.
    • ts: time.Time, The timestamp for which the query is executed.
  • Returns:
    • Vector: The result vector containing the query results.
    • error: An error if the query execution fails.

Function: Eval

  • Purpose: Evaluates the threshold rule against the fetched data and manages alert states.
  • Parameters:
    • ctx: context.Context, The context for the rule evaluation.
    • ts: time.Time, The timestamp for the evaluation.
  • Returns:
    • interface{}: The number of active alerts.
    • error: An error if the evaluation fails.

Function: String

  • Purpose: Returns a string representation of the ThresholdRule.
  • Parameters: None
  • Returns:
    • string: A YAML representation of the ThresholdRule.

Function: removeGroupinSetPoints

  • Purpose: Removes points with invalid timestamps or values (NaN, Inf) from a time series.
  • Parameters:
    • series: v3.Series, The input time series.
  • Returns:
    • []v3.Point: A slice of valid data points.
  1. Code Examples Not applicable as the code is mostly infrastructural.

  2. Clarity and Accuracy The documentation is based on the code and aims to provide a clear and accurate description of its functionality.

  3. Markdown & MDX Perfection All markdown is properly formatted.

  4. Edge Cases To Avoid Breaking MDX No MDX breaking elements found.

  5. Getting Started Relevance Include in Getting Started: NO