github.com/thanos-io/thanos@v0.32.5/pkg/testutil/testpromcompatibility/api_compatibility.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package testpromcompatibility
     5  
     6  import (
     7  	"encoding/json"
     8  	"time"
     9  
    10  	"github.com/prometheus/prometheus/model/labels"
    11  	"github.com/prometheus/prometheus/rules"
    12  )
    13  
    14  type RuleDiscovery struct {
    15  	RuleGroups []*RuleGroup `json:"groups"`
    16  }
    17  
    18  // Same as https://github.com/prometheus/prometheus/blob/c530b4b456cc5f9ec249f771dff187eb7715dc9b/web/api/v1/api.go#L955
    19  // but with Partial Response.
    20  type RuleGroup struct {
    21  	Name           string    `json:"name"`
    22  	File           string    `json:"file"`
    23  	Rules          []Rule    `json:"rules"`
    24  	Interval       float64   `json:"interval"`
    25  	EvaluationTime float64   `json:"evaluationTime"`
    26  	LastEvaluation time.Time `json:"lastEvaluation"`
    27  	Limit          int       `json:"limit"`
    28  
    29  	PartialResponseStrategy string `json:"partialResponseStrategy"`
    30  }
    31  
    32  // https://github.com/prometheus/prometheus/blob/c530b4b456cc5f9ec249f771dff187eb7715dc9b/web/api/v1/api.go#L1016
    33  // MarshalJSON marshals a rulegroup while ensuring that `rules' is always non-empty.
    34  func (r *RuleGroup) MarshalJSON() ([]byte, error) {
    35  	if r.Rules == nil {
    36  		r.Rules = make([]Rule, 0)
    37  	}
    38  	type plain RuleGroup
    39  	return json.Marshal((*plain)(r))
    40  }
    41  
    42  type Rule interface{}
    43  
    44  type AlertAPI struct {
    45  	Alerts []*Alert `json:"alerts"`
    46  }
    47  
    48  type Alert struct {
    49  	Labels      labels.Labels `json:"labels"`
    50  	Annotations labels.Labels `json:"annotations"`
    51  	State       string        `json:"state"`
    52  	ActiveAt    *time.Time    `json:"activeAt,omitempty"`
    53  	Value       string        `json:"value"`
    54  
    55  	PartialResponseStrategy string `json:"partialResponseStrategy"`
    56  }
    57  
    58  type AlertingRule struct {
    59  	// State can be "pending", "firing", "inactive".
    60  	State          string           `json:"state"`
    61  	Name           string           `json:"name"`
    62  	Query          string           `json:"query"`
    63  	Duration       float64          `json:"duration"`
    64  	Labels         labels.Labels    `json:"labels"`
    65  	Annotations    labels.Labels    `json:"annotations"`
    66  	Alerts         []*Alert         `json:"alerts"`
    67  	Health         rules.RuleHealth `json:"health"`
    68  	LastError      string           `json:"lastError,omitempty"`
    69  	EvaluationTime float64          `json:"evaluationTime"`
    70  	LastEvaluation time.Time        `json:"lastEvaluation"`
    71  	// Type of an AlertingRule is always "alerting".
    72  	Type string `json:"type"`
    73  }
    74  
    75  type RecordingRule struct {
    76  	Name           string           `json:"name"`
    77  	Query          string           `json:"query"`
    78  	Labels         labels.Labels    `json:"labels"`
    79  	Health         rules.RuleHealth `json:"health"`
    80  	LastError      string           `json:"lastError,omitempty"`
    81  	EvaluationTime float64          `json:"evaluationTime"`
    82  	LastEvaluation time.Time        `json:"lastEvaluation"`
    83  	// Type of a prometheusRecordingRule is always "recording".
    84  	Type string `json:"type"`
    85  }