stats

package
v1.20220411.3 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2022 License: MIT Imports: 14 Imported by: 2

Documentation

Overview

Package stats includes helpers for writing stats to collectors by adding listeners to logger instances.

It also includes sub-packages for working with distributed tracing.

Index

Constants

View Source
const (
	TagClass     string = "class"
	TagContainer string = "container"
	TagEnv       string = "env"
	TagError     string = "error"
	TagHostname  string = "hostname"
	TagJob       string = "job"
	TagService   string = "service"
	TagSeverity  string = "severity"
	TagVersion   string = "version"
)

Tag names are names for tags, either on metrics or traces.

View Source
const (
	FilterNameSanitization        = "sanitization"
	ListenerNameStats      string = "stats"
)

Specialized / default values

View Source
const (
	// EventAlertTypeInfo is the "info" AlertType for events
	EventAlertTypeInfo = "info"
	// EventAlertTypeError is the "error" AlertType for events
	EventAlertTypeError = "error"
	// EventAlertTypeWarning is the "warning" AlertType for events
	EventAlertTypeWarning = "warning"
	// EventAlertTypeSuccess is the "success" AlertType for events
	EventAlertTypeSuccess = "success"
)
View Source
const (
	// EventPriorityNormal is the "normal" Priority for events.
	EventPriorityNormal = "normal"
	// EventPriorityLow is the "low" Priority for events.
	EventPriorityLow = "low"
)
View Source
const (
	MetricNameError string = string(logger.Error)
)

MetricNames are names we use when sending data to the collectors.

Variables

This section is empty.

Functions

func AddDefaultTags added in v0.3.0

func AddDefaultTags(collector Collector, serviceName, serviceEnv, container string)

AddDefaultTags adds default tags to a stats collector.

func AddDefaultTagsFromEnv

func AddDefaultTagsFromEnv(collector Collector)

AddDefaultTagsFromEnv adds default tags to a collector from environment values.

func AddErrorListeners

func AddErrorListeners(log logger.Listenable, stats Collector, opts ...AddListenerOption)

AddErrorListeners adds error listeners.

func AddErrorListenersByClass added in v1.20201204.1

func AddErrorListenersByClass(log logger.Listenable, stats Collector, opts ...AddListenerOption)

AddErrorListenersByClass adds error listeners that add an exception class tag.

NOTE: this will create many tag values if you do not use exceptions correctly, that is, if you put variable data in the exception class. If there is any doubt which of these to use (AddErrorListeners or AddErrorListenersByClass) use the version that does not add the class information (AddErrorListeners).

func GetLoggerLabelsAsTags added in v1.20210918.2

func GetLoggerLabelsAsTags(ctx context.Context) (tags []string)

GetLoggerLabelsAsTags reads the logger labels map off the context and returns the keys and values formatted as a slice of stats tags.

func Runtime

func Runtime(collector Collector)

Runtime reports golang vm runtime stats.

func SplitTag added in v1.20210103.1

func SplitTag(tag string) (key, value string)

SplitTag splits a given tag in a key and a value

func Tag

func Tag(key, value string) string

Tag formats a tag with a given key and value. For tags in the form `key` use an empty string for the value.

Types

type AddListenerOption added in v1.20210917.5

type AddListenerOption func(*AddListenerOptions)

AddListenerOption mutates AddListenerOptions

func OptIncludeLoggerLabelsAsTags added in v1.20210917.5

func OptIncludeLoggerLabelsAsTags(include bool) AddListenerOption

OptIncludeLoggerLabelsAsTags includes logger labels as tags.

func OptRequestSanitizeDefaults added in v1.20210917.5

func OptRequestSanitizeDefaults(opts ...sanitize.RequestOption) AddListenerOption

OptRequestSanitizeDefaults includes logger labels as tags.

type AddListenerOptions added in v1.20210917.5

type AddListenerOptions struct {
	IncludeLoggerLabelsAsTags bool
	RequestSanitizeDefaults   []sanitize.RequestOption
}

AddListenerOptions are options for adding listeners.

func NewAddListenerOptions added in v1.20210917.5

func NewAddListenerOptions(opts ...AddListenerOption) AddListenerOptions

NewAddListenerOptions creates a new add listener options.

func (AddListenerOptions) GetLoggerLabelsAsTags added in v1.20210918.2

func (options AddListenerOptions) GetLoggerLabelsAsTags(ctx context.Context) (tags []string)

GetLoggerLabelsAsTags gets the logger tags from a context if they're set to be included.

type Collector

type Collector interface {
	Taggable
	Count(name string, value int64, tags ...string) error
	Increment(name string, tags ...string) error
	Gauge(name string, value float64, tags ...string) error
	Histogram(name string, value float64, tags ...string) error
	Distribution(name string, value float64, tags ...string) error
	TimeInMilliseconds(name string, value time.Duration, tags ...string) error
	Flush() error
	Close() error
}

Collector is a stats collector.

type Event

