Skip to content

Commit

Permalink
fix: handle key not found
Browse files Browse the repository at this point in the history
Signed-off-by: Tossaporn Jiw <[email protected]>
  • Loading branch information
Tossaporn Jiw committed Feb 26, 2025
1 parent 7c2485f commit eaa1c8a
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions notify/msteamsv2/msteamsv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,23 +240,23 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
},
{
Title: "Alert",
Value: data.CommonLabels["alertname"],
Value: extractKV(data.CommonLabels, "alertname"),
},
{
Title: "Summary",
Value: data.CommonAnnotations["summary"],
Value: extractKV(data.CommonAnnotations, "summary"),
},
{
Title: "Severity",
Value: renderSeverity(data.CommonLabels["severity"]),
},
{
Title: "In Host",
Value: data.CommonLabels["instance"],
Value: extractKV(data.CommonLabels, "instance"),
},
{
Title: "Description",
Value: data.CommonAnnotations["description"],
Value: extractKV(data.CommonAnnotations, "description"),
},
{
Title: "Common Labels",
Expand All @@ -273,14 +273,16 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
{
Type: "Action.OpenUrl",
Title: "View details",
URL: data.CommonAnnotations["runbook_url"],
URL: extractKV(data.CommonAnnotations, "runbook_url"),
},
},
},
},
},
}

// Check if summary exists in CommonLabels

if err = json.NewEncoder(&payload).Encode(t); err != nil {
return false, err
}
Expand Down Expand Up @@ -335,3 +337,10 @@ func renderCommonAnnotations(commonLabels template.KV) string {

return commonLabels.Remove(removeList).String()
}

func extractKV(kv template.KV, key string) string {
if v, ok := kv[key]; ok {
return v
}
return ""
}

0 comments on commit eaa1c8a

Please sign in to comment.