tgo

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2019 License: Apache-2.0 Imports: 14 Imported by: 7

README

tGo

GoDoc Go Report Card Build Status

Trivago go extensions and utilities. This package contains utility functions and structs that aid trivago with golang development across different projects.

This package and all subpackage match the golang standard library package names along with a "t" prefix. I.e. types that would be placed in the "net" package can be found in the "tnet" package, etc.. This prefix was chosen to allow mixing standard libary and tgo without having to rename package imports all the time.

Documentation

Index

Constants

View Source
const (
	// MetricProcessStart is the metric name storing the time when this process
	// has been started.
	MetricProcessStart = "ProcessStart"
	// MetricGoRoutines is the metric name storing the number of active go
	// routines.
	MetricGoRoutines = "GoRoutines"
	// MetricGoVersion holds the go version as Major*10000+Minor*100+Patch
	MetricGoVersion = "GoVersion"
	// MetricMemoryAllocated holds the currently active memory in bytes
	MetricMemoryAllocated = "GoMemoryAllocated"
	// MetricMemoryNumObjects holds the total number of allocated heap objects
	MetricMemoryNumObjects = "GoMemoryNumObjects"
	// MetricMemoryGCEnabled holds 1 or 0 depending on the state of garbage collection
	MetricMemoryGCEnabled = "GoMemoryGCEnabled"
)

Variables

View Source
var Metric = (*Metrics)(nil)

Metric allows any part of gollum to store and/or modify metric values by name.

View Source
var ProcessStartTime time.Time

ProcessStartTime stores the time this process has started. This value is also stored in the metric MetricProcessStart

View Source
var ShutdownCallback = func() {
	proc, _ := os.FindProcess(os.Getpid())
	proc.Signal(os.Interrupt)
}

ShutdownCallback holds the function that is called when RecoverShutdown detects a panic and could recover. By default this functions sends an os.Interrupt signal to the process. This function can be overwritten for customization.

Functions

func EnableGlobalMetrics

func EnableGlobalMetrics()

EnableGlobalMetrics initializes the Metric global variable (if it is not nil) This function is not threadsafe and should be called once directly after the process started.

func ErrorStackFormatCSV

func ErrorStackFormatCSV(errors []error) string

ErrorStackFormatNumbered returns errors separated by comma

func ErrorStackFormatNewline

func ErrorStackFormatNewline(errors []error) string

ErrorStackFormatNumbered returns errors separated by newline

func ErrorStackFormatNumbered

func ErrorStackFormatNumbered(errors []error) string

ErrorStackFormatNumbered returns errors with a number prefix, separated by newline.

func RecoverShutdown

func RecoverShutdown()

RecoverShutdown will trigger a shutdown via os.Interrupt if a panic was issued. A callstack will be printed like with RecoverTrace(). Typically used as "defer RecoverShutdown()".

func RecoverTrace

func RecoverTrace()

RecoverTrace will trigger a stack trace when a panic was recovered by this function. Typically used as "defer RecoverTrace()".

func ReturnAfter

func ReturnAfter(runtimeLimit time.Duration, callback func()) bool

ReturnAfter calls a function. If that function does not return after the given limit, the function returns regardless of the callback being done or not. This guarantees the call to finish before or at the given limit.

func WithRecover

func WithRecover(callback func())

WithRecover can be used instead of RecoverTrace when using a function without any parameters. E.g. "go WithRecover(myFunction)"

func WithRecoverShutdown

func WithRecoverShutdown(callback func())

WithRecoverShutdown can be used instead of RecoverShutdown when using a function without any parameters. E.g. "go WithRecoverShutdown(myFunction)"

Types

type ErrorStack

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

ErrorStack is a helper to store errors from multiple statements for batch handling. Convenience functions to wrap function calls of the form func() (<type>, error) do exist for all golang base types.

func NewErrorStack

func NewErrorStack() ErrorStack

NewErrorStack creates a new error stack

func (*ErrorStack) Clear

func (stack *ErrorStack) Clear()

Clear removes all errors from the stack

func (ErrorStack) Error

func (stack ErrorStack) Error() string

Error implements the Error interface

func (ErrorStack) Errors

func (stack ErrorStack) Errors() []error

Errors returns all gathered errors as an array

func (ErrorStack) Len

func (stack ErrorStack) Len() int

Len returns the number of error on the stack

func (*ErrorStack) OrNil

func (stack *ErrorStack) OrNil() error

OrNil returns this object or nil of no errors are stored

func (*ErrorStack) Pop

func (stack *ErrorStack) Pop() error

Pop removes an error from the top of the stack and returns it

func (*ErrorStack) Push

func (stack *ErrorStack) Push(err error) bool

Push adds a new error to the top of the error stack. Returns if err != nil.

func (*ErrorStack) PushAndDescribe

func (stack *ErrorStack) PushAndDescribe(message string, err error) bool

PushAndDescribe behaves like Push but allows to prepend a text before the error messages returned by err. The type of err will be lost.

func (*ErrorStack) Pushf

func (stack *ErrorStack) Pushf(message string, args ...interface{})

Pushf adds a new error message to the top of the error stack

func (*ErrorStack) SetFormat

