Skip to main content

api_params.go

api_params.go - Overview

This file defines API request and response structures related to alerting rules, including the PostableRule and GettableRule structs, as well as functions for parsing, validating, and testing alert rule data.

Detailed Documentation

AlertType

  • Purpose: Defines a type for representing the type of alert (e.g., metric-based, traces-based).
  • Type: string
  • Parameters: None
  • Returns: None

AlertTypeMetric

  • Purpose: Constant representing a metric-based alert type.
  • Value: "METRIC_BASED_ALERT"
  • Parameters: None
  • Returns: None

AlertTypeTraces

  • Purpose: Constant representing a traces-based alert type.
  • Value: "TRACES_BASED_ALERT"
  • Parameters: None
  • Returns: None

AlertTypeLogs

  • Purpose: Constant representing a logs-based alert type.
  • Value: "LOGS_BASED_ALERT"
  • Parameters: None
  • Returns: None

AlertTypeExceptions

  • Purpose: Constant representing an exceptions-based alert type.
  • Value: "EXCEPTIONS_BASED_ALERT"
  • Parameters: None
  • Returns: None

RuleDataKind

  • Purpose: Defines a type representing the format of the rule data (JSON or YAML).
  • Type: string
  • Parameters: None
  • Returns: None

RuleDataKindJson

  • Purpose: Constant representing the JSON rule data format.
  • Value: "json"
  • Parameters: None
  • Returns: None

RuleDataKindYaml

  • Purpose: Constant representing the YAML rule data format.
  • Value: "yaml"
  • Parameters: None
  • Returns: None

ErrFailedToParseJSON

  • Purpose: Error returned when JSON parsing fails.
  • Value: errors.New("failed to parse json")
  • Parameters: None
  • Returns: None

ErrFailedToParseYAML

  • Purpose: Error returned when YAML parsing fails.
  • Value: errors.New("failed to parse yaml")
  • Parameters: None
  • Returns: None

ErrInvalidDataType

  • Purpose: Error returned when the rule data type is invalid.
  • Value: errors.New("invalid data type")
  • Parameters: None
  • Returns: None

PostableRule

  • Purpose: Defines the structure for creating an alerting rule from an HTTP API request.
  • Fields:
    • AlertName (string): The name of the alert.
    • AlertType (AlertType): The type of the alert.
    • Description (string): A description of the alert.
    • RuleType (RuleType): The type of rule (e.g., threshold, PromQL).
    • EvalWindow (Duration): The evaluation window duration.
    • Frequency (Duration): The frequency of rule evaluation.
    • RuleCondition (*RuleCondition): The conditions for the rule.
    • Labels (map[string]string): Labels to associate with the alert.
    • Annotations (map[string]string): Annotations to associate with the alert.
    • Disabled (bool): Indicates if the rule is disabled.
    • Source (string): The source URL where the rule was created.
    • PreferredChannels ([]string): List of preferred channels for sending notifications.
    • Version (string): The version of the rule.
    • Expr (string): Legacy field for PromQL expression.
    • OldYaml (string): Legacy field for old YAML configuration.
  • Parameters: None
  • Returns: None

ParsePostableRule

  • Purpose: Parses a PostableRule from a JSON byte slice.
  • Parameters:
    • content ([]byte): The JSON content to parse.
  • Returns:
    • *PostableRule: A pointer to the parsed PostableRule.
    • error: An error if parsing fails.

parsePostableRule

  • Purpose: Parses a PostableRule from a byte slice with a specified data kind (JSON or YAML).
  • Parameters:
    • content ([]byte): The content to parse.
    • kind (RuleDataKind): The data kind (JSON or YAML).
  • Returns:
    • *PostableRule: A pointer to the parsed PostableRule.
    • error: An error if parsing fails.

parseIntoRule

  • Purpose: Parses and loads content into a provided PostableRule struct and performs validation.
  • Parameters:
    • initRule (PostableRule): The PostableRule struct to load data into.
    • content ([]byte): The content to parse.
    • kind (RuleDataKind): The data kind (JSON or YAML).
  • Returns:
    • *PostableRule: A pointer to the parsed and validated PostableRule.
    • error: An error if parsing or validation fails.

isValidLabelName

  • Purpose: Validates that a label name is valid (starts with a letter or underscore, followed by letters, numbers, or underscores).
  • Parameters:
    • ln (string): The label name to validate.
  • Returns:
    • bool: true if the label name is valid, false otherwise.

isValidLabelValue

  • Purpose: Checks if a label value is a valid UTF-8 string.
  • Parameters:
    • v (string): The label value to validate.
  • Returns:
    • bool: true if the label value is a valid UTF-8 string, false otherwise.

isAllQueriesDisabled

  • Purpose: Checks if all queries in a composite query are disabled.
  • Parameters:
    • compositeQuery (*v3.CompositeQuery): The composite query to check.
  • Returns:
    • bool: true if all queries are disabled, false otherwise.

PostableRule.Validate

  • Purpose: Validates a PostableRule, ensuring that required fields are present and valid.
  • Parameters: None
  • Returns:
    • error: An error if the PostableRule is invalid.

testTemplateParsing

  • Purpose: Tests the parsing of templates in alert labels and annotations.
  • Parameters:
    • rl (*PostableRule): The PostableRule containing the templates.
  • Returns:
    • []error: A slice of errors encountered during template parsing.

GettableRules

  • Purpose: Defines the structure for a list of GettableRule structs.
  • Fields:
    • Rules ([]*GettableRule): A slice of GettableRule pointers.
  • Parameters: None
  • Returns: None

GettableRule

  • Purpose: Defines the structure for an alerting rule that can be retrieved from an API.
  • Fields:
    • Id (string): The ID of the rule.
    • State (model.AlertState): The current state of the alert.
    • PostableRule (PostableRule): The PostableRule data.
    • CreatedAt (*time.Time): The creation timestamp.
    • CreatedBy (*string): The creator's identifier.
    • UpdatedAt (*time.Time): The update timestamp.
    • UpdatedBy (*string): The updater's identifier.
  • Parameters: None
  • Returns: None

Include in Getting Started: NO