type Event struct {
	// Title of the event.  Required.
	Title string
	// Text is the description of the event.  Required.
	Text string
	// Timestamp is a timestamp for the event.  If not provided, the dogstatsd
	// server will set this to the current time.
	Timestamp time.Time
	// Hostname for the event.
	Hostname string
	// AggregationKey groups this event with others of the same key.
	AggregationKey string
	// Priority of the event.  Can be statsd.Low or statsd.Normal.
	Priority string
	// SourceTypeName is a source type for the event.
	SourceTypeName string
	// AlertType can be statsd.Info, statsd.Error, statsd.Warning, or statsd.Success.
	// If absent, the default value applied by the dogstatsd server is Info.
	AlertType string
	// Tags for the event.
	Tags []string
}

Event is an event to be collected.

func (Event) Check

func (e Event) Check() error

Check verifies that an event is valid.

type EventCollector

type EventCollector interface {
	Taggable
	SendEvent(Event) error
	CreateEvent(title, text string, tags ...string) Event
}

EventCollector is a collector for events.

type MockCollector

type MockCollector struct {
	Field struct {
		Namespace   string
		DefaultTags []string
	}

	Metrics     chan MockMetric
	Errors      chan error
	FlushErrors chan error
	CloseErrors chan error
}

MockCollector is a mocked collector for stats.

func NewMockCollector

func NewMockCollector(capacity int) *MockCollector

NewMockCollector returns a new mock collector.

func (*MockCollector) AddDefaultTag

func (mc *MockCollector) AddDefaultTag(name, value string)

AddDefaultTag adds a default tag.

func (*MockCollector) AddDefaultTags added in v1.20201204.1

func (mc *MockCollector) AddDefaultTags(tags ...string)

AddDefaultTags adds default tags.

func (*MockCollector) AllMetrics added in v1.20210103.1

func (mc *MockCollector) AllMetrics() (output []MockMetric)

AllMetrics returns all the metrics from the collector.

func (MockCollector) Close added in v1.20201204.1

func (mc MockCollector) Close() error

Close returns an error from the errors channel if any.

func (MockCollector) Count

func (mc MockCollector) Count(name string, value int64, tags ...string) error

Count adds a mock count event to the event stream.

func (MockCollector) DefaultTags

func (mc MockCollector) DefaultTags() []string

DefaultTags returns the default tags set.

func (MockCollector) Distribution added in v1.20210103.1

func (mc MockCollector) Distribution(name string, value float64, tags ...string) error

Distribution adds a mock count event to the event stream with value (1).

func (MockCollector) Flush added in v1.20201204.1

func (mc MockCollector) Flush() error

Flush does nothing on a MockCollector.

func (MockCollector) Gauge

func (mc MockCollector) Gauge(name string, value float64, tags ...string) error

Gauge adds a mock count event to the event stream with value (1).

func (*MockCollector) GetCount added in v1.20201204.1

func (mc *MockCollector) GetCount(metricName string) (count int)

GetCount returns the number of events logged for a given metric name.

func (*MockCollector) GetTagCount added in v1.20210428.4

func (mc *MockCollector) GetTagCount(metricName string, tagName string) (count int)

GetTagCount returns the number of events logged for a given metric name & tag name.

func (MockCollector) Histogram

func (mc MockCollector) Histogram(name string, value float64, tags ...string) error

Histogram adds a mock count event to the event stream with value (1).

func (MockCollector) Increment

func (mc MockCollector) Increment(name string, tags ...string) error

Increment adds a mock count event to the event stream with value (1).

func (MockCollector) TimeInMilliseconds

func (mc MockCollector) TimeInMilliseconds(name string, value time.Duration, tags ...string) error

TimeInMilliseconds adds a mock time in millis event to the event stream with a value.

type MockEventCollector

type MockEventCollector struct {
	Events chan Event
	// contains filtered or unexported fields
}

MockEventCollector is a mocked collector for stats.

func NewMockEventCollector

func NewMockEventCollector() *MockEventCollector

NewMockEventCollector returns a new mock collector.

func (*MockEventCollector) AddDefaultTag

func (mec *MockEventCollector) AddDefaultTag(name, value string)

AddDefaultTag adds a default tag.

func (*MockEventCollector) AddDefaultTags added in v1.20201204.1

func (mec *MockEventCollector) AddDefaultTags(tags ...string)

AddDefaultTags adds default tags.

func (MockEventCollector) CreateEvent

func (mec MockEventCollector) CreateEvent(title, text string, tags ...string) Event

CreateEvent creates a mock event with the default tags.

func (MockEventCollector) DefaultTags

func (mec MockEventCollector) DefaultTags() []string

DefaultTags returns the default tags set.

func (MockEventCollector) SendEvent

func (mec MockEventCollector) SendEvent(e Event) error

SendEvent sends an event.

type MockMetric

type MockMetric struct {
	Name               string
	Count              int64
	Gauge              float64
	Histogram          float64
	Distribution       float64
	TimeInMilliseconds float64
	Tags               []string
}

MockMetric is a mock metric.

type MultiCollector added in v1.20201204.1

type MultiCollector []Collector

