metrics

package
v0.0.0-...-6d3478e Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2018 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package metrics defines a basic structure foundation for handling logs without much hassle, allow more different entries to be created. Inspired by https://medium.com/@tjholowaychuk/apex-log-e8d9627f4a9a.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBatchEmitterClosed = errors.New("batcher already closed")
)

errors.

View Source
var NilPair = (*Pair)(nil)

NilPair defines a nil starting pair.

Functions

func Apply

func Apply(en *Entry, mods ...EntryMod)

Apply runs all giving EntryMod functions provided on the provided Entry.

Types

type Collector

type Collector interface {
	Collect(string) Entry
}

Collector defines an interface which exposes a single method to collect internal data which is then returned as an Entry.

func Collect

func Collect(fn EntryEmitter) Collector

Collect returns a Collector which executes provided function when called by Metric to run.

type CommitFunction

type CommitFunction func([]Entry) error

CommitFunction defines a function type which is used to process a batch of Entry.

type ConditionalProcessors

type ConditionalProcessors interface {
	Processors
	Can(Entry) bool
}

ConditionalProcessors defines a Processor which first validate it's ability to process a giving Entry.

func Case

func Case(fn FilterFn, procs ...Processors) ConditionalProcessors

Case returns a Processor object with the provided Augmenters and Metrics implemement objects for receiving metric Entries, where entries are filtered out based on a provided function.

type DoFn

type DoFn func(Entry) error

DoFn defines a function type which takes a giving Entry.

type Entry

type Entry struct {
	ID        string      `json:"id"`
	Function  string      `json:"function"`
	File      string      `json:"file"`
	Type      string      `json:"type"`
	Line      int         `json:"line"`
	Level     Level       `json:"level"`
	Field     Field       `json:"fields"`
	Time      time.Time   `json:"time"`
	Message   string      `json:"message"`
	Filter    interface{} `json:"filter"`
	Tags      []string    `json:"tags"`
	Trace     Trace       `json:"trace"`
	Timelapse []Timelapse `json:"timelapse"`
}

Entry represent a giving record of data at a giving period of time.

type EntryEmitter

type EntryEmitter func(string) Entry

EntryEmitter defines a type which returns a entry when runned.

type EntryMod

type EntryMod func(*Entry)

EntryMod defines a function type which receives a pointer to an entry.

func Error

func Error(err error) EntryMod

Error returns a entry where the message is the provided error.Error() value and the error is added as a key-value within the Entry fields.

func Errorf

func Errorf(message string, m ...interface{}) EntryMod

Errorf returns a entry where the message is the provided error.Error() value produced from the message and its provided values and the error is added as a key-value within the Entry fields.

func Info

func Info(message string, m ...interface{}) EntryMod

Info returns an Entry with the level set to Info.

func Message

func Message(message string, m ...interface{}) EntryMod

Message returns a new Entry with the provided Level and message used.

func Partial

func Partial(mods ...EntryMod) EntryMod

Partial returns a new EntryMod which will always apply provided EntryMod to all provided Entry.

func RedAlert

func RedAlert(err error, message string, m ...interface{}) EntryMod

RedAlert returns an Entry with the level set to RedAlertLvl.

func Tags

func Tags(ts ...string) EntryMod

Tags returns an Entry with the tags value set to ts.

func Type

func Type(t string) EntryMod

Type returns an Entry with the type value set to t.

func With

func With(key string, value interface{}) EntryMod

With returns a Entry set to the LogLevel of the previous and adds the giving key-value pair to the entry.

func WithField

func WithField(filter interface{}) EntryMod

WithField returns a Entry and set the Filter to the provided value.

func WithFields

func WithFields(f Field) EntryMod

WithFields adds all field key-value pair into associated Entry returning the Entry.

func WithID

func WithID(id string) EntryMod

WithID returns a Entry and set the ID to the provided value.

func WithMessage

func WithMessage(level Level, message string, m ...interface{}) EntryMod

WithMessage returns a new Entry with the provided Level and message used.

func WithTimelapse

func WithTimelapse(message string, f Field) EntryMod

WithTimelapse returns a Timelapse with associated field and message.

func WithTrace

func WithTrace(t Trace) EntryMod

WithTrace returns itself after setting the giving trace value has the method trace for the giving Entry.

func YellowAlert

func YellowAlert(err error, message string, m ...interface{}) EntryMod

YellowAlert returns an Entry with the level set to YellowAlertLvl.

type Field

type Field map[string]interface{}

Field represents a giving map of values associated with a giving field value.

func (Field) Get

func (p Field) Get(key string) (value interface{}, found bool)

Get collects the value of a key if it exists.

func (Field) GetBool

func (p Field) GetBool(key string) (bool, bool)

GetBool collects the string value of a key if it exists.

func (Field) GetFloat32

func (p Field) GetFloat32(key string) (float32, bool)

GetFloat32 collects the string value of a key if it exists.

func (Field) GetFloat64

func (p Field) GetFloat64(key string) (float64, bool)

GetFloat64 collects the string value of a key if it exists.

func (Field) GetInt

func (p Field) GetInt(key string) (int, bool)

GetInt collects the string value of a key if it exists.

func (Field) GetInt16

func (p Field) GetInt16(key string) (int16, bool)

GetInt16 collects the string value of a key if it exists.

func (Field) GetInt32

func (p Field) GetInt32(key string) (int32, bool)

GetInt32 collects the string value of a key if it exists.

func (Field) GetInt64

func (p Field) GetInt64(key string) (int64, bool)

GetInt64 collects the string value of a key if it exists.

func (Field) GetInt8

