metrics

package module
v0.7.0-rc0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2022 License: Apache-2.0 Imports: 12 Imported by: 1

README

metrics

metrics package helps to create ydb-go-sdk traces with monitoring internal state of driver

Usage

import (
    "fmt"
    "sync/mutex"
    "time"
	
    "github.com/ydb-platform/ydb-go-sdk/v3"
    "github.com/ydb-platform/ydb-go-sdk-metrics"
)

// define custom gauge
type gauge struct {
    name string
    v    float64
    m    sync.Mutex
}

func (g *gauge) Inc() {
    g.m.Lock
    defer g.m.Unlock()
    g.v += 1
}

func (g *gauge) Dec() {
    g.m.Lock
    defer g.m.Unlock()
    g.v -= 1
}

func (g *gauge) Set(value float64) {
    g.m.Lock
    defer g.m.Unlock()
    g.v = value
}

func (g *gauge) Value() float64 {
    g.m.Lock
    defer g.m.Unlock()
    return g.v	
}

// define custom config
type config struct {
    gauges []gauge
    m      sync.Mutex
}

func (c *config) Details() Details {
    return metrics.DriverClusterEvents |
        metrics.DriverConnEvents |
        metrics.DriverCredentialsEvents |
        metrics.DriverDiscoveryEvents
}

func (c *config) Gauge(name string) Gauge {
    g := &gauge{
        name: name,		
    }
    c.m.Lock()
    defer c.m.Unlock()
    c.gauges = append(c.gauges, g)
    return g
}

func (c *config) Delimiter() *string {
    // use default delimiter
    return nil
}

func (c *config) Prefix() *string {
    // nothing prefix
    return nil
}

func (c *config) Name(Type) *string {
    // use default metric names
    return nil
}

func (c *config) Join(...Name) *string {
    // use default join
    return nil
}

func (c *config) ErrName(err error) *string {
    // use default error name func
    return nil
}

func main() {
    registry := &config{
        gauges: make([]gauge, 0),
    }
    // print registry values 
    go func() {
        for {
            time.Sleep(time.Minute)
            registry.m.Lock()
            fmt.Printf("[%s] registry state:\n", time.Now().String())
            for _, g := range registry.gauges {
                fmt.Printf(" - %s = %f\n", g.name, g.Value())
            }
            registry.m.Lock()
        }
    }
    db, err := ydb.New(
        context.Background(),
		ydb.MustConnectionString(connection),
		ydb.WithTraceDriver(metrics.Driver(registry))
	)
    // work with db
}

Documentation

Index

Constants

View Source
const (
	TagVersion    = "sdk"
	TagSource     = "source"
	TagName       = "name"
	TagMethod     = "method"
	TagError      = "error"
	TagErrCode    = "errCode"
	TagAddress    = "address"
	TagID         = "ID"
	TagNodeID     = "nodeID"
	TagDataCenter = "destination"
	TagState      = "state"
	TagIdempotent = "idempotent"
	TagSuccess    = "success"
	TagStage      = "stage"
)

Variables

This section is empty.

Functions

func Coordination added in v0.7.0

func Coordination(c Config) (t trace.Coordination)

func Discovery added in v0.7.0

func Discovery(c Config) (t trace.Discovery)

func Driver

func Driver(c Config) (t trace.Driver)

Driver makes Driver with metrics publishing

func Ratelimiter added in v0.7.0

func Ratelimiter(c Config) (t trace.Ratelimiter)

func Retry added in v0.7.0

func Retry(c Config) (t trace.Retry)

Retry makes table.RetryTrace with metrics publishing

func Scheme added in v0.7.0

func Scheme(c Config) (t trace.Scheme)

func Scripting added in v0.7.0

func Scripting(c Config) (t trace.Scripting)

func Table

func Table(c Config) (t trace.Table)

func WithTraces added in v0.7.0

func WithTraces(c Config) ydb.Option

Types

type Config

type Config interface {
	Registry

	// Details returns bitmask for customize details of metrics
	// If zero - use full set of driver metrics
	Details() trace.Details

	// WithSystem returns new Config with subsystem scope
	// Separator for split scopes of metrics provided Config implementation
	WithSystem(subsystem string) Config
}

type Gauge

type Gauge interface {
	Add(delta float64)
	Set(value float64)
}

Gauge tracks single float64 value.

type GaugeVec

type GaugeVec interface {
	With(map[string]string) Gauge
}

GaugeVec returns Gauge from GaugeVec by lables

type Label

type Label struct {
	Tag   string
	Value string
}

type Name

type Name string

type Registry added in v0.2.1

type Registry interface {
	// GaugeVec returns GaugeVec by name, subsystem and labels
	// If gauge by args already created - return gauge from cache
	// If gauge by args nothing - create and return newest gauge
	GaugeVec(name string, labelNames ...string) GaugeVec

	// TimerVec returns TimerVec by name, subsystem and labels
	// If timer by args already created - return timer from cache
	// If timer by args nothing - create and return newest timer
	TimerVec(name string, labelNames ...string) TimerVec
}

type Timer added in v0.5.0

type Timer interface {
	Record(d time.Duration)
}

Timer tracks distribution of value.

type TimerVec added in v0.5.0

type TimerVec interface {
	With(map[string]string) Timer
}

TimerVec stores multiple dynamically created timers

Jump to

Keyboard shortcuts

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