Skip to content

Commit

Permalink
Configure alertmanager dispatch logs with insight key (grafana#1379)
Browse files Browse the repository at this point in the history
* Support configuring am dispatch logger separately

Signed-off-by: Christopher Norris <christopher.norris@grafana.com>

* Fix tests and multitenant part

Signed-off-by: Christopher Norris <christopher.norris@grafana.com>

* Update changelog

Signed-off-by: Christopher Norris <christopher.norris@grafana.com>

* Change name and member visibility

Signed-off-by: Christopher Norris <christopher.norris@grafana.com>

* Remove spurious CHANGELOG entry

Signed-off-by: Christopher Norris <christopher.norris@grafana.com>

* Remove additional logging struct

Signed-off-by: Christopher Norris <christopher.norris@grafana.com>

* Add insight key to dispatcher and integration test

Signed-off-by: Christopher Norris <christopher.norris@grafana.com>

* Update CHANGELOG

Signed-off-by: Christopher Norris <christopher.norris@grafana.com>

* Format

Signed-off-by: Christopher Norris <christopher.norris@grafana.com>

* Use SyncBuffer

Signed-off-by: Christopher Norris <christopher.norris@grafana.com>

* Update CHANGELOG.md

Co-authored-by: Marco Pracucci <marco@pracucci.com>
  • Loading branch information
chroberts and pracucci authored Mar 30, 2022
1 parent 1fb9b04 commit 109af94
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [FEATURE] Distributor: Added the ability to forward specifics metrics to alternative remote_write API endpoints. #1052
* [ENHANCEMENT] Ruler: Add more detailed query information to ruler query stats logging. #1411
* [ENHANCEMENT] Admin: Admin API now has some styling. #1482 #1549
* [ENHANCEMENT] Alertmanager: added `insight=true` field to alertmanager dispatch logs. #1379
* [BUGFIX] Query-frontend: do not shard queries with a subquery unless the subquery is inside a shardable aggregation function call. #1542

### Mixin
Expand Down
2 changes: 1 addition & 1 deletion pkg/alertmanager/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func (am *Alertmanager) ApplyConfig(userID string, conf *config.Config, rawCfg s
am.marker,
timeoutFunc,
&dispatcherLimits{tenant: am.cfg.UserID, limits: am.cfg.Limits},
log.With(am.logger, "component", "dispatcher"),
log.With(am.logger, "component", "dispatcher", "insight", "true"),
am.dispatcherMetrics,
)

Expand Down
61 changes: 61 additions & 0 deletions pkg/alertmanager/alertmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/go-kit/log"
"github.com/grafana/dskit/concurrency"
"github.com/grafana/dskit/test"
"github.com/prometheus/alertmanager/cluster/clusterpb"
"github.com/prometheus/alertmanager/config"
Expand Down Expand Up @@ -134,6 +135,66 @@ route:
})
}

func TestDispatcherLoggerInsightKey(t *testing.T) {
var buf concurrency.SyncBuffer
logger := log.NewLogfmtLogger(&buf)

user := "test"
reg := prometheus.NewPedanticRegistry()
am, err := New(&Config{
UserID: user,
Logger: logger,
Limits: &mockAlertManagerLimits{maxDispatcherAggregationGroups: 10},
TenantDataDir: t.TempDir(),
ExternalURL: &url.URL{Path: "/am"},
ShardingEnabled: true,
Replicator: &stubReplicator{},
ReplicationFactor: 1,
PersisterConfig: PersisterConfig{Interval: time.Hour},
}, reg)
require.NoError(t, err)
defer am.StopAndWait()

cfgRaw := `receivers:
- name: 'prod'
route:
group_by: ['alertname']
group_wait: 10ms
group_interval: 10ms
receiver: 'prod'`

cfg, err := config.Load(cfgRaw)
require.NoError(t, err)
require.NoError(t, am.ApplyConfig(user, cfg, cfgRaw))

now := time.Now()
inputAlerts := []*types.Alert{
{
Alert: model.Alert{
Labels: model.LabelSet{
"alertname": model.LabelValue("Alert-1"),
"a": "b",
},
Annotations: model.LabelSet{"foo": "bar"},
StartsAt: now,
EndsAt: now.Add(5 * time.Minute),
GeneratorURL: "http://example.com/prometheus",
},
UpdatedAt: now,
Timeout: false,
},
}
require.NoError(t, am.alerts.Put(inputAlerts...))

test.Poll(t, 3*time.Second, true, func() interface{} {
logs := buf.String()
return strings.Contains(logs, "insight=true")
// Ensure that the dispatcher component emits logs with a "true" insight key,
// identifying these logs to be exposed to end users via the usage insights system.
})
}

var (
alert1 = model.Alert{
Labels: model.LabelSet{"alert": "first"},
Expand Down

0 comments on commit 109af94

Please sign in to comment.