MultiCollector is a class that wraps a set of statsd collectors

func (MultiCollector) AddDefaultTag added in v1.20201204.1

func (collectors MultiCollector) AddDefaultTag(name, value string)

AddDefaultTag implements Taggable.

func (MultiCollector) AddDefaultTags added in v1.20201204.1

func (collectors MultiCollector) AddDefaultTags(tags ...string)

AddDefaultTags implements Taggable.

func (MultiCollector) Close added in v1.20201204.1

func (collectors MultiCollector) Close() (err error)

Close closes all collectors.

func (MultiCollector) Count added in v1.20201204.1

func (collectors MultiCollector) Count(name string, value int64, tags ...string) (err error)

Count increments a counter by a value and writes to the collectors.

func (MultiCollector) DefaultTags added in v1.20201204.1

func (collectors MultiCollector) DefaultTags() (output []string)

DefaultTags returns the unique default tags for the collectors.

func (MultiCollector) Distribution added in v1.20210103.1

func (collectors MultiCollector) Distribution(name string, value float64, tags ...string) (err error)

Distribution sets a distribution value and writes to the collectors.

func (MultiCollector) Flush added in v1.20201204.1

func (collectors MultiCollector) Flush() (err error)

Flush forces a flush on all collectors.

func (MultiCollector) Gauge added in v1.20201204.1

func (collectors MultiCollector) Gauge(name string, value float64, tags ...string) (err error)

Gauge sets a gauge value and writes to the collectors.

func (MultiCollector) HasTagKey added in v1.20210103.1

func (collectors MultiCollector) HasTagKey(tagKey string) bool

HasTagKey returns if the collector has a given tag key in *any* collector's default tags.

func (MultiCollector) Histogram added in v1.20201204.1

func (collectors MultiCollector) Histogram(name string, value float64, tags ...string) (err error)

Histogram sets a histogram value and writes to the collectors.

func (MultiCollector) Increment added in v1.20201204.1

func (collectors MultiCollector) Increment(name string, tags ...string) (err error)

Increment increments a counter by 1 and writes to the collectors.

func (MultiCollector) TimeInMilliseconds added in v1.20201204.1

func (collectors MultiCollector) TimeInMilliseconds(name string, value time.Duration, tags ...string) (err error)

TimeInMilliseconds sets a timing value and writes to the different hosts

type Printer added in v1.20210103.1

type Printer struct {
	Field struct {
		Namespace   string
		DefaultTags []string
	}
	Output io.Writer
}

Printer is a collector that prints calls to a given writer.

func NewPrinter added in v1.20210103.1

func NewPrinter(log logger.Log) *Printer

NewPrinter creates a printer from a given logger.

func (*Printer) AddDefaultTag added in v1.20210103.1

func (p *Printer) AddDefaultTag(name, value string)

AddDefaultTag adds a default tag.

func (*Printer) AddDefaultTags added in v1.20210103.1

func (p *Printer) AddDefaultTags(tags ...string)

AddDefaultTags adds default tags.

func (Printer) Close added in v1.20210103.1

func (p Printer) Close() error

Close implemenents stats.Collector.

func (Printer) Count added in v1.20210103.1

func (p Printer) Count(name string, value int64, tags ...string) error

Count implemenents stats.Collector.

func (Printer) DefaultTags added in v1.20210103.1

func (p Printer) DefaultTags() []string

DefaultTags returns the default tags set.

func (Printer) Distribution added in v1.20210103.1

func (p Printer) Distribution(name string, value float64, tags ...string) error

Distribution implemenents stats.Collector.

func (Printer) Flush added in v1.20210103.1

func (p Printer) Flush() error

Flush implemenents stats.Collector.

func (Printer) Gauge added in v1.20210103.1

func (p Printer) Gauge(name string, value float64, tags ...string) error

Gauge implemenents stats.Collector.

func (Printer) Histogram added in v1.20210103.1

func (p Printer) Histogram(name string, value float64, tags ...string) error

Histogram implemenents stats.Collector.

func (Printer) Increment added in v1.20210103.1

func (p Printer) Increment(name string, tags ...string) error

Increment implemenents stats.Collector.

func (Printer) TimeInMilliseconds added in v1.20210103.1

func (p Printer) TimeInMilliseconds(name string, value time.Duration, tags ...string) error

TimeInMilliseconds implemenents stats.Collector.

type Taggable added in v1.0.1

type Taggable interface {
	AddDefaultTag(string, string)
	AddDefaultTags(...string)
	DefaultTags() []string
}

Taggable is an interface for specifying and retrieving default stats tags

Directories

Path Synopsis
Package dbstats provides shims for writing db logger events to a stats collector.
Package dbstats provides shims for writing db logger events to a stats collector.
Package grpcstats provides shims for writing rpc logger events to a stats collector.
Package grpcstats provides shims for writing rpc logger events to a stats collector.
Package httpstats provides shims for writing webutil logger events to a stats collector.
Package httpstats provides shims for writing webutil logger events to a stats collector.

Jump to

Keyboard shortcuts

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