metric

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2022 License: MPL-2.0 Imports: 6 Imported by: 2

README

项目简介

数据统计、监控采集等

Prometheus

Prometheus是用于事件监视和警报的免费软件应用程序。它将实时指标记录在使用HTTP拉模型构建的时间序列数据库中,具有灵活的查询和实时警报。

Prometheus基本概念

Prometheus将所有数据存储为时间序列,这里先来了解一下prometheus中的一些基本概念

指标名和标签

每个时间序列都由指标名和一组键值对(也称为标签)唯一标识。

metric的格式如下:

<metric name>{<label name>=<label value>, ...}

例如:

http_requests_total{host="192.10.0.1", method="POST", handler="/messages"}
  • http_requests_total是指标名;
  • host、method、handler是三个标签(label),也就是三个维度;
  • 查询语句可以基于这些标签or维度进行过滤和聚合;

Prometheus指标类型

Prometheus client库提供四种核心度量标准类型。注意是客户端。Prometheus服务端没有区分类型,将所有数据展平为无类型时间序列。

1.Counter:只增不减的累加指标

Counter就是一个计数器,表示一种累积型指标,该指标只能单调递增或在重新启动时重置为零,例如,您可以使用计数器来表示所服务的请求数,已完成的任务或错误。

2.Gauge:可增可减的测量指标

Gauge是最简单的度量类型,只有一个简单的返回值,可增可减,也可以set为指定的值。所以Gauge通常用于反映当前状态,比如当前温度或当前内存使用情况;当然也可以用于“可增加可减少”的计数指标。

3.Histogram:自带buckets区间用于统计分布的直方图

Histogram主要用于在设定的分布范围内(Buckets)记录大小或者次数。

例如http请求响应时间:0-100ms、100-200ms、200-300ms、>300ms 的分布情况,Histogram会自动创建3个指标,分别为:

  • 事件发送的总次数_count:比如当前一共发生了2次http请求
  • 所有事件产生值的大小的总和_sum:比如发生的2次http请求总的响应时间为150ms
  • 事件产生的值分布在bucket中的次数_bucket{le="上限"}:比如响应时间0-100ms的请求1次,100-200ms的请求1次,其他的0次
4.Summary:数据分布统计图

Summary和Histogram类似,都可以统计事件发生的次数或者大小,以及其分布情况。

Summary和Histogram都提供了对于事件的计数_count以及值的汇总_sum,因此使用_count,和_sum时间序列可以计算出相同的内容。

同时Summary和Histogram都可以计算和统计样本的分布情况,比如中位数,n分位数等等。不同在于Histogram可以通过histogram_quantile函数在服务器端计算分位数。 而Sumamry的分位数则是直接在客户端进行定义。因此对于分位数的计算。 Summary在通过PromQL进行查询时有更好的性能表现,而Histogram则会消耗更多的资源。相对的对于客户端而言Histogram消耗的资源更少。

Prometheus作业和实例

在Prometheus中,一个可以拉取数据的端点IP:Port叫做一个实例(instance),而具有多个相同类型实例的集合称作一个作业(job)

 - job: api-server
      - instance 1: 1.2.3.4:5670
      - instance 2: 1.2.3.4:5671
      - instance 3: 5.6.7.8:5670
      - instance 4: 5.6.7.8:5671

当Prometheus拉取指标数据时,会自动生成一些标签(label)用于区别抓取的来源:

  • job:配置的作业名;
  • instance:配置的实例名,若没有实例名,则是抓取的IP:Port。

对于每一个实例(instance)的抓取,Prometheus会默认保存以下数据:

  • up{job="", instance=""}:如果实例是健康的,即可达,值为1,否则为0;
  • scrape_duration_seconds{job="", instance=""}:抓取耗时;
  • scrape_samples_post_metric_relabeling{job="", instance=""}:指标重新标记后剩余的样本数。
  • scrape_samples_scraped{job="", instance=""}:实例暴露的样本数

该up指标对于监控实例健康状态很有用。

二、最简单的Exporter

当你安装好go的开发环境,并下载好Prometheus依赖包到vendor以后,就可以编译个最简单的Exporter,代码如下:

package main

import (
    "log"
    "net/http"
    "github.com/prometheus/client_golang/prometheus/promhttp"
)

func main() {
    http.Handle("/metrics", promhttp.Handler())
    log.Fatal(http.ListenAndServe(":8080", nil))
}

执行go build编译运行,然后访问http://127.0.0.1:8080/metrics就可以看到采集到的指标数据。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Avg

func Avg(iterator Iterator) float64

Avg the values within the window.

func Count

func Count(iterator Iterator) float64

Count sums the count value within the window.

func Max

func Max(iterator Iterator) float64

Max the values within the window.

func Min

func Min(iterator Iterator) float64

Min the values within the window.

func Sum

func Sum(iterator Iterator) float64

Sum the values within the window.

Types

type Aggregation

