Skip to content

Commit

Permalink
DEV-45327 - Add Logzio Account Id to alert annotations (#25)
Browse files Browse the repository at this point in the history
*What is this feature?*
For Logzio alert notifications, we add as part of the PostableAlert annotations the LogzioAccountId so we can use it on the notification templates for generating urls to the Logzio app

*Why do we need this feature?*
So url generated in alert notifications can redirect to the relevant account
  • Loading branch information
yasmin-tr authored Jun 10, 2024
1 parent 9a6ec9f commit c10e5f7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pkg/services/ngalert/models/alert_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ const (
// AutogeneratedRouteSettingsHashLabel a label name that contains the hash of the notification settings that will be used to send notifications for the alert.
// This should uniquely identify the notification settings (group_by, group_wait, group_interval, repeat_interval, mute_time_intervals) for the alert.
AutogeneratedRouteSettingsHashLabel = "__grafana_route_settings_hash__"

LogzioAccountIdAnnotation = "__logzioAccountId__" // LOGZ.IO GRAFANA CHANGE :: DEV-45327: Add switch to account query param
)

const (
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/ngalert/schedule/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func (sch *schedule) ruleRoutine(grafanaCtx context.Context, key ngmodels.AlertR
processDuration.Observe(sch.clock.Now().Sub(start).Seconds())

start = sch.clock.Now()
alerts := state.FromStateTransitionToPostableAlerts(processedStates, sch.stateManager, sch.appURL)
alerts := state.FromStateTransitionToPostableAlerts(processedStates, sch.stateManager, sch.appURL, e.logzHeaders) // LOGZ.IO GRAFANA CHANGE :: DEV-45327: Add switch to account query param
span.AddEvent("results processed", trace.WithAttributes(
attribute.Int64("state_transitions", int64(len(processedStates))),
attribute.Int64("alerts_to_send", int64(len(alerts.PostableAlerts))),
Expand Down
11 changes: 10 additions & 1 deletion pkg/services/ngalert/state/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package state
import (
"encoding/json"
"fmt"
"net/http" // LOGZ.IO GRAFANA CHANGE :: DEV-45327: Add switch to account query param
"net/url"
"path"
"strconv"
Expand Down Expand Up @@ -139,7 +140,7 @@ func errorAlert(labels, annotations data.Labels, alertState *State, urlStr strin
}
}

func FromStateTransitionToPostableAlerts(firingStates []StateTransition, stateManager *Manager, appURL *url.URL) apimodels.PostableAlerts {
func FromStateTransitionToPostableAlerts(firingStates []StateTransition, stateManager *Manager, appURL *url.URL, logzHeaders http.Header) apimodels.PostableAlerts { // LOGZ.IO GRAFANA CHANGE :: DEV-45327: Add switch to account query param
alerts := apimodels.PostableAlerts{PostableAlerts: make([]models.PostableAlert, 0, len(firingStates))}
var sentAlerts []*State
ts := time.Now()
Expand All @@ -149,6 +150,14 @@ func FromStateTransitionToPostableAlerts(firingStates []StateTransition, stateMa
continue
}
alert := StateToPostableAlert(alertState, appURL)
// LOGZ.IO GRAFANA CHANGE :: DEV-45327: Add switch to account query param
if logzHeaders != nil {
logzAccountId := logzHeaders.Get("Logzio-Account-Id")
if logzAccountId != "" {
alert.Annotations[ngModels.LogzioAccountIdAnnotation] = logzAccountId
}
}
// LOGZ.IO GRAFANA CHANGE :: End
alerts.PostableAlerts = append(alerts.PostableAlerts, *alert)
if alertState.StateReason == ngModels.StateReasonMissingSeries { // do not put stale state back to state manager
continue
Expand Down

0 comments on commit c10e5f7

Please sign in to comment.