count

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2022 License: MIT Imports: 11 Imported by: 23

Documentation

Index

Constants

View Source
const (
	DefaultInterval             int64 = 300000
	DefaultResetTimeout         int64 = 300000
	ConfigParameterInterval           = "interval"
	ConfigParameterResetTimeout       = "reset_timeout"
)

Variables

View Source
var CompositeCountersDescriptor = refer.NewDescriptor("pip-services", "counters", "composite", "*", "1.0")
View Source
var LogCountersDescriptor = refer.NewDescriptor("pip-services", "counters", "log", "*", "1.0")
View Source
var NullCountersDescriptor = refer.NewDescriptor("pip-services", "counters", "null", "*", "1.0")

Functions

func NewDefaultCountersFactory

func NewDefaultCountersFactory() *build.Factory

NewDefaultCountersFactory create a new instance of the factory.

Returns: *build.Factory

Types

type AtomicCounter

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

AtomicCounter data object to store measurement for a performance counter. This object is used by CachedCounters to store counters.

func NewAtomicCounter

func NewAtomicCounter(name string, typ CounterType) *AtomicCounter

NewAtomicCounter creates an instance of the data obejct

Parameters:
	- name string a counter name.
	- type CounterType a counter type.
Returns: *Counter

func (*AtomicCounter) Average

func (c *AtomicCounter) Average() float64

Average gets counter _average

Returns: float64

func (*AtomicCounter) CalculateStats

func (c *AtomicCounter) CalculateStats(value float64)

CalculateStats set up _last and calculates stats

Parameters: value float64

func (*AtomicCounter) Count

func (c *AtomicCounter) Count() int64

Count gets counter _count

Returns: int64

func (*AtomicCounter) GetCounter

func (c *AtomicCounter) GetCounter() Counter

GetCounter converts AtomicCounter to Counter struct

Returns: Counter

func (*AtomicCounter) Inc

func (c *AtomicCounter) Inc(value int64)

Inc increments _count for the provided value

Parameters: value float64

func (*AtomicCounter) Last

func (c *AtomicCounter) Last() float64

Last gets counter _last

Returns: float64

func (*AtomicCounter) Max

func (c *AtomicCounter) Max() float64

Max gets counter _max

Returns: float64

func (*AtomicCounter) Min

func (c *AtomicCounter) Min() float64

Min gets counter _min

Returns: float64

func (*AtomicCounter) Name

func (c *AtomicCounter) Name() string

Name gets counter _name

Returns: string

func (*AtomicCounter) SetLast

func (c *AtomicCounter) SetLast(value float64)

SetLast is a setter for the _last

Parameters: value float64

func (*AtomicCounter) SetTime

func (c *AtomicCounter) SetTime(value time.Time)

SetTime is a setter for the _time

Parameters: value time.Time

func (*AtomicCounter) Time

func (c *AtomicCounter) Time() time.Time

Time gets counter _time

Returns: time.Time

func (*AtomicCounter) Type

func (c *AtomicCounter) Type() CounterType

Type gets counter _type

Returns: int

type CachedCounters

type CachedCounters struct {
	Overrides ICachedCountersOverrides
	// contains filtered or unexported fields
}

CachedCounters abstract implementation of performance counters that measures and stores counters in memory. Child classes implement saving of the counters into various destinations.

Configuration parameters:
	- options:
		- interval: interval in milliseconds to save current counters measurements (default: 5 mins)
		- reset_timeout: timeout in milliseconds to reset the counters. 0 disables the reset (default: 0)

func InheritCacheCounters

func InheritCacheCounters(overrides ICachedCountersOverrides) *CachedCounters

InheritCacheCounters inherit cache counters from saver

Parameters:
	- save ICountersSaver
Returns: *CachedCounters

func (*CachedCounters) BeginTiming

func (c *CachedCounters) BeginTiming(ctx context.Context, name string) *CounterTiming

BeginTiming begins measurement of execution time interval. It returns Timing object which has to be called at Timing.EndTiming to end the measurement and update the counter.

