Repository: prometheus/alertmanager
File: api/api.go (Go)

Code before:
	if l == nil {
		l = promslog.NewNopLogger()
	}
	concurrency := opts.Concurrency
	if concurrency < 1 {
		concurrency = runtime.GOMAXPROCS(0)
		if concurrency < 8 {
			concurrency = 8
		}
	}

	v2, err := apiv2.NewAPI(
		opts.Alerts,
		opts.GroupFunc,
		opts.AlertStatusFunc,
		opts.GroupMutedFunc,
		opts.Silences,
		opts.Peer,
		l.With("version", "v2"),
		opts.Registry,
	)
	if err != nil {
		return nil, err
	}

	latency := prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Name:    "alertmanager_api_http_request_duration_seconds",
			Help:    "Histogram of latencies for api HTTP requests.",
			Buckets: prometheus.DefBuckets,
		},
		[]string{"code", "handler", "method"},
	)
	requestsInFlight := prometheus.NewGauge(prometheus.GaugeOpts{
		Name:        "alertmanager_http_requests_in_flight",
		Help:        "Current number of HTTP requests being processed.",
		ConstLabels: prometheus.Labels{"method": "get"},
	})
	concurrencyLimitExceeded := prometheus.NewCounter(prometheus.CounterOpts{
		Name:        "alertmanager_http_concurrency_limit_exceeded_total",
		Help:        "Total number of times an HTTP request failed because the concurrency limit was reached.",
		ConstLabels: prometheus.Labels{"method": "get"},
	})
	if opts.Registry != nil {
		if err := opts.Registry.Register(latency); err != nil {
			return nil, err
		}
		if err := opts.Registry.Register(requestsInFlight); err != nil {
			return nil, err
		}
		if err := opts.Registry.Register(concurrencyLimitExceeded); err != nil {

Review comment (suggestion):
Thinking about this a bit more. What if we pass the `alertmanager_http_request_duration_seconds` metric var from `main.go` to `api.New()`? We'd need to add `code` to the metric, but I think it would make more sense to have a single metric since it's all on the same HTTP server/mux.

Code after:
	}
	l := opts.Logger
	if l == nil {
		l = promslog.NewNopLogger()
	}
	concurrency := opts.Concurrency
	if concurrency < 1 {
		concurrency = runtime.GOMAXPROCS(0)
		if concurrency < 8 {
			concurrency = 8
		}
	}

	v2, err := apiv2.NewAPI(
		opts.Alerts,
		opts.GroupFunc,
		opts.AlertStatusFunc,
		opts.GroupMutedFunc,
		opts.Silences,
		opts.Peer,
		l.With("version", "v2"),
		opts.Registry,
	)
	if err != nil {
		return nil, err
	}

	requestsInFlight := prometheus.NewGauge(prometheus.GaugeOpts{
		Name:        "alertmanager_http_requests_in_flight",
		Help:        "Current number of HTTP requests being processed.",
		ConstLabels: prometheus.Labels{"method": "get"},
	})
	concurrencyLimitExceeded := prometheus.NewCounter(prometheus.CounterOpts{
		Name:        "alertmanager_http_concurrency_limit_exceeded_total",
		Help:        "Total number of times an HTTP request failed because the concurrency limit was reached.",
		ConstLabels: prometheus.Labels{"method": "get"},
	})
	if opts.Registry != nil {
		if err := opts.Registry.Register(requestsInFlight); err != nil {
			return nil, err
		}
		if err := opts.Registry.Register(concurrencyLimitExceeded); err != nil {
			return nil, err
		}
	}

	return &API{
		deprecationRouter:        NewV1DeprecationRouter(l.With("version", "v1")),
		v2:                       v2,
		requestDuration:          opts.RequestDuration,
		requestsInFlight:         requestsInFlight,