type Aggregation interface {
	// Min finds the min value within the window.
	Min() float64
	// Max finds the max value within the window.
	Max() float64
	// Avg computes average value within the window.
	Avg() float64
	// Sum computes sum value within the window.
	Sum() float64
}

Aggregation contains some common aggregation function. Each aggregation can compute summary statistics of window.

type Bucket

type Bucket struct {
	Points []float64
	Count  int64
	// contains filtered or unexported fields
}

Bucket contains multiple float64 points.

func (*Bucket) Add

func (b *Bucket) Add(offset int, val float64)

Add adds the given value to the point.

func (*Bucket) Append

func (b *Bucket) Append(val float64)

Append appends the given value to the bucket.

func (*Bucket) Next

func (b *Bucket) Next() *Bucket

Next returns the next bucket.

func (*Bucket) Reset

func (b *Bucket) Reset()

Reset empties the bucket.

type Counter

type Counter interface {
	Metric
}

Counter stores a numerical value that only ever goes up.

func NewCounter

func NewCounter(opts CounterOpts) Counter

NewCounter creates a new Counter based on the CounterOpts.

type CounterOpts

type CounterOpts Opts

CounterOpts is an alias of Opts.

type CounterVec

type CounterVec interface {
	// Inc increments the counter by 1. Use Add to increment it by arbitrary
	// non-negative values.
	Inc(labels ...string)
	// Add adds the given value to the counter. It panics if the value is <
	// 0.
	Add(v float64, labels ...string)
}

CounterVec counter vec.

func NewBusinessMetricCount

func NewBusinessMetricCount(name string, labels ...string) CounterVec

NewBusinessMetricCount business Metric count vec. name or labels should not be empty.

func NewCounterVec

func NewCounterVec(cfg *CounterVecOpts) CounterVec

NewCounterVec .

type CounterVecOpts

type CounterVecOpts VectorOpts

CounterVecOpts is an alias of VectorOpts.

type Gauge

type Gauge interface {
	Metric
	// Sets sets the value to the given number.
	Set(int64)
}

Gauge stores a numerical value that can be add arbitrarily.

func NewGauge

func NewGauge(opts GaugeOpts) Gauge

NewGauge creates a new Gauge based on the GaugeOpts.

type GaugeOpts

type GaugeOpts Opts

GaugeOpts is an alias of Opts.

type GaugeVec

type GaugeVec interface {
	// Set sets the Gauge to an arbitrary value.
	Set(v float64, labels ...string)
	// Inc increments the Gauge by 1. Use Add to increment it by arbitrary
	// values.
	Inc(labels ...string)
	// Add adds the given value to the Gauge. (The value can be negative,
	// resulting in a decrease of the Gauge.)
	Add(v float64, labels ...string)
}

GaugeVec gauge vec.

func NewBusinessMetricGauge

func NewBusinessMetricGauge(name string, labels ...string) GaugeVec

NewBusinessMetricGauge business Metric gauge vec. name or labels should not be empty.

func NewGaugeVec

func NewGaugeVec(cfg *GaugeVecOpts) GaugeVec

NewGaugeVec .

type GaugeVecOpts

type GaugeVecOpts VectorOpts

GaugeVecOpts is an alias of VectorOpts.

type HistogramVec

type HistogramVec interface {
	// Observe adds a single observation to the histogram.
	Observe(v int64, labels ...string)
}

HistogramVec gauge vec.

func NewBusinessMetricHistogram

func NewBusinessMetricHistogram(name string, buckets []float64, labels ...string) HistogramVec

NewBusinessMetricHistogram business Metric histogram vec. name or labels should not be empty.

func NewHistogramVec

func NewHistogramVec(cfg *HistogramVecOpts) HistogramVec

NewHistogramVec new a histogram vec.

type HistogramVecOpts

type HistogramVecOpts struct {
	Namespace string
	Subsystem string
	Name      string
	Help      string
	Labels    []string
	Buckets   []float64
}

HistogramVecOpts is histogram vector opts.

type Iterator

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

Iterator iterates the buckets within the window.

func (*Iterator) Bucket

func (i *Iterator) Bucket() Bucket

Bucket gets current bucket.

func (*Iterator) Next

func (i *Iterator) Next() bool

Next returns true util all of the buckets has been iterated.

type Metric

type Metric interface {
	// Add adds the given value to the counter.
	Add(int64)
	// Value gets the current value.
	// If the metric's type is PointGauge, RollingCounter, RollingGauge,
	// it returns the sum value within the window.
	Value() int64
}

Metric is a sample interface. Implementations of Metrics in metric package are Counter, Gauge, PointGauge, RollingCounter and RollingGauge.

type Opts

type Opts struct {
}

Opts contains the common arguments for creating Metric.

type PointGauge

type PointGauge interface {
	Aggregation
	Metric
	// Reduce applies the reduction function to all buckets within the window.
	Reduce(func(Iterator) float64) float64
}