Parameters
	- ctx context.Context
	- name string a counter name of Interval type.
Returns: *Timing a Timing callback object to end timing.

func (*CachedCounters) Clear

func (c *CachedCounters) Clear(ctx context.Context, name string)

Clear clears (resets) a counter specified by its name.

Parameters:
	- ctx context.Context
	- name string a counter name to clear.

func (*CachedCounters) ClearAll

func (c *CachedCounters) ClearAll(ctx context.Context)

ClearAll clears (resets) all counters.

Parameters:
	- ctx context.Context

func (*CachedCounters) Configure

func (c *CachedCounters) Configure(ctx context.Context, config *config.ConfigParams)

Configure configures component by passing configuration parameters.

Parameters:
	- ctx context.Context
	- config *config.ConfigParams configuration parameters to be set.

func (*CachedCounters) Dump

func (c *CachedCounters) Dump(ctx context.Context) error

Dump (saves) the current values of counters.

Parameters:
	- ctx context.Context

func (*CachedCounters) EndTiming

func (c *CachedCounters) EndTiming(ctx context.Context, name string, elapsed float64)

EndTiming ends measurement of execution elapsed time and updates specified counter.

see Timing.EndTiming
Parameters:
	- ctx context.Context
	- name string a counter name
	- elapsed float64 execution elapsed time in milliseconds to update the counter.

func (*CachedCounters) Get

func (c *CachedCounters) Get(ctx context.Context, name string, typ CounterType) (*AtomicCounter, bool)

Get a counter specified by its name. It counter does not exist or its type doesn't match the specified type it creates a new one.

Parameters:
	- ctx context.Context
	- name string a counter name to retrieve.
	- typ int a counter type.
Returns: *Counter an existing or newly created counter of the specified type.

func (*CachedCounters) GetAll

func (c *CachedCounters) GetAll() []*AtomicCounter

GetAll gets all captured counters.

Returns: []*AtomicCounter

func (*CachedCounters) GetAllCountersStats

func (c *CachedCounters) GetAllCountersStats() []Counter

GetAllCountersStats gets all captured counters stats.

Returns: []Counter

func (*CachedCounters) Increment

func (c *CachedCounters) Increment(ctx context.Context, name string, value int64)

Increment increments counter by given value.

Parameters:
	- ctx context.Context
	- name string a counter name of Increment type.
	- value int a value to add to the counter.

func (*CachedCounters) IncrementOne

func (c *CachedCounters) IncrementOne(ctx context.Context, name string)

IncrementOne increments counter by 1.

Parameters:
	- ctx context.Context
	- name string a counter name of Increment type.

func (*CachedCounters) Last

func (c *CachedCounters) Last(ctx context.Context, name string, value float64)

Last records the last calculated measurement value. Usually this method is used by metrics calculated externally.

Parameters:
	- ctx context.Context
	- name string a counter name of Last type.
	- value number a last value to record.

func (*CachedCounters) Stats

func (c *CachedCounters) Stats(ctx context.Context, name string, value float64)

Stats calculates min/average/max statistics based on the current and previous values.

Parameters:
	- ctx context.Context
	- name string a counter name of Statistics type
	- value float32 a value to update statistics

func (*CachedCounters) Timestamp

func (c *CachedCounters) Timestamp(ctx context.Context, name string, value time.Time)

Timestamp records the given timestamp.

Parameters:
	- ctx context.Context
	- name string a counter name of Timestamp type.
	- value time.Time a timestamp to record.

func (*CachedCounters) TimestampNow

func (c *CachedCounters) TimestampNow(ctx context.Context, name string)

TimestampNow records the current time as a timestamp.

Parameters:
	- ctx context.Context
	- name string a counter name of Timestamp type.

type CompositeCounters

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

