server

package
v0.30.2 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: 33 Imported by: 0

Documentation

Overview

Package server implements the HTTP and gRPC server used throughout Grafana Agent.

It is a grafana/agent-specific fork of github.com/weaveworks/common/server.

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultConfig = Config{
		GRPC:      DefaultGRPCConfig,
		HTTP:      DefaultHTTPConfig,
		LogLevel:  DefaultLogLevel,
		LogFormat: DefaultLogFormat,
	}

	DefaultHTTPConfig = HTTPConfig{}

	DefaultGRPCConfig = GRPCConfig{}

	DefaultLogLevel = func() logging.Level {
		var lvl logging.Level
		lvl.RegisterFlags(emptyFlagSet)
		return lvl
	}()
	DefaultLogFormat = func() logging.Format {
		var fmt logging.Format
		fmt.RegisterFlags(emptyFlagSet)
		return fmt
	}()
)

Default configuration structs.

View Source
var (
	DefaultFlags = Flags{
		RegisterInstrumentation: true,
		GracefulShutdownTimeout: 30 * time.Second,

		HTTP: DefaultHTTPFlags,
		GRPC: DefaultGRPCFlags,
	}

	DefaultHTTPFlags = HTTPFlags{
		InMemoryAddr:  "agent.internal:12345",
		ListenNetwork: "tcp",
		ListenAddress: "127.0.0.1:12345",
		ReadTimeout:   30 * time.Second,
		WriteTimeout:  30 * time.Second,
		IdleTimeout:   120 * time.Second,
	}

	DefaultGRPCFlags = GRPCFlags{
		InMemoryAddr:          "agent.internal:12346",
		ListenNetwork:         "tcp",
		ListenAddress:         "127.0.0.1:12346",
		MaxRecvMsgSize:        4 * 1024 * 1024,
		MaxSendMsgSize:        4 * 1024 * 1024,
		MaxConcurrentStreams:  100,
		MaxConnectionIdle:     infinity,
		MaxConnectionAge:      infinity,
		MaxConnectionAgeGrace: infinity,
		KeepaliveTime:         2 * time.Hour,
		KeepaliveTimeout:      20 * time.Second,
		MinTimeBetweenPings:   5 * time.Minute,
	}
)

Default options structs.

Functions

func GoKitLogger

func GoKitLogger(l log.Logger) logging.Interface

GoKitLogger creates a logging.Interface from a log.Logger.

func SignalContext

func SignalContext(ctx context.Context, l log.Logger) (context.Context, context.CancelFunc)

SignalContext wraps a ctx which will be canceled if an interrupt is received.

It is invalid to have two simultaneous SignalContexts per binary.

Types

type Config

type Config struct {
	LogLevel  logging.Level  `yaml:"log_level"`
	LogFormat logging.Format `yaml:"log_format"`

	GRPC GRPCConfig `yaml:",inline"`
	HTTP HTTPConfig `yaml:",inline"`
}

Config holds dynamic configuration options for a Server.

func (*Config) UnmarshalYAML

func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML unmarshals the server config with defaults applied.

type DialContextFunc added in v0.25.0

type DialContextFunc func(ctx context.Context, network string, addr string) (net.Conn, error)

DialContextFunc is a function matching the signature of net.Dialer.DialContext.

type Flags

type Flags struct {
	RegisterInstrumentation bool
	GracefulShutdownTimeout time.Duration

	LogSourceIPs       bool
	LogSourceIPsHeader string
	LogSourceIPsRegex  string

	GRPC GRPCFlags
	HTTP HTTPFlags
}

Flags hold static configuration options for a Server.

func (*Flags) RegisterFlags

func (f *Flags) RegisterFlags(fs *flag.FlagSet)

RegisterFlags registers flags for c to the given FlagSet.

type GRPCConfig

type GRPCConfig struct {
	TLSConfig TLSConfig `yaml:"grpc_tls_config"`
}

GRPCConfig holds dynamic configuration options for the gRPC server.

type GRPCFlags

