count

package
Version: v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2021 License: MIT Imports: 9 Imported by: 18

Documentation

Index

Constants

View Source
const (
	Interval   = 0
	LastValue  = 1
	Statistics = 2
	Timestamp  = 3
	Increment  = 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

Variables

This section is empty.

Functions

func NewDefaultCountersFactory

func NewDefaultCountersFactory() *build.Factory

Create a new instance of the factory. Returns *build.Factory

func TypeToString added in v1.0.3

func TypeToString(t int) string

TypeToString method converting counter type to string

Types

type CachedCounters

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

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

Inherit cache counters from saver Parameters:

- save ICountersSaver

Returns *CachedCounters

func (*CachedCounters) BeginTiming

func (c *CachedCounters) BeginTiming(name string) *CounterTiming

Begins measurement of execution time interval. It returns CounterTiming object which has to be called at CounterTiming.EndTiming to end the measurement and update the counter. Parameters

- name string
a counter name of Interval type.

Returns *CounterTiming a CounterTiming callback object to end timing.

func (*CachedCounters) Clear

func (c *CachedCounters) Clear(name string)

Clears (resets) a counter specified by its name. Parameters:

- name string
a counter name to clear.

func (*CachedCounters) ClearAll

func (c *CachedCounters) ClearAll()

Clears (resets) all counters.

func (*CachedCounters) Configure

func (c *CachedCounters) Configure(config *config.ConfigParams)

Configures component by passing configuration parameters. Parameters:

- config *config.ConfigParams
configuration parameters to be set.

func (*CachedCounters) Dump

func (c *CachedCounters) Dump() error

Dumps (saves) the current values of counters.

func (*CachedCounters) EndTiming

func (c *CachedCounters) EndTiming(name string, elapsed float32)

Ends measurement of execution elapsed time and updates specified counter. see CounterTiming.EndTiming Parameters:

- name string
a counter name
elapsed float32
execution elapsed time in milliseconds to update the counter.

func (*CachedCounters) Get

func (c *CachedCounters) Get(name string, typ int) *Counter

Gets 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:

- 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() []*Counter

Gets all captured counters. Returns []*Counter

func (*CachedCounters) Increment

func (c *CachedCounters) Increment(name string, value int)

Increments counter by given value. Parameters:

- name string
a counter name of Increment type.
- value int
a value to add to the counter.

func (*CachedCounters) IncrementOne

func (c *CachedCounters) IncrementOne(name string)

Increments counter by 1. Parameters:

- name string
a counter name of Increment type.

func (*CachedCounters) Last

func (c *CachedCounters) Last(name string, value float32)

Records the last calculated measurement value. Usually this method is used by metrics calculated externally. Parameters:

- name string
a counter name of Last type.
- value number
a last value to record.

func (*CachedCounters) Stats

func (c *CachedCounters) Stats(name string, value float32)

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

- name string
a counter name of Statistics type
- value float32
a value to update statistics

func (*CachedCounters) Timestamp

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

Records the given timestamp. Parameters:

- name string
a counter name of Timestamp type.
value time.Time
a timestamp to record.

func (*CachedCounters) TimestampNow

func (c *CachedCounters) TimestampNow(name string)

Records the current time as a timestamp. Parameters:

- name string
a counter name of Timestamp type.

type CompositeCounters

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

Aggregates all counters from component references under a single component.

It allows to capture 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(references: IReferences) {
        mc._counters.SetReferences(references);
    }

    func (mc * MyConponent) myMethod() {
        mc._counters.Increment("mycomponent.mymethod.calls");
        timing := mc._counters.BeginTiming("mycomponent.mymethod.exec_time");
		 defer timing.EndTiming();
		// do something

    	}
var mc MyComponent{};
mc._counters = NewCompositeCounters();

func NewCompositeCounters

func NewCompositeCounters() *CompositeCounters

Creates a new instance of the counters. Returns *CompositeCounters

func NewCompositeCountersFromReferences

func NewCompositeCountersFromReferences(references refer.IReferences) *CompositeCounters

Creates a new instance of the counters. Parameters:

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

Returns *CompositeCounters

func (*CompositeCounters) BeginTiming

func (c *CompositeCounters) BeginTiming(name string) *CounterTiming

Begins measurement of execution time interval. It returns CounterTiming object which has to be called at CounterTiming.EndCounterTiming to end the measurement and update the counter. Parameters:

- name string
a counter name of Interval type.

Returns *CounterTiming a CounterTiming callback object to end timing.

func (*CompositeCounters) EndTiming

func (c *CompositeCounters) EndTiming(name string, elapsed float32)

Ends measurement of execution elapsed time and updates specified counter. see CounterTiming.endCounterTiming Parameters:

- name string
a counter name
- elapsed float32
execution elapsed time in milliseconds to update the counter.

func (*CompositeCounters) Increment

func (c *CompositeCounters) Increment(name string, value int)

Increments counter by given value. Parameters:

- name string
a counter name of Increment type.
- value number
a value to add to the counter.

func (*CompositeCounters) IncrementOne

func (c *CompositeCounters) IncrementOne(name string)

Increments counter by 1. Parameters:

- name string
a counter name of Increment type.

func (*CompositeCounters) Last

func (c *CompositeCounters) Last(name string, value float32)

Records the last calculated measurement value. Usually this method is used by metrics calculated externally. Parameters:

- name string
a counter name of Last type.
- value float32
a last value to record.

func (*CompositeCounters) SetReferences

func (c *CompositeCounters) SetReferences(references refer.IReferences)

Sets references to dependent components. Parameters:

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

func (*CompositeCounters) Stats

func (c *CompositeCounters) Stats(name string, value float32)

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

- name string
a counter name of Statistics type
- value float32
a value to update statistics

func (*CompositeCounters) Timestamp

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

Records the given timestamp. Parameters:

- name string
a counter name of Timestamp type.
- value time.Time
a timestamp to record.

func (*CompositeCounters) TimestampNow

func (c *CompositeCounters) TimestampNow(name string)

Records the current time as a timestamp. Parameters:

- name string
a counter name of Timestamp type.

type Counter

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

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

func NewCounter

func NewCounter(name string, typ int) *Counter

Creates a instance of the data obejct Parameters:

- name string
a counter name.
- type CounterType
a counter type.

Returns *Counter

type CounterTiming added in v1.2.0

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

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

Example

timing := counters.BeginTiming("mymethod.exec_time");
defer  timing.EndTiming();

func NewCounterTiming added in v1.2.0

func NewCounterTiming(counter string, callback ICounterTimingCallback) *CounterTiming

Creates a new instance of the timing callback object. Parameters:

- counter string
an associated counter name
- callback ICounterTimingCallback
a callback that shall be called when EndTiming is called.

Retruns *CounterTiming

func NewEmptyCounterTiming added in v1.2.0

func NewEmptyCounterTiming() *CounterTiming

Creates a new instance of the timing callback object. Retruns *CounterTiming

func (*CounterTiming) EndTiming added in v1.2.0

func (c *CounterTiming) EndTiming()

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

type ICachedCountersOverrides added in v1.1.0

type ICachedCountersOverrides interface {
	Save(counters []*Counter) error
}

type ICounterTimingCallback added in v1.2.0

type ICounterTimingCallback interface {
	EndTiming(name string, elapsed float32)
}

Ends measurement of execution elapsed time and updates specified counter. see Timing.endTiming Parameters:

- name string
a counter name
- elapsed float32
execution elapsed time in milliseconds to update the counter.

type ICounters

type ICounters interface {
	// Begins measurement of execution time interval. It returns CounterTiming object which has to be called at CounterTiming.EndTiming to end the measurement and update the counter.
	BeginTiming(name string) *CounterTiming

	// Calculates min/average/max statistics based on the current and previous values.
	Stats(name string, value float32)

	// Records the last calculated measurement value.
	// Usually this method is used by metrics calculated externally.
	Last(name string, value float32)

	// Records the given timestamp.
	TimestampNow(name string)

	// Records the current time as a timestamp.
	Timestamp(name string, value time.Time)

	// Increments counter by 1.
	IncrementOne(name string)

	// Increments counter by given value.
	Increment(name string, value int)
}

type LogCounters

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

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(NewReferencesFromTuples(

NewDescriptor("pip-services", "logger", "console", "default", "1.0"), NewConsoleLogger()

));

counters.Increment("mycomponent.mymethod.calls"); timing := counters.BeginTiming("mycomponent.mymethod.exec_time"); defer timing.EndTiming();

// do something

counters.Dump();

func NewLogCounters

func NewLogCounters() *LogCounters

Creates a new instance of the counters. Returns *LogCounters

func (*LogCounters) Save

func (c *LogCounters) Save(counters []*Counter) error

Saves the current counters measurements. Parameters:

- counters []*Counter
current counters measurements to be saves.

func (*LogCounters) SetReferences

func (c *LogCounters) SetReferences(references refer.IReferences)

Sets references to dependent components. Parameters:

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

type NullCounters

type NullCounters struct{}

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

Creates a new instance of the counter. Returns *NullCounters

func (*NullCounters) BeginTiming

func (c *NullCounters) BeginTiming(name string) *CounterTiming

Begins measurement of execution time interval. It returns CounterTiming object which has to be called at CounterTiming.endCounterTiming to end the measurement and update the counter. Parameters:

- name string
a counter name of Interval type.

Returns *CounterTiming a CounterTiming callback object to end timing.

func (*NullCounters) Increment

func (c *NullCounters) Increment(name string, value float32)

Increments counter by given value. Parameters:

- name string
a counter name of Increment type.
- value float32
a value to add to the counter.

func (*NullCounters) IncrementOne

func (c *NullCounters) IncrementOne(name string)

Increments counter by 1. Parameters:

- name string
a counter name of Increment type.

func (*NullCounters) Last

func (c *NullCounters) Last(name string, value float32)

Records the last calculated measurement value. Usually this method is used by metrics calculated externally. Parameters:

- name string
a counter name of Last type.
- value float32
a last value to record.

func (*NullCounters) Stats

func (c *NullCounters) Stats(name string, value float32)

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

- name string
a counter name of Statistics type
- value float32
a value to update statistics

func (*NullCounters) Timestamp

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

Records the given timestamp. Parameters:

- name string
a counter name of Timestamp type.
- value time.Time
a timestamp to record.

func (*NullCounters) TimestampNow

func (c *NullCounters) TimestampNow(name string)

Records the current time as a timestamp. Parameters:

- 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