metrics

package
v0.0.0-...-42829d4 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2021 License: Apache-2.0 Imports: 11 Imported by: 27

Documentation

Overview

Usage:

import "github.com/baidu/go-lib/web-monitor/metrics"

// define counter struct type
type ServerState {
    ReqServed *Counter // field type must be *Counter or *Gauge or *State
    ConServed *Counter
    ConActive *Gauge
}

// create metrics
var m Metrics
var s ServerState
m.Init(&s, "PROXY", 20)

// counter operations
s.ConActive.Inc(2)
s.ConServed.Inc(1)
s.ReqServed.Inc(1)
s.ConActive.Dec(1)

m.Counter("CounterName").Inc(1)
m.Gauge("GaugeName").Inc(1)
m.State("StateName").Set("StateValue")

// get absoulute data for all metrics
stateData := m.GetAll()
// get diff data for all counters(gauge don't have diff data)
stateDiff := m.GetDiff()

Index

Constants

View Source
const (
	KindTotal = "total"
	KindDelta = "delta"
)
View Source
const (
	TypeGauge   = "Gauge"
	TypeCounter = "Counter"
	TypeState   = "State"
)
View Source
const (
	DefaultInterval = 20 // in seconds
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Counter

type Counter uint64

Counter is a cumulative metric that represents a single monotonically increasing counter whose value can only increase or be reset to zero on restart

func (*Counter) Get

func (c *Counter) Get() int64

Get gets counter

func (*Counter) Inc

func (c *Counter) Inc(delta uint)

Inc increases counter

func (*Counter) Type

func (c *Counter) Type() string

type Gauge

type Gauge int64

Gauge is a Metric that represents a single numerical value that can arbitrarily go up and down.

func (*Gauge) Dec

func (c *Gauge) Dec(delta uint)

Dec decreases gauge

func (*Gauge) Get

func (c *Gauge) Get() int64

Get gets gauge

func (*Gauge) Inc

func (c *Gauge) Inc(delta uint)

Inc increases gauge

func (*Gauge) Set

func (c *Gauge) Set(v int64)

Set sets gauge

func (*Gauge) Type

func (c *Gauge) Type() string

type Metrics

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

func NewEmptyMetrics

func NewEmptyMetrics(prefix string, intervalS int) *Metrics

NewEmptyMetrics initializes empty Metrics

func (*Metrics) Counter

func (m *Metrics) Counter(name string) *Counter

Counter get or create a Counter by name

func (*Metrics) Gauge

func (m *Metrics) Gauge(name string) *Gauge

Gauge get or create a Gauge by name

func (*Metrics) GetAll

func (m *Metrics) GetAll() *MetricsData

GetAll gets absoulute values for all counters

func (*Metrics) GetDiff

func (m *Metrics) GetDiff() *MetricsData

GetDiff gets diff values for all counters

func (*Metrics) Init

func (m *Metrics) Init(metrics interface{}, prefix string, interval int) error

Init initializes metrics

Params:

  • counters: a pointer to a sturct var; struct field type must be *Counter
  • prefix : prefix for counters
  • interval: diff interval (second), if <=0, use default value 20

func (*Metrics) State

func (m *Metrics) State(name string) *State

State get or create a State by name

type MetricsData

type MetricsData struct {
	Prefix      string
	Kind        string
	GaugeData   map[string]int64
	CounterData map[string]int64
	StateData   map[string]string
}

func NewMetricsData

func NewMetricsData(prefix string, kind string) *MetricsData

func (*MetricsData) Diff

func (d *MetricsData) Diff(last *MetricsData) *MetricsData

func (*MetricsData) Format

func (d *MetricsData) Format(params map[string][]string) ([]byte, error)

func (*MetricsData) KeyValueFormat

func (d *MetricsData) KeyValueFormat() []byte

func (*MetricsData) PrometheusFormat

func (d *MetricsData) PrometheusFormat() []byte

func (*MetricsData) Sum

func (d *MetricsData) Sum(d2 *MetricsData) *MetricsData

type State

type State atomic.Value

func (*State) Get

func (s *State) Get() string

Get gets state

func (*State) Set

func (s *State) Set(v string)

Set sets state

func (*State) Type

func (s *State) Type() string

Jump to

Keyboard shortcuts

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