metrics

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2021 License: Apache-2.0 Imports: 13 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"
	TagNodeID     = "nodeID"
	TagDataCenter = "destination"
	TagState      = "state"
	TagIdempotent = "idempotent"
	TagSuccess    = "success"
	TagStage      = "stage"
)
View Source
const (
	DriverSystemEvents = Details(1 << iota)
	DriverClusterEvents

	DriverCoreEvents
	DriverCredentialsEvents
	DriverDiscoveryEvents

	DriverConnEvents = driverNetEvents | DriverCoreEvents

	TableSessionEvents = tableSessionEvents | tableSessionQueryEvents | tableSessionTransactionEvents
	TablePoolEvents    = tablePoolLifeCycleEvents | tablePoolRetryEvents | tablePoolSessionLifeCycleEvents | tablePoolAPIEvents
)

Variables

This section is empty.

Functions

func Driver

func Driver(c Config) trace.Driver

Driver makes Driver with metrics publishing

func DriverWithRegistry added in v0.2.1

func DriverWithRegistry(registry Registry, opts ...option) trace.Driver

DriverWithRegistry makes trace.Driver with metrics registry and options

func Table

func Table(c Config) trace.Table

func TableWithRegistry added in v0.2.1

func TableWithRegistry(registry Registry, opts ...option) trace.Table

TableWithRegistry makes trace.Table with metrics registry and options

func WithDetails added in v0.2.1

func WithDetails(details Details) option

func WithNamespace added in v0.2.1

func WithNamespace(namespace string) option

func WithSeparator added in v0.2.1

func WithSeparator(separator string) option

Types

type Config

type Config interface {
	// Details returns bitmask for customize details of metrics
	// If zero - use full set of driver metrics
	Details() Details

	// 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, description string, labelNames ...string) GaugeVec

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

type Details

type Details int

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(name string, labels []string) GaugeVec
}

Jump to

Keyboard shortcuts

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