promgo

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: May 21, 2021 License: MIT Imports: 8 Imported by: 0

README

Promgo

Install

$ go get -u github.com/cyrnicolase/promgo

Usage


var (
    // APIRequestTotal 接口请求量
    APIRequestTotal promgo.Counter
)

func init() {
    rdb := redis.NewClient(&redis.Options{
        Addr: `:6379`,
    })

    APIRequestTotal = promgo.NewCounter(rdb, promgo.CounterOptions{
        Name: `api_request_total`,
        Help: `api request counter`,
        Labels: []string{`method`, `endpoint`},
    })

    promgo.GetDefaultRegistry().MustRegister(APIRequestTotal)
}

func main() {
    http.HandleFunc(`/index`, func(rw http.ResponseWriter, r *http.Request) {
        ctx, cancel := context.WithTimeout(r.Context(), time.Second)
        defer cancel()

        APIRequestTotal.Inc(ctx, promgo.ConstLables{
            `method`:   r.Method,
            `endpoint`: r.URL.Path,
        }) 

        fmt.Fprint(rw, `hello`)
    })

    http.HandleFunc(`/metrics`, promgo.Render())
    http.ListenAndServe(`:1234`, nil)
}

Documentation

Index

Constants

View Source
const (
	// CollectorPrefix ...
	CollectorPrefix = `prometheus`
	// FieldSeperator 域分隔符
	FieldSeperator = `__`
)
View Source
const (
	// WorkerCount 执行协程数量
	WorkerCount = 5
)

Variables

View Source
var (
	// DefaultBuckets 默认
	DefaultBuckets = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}
)

Functions

func Render

func Render() http.HandlerFunc

Render ...

Types

type Collector

type Collector interface {
	// Collect 收集指标值
	Collect(ch chan<- *MetricErr)

	// Describe 指标描述
	Describe() *Desc
}

Collector ...

type CollectorOptions

type CollectorOptions struct {
	Namespace string
	Name      string
	Help      string
	Labels    []string
}

CollectorOptions 参数管理

type CollectorRegister

type CollectorRegister interface {
	MustRegister(Collector)
	Register(Collector) error
	Unregister(Collector)
}

CollectorRegister ...

type ConstLabels

type ConstLabels map[string]string

ConstLabels 代表收集指标的标签 name->value 的映射关系

type Counter

type Counter interface {
	Collector

	Inc(context.Context, ConstLabels)
	IncBy(context.Context, float64, ConstLabels)
}

Counter 计数器

func NewCounter

func NewCounter(rdb redis.Cmdable, opts CounterOptions) Counter

NewCounter ...

type CounterOptions

type CounterOptions CollectorOptions

CounterOptions ...

type Desc

type Desc struct {
	Namespace string
	Name      string
	Help      string
	Type      ValueType
	Labels    []string // 标签
}

Desc 指标描述信息

func (Desc) GetHelp

func (d Desc) GetHelp() string

GetHelp ...

func (Desc) GetName

func (d Desc) GetName() string

GetName ...

func (Desc) GetNamespace

func (d Desc) GetNamespace() string

GetNamespace ...

func (Desc) GetType

func (d Desc) GetType() string

GetType ...

func (Desc) ID

func (d Desc) ID() string

ID 唯一标志

type Descs

type Descs []*Desc

Descs ...

func (Descs) Len

func (d Descs) Len() int

Len 长度

func (Descs) Less

func (d Descs) Less(i, j int) bool

Less 对比

func (Descs) Swap

func (d Descs) Swap(i, j int)

Swap 交换

type Gauge

Gauge dashboard

func NewGauge

func NewGauge(rdb redis.Cmdable, opts GaugeOptions) Gauge

NewGauge ...

type GaugeOptions

type GaugeOptions CollectorOptions

GaugeOptions ...

type Histogram

type Histogram interface {
	Collector

	Observe(context.Context, float64, ConstLabels)
	Linear(start, width float64, count int) []float64
	Exponential(start, factor float64, count int) []float64
}

Histogram 直方图

func NewHistogram

func NewHistogram(rdb redis.Cmdable, opts HistogramOptions, buckets []float64) Histogram

NewHistogram ...

type HistogramOptions

type HistogramOptions CollectorOptions

HistogramOptions 直方图参数

type Metric

type Metric struct {
	Desc        *Desc
	Value       float64     // 指标值
	ConstLabels ConstLabels // 常量标签值
}

Metric 指标

func NewMetric

func NewMetric(desc *Desc, v float64, cl ConstLabels) *Metric

NewMetric ...

func (Metric) GetFQName

func (m Metric) GetFQName() string

GetFQName 获取指标名

func (Metric) GetHelp

func (m Metric) GetHelp() string

GetHelp 返回指标解释信息

func (Metric) GetType

func (m Metric) GetType() string

GetType 获取数据类型

func (Metric) GetValue

func (m Metric) GetValue() float64

GetValue 获取值

func (Metric) ID

func (m Metric) ID() string

ID id

func (Metric) String

func (m Metric) String() string

String ...

type MetricErr

type MetricErr struct {
	Metric *Metric
	Err    error
}

MetricErr 指标以及生成错误

func NewMetricErr

func NewMetricErr(m *Metric, err error) *MetricErr

NewMetricErr 指标以及可能的错误

type Metrics

type Metrics []Metric

Metrics ...

func (Metrics) Len

func (m Metrics) Len() int

Len 实现sort.Sort 获取切片长度

func (Metrics) Less

func (m Metrics) Less(i, j int) bool

Less 实现sort.Sort比较相邻元素大小

func (Metrics) Swap

func (m Metrics) Swap(i, j int)

Swap 实现sort.Sort交换两个元素

type Registry

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

Registry ...

func GetDefaultRegistry

func GetDefaultRegistry() *Registry

GetDefaultRegistry ...

func NewRegistry

func NewRegistry() *Registry

NewRegistry ...

func (Registry) Collect

func (r Registry) Collect() []Metric

Collect 收集

func (Registry) MustRegister

func (r Registry) MustRegister(c Collector)

MustRegister 注册

func (Registry) Register

func (r Registry) Register(c Collector) error

Register 注册

func (Registry) Unregister

func (r Registry) Unregister(c Collector)

Unregister 取消注册

type ValueType

type ValueType string

ValueType ...

const (
	// CounterValue 计数器数字
	CounterValue ValueType = `counter`
	// GaugeValue 面版
	GaugeValue ValueType = `gauge`
	// HistogramValue 直方图
	HistogramValue ValueType = `histogram`
)

func (ValueType) String

func (vt ValueType) String() string

String 格式转换

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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