Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 0 additions & 55 deletions notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -1039,58 +1039,3 @@ func (r RetryStage) exec(ctx context.Context, l *slog.Logger, alerts ...*alert.A
}
}
}

// SetNotifiesStage sets the notification information about passed alerts. The
// passed alerts should have already been sent to the receivers.
type SetNotifiesStage struct {
nflog NotificationLog
recv *nflogpb.Receiver
}

// NewSetNotifiesStage returns a new instance of a SetNotifiesStage.
func NewSetNotifiesStage(l NotificationLog, recv *nflogpb.Receiver) *SetNotifiesStage {
return &SetNotifiesStage{
nflog: l,
recv: recv,
}
}

// Exec implements the Stage interface.
func (n SetNotifiesStage) Exec(ctx context.Context, l *slog.Logger, alerts ...*alert.Alert) (context.Context, []*alert.Alert, error) {
gkey, ok := GroupKey(ctx)
if !ok {
return ctx, nil, errors.New("group key missing")
}

ctx, span := tracer.Start(ctx, "notify.SetNotifiesStage.Exec",
trace.WithAttributes(attribute.String("alerting.group.key", gkey)),
trace.WithAttributes(attribute.Int("alerting.alerts.count", len(alerts))),
trace.WithSpanKind(trace.SpanKindInternal),
)
defer span.End()

firing, ok := FiringAlerts(ctx)
if !ok {
return ctx, nil, errors.New("firing alerts missing")
}

resolved, ok := ResolvedAlerts(ctx)
if !ok {
return ctx, nil, errors.New("resolved alerts missing")
}

repeat, ok := RepeatInterval(ctx)
if !ok {
return ctx, nil, errors.New("repeat interval missing")
}
expiry := 2 * repeat

span.SetAttributes(
attribute.Int("alerting.alerts.firing.count", len(firing)),
attribute.Int("alerting.alerts.resolved.count", len(resolved)),
)

// Extract receiver data from context if present (it's ok for it to be nil).
store, _ := NflogStore(ctx)
return ctx, alerts, n.nflog.Log(n.recv, gkey, firing, resolved, store, expiry)
}
80 changes: 80 additions & 0 deletions notify/set_notifies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package notify

import (
"context"
"errors"
"log/slog"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"

"github.com/prometheus/alertmanager/alert"
"github.com/prometheus/alertmanager/nflog/nflogpb"
)

// SetNotifiesStage sets the notification information about passed alerts. The
// passed alerts should have already been sent to the receivers.
type SetNotifiesStage struct {
nflog NotificationLog
recv *nflogpb.Receiver
}

// NewSetNotifiesStage returns a new instance of a SetNotifiesStage.
func NewSetNotifiesStage(l NotificationLog, recv *nflogpb.Receiver) *SetNotifiesStage {
return &SetNotifiesStage{
nflog: l,
recv: recv,
}
}

// Exec implements the Stage interface.
func (n SetNotifiesStage) Exec(ctx context.Context, l *slog.Logger, alerts ...*alert.Alert) (context.Context, []*alert.Alert, error) {
gkey, ok := GroupKey(ctx)
if !ok {
return ctx, nil, errors.New("group key missing")
}

ctx, span := tracer.Start(ctx, "notify.SetNotifiesStage.Exec",
trace.WithAttributes(attribute.String("alerting.group.key", gkey)),
trace.WithAttributes(attribute.Int("alerting.alerts.count", len(alerts))),
trace.WithSpanKind(trace.SpanKindInternal),
)
defer span.End()

firing, ok := FiringAlerts(ctx)
if !ok {
return ctx, nil, errors.New("firing alerts missing")
}

resolved, ok := ResolvedAlerts(ctx)
if !ok {
return ctx, nil, errors.New("resolved alerts missing")
}

repeat, ok := RepeatInterval(ctx)
if !ok {
return ctx, nil, errors.New("repeat interval missing")
}
expiry := 2 * repeat

span.SetAttributes(
attribute.Int("alerting.alerts.firing.count", len(firing)),
attribute.Int("alerting.alerts.resolved.count", len(resolved)),
)

// Extract receiver data from context if present (it's ok for it to be nil).
store, _ := NflogStore(ctx)
return ctx, alerts, n.nflog.Log(n.recv, gkey, firing, resolved, store, expiry)
}