Skip to main content

anomaly.go

anomaly.go - Overview

  1. Overview This file defines the AnomalyRule type, which is responsible for evaluating anomaly detection rules. It uses anomaly detection providers to identify anomalous behavior based on historical data and configured seasonality.

  2. Detailed Documentation

const RuleTypeAnomaly

  • Purpose: Defines a constant string representing the anomaly rule type.
  • Value: "anomaly_rule"

type AnomalyRule

  • Purpose: Represents an anomaly detection rule, encapsulating the base rule and anomaly-specific configurations.
  • Fields:
    • BaseRule: A pointer to the base rule struct, providing common rule functionalities.
    • mtx: A mutex to protect concurrent access to the rule's state.
    • reader: An interfaces.Reader for fetching data.
    • querierV2: An interfaces.Querier for executing metrics queries using the new query builder.
    • provider: An anomaly.Provider that provides anomaly detection capabilities.
    • seasonality: An anomaly.Seasonality that defines the seasonality to be used.

func NewAnomalyRule

  • Purpose: Creates a new AnomalyRule instance.
  • Parameters:
    • id (string): The unique identifier for the rule.
    • p (*baserules.PostableRule): The postable rule containing the rule's configuration.
    • featureFlags (interfaces.FeatureLookup): Provides feature flag lookup capabilities.
    • reader (interfaces.Reader): The reader interface for data retrieval.
    • cache (cache.Cache): The cache implementation for storing results.
    • opts (...baserules.RuleOption): Optional rule options.
  • Returns:
    • *AnomalyRule: A pointer to the newly created AnomalyRule instance.
    • error: An error if the rule creation fails.

func (r *AnomalyRule) Type

  • Purpose: Returns the type of the rule.
  • Returns:
    • baserules.RuleType: The rule type, which is RuleTypeAnomaly.

func (r *AnomalyRule) prepareQueryRange

  • Purpose: Prepares the query range parameters for fetching data.
  • Parameters:
    • ts (time.Time): The timestamp for which to prepare the query.
  • Returns:
    • *v3.QueryRangeParamsV3: The prepared query range parameters.
    • error: An error if preparing the query range fails.

func (r *AnomalyRule) GetSelectedQuery

  • Purpose: Returns the name of the selected query.
  • Returns:
    • string: The name of the selected query from the rule's condition.

func (r *AnomalyRule) buildAndRunQuery

  • Purpose: Builds and runs the query to fetch data and identify anomalies.
  • Parameters:
    • ctx (context.Context): The context for the query.
    • ts (time.Time): The timestamp for which to run the query.
  • Returns:
    • baserules.Vector: A vector of samples representing the query result and anomaly scores.
    • error: An error if building or running the query fails.

func (r *AnomalyRule) Eval

  • Purpose: Evaluates the anomaly rule at a given timestamp.
  • Parameters:
    • ctx (context.Context): The context for evaluation.
    • ts (time.Time): The timestamp for evaluation.
  • Returns:
    • interface{}: The number of active alerts.
    • error: An error if evaluation fails.

func (r *AnomalyRule) String

  • Purpose: Returns a string representation of the anomaly rule (YAML format).
  • Returns:
    • string: YAML representation of the rule.

Include in Getting Started: NO