metric

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2021 License: MIT Imports: 4 Imported by: 0

README

监控主要分为3部分

  • 请求监控基本指标: QPS、请求响应时间、请求体大小和响应体大小,可以使用本框架的 PromMiddleware 中间件来进行监控
  • Go 进程监控: Goroutine使用数量、GC回收时间、内存使用情况、活动对象数、对象分配的速率等,框架已内置 /metrics, 只需要在 Prometheus 接入即可。 可以使用Grafana插件6671来查看
  • 具体业务监控: 比如cache miss等

Go 进程监控图示例 Go processes

应用监控 面板配置 ,可以直接导入到Grafana进行使用。

/metrics 访问后返回示例信息如下:

# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 5.74e-06
go_gc_duration_seconds{quantile="0.25"} 3.1516e-05
go_gc_duration_seconds{quantile="0.5"} 0.000159558
go_gc_duration_seconds{quantile="0.75"} 0.002622183
go_gc_duration_seconds{quantile="1"} 0.002622183
go_gc_duration_seconds_sum 0.002818997
go_gc_duration_seconds_count 4
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 20
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version="go1.13.8"} 1
# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 2.1649928e+07
# HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.
# TYPE go_memstats_alloc_bytes_total counter
go_memstats_alloc_bytes_total 2.7186312e+07
# HELP go_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table.
# TYPE go_memstats_buck_hash_sys_bytes gauge
go_memstats_buck_hash_sys_bytes 1.44869e+06
# HELP go_memstats_frees_total Total number of frees.
# TYPE go_memstats_frees_total counter
go_memstats_frees_total 51734
# HELP go_memstats_gc_cpu_fraction The fraction of this program's available CPU time used by the GC since the program started.
# TYPE go_memstats_gc_cpu_fraction gauge
go_memstats_gc_cpu_fraction 0.06674713461941606
# HELP go_memstats_gc_sys_bytes Number of bytes used for garbage collection system metadata.
# TYPE go_memstats_gc_sys_bytes gauge
go_memstats_gc_sys_bytes 2.38592e+06
# HELP go_memstats_heap_alloc_bytes Number of heap bytes allocated and still in use.
# TYPE go_memstats_heap_alloc_bytes gauge
go_memstats_heap_alloc_bytes 2.1649928e+07
# HELP go_memstats_heap_idle_bytes Number of heap bytes waiting to be used.
# TYPE go_memstats_heap_idle_bytes gauge
go_memstats_heap_idle_bytes 4.1934848e+07
# HELP go_memstats_heap_inuse_bytes Number of heap bytes that are in use.
# TYPE go_memstats_heap_inuse_bytes gauge
go_memstats_heap_inuse_bytes 2.4190976e+07
# HELP go_memstats_heap_objects Number of allocated objects.
# TYPE go_memstats_heap_objects gauge
go_memstats_heap_objects 18945
# HELP go_memstats_heap_released_bytes Number of heap bytes released to OS.
# TYPE go_memstats_heap_released_bytes gauge
go_memstats_heap_released_bytes 4.1885696e+07
# HELP go_memstats_heap_sys_bytes Number of heap bytes obtained from system.
# TYPE go_memstats_heap_sys_bytes gauge
go_memstats_heap_sys_bytes 6.6125824e+07
# HELP go_memstats_last_gc_time_seconds Number of seconds since 1970 of last garbage collection.
# TYPE go_memstats_last_gc_time_seconds gauge
go_memstats_last_gc_time_seconds 1.6127124443332071e+09
# HELP go_memstats_lookups_total Total number of pointer lookups.
# TYPE go_memstats_lookups_total counter
go_memstats_lookups_total 0
# HELP go_memstats_mallocs_total Total number of mallocs.
# TYPE go_memstats_mallocs_total counter
go_memstats_mallocs_total 70679
# HELP go_memstats_mcache_inuse_bytes Number of bytes in use by mcache structures.
# TYPE go_memstats_mcache_inuse_bytes gauge
go_memstats_mcache_inuse_bytes 13888
# HELP go_memstats_mcache_sys_bytes Number of bytes used for mcache structures obtained from system.
# TYPE go_memstats_mcache_sys_bytes gauge
go_memstats_mcache_sys_bytes 16384
# HELP go_memstats_mspan_inuse_bytes Number of bytes in use by mspan structures.
# TYPE go_memstats_mspan_inuse_bytes gauge
go_memstats_mspan_inuse_bytes 85680
# HELP go_memstats_mspan_sys_bytes Number of bytes used for mspan structures obtained from system.
# TYPE go_memstats_mspan_sys_bytes gauge
go_memstats_mspan_sys_bytes 98304
# HELP go_memstats_next_gc_bytes Number of heap bytes when next garbage collection will take place.
# TYPE go_memstats_next_gc_bytes gauge
go_memstats_next_gc_bytes 3.4761504e+07
# HELP go_memstats_other_sys_bytes Number of bytes used for other system allocations.
# TYPE go_memstats_other_sys_bytes gauge
go_memstats_other_sys_bytes 2.014726e+06
# HELP go_memstats_stack_inuse_bytes Number of bytes in use by the stack allocator.
# TYPE go_memstats_stack_inuse_bytes gauge
go_memstats_stack_inuse_bytes 983040
# HELP go_memstats_stack_sys_bytes Number of bytes obtained from system for stack allocator.
# TYPE go_memstats_stack_sys_bytes gauge
go_memstats_stack_sys_bytes 983040
# HELP go_memstats_sys_bytes Number of bytes obtained from system.
# TYPE go_memstats_sys_bytes gauge
go_memstats_sys_bytes 7.3072888e+07
# HELP go_threads Number of OS threads created.
# TYPE go_threads gauge
go_threads 19
# HELP promhttp_metric_handler_requests_in_flight Current number of scrapes being served.
# TYPE promhttp_metric_handler_requests_in_flight gauge
promhttp_metric_handler_requests_in_flight 1
# HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code.
# TYPE promhttp_metric_handler_requests_total counter
promhttp_metric_handler_requests_total{code="200"} 1
promhttp_metric_handler_requests_total{code="500"} 0
promhttp_metric_handler_requests_total{code="503"} 0
# HELP eagle_http_request_count_total Total number of HTTP requests made.
# TYPE eagle_http_request_count_total counter
eagle_http_request_count_total{endpoint="/favicon.ico",method="GET",status="404"} 1
# HELP eagle_http_request_duration_seconds HTTP request latencies in seconds.
# TYPE eagle_http_request_duration_seconds histogram
eagle_http_request_duration_seconds_bucket{endpoint="/favicon.ico",method="GET",status="404",le="0.005"} 1
eagle_http_request_duration_seconds_bucket{endpoint="/favicon.ico",method="GET",status="404",le="0.01"} 1
eagle_http_request_duration_seconds_bucket{endpoint="/favicon.ico",method="GET",status="404",le="0.025"} 1
eagle_http_request_duration_seconds_bucket{endpoint="/favicon.ico",method="GET",status="404",le="0.05"} 1
eagle_http_request_duration_seconds_bucket{endpoint="/favicon.ico",method="GET",status="404",le="0.1"} 1
eagle_http_request_duration_seconds_bucket{endpoint="/favicon.ico",method="GET",status="404",le="0.25"} 1
eagle_http_request_duration_seconds_bucket{endpoint="/favicon.ico",method="GET",status="404",le="0.5"} 1
eagle_http_request_duration_seconds_bucket{endpoint="/favicon.ico",method="GET",status="404",le="1"} 1
eagle_http_request_duration_seconds_bucket{endpoint="/favicon.ico",method="GET",status="404",le="2.5"} 1
eagle_http_request_duration_seconds_bucket{endpoint="/favicon.ico",method="GET",status="404",le="5"} 1
eagle_http_request_duration_seconds_bucket{endpoint="/favicon.ico",method="GET",status="404",le="10"} 1
eagle_http_request_duration_seconds_bucket{endpoint="/favicon.ico",method="GET",status="404",le="+Inf"} 1
eagle_http_request_duration_seconds_sum{endpoint="/favicon.ico",method="GET",status="404"} 4.2263e-05
eagle_http_request_duration_seconds_count{endpoint="/favicon.ico",method="GET",status="404"} 1
# HELP eagle_http_request_size_bytes HTTP request sizes in bytes.
# TYPE eagle_http_request_size_bytes summary
eagle_http_request_size_bytes_sum{endpoint="/favicon.ico",method="GET",status="404"} 597
eagle_http_request_size_bytes_count{endpoint="/favicon.ico",method="GET",status="404"} 1
# HELP eagle_http_response_size_bytes HTTP request sizes in bytes.
# TYPE eagle_http_response_size_bytes summary
eagle_http_response_size_bytes_sum{endpoint="/favicon.ico",method="GET",status="404"} 19
eagle_http_response_size_bytes_count{endpoint="/favicon.ico",method="GET",status="404"} 1
# HELP eagle_uptime HTTP service uptime.
# TYPE eagle_uptime counter
eagle_uptime 9

Reference

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 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 NewCounterVec

func NewCounterVec(cfg *CounterVecOpts) CounterVec

NewCounterVec .

type CounterVecOpts

type CounterVecOpts VectorOpts

CounterVecOpts is an alias of VectorOpts.

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)
	Dec(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 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 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 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 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