reporter

package module
v0.5.2-0...-d95c312 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

README

go-reporter

Actions Status

Configuration

go-reporter is configured through a variable of type Configuration.

config := reporter.Configuration{...}
r, err := reporter.New(config)
// check err
log.SetOutput(r)

It can also be configured through a YAML file.

Currently, due to a technical limitation, if a key is unknown it will just be ignored. Be extra-careful that all keys are correctly spelled.

Configuration

Logging

Here is an example configuration:

reporting:
  logging:
    level: info
    console: true
    syslog: false
    format: json
    files:
      - /var/log/project/project.log
      - json:/var/log/project/project.log

level specify the maximum log level to use. It can be one of:

  • crit
  • error
  • warn
  • info
  • debug

console enables logging to console while syslog enables logging to the local syslog daemon. No configuration knobs are available for those targets.

format allows you to set the log format for console and syslog. Currently, json and plain are supported (default to plain).

files allows one to set a list of files to log to. It's possible to prefix the file with the expected format. Currently, only json: is allowed to get JSON event format.

Metrics

Metrics can be exported using various output plugins. Here is an example:

reporting:
  metrics:
    - expvar:
        listen: 127.0.0.1:8123
    - collectd:
        connect: 127.0.0.1:25826
        interval: 10s
        exclude:
          - go.runtime.*

Note that the configuration should be a list of output plugins. An output plugin is a map from plugin type to its configuration. Only one item per map is allowed.

Intervals are specified with a number and a unit. For example:

  • 5s
  • 1m
  • 30m
expvar

The expvar output supports the following settings:

  • listen: the network address the expvar HTTP endpoint binds to

The metrics are then available at the root of the provided HTTP endpoint. Moreover, when querying /healthz, one can get the status of all healthcheckers. If one of them is unhealthy, it will return a 542 error, otherwise it will return a 200 status code. The returned JSON object contains a status key which can either be ok or fail. It also contains a detail key which contains another object which maps healthcheck names with either +ok if they are healthy or the error string prefixed by ! if not.

file

The file output supports the following settings:

  • path: path to export file
  • interval: internal at which to flush the metrics to the output plugin

At each tick, the current metric values will be written to the specified file as a one-line JSON object. For debug purpose, it's possible to filter the metrics concerning only project by using the following command::

tail -f /var/log/project/metrics \
  | jq 'with_entries(select(.key | startswith("project.")))'
collectd

The collectd output supports the following settings:

  • interval: interval at which to flush metrics to the output plugin
  • connect (optional): collectd server (default 127.0.0.1:25826)
  • exclude (optional): list of metric names patterns (shell globbing) to exclude from reporting

For collectd output to work correctly, you need to append the following to types.db file::

histogram count:COUNTER:0:U, max:GAUGE:U:U, mean:GAUGE:U:U, min:GAUGE:U:U, stddev:GAUGE:0:U, p50:GAUGE:U:U, p75:GAUGE:U:U, p95:GAUGE:U:U, p98:GAUGE:U:U, p99:GAUGE:U:U, p999:GAUGE:U:U
meter     count:COUNTER:0:U, m1_rate:GAUGE:0:U, m5_rate:GAUGE:0:U, m15_rate:GAUGE:0:U, mean_rate:GAUGE:0:U
timer     max:GAUGE:U:U, mean:GAUGE:U:U, min:GAUGE:U:U, stddev:GAUGE:0:U, p50:GAUGE:U:U, p75:GAUGE:U:U, p95:GAUGE:U:U, p98:GAUGE:U:U, p99:GAUGE:U:U, p999:GAUGE:U:U
prometheus

The prometheus output supports the following settings:

  • listen: the network address the Prometheus scraping endpoint binds to
  • interval: interval at which to flush the metrics to the plugin
  • namespace: Prometheus metrics namespace
  • subsystem (optional): Prometheus metrics subsystem
  • cacertfile (optional): path to a TLS CA certificate file if exposing the scraping endpoint via TLS
  • certfile (optional): path to a TLS certificate file if exposing the scraping endpoint via TLS
  • keyfile (optional): path to a TLS certificate private key file if exposing the scraping endpoint via TLS
prompushgw

The prompushgw output supports the following settings:

  • url: the URL of the Prometheus Push Gateway URL to push metrics to
  • job: the job label to apply to the pushed metrics
  • cacertfile (optional): path to a TLS CA certificate file if exposing the scraping endpoint via TLS
  • certfile (optional): path to a TLS certificate file if exposing the scraping endpoint via TLS
  • keyfile (optional): path to a TLS certificate private key file if exposing the scraping endpoint via TLS
