...

Source file src/github.com/prometheus/alertmanager/api/v2/compat.go

Documentation: github.com/prometheus/alertmanager/api/v2

     1  // Copyright 2021 Prometheus Team
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package v2
    15  
    16  import (
    17  	"fmt"
    18  	"time"
    19  
    20  	"github.com/go-openapi/strfmt"
    21  	prometheus_model "github.com/prometheus/common/model"
    22  
    23  	open_api_models "github.com/prometheus/alertmanager/api/v2/models"
    24  	"github.com/prometheus/alertmanager/silence/silencepb"
    25  	"github.com/prometheus/alertmanager/types"
    26  )
    27  
    28  // GettableSilenceFromProto converts *silencepb.Silence to open_api_models.GettableSilence.
    29  func GettableSilenceFromProto(s *silencepb.Silence) (open_api_models.GettableSilence, error) {
    30  	start := strfmt.DateTime(s.StartsAt)
    31  	end := strfmt.DateTime(s.EndsAt)
    32  	updated := strfmt.DateTime(s.UpdatedAt)
    33  	state := string(types.CalcSilenceState(s.StartsAt, s.EndsAt))
    34  	sil := open_api_models.GettableSilence{
    35  		Silence: open_api_models.Silence{
    36  			StartsAt:  &start,
    37  			EndsAt:    &end,
    38  			Comment:   &s.Comment,
    39  			CreatedBy: &s.CreatedBy,
    40  		},
    41  		ID:        &s.Id,
    42  		UpdatedAt: &updated,
    43  		Status: &open_api_models.SilenceStatus{
    44  			State: &state,
    45  		},
    46  	}
    47  
    48  	for _, m := range s.Matchers {
    49  		matcher := &open_api_models.Matcher{
    50  			Name:  &m.Name,
    51  			Value: &m.Pattern,
    52  		}
    53  		f := false
    54  		t := true
    55  		switch m.Type {
    56  		case silencepb.Matcher_EQUAL:
    57  			matcher.IsEqual = &t
    58  			matcher.IsRegex = &f
    59  		case silencepb.Matcher_NOT_EQUAL:
    60  			matcher.IsEqual = &f
    61  			matcher.IsRegex = &f
    62  		case silencepb.Matcher_REGEXP:
    63  			matcher.IsEqual = &t
    64  			matcher.IsRegex = &t
    65  		case silencepb.Matcher_NOT_REGEXP:
    66  			matcher.IsEqual = &f
    67  			matcher.IsRegex = &t
    68  		default:
    69  			return sil, fmt.Errorf(
    70  				"unknown matcher type for matcher '%v' in silence '%v'",
    71  				m.Name,
    72  				s.Id,
    73  			)
    74  		}
    75  		sil.Matchers = append(sil.Matchers, matcher)
    76  	}
    77  
    78  	return sil, nil
    79  }
    80  
    81  // PostableSilenceToProto converts *open_api_models.PostableSilenc to *silencepb.Silence.
    82  func PostableSilenceToProto(s *open_api_models.PostableSilence) (*silencepb.Silence, error) {
    83  	sil := &silencepb.Silence{
    84  		Id:        s.ID,
    85  		StartsAt:  time.Time(*s.StartsAt),
    86  		EndsAt:    time.Time(*s.EndsAt),
    87  		Comment:   *s.Comment,
    88  		CreatedBy: *s.CreatedBy,
    89  	}
    90  	for _, m := range s.Matchers {
    91  		matcher := &silencepb.Matcher{
    92  			Name:    *m.Name,
    93  			Pattern: *m.Value,
    94  		}
    95  		isEqual := true
    96  		if m.IsEqual != nil {
    97  			isEqual = *m.IsEqual
    98  		}
    99  		isRegex := false
   100  		if m.IsRegex != nil {
   101  			isRegex = *m.IsRegex
   102  		}
   103  
   104  		switch {
   105  		case isEqual && !isRegex:
   106  			matcher.Type = silencepb.Matcher_EQUAL
   107  		case !isEqual && !isRegex:
   108  			matcher.Type = silencepb.Matcher_NOT_EQUAL
   109  		case isEqual && isRegex:
   110  			matcher.Type = silencepb.Matcher_REGEXP
   111  		case !isEqual && isRegex:
   112  			matcher.Type = silencepb.Matcher_NOT_REGEXP
   113  		}
   114  		sil.Matchers = append(sil.Matchers, matcher)
   115  	}
   116  	return sil, nil
   117  }
   118  
   119  // AlertToOpenAPIAlert converts internal alerts, alert types, and receivers to *open_api_models.GettableAlert.
   120  func AlertToOpenAPIAlert(alert *types.Alert, status types.AlertStatus, receivers []string) *open_api_models.GettableAlert {
   121  	startsAt := strfmt.DateTime(alert.StartsAt)
   122  	updatedAt := strfmt.DateTime(alert.UpdatedAt)
   123  	endsAt := strfmt.DateTime(alert.EndsAt)
   124  
   125  	apiReceivers := make([]*open_api_models.Receiver, 0, len(receivers))
   126  	for i := range receivers {
   127  		apiReceivers = append(apiReceivers, &open_api_models.Receiver{Name: &receivers[i]})
   128  	}
   129  
   130  	fp := alert.Fingerprint().String()
   131  	state := string(status.State)
   132  	aa := &open_api_models.GettableAlert{
   133  		Alert: open_api_models.Alert{
   134  			GeneratorURL: strfmt.URI(alert.GeneratorURL),
   135  			Labels:       ModelLabelSetToAPILabelSet(alert.Labels),
   136  		},
   137  		Annotations: ModelLabelSetToAPILabelSet(alert.Annotations),
   138  		StartsAt:    &startsAt,
   139  		UpdatedAt:   &updatedAt,
   140  		EndsAt:      &endsAt,
   141  		Fingerprint: &fp,
   142  		Receivers:   apiReceivers,
   143  		Status: &open_api_models.AlertStatus{
   144  			State:       &state,
   145  			SilencedBy:  status.SilencedBy,
   146  			InhibitedBy: status.InhibitedBy,
   147  		},
   148  	}
   149  
   150  	if aa.Status.SilencedBy == nil {
   151  		aa.Status.SilencedBy = []string{}
   152  	}
   153  
   154  	if aa.Status.InhibitedBy == nil {
   155  		aa.Status.InhibitedBy = []string{}
   156  	}
   157  
   158  	return aa
   159  }
   160  
   161  // OpenAPIAlertsToAlerts converts open_api_models.PostableAlerts to []*types.Alert.
   162  func OpenAPIAlertsToAlerts(apiAlerts open_api_models.PostableAlerts) []*types.Alert {
   163  	alerts := []*types.Alert{}
   164  	for _, apiAlert := range apiAlerts {
   165  		alert := types.Alert{
   166  			Alert: prometheus_model.Alert{
   167  				Labels:       APILabelSetToModelLabelSet(apiAlert.Labels),
   168  				Annotations:  APILabelSetToModelLabelSet(apiAlert.Annotations),
   169  				StartsAt:     time.Time(apiAlert.StartsAt),
   170  				EndsAt:       time.Time(apiAlert.EndsAt),
   171  				GeneratorURL: string(apiAlert.GeneratorURL),
   172  			},
   173  		}
   174  		alerts = append(alerts, &alert)
   175  	}
   176  
   177  	return alerts
   178  }
   179  
   180  // ModelLabelSetToAPILabelSet converts prometheus_model.LabelSet to open_api_models.LabelSet.
   181  func ModelLabelSetToAPILabelSet(modelLabelSet prometheus_model.LabelSet) open_api_models.LabelSet {
   182  	apiLabelSet := open_api_models.LabelSet{}
   183  	for key, value := range modelLabelSet {
   184  		apiLabelSet[string(key)] = string(value)
   185  	}
   186  
   187  	return apiLabelSet
   188  }
   189  
   190  // APILabelSetToModelLabelSet converts open_api_models.LabelSet to prometheus_model.LabelSet.
   191  func APILabelSetToModelLabelSet(apiLabelSet open_api_models.LabelSet) prometheus_model.LabelSet {
   192  	modelLabelSet := prometheus_model.LabelSet{}
   193  	for key, value := range apiLabelSet {
   194  		modelLabelSet[prometheus_model.LabelName(key)] = prometheus_model.LabelValue(value)
   195  	}
   196  
   197  	return modelLabelSet
   198  }
   199  

View as plain text