export

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: ISC Imports: 8 Imported by: 0

Documentation

Overview

Package export provides an interface to instrument Go code.

Instrumenting Go code with this package is very similar to the "expvar" package in the vanilla Go distribution. In fact, the variables exported with this package are also registered with the "expvar" package, so that you can also use other existing metric collection frameworks with it. This package differs in that it has an explicitly cumulative type, Derive.

The intended usage pattern of this package is as follows: First, global variables are initialized with NewDerive(), NewGauge(), NewDeriveString() or NewGaugeString(). The Run() function is called as a separate goroutine as part of your program's initialization and, last but not least, the variables are updated with their respective update functions, Add() for Derive and Set() for Gauge.

// Initialize global variable.
var requestCounter = export.NewDeriveString("example.com/golang/total_requests")

// Call Run() in its own goroutine.
func main() {
        ctx := context.Background()

        // Any other type implementing api.Writer works.
        client, err := network.Dial(
                net.JoinHostPort(network.DefaultIPv6Address, network.DefaultService),
                network.ClientOptions{})
        if err !=  nil {
                log.Fatal(err)
        }
        go export.Run(ctx, client, export.Options{
                Interval: 10 * time.Second,
        })
        // …
}

// Update variable.
func requestHandler(w http.ResponseWriter, req *http.Request) {
        defer requestCounter.Add(1)
        // …
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Publish

func Publish(v Var)

Publish adds v to the internal list of exported metrics.

func Run

func Run(ctx context.Context, w api.Writer, opts Options) error

Run periodically calls the ValueList function of each Var, sets the Time and Interval fields and passes it w.Write(). This function blocks until the context is cancelled.

Types

type Derive

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

Derive represents a cumulative integer data type, for example "requests served since server start". It implements the Var and expvar.Var interfaces.

func NewDerive

func NewDerive(id api.Identifier) *Derive

NewDerive initializes a new Derive, registers it with the "expvar" package and returns it. The initial value is zero.

func NewDeriveString

func NewDeriveString(s string) *Derive

NewDeriveString parses s as an Identifier and returns a new Derive. If parsing s fails, it will panic. This simplifies initializing global variables.

func (*Derive) Add

func (d *Derive) Add(diff int)

Add adds diff to d.

func (*Derive) String

func (d *Derive) String() string

String returns the string representation of d.

func (*Derive) ValueList

func (d *Derive) ValueList() *api.ValueList

ValueList returns the ValueList representation of d. Both, Time and Interval are set to zero.

type Gauge

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

Gauge represents an absolute floating point data type, for example "heap memory used". It implements the Var and expvar.Var interfaces.

func NewGauge

func NewGauge(id api.Identifier) *Gauge

NewGauge initializes a new Gauge, registers it with the "expvar" package and returns it. The initial value is NaN.

func NewGaugeString

func NewGaugeString(s string) *Gauge

NewGaugeString parses s as an Identifier and returns a new Gauge. If parsing s fails, it will panic. This simplifies initializing global variables.

func (*Gauge) Set

func (g *Gauge) Set(v float64)

Set sets g to v.

func (*Gauge) String

func (g *Gauge) String() string

String returns the string representation of g.

func (*Gauge) ValueList

func (g *Gauge) ValueList() *api.ValueList

ValueList returns the ValueList representation of g. Both, Time and Interval are set to zero.

type Options

type Options struct {
	Interval time.Duration
}

Options holds options for the Run() function.

type Var

type Var interface {
	ValueList() *api.ValueList
}

Var is an abstract type for metrics exported by this package.

Jump to

Keyboard shortcuts

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