Prometheusノート(一)metric type
19094 ワード
go言語学習交流グループへようこそ636728449
Prometheusノート(二)モニタgoプロジェクトリアルタイムgrafana展示Prometheusノート(一)metric type
Prometheusノート(一)metric type 1、Counter 1.1 Counter 1.2 CounterVec 2、Gauge 2.1 Gauge 2.2 GaugeVec 3、Summary 4、Histogram 二、参考資料 Prometheusノート(一)metric type
Prometheusノート(二)モニタgoプロジェクトリアルタイムgrafana展示Prometheusノート(一)metric type
文書ディレクトリ
Prometheusノート(一)metric type
Prometheusクライアント・ライブラリには、4つのコア・メトリックの標準タイプがあります.これらは現在、クライアント・ライブラリでのみ区別されています(特定のタイプの使用のためにカスタマイズされたAPIを有効にします).Prometheusサーバでは、タイプ情報が使用されておらず、すべてのデータがタイプなしの時系列にフラット化されています.(本明細書のすべてのサンプルコードはgoを用いて例示される)
1、Counter
カウンタは、単一の単調なインクリメントカウンタを表す累積量であり、その値は増加または再起動時にゼロにリセットするしかありません.たとえば、カウンタを使用して、サービスの合計リクエスト数、完了したタスク、またはエラーの合計数を表すことができます.カウンタを使用して、減少する可能性のある値を監視しないでください.たとえば、カウンタを使用して現在実行中のプロセス数を処理するのではなく、Gaugeを使用します.
counterには主に2つの方法があります.// counter 1.
Inc()
// counter , < 0 panic.
Add(float64)
1.1 Counter
一般的なmetricコンテナで使用する手順は、次のとおりです.
1、metricコンテナを初期化する
2.レジスター登録容器
3、コンテナに値を追加する
使用例://step1: counter
pushCounter := prometheus.NewCounter(prometheus.CounterOpts{
Name: "repository_pushes", // : help
})
err := prometheus.Register(pushCounter) // .
if err != nil {
fmt.Println("Push counter couldn't be registered, no counting will happen:", err)
return
}
// Try it once more, this time with a help string.
pushCounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "repository_pushes",
Help: "Number of pushes to external repository.",
})
//setp2:
err = prometheus.Register(pushCounter)
if err != nil {
fmt.Println("Push counter couldn't be registered AGAIN, no counting will happen:", err)
return
}
pushComplete := make(chan struct{})
// TODO: Start a goroutine that performs repository pushes and reports
// each completion via the channel.
for range pushComplete {
//step3:
pushCounter.Inc()
}
出力:Push counter couldn't be registered, no counting will happen: descriptor Desc{fqName: "repository_pushes", help: "", constLabels: {}, variableLabels: []} is invalid: empty help string
1.2 CounterVec
CounterVecは、同じ説明を持つカウンタのセットですが、変数ラベルには異なる値があります.このメソッドは、応答コードやメソッドパーティションのHTTPリクエスト数など、さまざまな次元で分割された同じ内容を計算する場合に使用します.NewCounterVecを使用してインスタンスを作成します.//step1:
httpReqs := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
},
[]string{"code", "method"},
)
//step2:
prometheus.MustRegister(httpReqs)
httpReqs.WithLabelValues("404", "POST").Add(42)
// If you have to access the same set of labels very frequently, it
// might be good to retrieve the metric only once and keep a handle to
// it. But beware of deletion of that metric, see below!
//step3: , Inc() Add()
m := httpReqs.WithLabelValues("200", "GET")
for i := 0; i < 1000000; i++ {
m.Inc()
}
// Delete a metric from the vector. If you have previously kept a handle
// to that metric (as above), future updates via that handle will go
// unseen (even if you re-create a metric with the same label set
// later).
httpReqs.DeleteLabelValues("200", "GET")
// Same thing with the more verbose Labels syntax.
httpReqs.Delete(prometheus.Labels{"method": "GET", "code": "200"})
2、Gauge
2.1 Gauge
Gaugeは、温度や現在のメモリの使用状況、または動作するgoroutineの数などの測定値を任意に大きく小さくすることができる数値を格納するために使用することができる.
主に以下の4つの方法があります// Gauge .
Set(float64)
// Gauge 1.
Inc()
// Gauge 1.
Dec()
// Gauge 。( )
Add(float64)
// Gauge 。( )
Sub(float64)
サンプルコード(CPUの温度をリアルタイムで統計)://step1:
cpuTemprature := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "CPU_Temperature",
Help: "the temperature of CPU",
})
//step2:
prometheus.MustRegister(cpuTemprature)
// cpu
func(){
tem = getCpuTemprature()
//step3: 。
cpuTemprature.Set(tem)
}
2.2 GaugeVec
一度に4つのcpuの温度を統計すると仮定すると、この時はGaugeVecを使用するのに適しています.cpusTemprature := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "CPUs_Temperature",
Help: "the temperature of CPUs.",
},
[]string{
// Which cpu temperature?
"cpuName",
},
)
prometheus.MustRegister(cpusTemprature)
cpusTemprature.WithLabelValues("cpu1").Set(temperature1)
cpusTemprature.WithLabelValues("cpu2").Set(temperature2)
cpusTemprature.WithLabelValues("cpu3").Set(temperature3)
3、Summary
Summaryは、イベントまたはサンプルストリームから単一の観測をキャプチャし、従来の要約統計と同様に要約する:1.総和を観察します.観察カウント、3.順位推定.典型的な例は、要求遅延を観察することである.デフォルトでは、Summaryは遅延の中央値を提供します.temps := prometheus.NewSummary(prometheus.SummaryOpts{
Name: "pond_temperature_celsius",
Help: "The temperature of the frog pond.",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
})
// Simulate some observations.
for i := 0; i < 1000; i++ {
temps.Observe(30 + math.Floor(120*math.Sin(float64(i)*0.1))/10)
}
// Just for demonstration, let's check the state of the summary by
// (ab)using its Write method (which is usually only used by Prometheus
// internally).
metric := &dto.Metric{}
temps.Write(metric)
fmt.Println(proto.MarshalTextString(metric))
4、Histogram
主に、一定の時間範囲内でデータをサンプリングし(通常は要求持続時間または応答サイズ)、その指定区間および総数を統計することができ、通常、ビット数のヒストグラムを計算するために使用されます.temps := prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "pond_temperature_celsius",
Help: "The temperature of the frog pond.", // Sorry, we can't measure how badly it smells.
Buckets: prometheus.LinearBuckets(20, 5, 5), // 5 buckets, each 5 centigrade wide.
})
// Simulate some observations.
for i := 0; i < 1000; i++ {
temps.Observe(30 + math.Floor(120*math.Sin(float64(i)*0.1))/10)
}
// Just for demonstration, let's check the state of the histogram by
// (ab)using its Write method (which is usually only used by Prometheus
// internally).
metric := &dto.Metric{}
temps.Write(metric)
fmt.Println(proto.MarshalTextString(metric))
go言語学習交流グループへようこそ636728449
二、参考資料
[1] https://godoc.org/github.com/prometheus/client_golang/prometheus [2] https://prometheus.io/docs/introduction/overview/
// counter 1.
Inc()
// counter , < 0 panic.
Add(float64)
//step1: counter
pushCounter := prometheus.NewCounter(prometheus.CounterOpts{
Name: "repository_pushes", // : help
})
err := prometheus.Register(pushCounter) // .
if err != nil {
fmt.Println("Push counter couldn't be registered, no counting will happen:", err)
return
}
// Try it once more, this time with a help string.
pushCounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "repository_pushes",
Help: "Number of pushes to external repository.",
})
//setp2:
err = prometheus.Register(pushCounter)
if err != nil {
fmt.Println("Push counter couldn't be registered AGAIN, no counting will happen:", err)
return
}
pushComplete := make(chan struct{})
// TODO: Start a goroutine that performs repository pushes and reports
// each completion via the channel.
for range pushComplete {
//step3:
pushCounter.Inc()
}
Push counter couldn't be registered, no counting will happen: descriptor Desc{fqName: "repository_pushes", help: "", constLabels: {}, variableLabels: []} is invalid: empty help string
//step1:
httpReqs := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
},
[]string{"code", "method"},
)
//step2:
prometheus.MustRegister(httpReqs)
httpReqs.WithLabelValues("404", "POST").Add(42)
// If you have to access the same set of labels very frequently, it
// might be good to retrieve the metric only once and keep a handle to
// it. But beware of deletion of that metric, see below!
//step3: , Inc() Add()
m := httpReqs.WithLabelValues("200", "GET")
for i := 0; i < 1000000; i++ {
m.Inc()
}
// Delete a metric from the vector. If you have previously kept a handle
// to that metric (as above), future updates via that handle will go
// unseen (even if you re-create a metric with the same label set
// later).
httpReqs.DeleteLabelValues("200", "GET")
// Same thing with the more verbose Labels syntax.
httpReqs.Delete(prometheus.Labels{"method": "GET", "code": "200"})
// Gauge .
Set(float64)
// Gauge 1.
Inc()
// Gauge 1.
Dec()
// Gauge 。( )
Add(float64)
// Gauge 。( )
Sub(float64)
//step1:
cpuTemprature := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "CPU_Temperature",
Help: "the temperature of CPU",
})
//step2:
prometheus.MustRegister(cpuTemprature)
// cpu
func(){
tem = getCpuTemprature()
//step3: 。
cpuTemprature.Set(tem)
}
cpusTemprature := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "CPUs_Temperature",
Help: "the temperature of CPUs.",
},
[]string{
// Which cpu temperature?
"cpuName",
},
)
prometheus.MustRegister(cpusTemprature)
cpusTemprature.WithLabelValues("cpu1").Set(temperature1)
cpusTemprature.WithLabelValues("cpu2").Set(temperature2)
cpusTemprature.WithLabelValues("cpu3").Set(temperature3)
temps := prometheus.NewSummary(prometheus.SummaryOpts{
Name: "pond_temperature_celsius",
Help: "The temperature of the frog pond.",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
})
// Simulate some observations.
for i := 0; i < 1000; i++ {
temps.Observe(30 + math.Floor(120*math.Sin(float64(i)*0.1))/10)
}
// Just for demonstration, let's check the state of the summary by
// (ab)using its Write method (which is usually only used by Prometheus
// internally).
metric := &dto.Metric{}
temps.Write(metric)
fmt.Println(proto.MarshalTextString(metric))
temps := prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "pond_temperature_celsius",
Help: "The temperature of the frog pond.", // Sorry, we can't measure how badly it smells.
Buckets: prometheus.LinearBuckets(20, 5, 5), // 5 buckets, each 5 centigrade wide.
})
// Simulate some observations.
for i := 0; i < 1000; i++ {
temps.Observe(30 + math.Floor(120*math.Sin(float64(i)*0.1))/10)
}
// Just for demonstration, let's check the state of the histogram by
// (ab)using its Write method (which is usually only used by Prometheus
// internally).
metric := &dto.Metric{}
temps.Write(metric)
fmt.Println(proto.MarshalTextString(metric))