CompositeCounters aggregates all counters from component references under a single component. It allows capturing metrics and conveniently send them to multiple destinations.

	References:
		- *:counters:*:*:1.0 (optional) ICounters components to pass collected measurements
	see ICounters

	Example:
 	type MyComponent {
			_counters CompositeCounters = new CompositeCounters();
 	}
		func (mc *MyConponent) SetReferences(ctx context.Context, references refer.IReferences) {
			mc._counters.SetReferences(ctx, references);
		}

		func (mc * MyConponent) myMethod() {
			mc._counters.Increment(context.Background(), "mycomponent.mymethod.calls");
			timing := mc._counters.BeginTiming(context.Background(), "mycomponent.mymethod.exec_time");
			defer timing.EndTiming(context.Background());
 		// do something
		}
		var mc MyComponent{};
		mc._counters = NewCompositeCounters();

func NewCompositeCounters

func NewCompositeCounters() *CompositeCounters

NewCompositeCounters creates a new instance of the counters.

Returns: *CompositeCounters

func NewCompositeCountersFromReferences

func NewCompositeCountersFromReferences(ctx context.Context, references refer.IReferences) *CompositeCounters

NewCompositeCountersFromReferences creates a new instance of the counters.

Parameters:
	- ctx context.Context
	- references is a refer.IReferences to locate the component dependencies.
Returns: *CompositeCounters

func (*CompositeCounters) BeginTiming

func (c *CompositeCounters) BeginTiming(ctx context.Context, name string) *CounterTiming

BeginTiming begins measurement of execution time interval. It returns Timing object which has to be called at Timing.endTiming to end the measurement and update the counter.

Parameters:
	- ctx context.Context
	- name string a counter name of Interval type.
Returns: *Timing a Timing callback object to end timing.

func (*CompositeCounters) EndTiming

func (c *CompositeCounters) EndTiming(ctx context.Context, name string, elapsed float64)

EndTiming ends measurement of execution elapsed time and updates specified counter.

see Timing.EndTiming
Parameters:
	- ctx context.Context
	- name string a counter name
	- elapsed float64 execution elapsed time in milliseconds to update the counter.

func (*CompositeCounters) Increment

func (c *CompositeCounters) Increment(ctx context.Context, name string, value int64)

Increment increments counter by given value.

Parameters:
	- ctx context.Context
	- name string a counter name of Increment type.
	- value number a value to add to the counter.

func (*CompositeCounters) IncrementOne

func (c *CompositeCounters) IncrementOne(ctx context.Context, name string)

IncrementOne increments counter by 1.

Parameters:
	- ctx context.Context
	- name string a counter name of Increment type.

func (*CompositeCounters) Last

func (c *CompositeCounters) Last(ctx context.Context, name string, value float64)

Last records the last calculated measurement value. Usually this method is used by metrics calculated externally.

Parameters:
	- ctx context.Context
	- name string a counter name of Last type.
	- value float64 a last value to record.

func (*CompositeCounters) SetReferences

func (c *CompositeCounters) SetReferences(ctx context.Context, references refer.IReferences)

SetReferences references to dependent components.

Parameters:
	- ctx context.Context
	- references refer.IReferences references to locate the component dependencies.

func (*CompositeCounters) Stats

func (c *CompositeCounters) Stats(ctx context.Context, name string, value float64)

Stats calculates min/average/max statistics based on the current and previous values.

Parameters:
	- ctx context.Context
	- name string a counter name of Statistics type
	- value float64 a value to update statistics

func (*CompositeCounters) Timestamp

func (c *CompositeCounters) Timestamp(ctx context.Context, name string, value time.Time)

Timestamp records the given timestamp.

Parameters:
	- ctx context.Context
	- name string a counter name of Timestamp type.
	- value time.Time a timestamp to record.

func (*CompositeCounters) TimestampNow

func (c *CompositeCounters) TimestampNow(ctx context.Context, name string)

TimestampNow records the current time as a timestamp.

Parameters:
	- ctx context.Context
	- name string a counter name of Timestamp type.

type Counter