func (p Field) GetInt8(key string) (int8, bool)

GetInt8 collects the string value of a key if it exists.

func (Field) GetString

func (p Field) GetString(key string) (string, bool)

GetString collects the string value of a key if it exists.

type FilterFn

type FilterFn func(Entry) bool

FilterFn defines a function type which takes a giving Entry returning a bool to indicate filtering state.

type Level

type Level int

Level defines a int type which represent the a giving level of entry for a giving entry.

const (
	RedAlertLvl    Level = iota // Immediately notify everyone by mail level, because this is bad
	YellowAlertLvl              // Immediately notify everyone but we can wait to tomorrow
	ErrorLvl                    // Error occured with some code due to normal opperation or odd behaviour (not critical)
	InfoLvl                     // Information for view about code operation (replaces Debug, Notice, Trace).
)

level constants

func GetLevel

func GetLevel(lvl string) Level

GetLevel returns Level value for the giving string. It returns -1 if it does not know the level string.

func (Level) String

func (l Level) String() string

String returns the string version of the Level.

type MetricConsumer

type MetricConsumer interface {
	Processors
	Run(<-chan struct{})
}

MetricConsumer exposes a interface which allows the consumption of Entries into a underline system, which can be started through it's run method.

func BatchConsumer

func BatchConsumer(maxSize int, maxwait time.Duration, fn CommitFunction) MetricConsumer

BatchConsumer returns a new instance of a batchConsumer.

type Metrics

type Metrics interface {
	Send(Entry) error
	Emit(...EntryMod) error
	CollectMetrics(string) error
}

Metrics defines an interface with a single method for receiving new Entry objects.

func New

func New(vals ...interface{}) Metrics

New returns a Metrics object with the provided Augmenters and Metrics implemement objects for receiving metric Entries.

type Pair

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

Pair defines a struct for storing a linked pair of key and values.

func Append

func Append(p *Pair, key string, value interface{}) *Pair

Append returns a new Pair with the giving key and with the provded Pair set as it's previous link.

func NewPair

func NewPair(key string, value interface{}) *Pair

NewPair returns a a key-value pair chain for setting fields.

func (*Pair) Append

func (p *Pair) Append(key string, val interface{}) *Pair

Append returns a new pair with the giving key and value and its previous set to this pair.

func (*Pair) Fields

func (p *Pair) Fields() Field

Fields returns all internal pair data as a map.

func (*Pair) Get

func (p *Pair) Get(key string) (value interface{}, found bool)

Get collects the value of a key if it exists.

func (*Pair) GetBool

func (p *Pair) GetBool(key string) (bool, bool)

GetBool collects the string value of a key if it exists.

func (*Pair) GetFloat32

func (p *Pair) GetFloat32(key string) (float32, bool)

GetFloat32 collects the string value of a key if it exists.

func (*Pair) GetFloat64

func (p *Pair) GetFloat64(key string) (float64, bool)

GetFloat64 collects the string value of a key if it exists.

func (*Pair) GetInt

func (p *Pair) GetInt(key string) (int, bool)

GetInt collects the string value of a key if it exists.

func (*Pair) GetInt16

func (p *Pair) GetInt16(key string) (int16, bool)

GetInt16 collects the string value of a key if it exists.

func (*Pair) GetInt32

func (p *Pair) GetInt32(key string) (int32, bool)

GetInt32 collects the string value of a key if it exists.

func (*Pair) GetInt64

func (p *Pair) GetInt64(key string) (int64, bool)

GetInt64 collects the string value of a key if it exists.

func (*Pair) GetInt8

func (p *Pair) GetInt8(key string) (int8, bool)

GetInt8 collects the string value of a key if it exists.

func (*Pair) GetString

func (p *Pair) GetString(key string) (string, bool)

GetString collects the string value of a key if it exists.

func (*Pair) Root

func (p *Pair) Root() *Pair

Root returns the root Pair in the chain which links all pairs together.

type Processors

type Processors interface {
	Handle(Entry) error
}

Processors implements a single method to process a Entry.

func DoWith

func DoWith(do DoFn) Processors

DoWith returns a Metrics object where all entries are applied to the provided function.

func FilterLevel

func FilterLevel(l Level, procs ...Processors) Processors

FilterLevel will return a metrics where all Entry will be filtered by their Entry.Level if the level giving is greater or equal to the provided, then it will be received by the metrics subscribers.

func Switch

func Switch(conditions ...ConditionalProcessors) Processors

Switch returns a new instance of a SwitchMaster.

type Timelapse

type Timelapse struct {
	Message string    `json:"message"`
	Time    time.Time `json:"time"`
	Field   Field     `json:"fields"`
}

Timelapse defines a message attached with a giving time value.

type Trace

type Trace struct {
	File       string    `json:"file"`
	Package    string    `json:"Package"`
	Function   string    `json:"function"`
	LineNumber int       `json:"line_number"`
	Stack      []byte    `json:"stack"`
	Comments   []string  `json:"comments"`
	Time       time.Time `json:"end_time"`
}

Trace defines a structure which contains the stack, start and endtime on a given from a trace call to trace a given call with stack details and execution time.

func NewTrace

func NewTrace(comments ...string) Trace

NewTrace returns a Trace object which is used to track the execution and stack details of a given trace call.

func NewTraceWithCallDepth

func NewTraceWithCallDepth(depth int, comments ...string) Trace

NewTraceWithCallDepth returns a Trace object which is used to track the execution and stack details of a given trace call.

func (Trace) String

func (t Trace) String() string

String returns the giving trace timestamp for the execution time.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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