* TP-52454| created zenduty integration * TP-52454| added migration script for external team table * TP-52454| added extra logs * TP-52454| modified logs * TP-52454|added extra logs * TP-52454| changed post url for zenduty * TP-52454| fixed bugs in zenduty client * TP-52454| created constants for environmental varibales * TP-52454| enabled zenduty if severity is less than or equal to the defined config
77 lines
2.7 KiB
Go
77 lines
2.7 KiB
Go
package alertClient
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/spf13/viper"
|
|
"go.uber.org/zap"
|
|
"houston/common/util"
|
|
"houston/logger"
|
|
"houston/pkg/rest"
|
|
request "houston/service/request"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type ZendutyClient struct {
|
|
RestClient rest.HttpRestClient
|
|
DefaultTimeout time.Duration
|
|
BaseURL string
|
|
AuthorizationToken string
|
|
}
|
|
|
|
func NewZendutyClient() *ZendutyClient {
|
|
return &ZendutyClient{
|
|
RestClient: rest.NewHttpRestClient(),
|
|
DefaultTimeout: viper.GetDuration(util.DefaultZendutyTimeout),
|
|
BaseURL: viper.GetString(util.ZendutyBaseUrl),
|
|
AuthorizationToken: viper.GetString(util.ZendutyAuthorizationToken),
|
|
}
|
|
}
|
|
|
|
const createLogTag = "[create-zenduty-alert]"
|
|
|
|
func (zendutyClient *ZendutyClient) CreateIncident(alertRequest request.AlertRequest) error {
|
|
fullURL := zendutyClient.BaseURL + viper.GetString(util.ZendutyIncidentUrl)
|
|
incidentName := alertRequest.Name
|
|
requestHeaders := map[string]string{"Authorization": zendutyClient.AuthorizationToken}
|
|
|
|
zendutyAlertRequest := request.ZendutyAlertRequest{
|
|
Title: alertRequest.Title,
|
|
Summary: alertRequest.Description,
|
|
Service: alertRequest.ExternalTeamMetadata.ServiceId,
|
|
}
|
|
payload, marshalError := json.Marshal(zendutyAlertRequest)
|
|
if marshalError != nil {
|
|
logger.Error(fmt.Sprintf("%s error in json marshalling for %s", createLogTag, incidentName), zap.Error(marshalError))
|
|
return marshalError
|
|
}
|
|
currentResponse, clientErr := zendutyClient.RestClient.PostWithTimeout(fullURL, *bytes.NewBuffer(payload),
|
|
requestHeaders, zendutyClient.DefaultTimeout, nil)
|
|
if clientErr != nil {
|
|
logger.Error(fmt.Sprintf("%s error while posting to zenduty for %s", createLogTag, incidentName), zap.Error(clientErr))
|
|
return clientErr
|
|
}
|
|
if currentResponse == nil || currentResponse.Body == nil {
|
|
logger.Error(fmt.Sprintf("%s response from zenduty is nil for %s", createLogTag, incidentName))
|
|
return errors.New("response from zenduty is nil")
|
|
}
|
|
// Have to store the response details to track zenduty incidents in future
|
|
responseBody, err := io.ReadAll(currentResponse.Body)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("%s error while reading response body", createLogTag), zap.Error(err))
|
|
} else {
|
|
logger.Info(fmt.Sprintf("%s response body from post request for %s is: %s", createLogTag, incidentName, string(responseBody)))
|
|
}
|
|
if currentResponse.StatusCode != http.StatusCreated {
|
|
logger.Error(fmt.Sprintf("%s error occured from zenduty with status code %d for %s", createLogTag,
|
|
currentResponse.StatusCode, incidentName))
|
|
return errors.New("error occurred from zenduty")
|
|
}
|
|
logger.Info(fmt.Sprintf("%s successfully sent alert for %s", createLogTag, incidentName))
|
|
return nil
|
|
}
|