observance

package
v3.0.1 Latest Latest
Warning

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

Go to latest
Published: May 5, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	AppName              string
	LogLevel             string
	SentryURL            string
	Version              string
	MetricsURL           string
	MetricsFlushInterval time.Duration
	// LoggedHeaders is map of header names and log field names. If those headers are present in the request,
	// the method CopyWithRequest will add them to the logger with the given field name.
	// E.g. map[string]string{"FastBill-RequestId": "requestId"} means that if the header "FastBill-RequestId" was found
	// in the request headers the value will be added to the logger under the name "requestId".
	LoggedHeaders map[string]string
}

Config contains all config variables for setting up observability (logging, metrics).

type Fields

type Fields = map[string]interface{}

Fields is a type alias to ease reading.

type Logger

type Logger interface {
	Level() string
	Trace(msg interface{})
	Debug(msg interface{})
	Info(msg interface{})
	Warn(msg interface{})
	Error(msg interface{})
	WithField(key string, value interface{}) Logger
	WithFields(fields Fields) Logger
	WithError(err error) Logger
	SetOutput(w io.Writer)
}

Logger is a general interface to be implemented for multiple loggers.

func NewLogrus

func NewLogrus(logLevel string, appName string, sentryURL string, version string) (Logger, error)

NewLogrus creates a Logrus logger that fulfils the Logger interface with Sentry integration. All log messages will contain app name, pid and hostname/containerID.

type LogrusLogger

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

LogrusLogger wraps Logrus to provide an implementation of the Logger interface.

func (*LogrusLogger) Debug

func (l *LogrusLogger) Debug(msg interface{})

Debug writes a log entry with level "debug".

func (*LogrusLogger) Error

func (l *LogrusLogger) Error(msg interface{})

Error writes a log entry with level "error".

func (*LogrusLogger) Info

func (l *LogrusLogger) Info(msg interface{})

Info writes a log entry with level "info".

func (*LogrusLogger) Level

func (l *LogrusLogger) Level() string

Level returns the log level that was set for the logger. Only entries with that level or above with be logged.

func (*LogrusLogger) SetOutput

func (l *LogrusLogger) SetOutput(w io.Writer)

SetOutput changes where the logs are written to. The default is Stdout.

func (*LogrusLogger) Trace

func (l *LogrusLogger) Trace(msg interface{})

Trace writes a log entry with level "trace".

func (*LogrusLogger) Warn

func (l *LogrusLogger) Warn(msg interface{})

Warn writes a log entry with level "warning".

func (*LogrusLogger) WithError

func (l *LogrusLogger) WithError(err error) Logger

WithError adds an error for logging.

func (*LogrusLogger) WithField

func (l *LogrusLogger) WithField(key string, value interface{}) Logger

WithField adds an additional field for logging.

func (*LogrusLogger) WithFields

func (l *LogrusLogger) WithFields(fields Fields) Logger

WithFields allows to add multiple additional fields to the logging. The argument needs to be of type Fields (map[string]interface{}).

type LogrusTestLogger

type LogrusTestLogger struct {
	LogrusLogger
	// contains filtered or unexported fields
}

LogrusTestLogger implements TestLogger.

func (*LogrusTestLogger) Entries

func (l *LogrusTestLogger) Entries() []TestLogEntry

Entries returns all recorded log entries.

func (*LogrusTestLogger) LastEntry

func (l *LogrusTestLogger) LastEntry() TestLogEntry

LastEntry returns the last recorded log entry.

func (*LogrusTestLogger) Reset

func (l *LogrusTestLogger) Reset()

Reset clears the recorded logs to start fresh.

type Measurer

type Measurer interface {
	Increment(name string)
	SetGauge(name string, value float64)
	SetGaugeInt64(name string, value int64)
	DurationSince(name string, start time.Time)
}

Measurer defines the generic interface capturing metrics.

type Obs

type Obs struct {
	Logger  Logger
	Metrics Measurer
	// contains filtered or unexported fields
}

Obs is a wrapper for all things that helps to observe the operation of the service: logging, monitoring, tracing

func NewObs

func NewObs(config Config) (*Obs, error)

NewObs creates a new observance instance for logging. Optional: If a Sentry URL was provided logs with level error will be sent to Sentry. Optional: If a metrics URL was provided a Prometheus Pushgateway metrics can be captured.

func (*Obs) CopyWithRequest

func (o *Obs) CopyWithRequest(r *http.Request) *Obs

CopyWithRequest creates a new observance and adds request-specific fields to the logger (and maybe at some point to the other parts of observance, too). The headers specified in the config (LoggedHeaders) will be added as log fields with their specified field names.

func (*Obs) PanicRecover

func (o *Obs) PanicRecover()

PanicRecover can be used to recover panics in the main thread and log the messages.

type PrometheusMetrics

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

PrometheusMetrics is an implementation of Measurer.

func NewPrometheusMetrics

func NewPrometheusMetrics(url, appName string, flushInterval time.Duration, logger Logger) *PrometheusMetrics

NewPrometheusMetrics creates a new metrics instance to collect metrics.

func (*PrometheusMetrics) DurationSince

func (m *PrometheusMetrics) DurationSince(name string, start time.Time)

DurationSince is a utility method that accepts a metrics name and start time. It then calculates the duration between the start time and now. The result is converted to milliseconds and then tracked using SetGauge.

func (*PrometheusMetrics) Increment

func (m *PrometheusMetrics) Increment(name string)

Increment is used to count occurances. It can only be used for values that never decrease.

func (*PrometheusMetrics) SetGauge

func (m *PrometheusMetrics) SetGauge(name string, value float64)

SetGauge is used to track a float64 value over time.

func (*PrometheusMetrics) SetGaugeInt64

func (m *PrometheusMetrics) SetGaugeInt64(name string, value int64)

SetGaugeInt64 is used to track an int64 value over time. The integer value will be converted to a float to fit the prometheus API.

type TestLogEntry

type TestLogEntry struct {
	Level   string
	Message string
	Data    map[string]interface{}
}

TestLogEntry represents one recorded log entry in the test logger.

type TestLogger

type TestLogger interface {
	Logger
	LastEntry() TestLogEntry
	Entries() []TestLogEntry
	Reset()
}

TestLogger is an extended Logger interface for testing. It allows to check the logs that were recorded.

func NewTestLogger

func NewTestLogger() TestLogger

NewTestLogger creates a new TestLogger that can be used to create a test observance instance.

Example
logger := NewTestLogger()
obs := &Obs{Logger: logger}
obs.Logger.WithField("testField", "testValue").Error("testMessage1")
fmt.Println(logger.LastEntry().Level, logger.LastEntry().Message, logger.LastEntry().Data)

obs.Logger.Info("testMessage2")
fmt.Printf("%+v", logger.Entries())
Output:

error testMessage1 map[testField:testValue]
[{Level:error Message:testMessage1 Data:map[testField:testValue]} {Level:info Message:testMessage2 Data:map[]}]

Jump to

Keyboard shortcuts

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