observability

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2021 License: MIT Imports: 20 Imported by: 0

README

observability-go

Documentation

Overview

Package observability for logging, tracing and exporting metrics.

Index

Constants

View Source
const (
	// DefaultMetricsCollectionInterval is 15 seconds.
	DefaultMetricsCollectionInterval = 15
	// DefaultLogLevel is ErrorLevel.
	DefaultLogLevel = ErrorLevel
	// DefaultSamplingRate is 5%.
	DefaultSamplingRate = 0.05
)

Variables

This section is empty.

Functions

func InitDefaults

func InitDefaults(appID, version, tracerEndpoint, sentryDSN string, fields map[string]interface{})

InitDefaults initializes Logger, MetricsExporter and Tracer with default values.

func SetLogger

func SetLogger(logger Logger, fields map[string]interface{})

SetLogger sets global logger.

func SetMetricsExporter

func SetMetricsExporter(exporter MetricsExporter)

SetMetricsExporter sets global metrics exporter.

func SetTracer

func SetTracer(tracer Tracer)

SetTracer sets global tracer.

Types

type Level

type Level uint32

Level log level type.

const (
	// PanicLevel level, highest level of severity. Logs and then calls panic with the
	// message passed to Debug, Info, ...
	PanicLevel Level = iota
	// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
	// logging level is set to Panic.
	FatalLevel
	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
	// Commonly used for hooks to send errors to an error tracking service.
	ErrorLevel
	// WarnLevel level. Non-critical entries that deserve eyes.
	WarnLevel
	// InfoLevel level. General operational entries about what's going on inside the
	// application.
	InfoLevel
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	DebugLevel
	// TraceLevel level. Designates finer-grained informational events than the Debug.
	TraceLevel
)

These are the different logging levels. You can set the logging level to log on your instance of logger.

type Logger

type Logger interface {
	Debug(args ...interface{})
	Debugf(format string, args ...interface{})

	Info(args ...interface{})
	Infof(format string, args ...interface{})

	Error(args ...interface{})
	Errorf(format string, args ...interface{})

	Warning(args ...interface{})
	Warningf(format string, args ...interface{})

	Panic(args ...interface{})
	Panicf(format string, args ...interface{})

	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})

	WithFields(map[string]interface{}) Logger
	WithField(key string, value interface{}) Logger

	SetLevel(level Level)
}

Logger ya know, for logging.

func DefaultLogger

func DefaultLogger() Logger

DefaultLogger creates instance of logger if no config provided.

func GetLogger

func GetLogger() Logger

GetLogger returns global Logger.

func NewLogger

func NewLogger(config LoggerConfiguration, appID, version string, fields map[string]interface{}) Logger

NewLogger creates new Logger instance.

type LoggerConfiguration

type LoggerConfiguration struct {
	MinLevel  Level
	SentryDSN string
}

LoggerConfiguration sets log formatter and severity.

type MetricsConfiguration

type MetricsConfiguration struct {
	CollectionInterval int
}

MetricsConfiguration sets metrics collection interval.

type MetricsExporter

type MetricsExporter interface {
	HTTPHandler(w http.ResponseWriter, r *http.Request)
	AddOperationCall(operationID string, duration time.Duration, result interface{}, fields map[string]interface{})
	Start()
	Stop()
}

MetricsExporter interface.

func GetMetricsExporter

func GetMetricsExporter() MetricsExporter

GetMetricsExporter returns global MetricsExporter or nil, if it wasn't initialized.

func NewMetricsExporter

func NewMetricsExporter(config MetricsConfiguration, fields map[string]interface{}) MetricsExporter

NewMetricsExporter initializes new prometheus exporter.

type Operation

type Operation interface {
	End(result interface{})
}

Operation contains all info to log, trace and gather metrics about concrete operation.

func StartOperation

func StartOperation(ctx context.Context, operationID string, fields map[string]interface{}) (context.Context, Operation)

StartOperation logs info message, starts tracing span and gets initial info for gathering metrics.

type SentryEventIdentityModifier

type SentryEventIdentityModifier struct{}

SentryEventIdentityModifier is a sentry event modifier that simply passes through the event.

func (*SentryEventIdentityModifier) ApplyToEvent

func (m *SentryEventIdentityModifier) ApplyToEvent(event *sentry.Event, hint *sentry.EventHint) *sentry.Event

ApplyToEvent simply returns the event (ignoring the hint).

type SentryHook

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

SentryHook implements logrus.Hook to send errors to sentry.

func NewSentryHook

func NewSentryHook(dsn, release string) (*SentryHook, error)

NewSentryHook creates a sentry hook for logrus given a sentry dsn.

func (*SentryHook) Fire

func (h *SentryHook) Fire(e *log.Entry) error

Fire is an event handler for logrus. This is a part of logrus.Hook.

func (*SentryHook) Levels

func (h *SentryHook) Levels() []log.Level

Levels returns the levels this hook is enabled for. This is a part of logrus.Hook.

type Span

type Span interface {
	End()
}

Span interface for wrapping Jaeger span.

type Tracer

type Tracer interface {
	// Starts a span.
	Start(ctx context.Context, operationID string, fields map[string]interface{}) (context.Context, Span)
}

Tracer interface for wrapping Jaeger tracer.

func NewTracer

func NewTracer(config TracingConfiguration, appID string) Tracer

NewTracer traces exporter with provided values.

type TracingConfiguration

type TracingConfiguration struct {
	TracerEndpoint      string
	SamplingProbability float64
}

TracingConfiguration sets tracer service endpoint and sampling rate.

Jump to

Keyboard shortcuts

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