type Counter struct {
	Name    string      `json:"name"`
	Type    CounterType `json:"type"`
	Last    float64     `json:"last"`
	Count   int64       `json:"count"`
	Min     float64     `json:"min"`
	Max     float64     `json:"max"`
	Average float64     `json:"average"`
	Time    time.Time   `json:"time"`
}

Counter data object to store measurement for a performance counter. This object is used by CachedCounters to store counters.

type CounterTiming added in v1.0.5

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

CounterTiming callback object returned by ICounters.beginTiming to end timing of execution block and update the associated counter.

Example:
	timing := counters.BeginTiming(contex.Background(), "mymethod.exec_time")
	defer  timing.EndTiming()

func NewCounterTiming added in v1.0.5

func NewCounterTiming(counter string, callback ITimingCallback) *CounterTiming

NewCounterTiming creates a new instance of the timing callback object.

Parameters:
	- counter string an associated counter name
	- callback ITimingCallback a callback that shall be called when EndTiming is called.
Returns: *Timing

func NewEmptyCounterTiming added in v1.0.5

func NewEmptyCounterTiming() *CounterTiming

NewEmptyCounterTiming creates a new instance of the timing callback object.

Returns: *CounterTiming

func (*CounterTiming) EndTiming added in v1.0.5

func (c *CounterTiming) EndTiming(ctx context.Context)

EndTiming ends timing of an execution block, calculates elapsed time and updates the associated counter.

type CounterType

type CounterType uint8
const (
	Interval   CounterType = 0
	LastValue  CounterType = 1
	Statistics CounterType = 2
	Timestamp  CounterType = 3
	Increment  CounterType = 4
)

Types of counters that measure different types of metrics Interval: = 0 Counters that measure execution time intervals LastValue: = 1 Counters that keeps the latest measured value Statistics: = 2 Counters that measure min/average/max statistics Timestamp: = 3 Counter that record timestamps Increment: = 4 Counter that increment counters

func NewCounterTypeFromString

func NewCounterTypeFromString(value string) CounterType

NewCounterTypeFromString creates new CounterType from string

func (CounterType) MarshalJSON

func (c CounterType) MarshalJSON() ([]byte, error)

func (CounterType) ToString

func (c CounterType) ToString() string

ToString method converting counter type to string

func (*CounterType) UnmarshalJSON

func (c *CounterType) UnmarshalJSON(data []byte) (err error)

type ICachedCountersOverrides added in v1.0.4

type ICachedCountersOverrides interface {
	Save(ctx context.Context, counters []Counter) error
}

type ICounters

type ICounters interface {
	// BeginTiming begins measurement of execution time interval.
	// It returns Timing object which has to be called at
	// Timing.endTiming to end the measurement and update the counter.
	BeginTiming(ctx context.Context, name string) *CounterTiming

	// Stats calculates min/average/max statistics based on the current and previous values.
	Stats(ctx context.Context, name string, value float64)

	// Last records the last calculated measurement value.
	// Usually this method is used by metrics calculated externally.
	Last(ctx context.Context, name string, value float64)

	// TimestampNow records the given timestamp.
	TimestampNow(ctx context.Context, name string)

	// Timestamp records the current time as a timestamp.
	Timestamp(ctx context.Context, name string, value time.Time)

	// IncrementOne increments counter by 1.
	IncrementOne(ctx context.Context, name string)

	// Increment increments counter by given value.
	Increment(ctx context.Context, name string, value int64)
}

ICounters interface for performance counters that measure execution metrics. The performance counters measure how code is performing: how fast or slow, how many transactions performed, how many objects are stored, what was the latest transaction time and so on. They are critical to monitor and improve performance, scalability and reliability of code in production.

type ITimingCallback

type ITimingCallback interface {
	EndTiming(ctx context.Context, name string, elapsed float64)
}

ITimingCallback ends measurement of execution elapsed time and updates specified counter.

see Timing.EndTiming
Parameters:
	- ctx context.Context
	- name string a counter name
	- elapsed float32 execution elapsed time in milliseconds to update the counter.

type LogCounters

type LogCounters struct {
	*CachedCounters
	// contains filtered or unexported fields
}

