exportertools

package
v0.0.0-...-d591cd7 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2017 License: MIT Imports: 11 Imported by: 4

README

GoDoc

Getting Started

1. Bootstrap your Exporter by creating a struct and embedding *BaseExporter. Your Exporter must satisfy the Exporter interface.

type Exporter interface {

	// to be implemented by custom exporter
	Setup() error
	Close() error

	Process()

	// satisfy via GenericCollect & GenericDescribe, or custom implementation
	prometheus.Collector
}

Note that (\*BaseExporter).Close() should be called from custom Close() for gracefully shutdown exporter's Process() goroutines.

2. Implement methods the Setup() and Close() methods required by the Exporter interface to create/destroy infrastructure connections.

3. For each group of metrics to be collected by the Exporter, satisfy the MetricCollector interface then add to your Exporter via AddCollector() provided by the BaseExporter, within your Exporter's Setup() method.

type MetricCollector interface {
	Collect() ([]*Metric, error)
}

4. Implement prometheus.Collector using the helpers:

func (e *customExporter) Describe(ch chan<- *prometheus.Desc) {
	exporttools.GenericDescribe(e.BaseExporter, ch)
}

func (e *customExporter) Collect(ch chan<- prometheus.Metric) {
	exporttools.GenericCollect(e.BaseExporter, ch)
}

5. Calling Register(exporter) will enable collection for all metric groups.

func main()
  exporter := postgres.NewCustomExporter()
  err := exportertools.Register(exporter)
  if err != nil {
    log.Fatal(err)
  }
}

6. Dispatcher component gets services, sends new services to be Registered to the channel and services to be Unregistered to another one.

  • Send callback function to the Dispatcher instance. Function should return actual services list.
cb := func() (list []*hubble.ServiceAtomic, err error) {
    services, err := h.Services(filterCB)
    if err != nil {
        return list, err
    }
    for _, svc := range services {
        for _, el := range svc.MakeAtomic(nil) {
            list = append(list, el)
        }
    }
    return list, err
}
  • Set interval between cb executions, then run Dispatcher in background
d = hubble.NewDispatcher(*updateInterval)
go d.Run(cb)
  • Listen for ToRegister and ToUnregister channels for new objects. Register and Unregister theirs exporters.
func listenAndRegister() {
    pgMetricsParsed := exporter.AddFromFile(*queriesPath)

    for svc := range d.ToRegister {
        if len(svc.ExporterOptions) > 1 {
            config := exporter.Config{
                DSN:             util.PgConnURL(svc),
                Labels:          svc.ExtraLabels,
                ExporterOptions: svc.ExporterOptions,
                CacheTTL:        *scrapeInterval,
                PgMetrics:       pgMetricsParsed,
            }
            exp, err := exporter.CreateAndRegister(&config)
            if err == nil {
                d.Register(svc, exp)
                log.Infof("Registered %v %v", svc.Name, svc.Address)
            } else {
                log.Warnf("Register was failed for service %v %v %v", svc.Name, svc.Address, err)
                exp.Close()
            }
        }
    }
}

func listenAndUnregister() {
    for m := range d.ToUnregister {
        for h, svc := range m {
            exporter := d.Exporters[h].(*exporter.PostgresExporter)
            err := exporter.Close()
            if err != nil {
                log.Warnf("Unregister() for %v %v returned %v:", svc.Name, svc.Address, err)
            } else {
                log.Infof("Unregister service %v %v", svc.Name, svc.Address)
            }
            d.UnregisterWithHash(h)
        }
    }
}

Inspired by exporttools

Documentation

Index

Constants

View Source
const VERSION string = "0.2"

Version

Variables

This section is empty.

Functions

func GenericCollect

func GenericCollect(be *BaseExporter, ch chan<- prometheus.Metric)

GenericCollect generates prom metric from cached metric values

func GenericDescribe

func GenericDescribe(be *BaseExporter, ch chan<- *prometheus.Desc)

GenericDescribe generates prom descriptor from cached metric names

func Register

func Register(exporter Exporter) (err error)

func Unregister

func Unregister(exporter Exporter) error

Types

type BaseExporter

type BaseExporter struct {
	Control          chan bool
	Name             string
	Cache            *Cache
	MetricCollectors []MetricCollector
	Labels           map[string]string
}

BaseExporter provides convinient caching and metrics processing. BaseExporter should be embedded into custom exporter struct

func NewBaseExporter

func NewBaseExporter(name string, ttl int, labels map[string]string) *BaseExporter

func (*BaseExporter) AddCollector

func (e *BaseExporter) AddCollector(m MetricCollector)

func (*BaseExporter) Close

func (e *BaseExporter) Close() error

func (*BaseExporter) Collect

func (e *BaseExporter) Collect(ch chan<- prometheus.Metric)

Satisfies Exporter interface and calls GenericCollect which works with cache

func (*BaseExporter) Describe

func (e *BaseExporter) Describe(ch chan<- *prometheus.Desc)

Satisfies Exporter interface and calls GenericDescribe which works with cache

func (*BaseExporter) Process

func (e *BaseExporter) Process()

Process runs metric collection in async mode

func (*BaseExporter) Setup

func (e *BaseExporter) Setup() error

type Cache

type Cache struct {
	*cache.Cache
	TTL time.Duration
	// contains filtered or unexported fields
}

Cache for metrics values. Scraping metrics is async.

func NewCache

func NewCache(interval int) *Cache

func (*Cache) Get

func (c *Cache) Get(k string) (resp *Metric, err error)

GetActual returns actual values from the cache

func (*Cache) MetricNames

func (c *Cache) MetricNames() []string

func (*Cache) Set

func (c *Cache) Set(m *Metric)

type Exporter

type Exporter interface {
	Setup() error
	Close() error

	Process()

	prometheus.Collector
}

type Metric

type Metric struct {
	Name        string
	Description string
	Type        MetricType
	Value       interface{}
	Labels      map[string]string
}

func (*Metric) PromDescription

func (m *Metric) PromDescription(exporterName string) *prometheus.Desc

func (*Metric) PromType

func (m *Metric) PromType() prometheus.ValueType

func (*Metric) PromValue

func (m *Metric) PromValue() float64

type MetricCollector

type MetricCollector interface {
	Collect() ([]*Metric, error)
}

type MetricType

type MetricType int
const (
	Counter MetricType = iota
	Gauge   MetricType = iota
	Untyped MetricType = iota
)

func StringToType

func StringToType(s string) MetricType

Jump to

Keyboard shortcuts

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