Skip to main content

threshold_rule_test.go

threshold_rule_test.go - Overview

This file contains unit tests for the threshold rule functionality in the SigNoz query service. It tests the ShouldAlert method of ThresholdRule to verify if it correctly determines when an alert should be triggered based on the provided data and rule conditions. It also includes tests for label normalization and preparing links to logs and traces.

Detailed Documentation

TestThresholdRuleShouldAlert

  • Purpose: Tests the ShouldAlert method of the ThresholdRule struct. It covers various comparison operators (Equals, Greater Than, Not Equals, Less Than), match types (Always, Once, OnAverage, InTotal, Last), and target values to ensure the alert logic works correctly.
  • Parameters:
    • t (*testing.T): Testing object for standard Go testing.
  • Returns: None

TestNormalizeLabelName

  • Purpose: Tests the NormalizeLabelName function from the common package. This function is responsible for normalizing label names by replacing invalid characters with underscores.
  • Parameters:
    • t (*testing.T): Testing object for standard Go testing.
  • Returns: None

TestPrepareLinksToLogs

  • Purpose: Tests the prepareLinksToLogs method of the ThresholdRule struct, ensuring it generates the correct links to logs based on the alert timestamp and labels.
  • Parameters:
    • t (*testing.T): Testing object for standard Go testing.
  • Returns: None

TestPrepareLinksToTraces

  • Purpose: Tests the prepareLinksToTraces method of the ThresholdRule struct, verifying it generates the correct links to traces based on the alert timestamp and labels.
  • Parameters:
    • t (*testing.T): Testing object for standard Go testing.
  • Returns: None

TestThresholdRuleLabelNormalization

  • Purpose: Tests if the labels are correctly normalized when the ShouldAlert method is called.
  • Parameters:
    • t (*testing.T): Testing object for standard Go testing.
  • Returns: None

TestThresholdRuleEvalDelay

  • Purpose: Tests if the Eval method respects the specified evaluation delay when querying data.
  • Parameters:
    • t (*testing.T): Testing object for standard Go testing.
  • Returns: None

TestThresholdRuleClickHouseTmpl

  • Purpose: Tests if the clickhouse query prepared takes the evaluation delay in account.
  • Parameters:
    • t (*testing.T): Testing object for standard Go testing.
  • Returns: None

queryMatcherAny

  • Purpose: A mock struct implementing the telemetrystore.QueryMatcher interface for testing purposes. Its Match method always returns nil.
  • Parameters: None
  • Returns: None

(*queryMatcherAny) Match

  • Purpose: This Match method always returns nil, indicating a successful match for any query.
  • Parameters:
    • x (string): First string to compare
    • y (string): Second string to compare
  • Returns: error: Always returns nil.

TestThresholdRuleUnitCombinations

  • Purpose: Tests the unit conversion logic within the Eval method of ThresholdRule when dealing with different units for the target value and the metric value.
  • Parameters:
    • t (*testing.T): Testing object for standard Go testing.
  • Returns: None

TestThresholdRuleNoData

  • Purpose: Tests the scenario where no data is returned for the query and the AlertOnAbsent flag is set to true.
  • Parameters:
    • t (*testing.T): Testing object for standard Go testing.
  • Returns: None
  • Purpose: Tests if the alert contains related traces.
  • Parameters:
    • t (*testing.T): Testing object for standard Go testing.
  • Returns: None
  • Purpose: Tests if the alert contains related logs.
  • Parameters:
    • t (*testing.T): Testing object for standard Go testing.
  • Returns: None

TestThresholdRuleShiftBy

  • Purpose: Tests shift by on rules.
  • Parameters:
    • t (*testing.T): Testing object for standard Go testing.
  • Returns: None

Code Examples

None

Include in Getting Started: NO