LogCounters performance counters that periodically dumps counters measurements to logger.

Configuration parameters:
	- options:
		- interval: interval in milliseconds to save current counters measurements (default: 5 mins)
		- reset_timeout: timeout in milliseconds to reset the counters. 0 disables the reset (default: 0)
References:
	- *:logger:*:*:1.0 ILogger components to dump the captured counters
	- *:context-info:*:*:1.0 (optional) ContextInfo to detect the context id and specify counters source

see Counter
see CachedCounters
see CompositeLogger

Example:
	counters := NewLogCounters();
	counters.SetReferences(context.Background(), NewReferencesFromTuples(
		NewDescriptor("pip-services", "logger", "console", "default", "1.0"), NewConsoleLogger()
	));
	counters.IncrementOne(context.Background(), "mycomponent.mymethod.calls")
	timing := counters.BeginTiming(context.Background(),"mycomponent.mymethod.exec_time")
	defer timing.EndTiming(context.Background())

	// do something
	counters.Dump(context.Background())

func NewLogCounters

func NewLogCounters() *LogCounters

NewLogCounters creates a new instance of the counters.

Returns: *LogCounters

func (*LogCounters) Save

func (c *LogCounters) Save(ctx context.Context, counters []Counter) error

Save the current counters measurements.

Parameters:
	- ctx context.Context
	- counters []*Counter current counters measurements to be saves.

func (*LogCounters) SetReferences

func (c *LogCounters) SetReferences(ctx context.Context, references refer.IReferences)

SetReferences sets references to dependent components.

Parameters:
	- references refer.IReferences references to locate the component dependencies.

type NullCounters

type NullCounters struct{}

NullCounters dummy implementation of performance counters that doesn't do anything. It can be used in testing or in situations when counters is required but shall be disabled.

func NewNullCounters

func NewNullCounters() *NullCounters

NewNullCounters creates a new instance of the counter.

Returns: *NullCounters

func (*NullCounters) BeginTiming

func (c *NullCounters) BeginTiming(ctx context.Context, name string) *CounterTiming

BeginTiming begins measurement of execution time interval. It returns Timing object which has to be called at Timing.EndTiming to end the measurement and update the counter.

Parameters:
	- ctx context.Context
	- name string a counter name of Interval type.
Returns: *Timing a Timing callback object to end timing.

func (*NullCounters) Increment

func (c *NullCounters) Increment(ctx context.Context, name string, value int64)

Increment increments counter by given value.

Parameters:
	- ctx context.Context
	- name string a counter name of Increment type.
	- value int64 a value to add to the counter.

func (*NullCounters) IncrementOne

func (c *NullCounters) IncrementOne(ctx context.Context, name string)

IncrementOne increments counter by 1.

Parameters:
	- ctx context.Context
	- name string a counter name of Increment type.

func (*NullCounters) Last

func (c *NullCounters) Last(ctx context.Context, name string, value float64)

Last records the last calculated measurement value. Usually this method is used by metrics calculated externally.

Parameters:
	- ctx context.Context
	- name string a counter name of Last type.
	- value float64 a last value to record.

func (*NullCounters) Stats

func (c *NullCounters) Stats(ctx context.Context, name string, value float64)

Stats calculates min/average/max statistics based on the current and previous values. Parameters:

  • ctx context.Context
  • name string a counter name of Statistics type
  • value float64 a value to update statistics

func (*NullCounters) Timestamp

func (c *NullCounters) Timestamp(ctx context.Context, name string, value time.Time)

Timestamp records the given timestamp.

Parameters:
	- ctx context.Context
	- name string a counter name of Timestamp type.
	- value time.Time a timestamp to record.

func (*NullCounters) TimestampNow

func (c *NullCounters) TimestampNow(ctx context.Context, name string)

TimestampNow records the current time as a timestamp.

Parameters:
	- ctx context.Context
	- name string a counter name of Timestamp type.

Jump to

Keyboard shortcuts

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