...

Source file src/github.com/prometheus/alertmanager/notify/webex/webex.go

Documentation: github.com/prometheus/alertmanager/notify/webex

     1  // Copyright 2022 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 webex
    15  
    16  import (
    17  	"bytes"
    18  	"context"
    19  	"encoding/json"
    20  	"net/http"
    21  
    22  	"github.com/go-kit/log"
    23  	"github.com/go-kit/log/level"
    24  	commoncfg "github.com/prometheus/common/config"
    25  
    26  	"github.com/prometheus/alertmanager/config"
    27  	"github.com/prometheus/alertmanager/notify"
    28  	"github.com/prometheus/alertmanager/template"
    29  	"github.com/prometheus/alertmanager/types"
    30  )
    31  
    32  const (
    33  	// maxMessageSize represents the maximum message length that Webex supports.
    34  	maxMessageSize = 7439
    35  )
    36  
    37  type Notifier struct {
    38  	conf    *config.WebexConfig
    39  	tmpl    *template.Template
    40  	logger  log.Logger
    41  	client  *http.Client
    42  	retrier *notify.Retrier
    43  }
    44  
    45  // New returns a new Webex notifier.
    46  func New(c *config.WebexConfig, t *template.Template, l log.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
    47  	client, err := commoncfg.NewClientFromConfig(*c.HTTPConfig, "webex", httpOpts...)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	n := &Notifier{
    53  		conf:    c,
    54  		tmpl:    t,
    55  		logger:  l,
    56  		client:  client,
    57  		retrier: &notify.Retrier{},
    58  	}
    59  
    60  	return n, nil
    61  }
    62  
    63  type webhook struct {
    64  	Markdown string `json:"markdown"`
    65  	RoomID   string `json:"roomId,omitempty"`
    66  }
    67  
    68  // Notify implements the Notifier interface.
    69  func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
    70  	key, err := notify.ExtractGroupKey(ctx)
    71  	if err != nil {
    72  		return false, err
    73  	}
    74  
    75  	level.Debug(n.logger).Log("incident", key)
    76  
    77  	data := notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
    78  	tmpl := notify.TmplText(n.tmpl, data, &err)
    79  	if err != nil {
    80  		return false, err
    81  	}
    82  
    83  	message := tmpl(n.conf.Message)
    84  	if err != nil {
    85  		return false, err
    86  	}
    87  
    88  	message, truncated := notify.TruncateInBytes(message, maxMessageSize)
    89  	if truncated {
    90  		level.Debug(n.logger).Log("msg", "message truncated due to exceeding maximum allowed length by webex", "truncated_message", message)
    91  	}
    92  
    93  	w := webhook{
    94  		Markdown: message,
    95  		RoomID:   n.conf.RoomID,
    96  	}
    97  
    98  	var payload bytes.Buffer
    99  	if err = json.NewEncoder(&payload).Encode(w); err != nil {
   100  		return false, err
   101  	}
   102  
   103  	resp, err := notify.PostJSON(ctx, n.client, n.conf.APIURL.String(), &payload)
   104  	if err != nil {
   105  		return true, notify.RedactURL(err)
   106  	}
   107  
   108  	shouldRetry, err := n.retrier.Check(resp.StatusCode, resp.Body)
   109  	if err != nil {
   110  		return shouldRetry, err
   111  	}
   112  
   113  	return false, nil
   114  }
   115  

View as plain text