type GRPCFlags struct {
	UseTLS bool

	InMemoryAddr string

	ListenNetwork string
	ListenAddress string // host:port
	ConnLimit     int

	MaxRecvMsgSize           int
	MaxSendMsgSize           int
	MaxConcurrentStreams     uint
	MaxConnectionIdle        time.Duration
	MaxConnectionAge         time.Duration
	MaxConnectionAgeGrace    time.Duration
	KeepaliveTime            time.Duration
	KeepaliveTimeout         time.Duration
	MinTimeBetweenPings      time.Duration
	PingWithoutStreamAllowed bool
}

GRPCFlags hold static configuration options for the gRPC server.

func (GRPCFlags) ListenHostPort added in v0.26.0

func (f GRPCFlags) ListenHostPort() (host string, port int, err error)

ListenHostPort splits the ListenAddress into a listen host and listen port. Returns an error if the ListenAddress isn't valid.

func (*GRPCFlags) RegisterFlags

func (f *GRPCFlags) RegisterFlags(fs *flag.FlagSet)

RegisterFlags registers flags for c to the given FlagSet.

type HTTPConfig

type HTTPConfig struct {
	TLSConfig TLSConfig `yaml:"http_tls_config"`
}

HTTPConfig holds dynamic configuration options for the HTTP server.

type HTTPFlags

type HTTPFlags struct {
	UseTLS bool

	InMemoryAddr string

	ListenNetwork string
	ListenAddress string // host:port
	ConnLimit     int

	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	IdleTimeout  time.Duration
}

HTTPFlags hold static configuration options for the HTTP server.

func (HTTPFlags) ListenHostPort added in v0.26.0

func (f HTTPFlags) ListenHostPort() (host string, port int, err error)

ListenHostPort splits the ListenAddress into a listen host and listen port. Returns an error if the ListenAddress isn't valid.

func (*HTTPFlags) RegisterFlags

func (f *HTTPFlags) RegisterFlags(fs *flag.FlagSet)

RegisterFlags registers flags for c to the given FlagSet.

type HookLogger added in v0.29.0

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

HookLogger is used to temporarily redirect

func (*HookLogger) Log added in v0.29.0

func (hl *HookLogger) Log(kvps ...interface{}) error

Log implements log.Logger.

func (*HookLogger) Set added in v0.29.0

func (hl *HookLogger) Set(l log.Logger)

Set where HookedLogger should tee logs to. If a nil logger is passed, the HookedLogger is disabled.

type Logger

type Logger struct {

	// HookLogger is used to temporarily hijack logs for support bundles.
	HookLogger HookLogger
	// contains filtered or unexported fields
}

Logger implements Go Kit's log.Logger interface. It supports being dynamically updated at runtime.

func NewLogger

func NewLogger(cfg *Config) *Logger

NewLogger creates a new Logger.

func NewLoggerFromLevel

func NewLoggerFromLevel(lvl logging.Level, fmt logging.Format) *Logger

NewLoggerFromLevel creates a new logger from logging.Level and logging.Format.

func (*Logger) ApplyConfig

func (l *Logger) ApplyConfig(cfg *Config) error

ApplyConfig applies configuration changes to the logger.

func (*Logger) Log

func (l *Logger) Log(kvps ...interface{}) error

Log logs a log line.

type Server

type Server struct {
	HTTP       *mux.Router
	HTTPServer *http.Server
	GRPC       *grpc.Server

	// DialContext creates a connection to the given network/address. If address
	// matches the Server's internal HTTP or gRPC address, an internal in-memory
	// connection will be opened.
	DialContext DialContextFunc
	// contains filtered or unexported fields
}

Server wraps an HTTP and gRPC server with some common initialization.

Unless instrumentation is disabled in the Servers config, Prometheus metrics will be automatically generated for the server.

func New

func New(l log.Logger, r prometheus.Registerer, g prometheus.Gatherer, cfg Config, flags Flags) (srv *Server, err error)

New creates a new Server with the given config.

r is used to register Server-specific metrics. If r is nil, no metrics will be registered.

g is used for collecting metrics from the instrumentation handlers, when enabled. If g is nil, a /metrics endpoint will not be registered.