Error reporting

Error reporting is done with Sentry. Here is an example configuration:

reporting:
  sentry:
    dsn: https://public:secret@sentry.example.com/project
    tags:
      environment: production
    wait: false

Sentry calls are performed asynchronously by default. To wait for the Sentry API to acknowledge the event, set wait: true.

Prefix

The prefix of the logs and metrics.

reporting:
  prefix: myproject

Documentation

Overview

Package reporter is a façade for reporting duties.

Such a façade includes logging, error handling and metrics.

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfiguration = Configuration{
	Logging: logger.DefaultConfiguration,
}

DefaultConfiguration is the default reporter configuration.

Functions

This section is empty.

Types

type Configuration

type Configuration struct {
	Logging logger.Configuration
	Sentry  sentry.Configuration
	Metrics metrics.Configuration
	Prefix  string
}

Configuration contains the reporter configuration.

func (*Configuration) UnmarshalYAML

func (configuration *Configuration) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML parses a reporter configuration from YAML.

type Healthcheck

type Healthcheck struct {
	metrics.Healthcheck
}

Healthcheck holds healthcheck state.

type Reporter

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

Reporter contains the state for a reporter.

func New

func New(config Configuration) (*Reporter, error)

New creates a new reporter from a configuration.

func NewMock

func NewMock() *Reporter

NewMock creates a new reporter that does almost nothing. It can be used with tests. There is no need to call Close with the returned reporter.

func NewSilentMock

func NewSilentMock() *Reporter

NewSilentMock creates a new reporter that does nothing.

func (*Reporter) Config

func (r *Reporter) Config() *Configuration

Config returns the original configuration provided during reporter initialization.

func (*Reporter) Counter

func (r *Reporter) Counter(name string) metrics.Counter

Counter returns a counter with the given name.

func (*Reporter) Debug

func (r *Reporter) Debug(msg string, ctx ...interface{})

Debug logs a debug message with additional context.

func (*Reporter) Error

func (r *Reporter) Error(err error, msg string, ctx ...interface{}) error

Error logs an error message with additional context. It takes the error as a first argument, a descriptive message (or "") as a second. It will add "err" with the error as a context. It will return a wrapped error with the message, except if message was empty. In this case, it returns the original error.

func (*Reporter) Gauge

func (r *Reporter) Gauge(name string) metrics.Gauge

Gauge returns a gauge with the given name.

func (*Reporter) GaugeFloat64

func (r *Reporter) GaugeFloat64(name string) metrics.GaugeFloat64

GaugeFloat64 returns a 64-bit float gauge with the given name.

func (*Reporter) Healthcheck

func (r *Reporter) Healthcheck(name string, f func(h Healthcheck)) Healthcheck

Healthcheck returns an healthcheck with the given name.

func (*Reporter) Histogram

func (r *Reporter) Histogram(name string) metrics.Histogram

Histogram returns an histogram with the given name. This uses an exponentially-decating sample with a forward-decaying priority reservoir. It uses fixed values for the reservoir size and alpha. Those values were scientifically obtained through random Google searches.

func (*Reporter) Info

func (r *Reporter) Info(msg string, ctx ...interface{})

Info logs an info message with additional context.

func (*Reporter) Logger

func (r *Reporter) Logger() log.Logger

Logger returns the reporter logger instance.

func (*Reporter) Meter

func (r *Reporter) Meter(name string) metrics.Meter

Meter returns a meter with the given name.

func (*Reporter) Push

func (r *Reporter) Push() error

Push pushes registered metrics to a push gateway.

func (*Reporter) Start

func (r *Reporter) Start() error

Start will start the reporter component

func (*Reporter) Stop

func (r *Reporter) Stop() error

Stop will stop reporting and clean the associated resources.

func (*Reporter) Timer

func (r *Reporter) Timer(name string) metrics.Timer

Timer returns a timer with the given name.

func (*Reporter) Warn

func (r *Reporter) Warn(msg string, ctx ...interface{})

Warn logs a warning message with additional context.

func (*Reporter) Write

func (r *Reporter) Write(p []byte) (n int, err error)

Write will take some bytes and log them.

Directories

Path Synopsis
Package logger handles logging.
Package logger handles logging.
Package metrics handles metrics.
Package metrics handles metrics.
Package sentry integrates Sentry with your project.
Package sentry integrates Sentry with your project.

Jump to

Keyboard shortcuts

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