main

mattermost/focalboard

Last updated at: 29/12/2023 09:46

service.go

TLDR

The file service.go in the metrics package provides a service for running a Prometheus server. It includes methods for creating a new Prometheus server, starting the server, and shutting down the server.

Methods

NewMetricsServer

The NewMetricsServer method is a factory method that creates a new Prometheus server. It takes an address, a metrics service, and a logger as parameters and returns a new Service instance.

Run

The Run method starts the Prometheus server. It listens for incoming requests and serves metrics data. It returns an error if there's an issue starting the server.

Shutdown

The Shutdown method shuts down the Prometheus server. It closes the server and stops accepting new requests. It returns an error if there's an issue shutting down the server.

package metrics

import (
	"net/http"

	"github.com/pkg/errors"
	"github.com/prometheus/client_golang/prometheus/promhttp"

	"github.com/mattermost/mattermost-server/v6/shared/mlog"
)

// Service prometheus to run the server.
type Service struct {
	*http.Server
}

// NewMetricsServer factory method to create a new prometheus server.
func NewMetricsServer(address string, metricsService *Metrics, logger mlog.LoggerIFace) *Service {
	return &Service{
		&http.Server{ //nolint:gosec
			Addr: address,
			Handler: promhttp.HandlerFor(metricsService.registry, promhttp.HandlerOpts{
				ErrorLog: logger.StdLogger(mlog.LvlError),
			}),
		},
	}
}

// Run will start the prometheus server.
func (h *Service) Run() error {
	return errors.Wrap(h.Server.ListenAndServe(), "prometheus ListenAndServe")
}

// Shutdown will shutdown the prometheus server.
func (h *Service) Shutdown() error {
	return errors.Wrap(h.Server.Close(), "prometheus Close")
}