func (*Server) ApplyConfig

func (s *Server) ApplyConfig(cfg Config) error

ApplyConfig applies changes to the Server block.

func (*Server) Close

func (s *Server) Close() error

Close forcibly closes the server's listeners.

func (*Server) GRPCAddress

func (s *Server) GRPCAddress() net.Addr

GRPCAddress returns the GRPC net.Addr of this Server.

func (*Server) HTTPAddress

func (s *Server) HTTPAddress() net.Addr

HTTPAddress returns the HTTP net.Addr of this Server.

func (*Server) Run

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

Run the server until en error is received or the given context is canceled. Run may not be re-called after it exits.

type TLSCipher

type TLSCipher uint16

TLSCipher holds the ID of a tls.CipherSuite.

func (TLSCipher) MarshalYAML

func (c TLSCipher) MarshalYAML() (interface{}, error)

MarshalYAML marshals the name of the cipher suite.

func (*TLSCipher) UnmarshalYAML

func (c *TLSCipher) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML unmarshals the name of a cipher suite to its ID.

type TLSConfig

type TLSConfig struct {
	TLSCertPath              string                    `yaml:"cert_file,omitempty"`
	TLSKeyPath               string                    `yaml:"key_file,omitempty"`
	ClientAuth               string                    `yaml:"client_auth_type,omitempty"`
	ClientCAs                string                    `yaml:"client_ca_file,omitempty"`
	CipherSuites             []TLSCipher               `yaml:"cipher_suites,omitempty"`
	CurvePreferences         []TLSCurve                `yaml:"curve_preferences,omitempty"`
	MinVersion               TLSVersion                `yaml:"min_version,omitempty"`
	MaxVersion               TLSVersion                `yaml:"max_version,omitempty"`
	PreferServerCipherSuites bool                      `yaml:"prefer_server_cipher_suites,omitempty"`
	WindowsCertificateFilter *WindowsCertificateFilter `yaml:"windows_certificate_filter,omitempty"`
}

TLSConfig holds dynamic configuration options for TLS.

type TLSCurve

type TLSCurve tls.CurveID

TLSCurve holds the ID of a TLS elliptic curve.

func (*TLSCurve) MarshalYAML

func (c *TLSCurve) MarshalYAML() (interface{}, error)

MarshalYAML marshals the ID of a TLS elliptic curve into its name.

func (*TLSCurve) UnmarshalYAML

func (c *TLSCurve) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML unmarshals the name of a TLS elliptic curve into its ID.

type TLSVersion

type TLSVersion uint16

TLSVersion holds a TLS version ID.

func (*TLSVersion) MarshalYAML

func (tv *TLSVersion) MarshalYAML() (interface{}, error)

MarshalYAML marshals the ID of a TLS version into its name.

func (*TLSVersion) UnmarshalYAML

func (tv *TLSVersion) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML unmarshals the name of a TLS version into its ID.

type WindowsCertificateFilter added in v0.25.0

type WindowsCertificateFilter struct {
	Server *WindowsServerFilter `yaml:"server,omitempty"`
	Client *WindowsClientFilter `yaml:"client,omitempty"`
}

WindowsCertificateFilter represents the configuration for accessing the Windows store

type WindowsClientFilter added in v0.25.0

type WindowsClientFilter struct {
	IssuerCommonNames []string `yaml:"issuer_common_names,omitempty"`
	SubjectRegEx      string   `yaml:"subject_regex,omitempty"`
	TemplateID        string   `yaml:"template_id,omitempty"`
}

WindowsClientFilter is used to select a client root CA certificate

type WindowsServerFilter added in v0.25.0

type WindowsServerFilter struct {
	Store             string   `yaml:"store,omitempty"`
	SystemStore       string   `yaml:"system_store,omitempty"`
	IssuerCommonNames []string `yaml:"issuer_common_names,omitempty"`
	TemplateID        string   `yaml:"template_id,omitempty"`

	RefreshInterval time.Duration `yaml:"refresh_interval,omitempty"`
}

WindowsServerFilter is used to select a server certificate

Jump to

Keyboard shortcuts

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