PointGauge represents a ring window. Every buckets within the window contains one point. When the window is full, the earliest point will be overwrite.

func NewPointGauge

func NewPointGauge(opts PointGaugeOpts) PointGauge

NewPointGauge creates a new PointGauge based on PointGaugeOpts.

type PointGaugeOpts

type PointGaugeOpts struct {
	// Size represents the bucket size within the window.
	Size int
}

PointGaugeOpts contains the arguments for creating PointGauge.

type PointPolicy

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

PointPolicy is a policy of points within the window. PointPolicy wraps the window and make it seem like ring-buf. When using PointPolicy, every buckets within the windows contains at more one point. e.g. [[1], [2], [3]]

func NewPointPolicy

func NewPointPolicy(window *Window) *PointPolicy

NewPointPolicy creates a new PointPolicy.

func (*PointPolicy) Append

func (p *PointPolicy) Append(val float64)

Append appends the given points to the window.

func (*PointPolicy) Reduce

func (p *PointPolicy) Reduce(f func(Iterator) float64) float64

Reduce applies the reduction function to all buckets within the window.

type RollingCounter

type RollingCounter interface {
	Metric
	Aggregation
	Timespan() int
	// Reduce applies the reduction function to all buckets within the window.
	Reduce(func(Iterator) float64) float64
}

RollingCounter represents a ring window based on time duration. e.g. [[1], [3], [5]]

func NewRollingCounter

func NewRollingCounter(opts RollingCounterOpts) RollingCounter

NewRollingCounter creates a new RollingCounter bases on RollingCounterOpts.

type RollingCounterOpts

type RollingCounterOpts struct {
	Size           int
	BucketDuration time.Duration
}

RollingCounterOpts contains the arguments for creating RollingCounter.

type RollingGauge

type RollingGauge interface {
	Metric
	Aggregation
	// Reduce applies the reduction function to all buckets within the window.
	Reduce(func(Iterator) float64) float64
}

RollingGauge represents a ring window based on time duration. e.g. [[1, 2], [1, 2, 3], [1,2, 3, 4]]

func NewRollingGauge

func NewRollingGauge(opts RollingGaugeOpts) RollingGauge

NewRollingGauge creates a new RollingGauge baseed on RollingGaugeOpts.

type RollingGaugeOpts

type RollingGaugeOpts struct {
	Size           int
	BucketDuration time.Duration
}

RollingGaugeOpts contains the arguments for creating RollingGauge.

type RollingPolicy

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

RollingPolicy is a policy for ring window based on time duration. RollingPolicy moves bucket offset with time duration. e.g. If the last point is appended one bucket duration ago, RollingPolicy will increment current offset.

func NewRollingPolicy

func NewRollingPolicy(window *Window, opts RollingPolicyOpts) *RollingPolicy

NewRollingPolicy creates a new RollingPolicy based on the given window and RollingPolicyOpts.

func (*RollingPolicy) Add

func (r *RollingPolicy) Add(val float64)

Add adds the given value to the latest point within bucket.

func (*RollingPolicy) Append

func (r *RollingPolicy) Append(val float64)

Append appends the given points to the window.

func (*RollingPolicy) Reduce

func (r *RollingPolicy) Reduce(f func(Iterator) float64) (val float64)

Reduce applies the reduction function to all buckets within the window.

type RollingPolicyOpts

type RollingPolicyOpts struct {
	BucketDuration time.Duration
}

RollingPolicyOpts contains the arguments for creating RollingPolicy.

type VectorOpts

type VectorOpts struct {
	Namespace string
	Subsystem string
	Name      string
	Help      string
	Labels    []string
}

VectorOpts contains the common arguments for creating vec Metric..

type Window

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

Window contains multiple buckets.

func NewWindow

func NewWindow(opts WindowOpts) *Window

NewWindow creates a new Window based on WindowOpts.

func (*Window) Add

func (w *Window) Add(offset int, val float64)

Add adds the given value to the latest point within bucket where index equals the given offset.

func (*Window) Append

func (w *Window) Append(offset int, val float64)

Append appends the given value to the bucket where index equals the given offset.

func (*Window) Bucket

func (w *Window) Bucket(offset int) Bucket

Bucket returns the bucket where index equals the given offset.

func (*Window) Iterator

func (w *Window) Iterator(offset int, count int) Iterator

Iterator returns the bucket iterator.

func (*Window) ResetBucket

func (w *Window) ResetBucket(offset int)

ResetBucket empties the bucket based on the given offset.

func (*Window) ResetBuckets

func (w *Window) ResetBuckets(offsets []int)

ResetBuckets empties the buckets based on the given offsets.

func (*Window) ResetWindow

func (w *Window) ResetWindow()

ResetWindow empties all buckets within the window.

func (*Window) Size

func (w *Window) Size() int

Size returns the size of the window.

type WindowOpts

type WindowOpts struct {
	Size int
}

WindowOpts contains the arguments for creating Window.

Jump to

Keyboard shortcuts

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