New creates a new Alertmanager.
(cfg *Config, reg *prometheus.Registry)
| 168 | |
| 169 | // New creates a new Alertmanager. |
| 170 | func New(cfg *Config, reg *prometheus.Registry) (*Alertmanager, error) { |
| 171 | if cfg.TenantDataDir == "" { |
| 172 | return nil, fmt.Errorf("directory for tenant-specific AlertManager is not configured") |
| 173 | } |
| 174 | |
| 175 | am := &Alertmanager{ |
| 176 | cfg: cfg, |
| 177 | logger: log.With(cfg.Logger, "user", cfg.UserID), |
| 178 | stop: make(chan struct{}), |
| 179 | configHashMetric: promauto.With(reg).NewGauge(prometheus.GaugeOpts{ |
| 180 | Name: "alertmanager_config_hash", |
| 181 | Help: "Hash of the currently loaded alertmanager configuration.", |
| 182 | }), |
| 183 | |
| 184 | rateLimitedNotifications: promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ |
| 185 | Name: "alertmanager_notification_rate_limited_total", |
| 186 | Help: "Number of rate-limited notifications per integration.", |
| 187 | }, []string{"integration"}), // "integration" is consistent with other alertmanager metrics. |
| 188 | |
| 189 | requestDuration: promauto.With(reg).NewHistogramVec( |
| 190 | prometheus.HistogramOpts{ |
| 191 | Name: "alertmanager_http_request_duration_seconds", |
| 192 | Help: "Histogram of latencies for HTTP requests.", |
| 193 | Buckets: prometheus.DefBuckets, |
| 194 | NativeHistogramBucketFactor: 1.1, |
| 195 | NativeHistogramMaxBucketNumber: 100, |
| 196 | NativeHistogramMinResetDuration: 1 * time.Hour, |
| 197 | }, |
| 198 | []string{"handler", "method", "code"}, |
| 199 | ), |
| 200 | } |
| 201 | |
| 202 | am.registry = reg |
| 203 | |
| 204 | // We currently have 3 operational modes: |
| 205 | // 1) Alertmanager clustering with upstream Gossip |
| 206 | // 2) Alertmanager sharding and ring-based replication |
| 207 | // 3) Alertmanager no replication |
| 208 | // These are covered in order. |
| 209 | if cfg.Peer != nil { |
| 210 | level.Debug(am.logger).Log("msg", "starting tenant alertmanager with gossip-based replication") |
| 211 | am.state = cfg.Peer |
| 212 | } else if cfg.ShardingEnabled { |
| 213 | level.Debug(am.logger).Log("msg", "starting tenant alertmanager with ring-based replication") |
| 214 | state := newReplicatedStates(cfg.UserID, cfg.ReplicationFactor, cfg.Replicator, cfg.Store, am.logger, am.registry) |
| 215 | am.state = state |
| 216 | am.persister = newStatePersister(cfg.PersisterConfig, cfg.UserID, state, cfg.Store, am.logger, am.registry) |
| 217 | } else { |
| 218 | level.Debug(am.logger).Log("msg", "starting tenant alertmanager without replication") |
| 219 | am.state = &NilPeer{} |
| 220 | } |
| 221 | |
| 222 | var err error |
| 223 | |
| 224 | notificationFile := filepath.Join(cfg.TenantDataDir, notificationLogSnapshot) |
| 225 | am.nflog, err = nflog.New(nflog.Options{ |
| 226 | SnapshotFile: notificationFile, |
| 227 | Retention: cfg.Retention, |