Factor out pagerduty

This commit is contained in:
Ian Gulliver
2024-11-26 13:37:13 -06:00
parent a425ced673
commit c40777b689
2 changed files with 77 additions and 56 deletions

64
main.go
View File

@@ -1,11 +1,8 @@
package main package main
import ( import (
"bytes"
"encoding/json"
"fmt" "fmt"
"html/template" "html/template"
"io"
"log" "log"
"net/http" "net/http"
"os" "os"
@@ -14,22 +11,10 @@ import (
"github.com/openai/openai-go" "github.com/openai/openai-go"
) )
type PDAlert struct {
RoutingKey string `json:"routing_key"`
EventAction string `json:"event_action"`
Payload PDPayload `json:"payload"`
}
type PDPayload struct {
Summary string `json:"summary"`
Source string `json:"source"`
Severity string `json:"severity"`
}
type PHandler struct { type PHandler struct {
tmpl *template.Template tmpl *template.Template
routingKey string pd *pdClient
mux *http.ServeMux mux *http.ServeMux
} }
func NewPHandler(routingKey string) (*PHandler, error) { func NewPHandler(routingKey string) (*PHandler, error) {
@@ -45,9 +30,9 @@ func NewPHandler(routingKey string) (*PHandler, error) {
} }
ph := &PHandler{ ph := &PHandler{
tmpl: tmpl, tmpl: tmpl,
routingKey: routingKey, pd: newPDClient(routingKey),
mux: http.NewServeMux(), mux: http.NewServeMux(),
} }
ph.mux.HandleFunc("/{$}", ph.serveRoot) ph.mux.HandleFunc("/{$}", ph.serveRoot)
@@ -80,44 +65,11 @@ func (ph *PHandler) serveRoot(w http.ResponseWriter, r *http.Request) {
return return
} }
buf := &bytes.Buffer{} err = ph.pd.sendAlert(m)
err = json.NewEncoder(buf).Encode(PDAlert{
RoutingKey: ph.routingKey,
EventAction: "trigger",
Payload: PDPayload{
Summary: m,
Source: r.RemoteAddr,
Severity: "critical",
},
})
if err != nil { if err != nil {
sendError(w, http.StatusInternalServerError, "Create PD request: %s", err) sendError(w, http.StatusInternalServerError, "Send PD alert: %s", err)
return return
} }
req, err := http.NewRequest("POST", "https://events.pagerduty.com/v2/enqueue", buf)
if err != nil {
sendError(w, http.StatusInternalServerError, "Create HTTP request: %s", err)
return
}
c := &http.Client{}
res, err := c.Do(req)
if err != nil {
sendError(w, http.StatusInternalServerError, "Call PagerDuty: %s", err)
return
}
body, _ := io.ReadAll(res.Body)
_ = res.Body.Close()
if res.StatusCode != 202 {
sendError(w, http.StatusInternalServerError, "PagerDuty error: %s", string(body))
return
}
sendResponse(w, "Page sent") sendResponse(w, "Page sent")
} }

69
pagerduty.go Normal file
View File

@@ -0,0 +1,69 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
type pdAlert struct {
RoutingKey string `json:"routing_key"`
EventAction string `json:"event_action"`
Payload pdPayload `json:"payload"`
}
type pdPayload struct {
Summary string `json:"summary"`
Source string `json:"source"`
Severity string `json:"severity"`
}
type pdClient struct {
c *http.Client
routingKey string
}
func newPDClient(routingKey string) *pdClient {
return &pdClient{
c: &http.Client{},
routingKey: routingKey,
}
}
func (pd *pdClient) sendAlert(msg string) error {
buf := &bytes.Buffer{}
err := json.NewEncoder(buf).Encode(pdAlert{
RoutingKey: pd.routingKey,
EventAction: "trigger",
Payload: pdPayload{
Summary: msg,
Source: "p",
Severity: "critical",
},
})
if err != nil {
return fmt.Errorf("Encode PD alert: %s", err)
}
req, err := http.NewRequest("POST", "https://events.pagerduty.com/v2/enqueue", buf)
if err != nil {
return fmt.Errorf("Create HTTP request: %s", err)
}
resp, err := pd.c.Do(req)
if err != nil {
return fmt.Errorf("Call PagerDuty: %s", err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != 202 {
return fmt.Errorf("PagerDuty error: %s", string(body))
}
return nil
}