github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/alertmanager/merger/v1_silences.go (about)

     1  package merger
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	v2_models "github.com/prometheus/alertmanager/api/v2/models"
     8  )
     9  
    10  // V1Silences implements the Merger interface for GET /v1/silences. Unlike for alerts, the API
    11  // definitions for silences are almost identical between v1 and v2. The differences are that the
    12  // fields in the JSON output are ordered differently, and the timestamps have more precision in v1,
    13  // but these differences should not be problematic to clients. Therefore, the implementation
    14  // re-uses the v2 types, with additional handling for the enclosing status/data fields.
    15  type V1Silences struct{}
    16  
    17  func (V1Silences) MergeResponses(in [][]byte) ([]byte, error) {
    18  	type bodyType struct {
    19  		Status string                     `json:"status"`
    20  		Data   v2_models.GettableSilences `json:"data"`
    21  	}
    22  
    23  	silences := make(v2_models.GettableSilences, 0)
    24  	for _, body := range in {
    25  		parsed := bodyType{}
    26  		if err := json.Unmarshal(body, &parsed); err != nil {
    27  			return nil, err
    28  		}
    29  		if parsed.Status != statusSuccess {
    30  			return nil, fmt.Errorf("unable to merge response of status: %s", parsed.Status)
    31  		}
    32  		silences = append(silences, parsed.Data...)
    33  	}
    34  
    35  	merged, err := mergeV2Silences(silences)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	body := bodyType{
    40  		Status: statusSuccess,
    41  		Data:   merged,
    42  	}
    43  
    44  	return json.Marshal(body)
    45  }