report

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2021 License: MIT Imports: 8 Imported by: 0

README

reporter

中文/Chinese

Description

There are an interface(endpoint) that collect information in our servers —— It allows us to report all the information we want to visualize in Grafana, such as Memory, Goroutine or other information.

This module can auto report information with your application. Or use custom a reporter to report by yourself.

IMPORTANT: Only support v2 endpoint, please read document.

Usage

You can use global report. ()

You can call method that start with With... if you need update config with global report.

package main
import "github.com/baishan-development-guizhou/golang-library/report"

func main() {
    report.Global().WithUrl("http://127.0.0.1:10699/v1/push").
    	Send(report.Point{Value:100})
}

Also create a new reporter.

package main
import (
    "github.com/baishan-development-guizhou/golang-library/report"
    "time"
)

func main() {
	report.Configure().
		AddReporter(report.Point{Value:100},time.Minute).
		Start()
}

More examples see examples dir.

Configure

report.Options can config by yourself.

Name Description Default
Reporters Only use new report, the new report will auto request when the application call Start. []
addr Config this by calling WithAddr(string). Server endpoint. http://127.0.0.1:10699/v2/push
defaultValue Config this by calling WithDefaultValue(string). DefaultValue is the default value in shortcut methods, like SendPayLoad, SendPayLoadWithPoint. Point{Metric: "monitor-bigdata-test", Endpoint: "", Step: 60, Tags: Fields{"name=": "default"}}
context Config this by calling WithContext(context). Context will be bound to ticker. context.Background()
client Config this by calling WithClient(client). Request endpoint by this client. http.Client{Timeout: time.Second * 20}
log Config this by calling WithLog(log). log.G()
before,after, errorHandler Config hook by WithBefore, WithAfter, WithErrorHandler. func

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fields

type Fields map[string]interface{}

type Options

type Options struct {
	// Reporters is an init value. It will Send when call Start method.
	Reporters []Reporter

	Dev bool
	// contains filtered or unexported fields
}

Options can be custom configured by Configure or WithConfigure.

func Configure

func Configure() *Options

Configure report options and return a new Options.

func Global

func Global() *Options

Global returns global options.

func Send

func Send(point Point) *Options

Send will add a Point to chan, and only add once. Support chain call.

func SendPayLoad

func SendPayLoad(fields Fields, tags Tags, value float64) *Options

SendPayLoad will add a Point to chan with some fields, and only add once. Support chain call.

func SendPayLoadWithPoint

func SendPayLoadWithPoint(point Point) *Options

SendPayLoadWithPoint will add a Point to chan with another Point, and only add once. Support chain call.

func SendReporter

func SendReporter(reporter Reporter) *Options

SendReporter will add a Point to chan with Interval. Support chain call.

func SendReporterPayLoad

func SendReporterPayLoad(point func() Point, interval time.Duration) *Options

SendReporterPayLoad will add a Point to chan with Interval. Support chain call.

func WithAfter

func WithAfter(after func(point Point, success bool)) *Options

WithAfter can custom after hook. Support chain call.

func WithBefore

func WithBefore(before func(point Point) Point) *Options

WithBefore can custom before hook. Support chain call.

func WithClient

func WithClient(client *http.Client) *Options

WithClient can custom http client. Support chain call.

func WithConfigure

func WithConfigure(options *Options) *Options

WithConfigure report options and return a exist Options.

func WithContext

func WithContext(context context.Context) *Options

WithContext will update contest. Support chain call.

func WithDefaultValue

func WithDefaultValue(defaultValue *Point) *Options

WithUrl can only update `defaultValue` parameters. Support chain call.

func WithDev added in v0.1.1

func WithDev(dev bool) *Options

WithDev will output information.

func WithErrorHandler

func WithErrorHandler(errorHandler func(point Point, status int)) *Options

WithErrorHandler can custom error handle. Support chain call.

func WithLog

func WithLog(log log.Logger) *Options

WithLog can update log. Support chain call.

func WithUrl

func WithUrl(url string) *Options

WithUrl can only update `url` parameters. Support chain call.

func (*Options) AddReporter

func (o *Options) AddReporter(point func() Point, interval time.Duration) *Options

AddReporter will add a report to the collection of report. IMPORTANT: This method must be called before the Start. Support chain call.

func (*Options) Send

func (o *Options) Send(point Point) *Options

Send will add a Point to chan, and only add once. Support chain call.

func (*Options) SendPayLoad

func (o *Options) SendPayLoad(fields Fields, tags Tags, value float64) *Options

SendPayLoad will add a Point to chan with some fields, and only add once. Support chain call.

func (*Options) SendPayLoadWithPoint

func (o *Options) SendPayLoadWithPoint(point Point) *Options

SendPayLoadWithPoint will add a Point to chan with another Point, and only add once.

func (*Options) SendReporter

func (o *Options) SendReporter(reporter Reporter) *Options

SendReporter will add a Point to chan with Interval. Support chain call.

func (*Options) SendReporterPayLoad

func (o *Options) SendReporterPayLoad(point func() Point, interval time.Duration) *Options

SendReporterPayLoad will add a Point to chan with Interval. Support chain call.

func (*Options) Start

func (o *Options) Start()

Start will start report.

func (*Options) WithAfter

func (o *Options) WithAfter(after func(point Point, success bool)) *Options

WithAfter can custom after hook. Support chain call.

func (*Options) WithBefore

func (o *Options) WithBefore(before func(point Point) Point) *Options

WithBefore can custom before hook. Support chain call.

func (*Options) WithClient

func (o *Options) WithClient(client *http.Client) *Options

WithClient can custom http client. Support chain call.

func (*Options) WithConfigure

func (o *Options) WithConfigure(options Options) *Options

WithConfigure report options and return a exist Options.

func (*Options) WithContext

func (o *Options) WithContext(context context.Context) *Options

WithContext will update contest. Support chain call.

func (*Options) WithDefaultValue

func (o *Options) WithDefaultValue(defaultValue *Point) *Options

WithUrl can only update `defaultValue` parameters. Support chain call.

func (*Options) WithErrorHandler

func (o *Options) WithErrorHandler(errorHandler func(point Point, status int)) *Options

WithErrorHandler can custom error handle. Support chain call.

func (*Options) WithLog

func (o *Options) WithLog(log log.Logger) *Options

WithLog can update log. Support chain call.

func (*Options) WithUrl

func (o *Options) WithUrl(url string) *Options

WithUrl can only update `url` parameters. Support chain call.

type Point

type Point struct {
	Name     string  `json:"name"`
	Endpoint string  `json:"endpoint"`
	Value    float64 `json:"value"`
	Step     int     `json:"step"`
	Fields   Fields  `json:"fields"`
	Tags     Tags    `json:"tags"`
	Time     int64   `json:"time"`
}

Point is an immutable structure.

type Reporter

type Reporter struct {
	Point    func() Point
	Interval time.Duration
}

Reporter will send Point at regular intervals.

func ApplicationReporter

func ApplicationReporter(duration time.Duration) *Reporter

ApplicationReporter will report goroutine number and memory with this application. duration is sending Interval.

func ApplicationReporterWithAnother added in v0.1.1

func ApplicationReporterWithAnother(pointFunc func() Point, duration time.Duration) *Reporter

ApplicationReporterWithAnother will report goroutine number and memory with this application By another point. duration is sending Interval.

type Tags

type Tags map[string]string

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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