func (stack *ErrorStack) SetFormat(formatter ErrorStackFormatter)

SetFormat set the format used when Error() is called.

func (ErrorStack) Top

func (stack ErrorStack) Top() error

Top returns the error on top of the stack (last error pushed)

type ErrorStackFormatter

type ErrorStackFormatter func([]error) string

ErrorStackFormatter is used by ErroStack to generate a single error string from an array of errors.

type MetricServer

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

MetricServer contains state information about the metric server process

func NewMetricServer

func NewMetricServer() *MetricServer

NewMetricServer creates a new server state for a metric server based on the global Metric variable.

func NewMetricServerFor

func NewMetricServerFor(m *Metrics) *MetricServer

NewMetricServerFor creates a new server state for a metric server based on a custom Metrics variable

func (*MetricServer) Start

func (server *MetricServer) Start(address string)

Start causes a metric server to listen for a specific address and port. If this address/port is accessed a JSON containing all metrics will be returned and the connection is closed. You can use the standard go notation for addresses like ":80".

func (*MetricServer) Stop

func (server *MetricServer) Stop()

Stop notifies the metric server to halt.

type Metrics

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

Metrics is the container struct for runtime metrics that can be used with the metrics server.

func NewMetrics

func NewMetrics() *Metrics

NewMetrics creates a new metrics container. To initialize the global Metrics variable use EnableGlobalMetrics.

func (*Metrics) Add

func (met *Metrics) Add(name string, value int64)

Add adds a number to a given metric.

func (*Metrics) AddF

func (met *Metrics) AddF(name string, value float64)

AddF is Add for float64 values (conversion to int64)

func (*Metrics) AddI

func (met *Metrics) AddI(name string, value int)

AddI is Add for int values (conversion to int64)

func (*Metrics) Close

func (met *Metrics) Close()

Close stops the internal go routines used for e.g. sampling

func (*Metrics) Dec

func (met *Metrics) Dec(name string)

Dec subtracts 1 from a given metric.

func (*Metrics) Dump

func (met *Metrics) Dump() ([]byte, error)

Dump creates a JSON string from all stored metrics.

func (*Metrics) FetchAndReset

func (met *Metrics) FetchAndReset(keys ...string) map[string]int64

FetchAndReset resets all of the given keys to 0 and returns the value before the reset as array. If a given metric does not exist it is ignored. This locks all writes in the process.

func (*Metrics) Get

func (met *Metrics) Get(name string) (int64, error)

Get returns the value of a given metric or rate. If the value does not exists error is non-nil and the returned value is 0.

func (*Metrics) Inc

func (met *Metrics) Inc(name string)

Inc adds 1 to a given metric.

func (*Metrics) InitSystemMetrics

func (met *Metrics) InitSystemMetrics()

InitSystemMetrics Adds system metrics (memory, go routines, etc.) to this metric storage. System metrics need to be updated manually by calling UpdateSystemMetrics().

func (*Metrics) New

func (met *Metrics) New(name string)

New creates a new metric under the given name with a value of 0

func (*Metrics) NewRate

func (met *Metrics) NewRate(baseMetric string, name string, interval time.Duration, numSamples uint8, numMedianSamples uint8, relative bool) error

NewRate creates a new rate. Rates are based on another metric and sample this given base metric every second. When numSamples have been stored, old samples are overriden (oldest first). Retrieving samples via GetRate will calculate the median of a set of means. numMedianSamples defines how many values will be used for mean calculation. A value of 0 will calculate the mean value of all samples. A value of 1 or a value >= numSamples will build a median over all samples. Any other value will divide the stored samples into the given number of groups and build a median over the mean of all these groups. The relative parameter defines if the samples are taking by storing the current value (false) or the difference to the last sample (true).

func (*Metrics) ResetMetrics

func (met *Metrics) ResetMetrics()

ResetMetrics resets all registered key values to 0 expect for system Metrics. This locks all writes in the process.

func (*Metrics) Set

func (met *Metrics) Set(name string, value int64)

Set sets a given metric to a given value.

func (*Metrics) SetB

func (met *Metrics) SetB(name string, value bool)

SetB is Set for boolean values (conversion to 0/1)

func (*Metrics) SetF

func (met *Metrics) SetF(name string, value float64)

SetF is Set for float64 values (conversion to int64)

func (*Metrics) SetI

func (met *Metrics) SetI(name string, value int)

SetI is Set for int values (conversion to int64)

func (*Metrics) Sub

func (met *Metrics) Sub(name string, value int64)

Sub subtracts a number to a given metric.

func (*Metrics) SubF

func (met *Metrics) SubF(name string, value float64)

SubF is Sub for float64 values (conversion to int64)

func (*Metrics) SubI

func (met *Metrics) SubI(name string, value int)

SubI is SubI for int values (conversion to int64)

func (*Metrics) UpdateSystemMetrics

func (met *Metrics) UpdateSystemMetrics()

UpdateSystemMetrics updates all default or system based metrics like memory consumption and number of go routines. This function is not called automatically.

Directories

Path Synopsis
This package provides a simple health check server.
This package provides a simple health check server.

Jump to

Keyboard shortcuts

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