flexmetrics

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

README

Metrics Server

Go Reference Go

The package go-flexible/flexmetrics provides a default set of configuration for hosting prometheus and pprof metrics.

Configuration

The metric server can be configured through the environment to match setup in the infrastructure.

  • PROMETHEUS_ADDR default: :9090
  • PROMETHEUS_PATH default: /metrics

Examples

Starting server and exposing metrics
// Rely on the package defaults
srv := flexmetrics.New()
srv.Run(ctx)

// Or bring your own
httpServer := &http.Server{
        Addr: ":8081",
}
srv := flexmetrics.New(
        flexmetrics.WithServer(httpServer),
        flexmetrics.WithPath("/__/metrics"),
)
srv.Run(ctx)

pprof metrics will be exposed on:

/debug/pprof/
/debug/pprof/cmdline
/debug/pprof/profile
/debug/pprof/symbol
/debug/pprof/trace

Documentation

Index

Examples

Constants

View Source
const (
	// DefaultAddr is the port that we listen to the prometheus path on by default.
	DefaultAddr = "0.0.0.0:9090"

	// DefaultPath is the path where we expose prometheus by default.
	DefaultPath = "/metrics"

	// DefaultReadTimeout is the default read timeout for the http server.
	DefaultReadTimeout = 5 * time.Second

	// DefaultReadHeaderTimeout is the default read header timeout for the http server.
	DefaultReadHeaderTimeout = 1 * time.Second

	// DefaultIdleTimeout is the default idle timeout for the http server.
	DefaultIdleTimeout = 1 * time.Second

	// DefaultWriteTimeout is the default write timeout for the http server.
	DefaultWriteTimeout = 15 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger interface {
	Printf(format string, v ...interface{})
}

Logger defines any logger able to call Printf.

type Option

type Option func(s *Server)

Option is a type of func that allows you change defaults of the *Server returned by New.

func WithAddr

func WithAddr(addr string) Option

WithAddr allows you to specify the address the HTTP server should bind to. If provided, this option will override the use of METRICS_ADDR environment variable.

func WithLogger

func WithLogger(l Logger) Option

WithLogger allows you to set a logger for the server.

func WithPath

func WithPath(path string) Option

WithPath allows you to specify the path Prometheus metrics should be served on. If provided, this option will override the use of METRICS_PROMETHEUS_PATH environment variable.

func WithServer

func WithServer(server *http.Server) Option

WithServer allows you to provide your own HTTP server to be used to serve metrics.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server represents a prometheus metrics server.

func New

func New(options ...Option) *Server

New creates a new default metrics server.

func (*Server) Halt

func (s *Server) Halt(ctx context.Context) error

Halt will attempt to gracefully shut down the server.

func (*Server) Run

func (s *Server) Run(ctx context.Context) error

Run will start the metrics server.

Example
srv := New()
_ = srv.Run(ctx)
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL