-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into chore/retry-processor
- Loading branch information
Showing
5 changed files
with
234 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type Alerter interface { | ||
Send(ctx context.Context, msg AlertMessage) error | ||
} | ||
|
||
type AlertMessage struct { | ||
Success bool | ||
StatusCode int | ||
Timestamp time.Time | ||
MonitorID string | ||
MonitorName string | ||
Latency int64 | ||
} | ||
|
||
type TelegramProvider struct { | ||
url string | ||
chatID string | ||
} | ||
|
||
type TelegramProviderConfig struct { | ||
Url string | ||
ChatID string | ||
} | ||
|
||
func NewTelegramAlertProvider(config TelegramProviderConfig) *TelegramProvider { | ||
return &TelegramProvider{ | ||
url: config.Url, | ||
} | ||
} | ||
|
||
func (t TelegramProvider) Send(ctx context.Context, msg AlertMessage) error { | ||
if t.url == "" || t.chatID == "" { | ||
return fmt.Errorf("can't make a telegram alert request: some config is not set") | ||
} | ||
|
||
// Perhaps we can use a template file instead. | ||
title := "🔴 Down" | ||
if msg.Success { | ||
title = "✅ Up" | ||
} | ||
text := fmt.Sprintf(title+` | ||
**MonitorID:** %s | ||
**MonitorName:** %s | ||
**StatusCode:** %d | ||
**Latency:** %d | ||
**Timestamp:** %s`, | ||
msg.MonitorID, | ||
msg.MonitorName, | ||
msg.StatusCode, | ||
msg.Latency, | ||
msg.Timestamp.Format(time.RFC3339), | ||
) | ||
payload := map[string]any{ | ||
"chat_id": t.chatID, | ||
"text": text, | ||
"parse_mode": "Markdown", | ||
} | ||
payloadByte, _ := json.Marshal(payload) | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, t.url, bytes.NewReader(payloadByte)) | ||
if err != nil { | ||
return fmt.Errorf("failed to send telegram alert: %w", err) | ||
} | ||
defer req.Body.Close() | ||
|
||
req.Header.Set("Content-Type", "application/json") | ||
|
||
client := http.Client{Timeout: time.Second * 3} | ||
|
||
_, err = client.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("failed to make request: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type IncidentDataReader struct { | ||
db *sql.DB | ||
} | ||
|
||
func NewIncidentDataReader(db *sql.DB) *IncidentDataReader { | ||
return &IncidentDataReader{db: db} | ||
} | ||
|
||
func (r *IncidentDataReader) ReadRelatedIncidents(ctx context.Context, incidentTitle string, monitorID string) ([]Incident, error) { | ||
dbCon, err := r.db.Conn(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer func() { | ||
err := dbCon.Close() | ||
if err != nil { | ||
log.Warn().Stack().Err(err).Msg("Failed to close connection") | ||
} | ||
}() | ||
|
||
rows, err := dbCon.QueryContext(ctx, "SELECT monitor_id, title, description, timestamp, severity, status FROM incident_data WHERE monitor_id = ? AND title = ? ORDER BY created_at DESC", monitorID, incidentTitle) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read related incidents: %w", err) | ||
} | ||
defer func() { | ||
err := rows.Close() | ||
if err != nil { | ||
log.Warn().Stack().Err(err).Msg("Failed to close rows") | ||
} | ||
}() | ||
|
||
var incidents []Incident | ||
for rows.Next() { | ||
var incident Incident | ||
err := rows.Scan(&incident.MonitorID, &incident.Title, &incident.Description, &incident.Timestamp, &incident.Severity, &incident.Status) | ||
if err != nil { | ||
return nil, err | ||
} | ||
incidents = append(incidents, incident) | ||
} | ||
|
||
return incidents, nil | ||
} | ||
|
||
func (r *IncidentDataReader) ReadIncidentByTimestamp(ctx context.Context, incidentTitle string, monitorID string, timestamp time.Time) (Incident, error) { | ||
dbCon, err := r.db.Conn(ctx) | ||
if err != nil { | ||
return Incident{}, err | ||
} | ||
defer func() { | ||
err := dbCon.Close() | ||
if err != nil { | ||
log.Warn().Stack().Err(err).Msg("Failed to close connection") | ||
} | ||
}() | ||
|
||
var incidentDetail Incident | ||
err = dbCon. | ||
QueryRowContext(ctx, "SELECT monitor_id, title, description, timestamp, severity, status FROM incident_data WHERE monitor_id = ? AND title = ? AND timestamp = ?", monitorID, incidentTitle, timestamp). | ||
Scan(incidentDetail.MonitorID, incidentDetail.Title, incidentDetail.Description, incidentDetail.Timestamp, incidentDetail.Severity, incidentDetail.Status) | ||
if err != nil { | ||
return Incident{}, err | ||
} | ||
|
||
return incidentDetail, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters