本文整理汇总了Golang中github.com/prometheus/client_golang/prometheus.CounterOpts类的典型用法代码示例。如果您正苦于以下问题:Golang CounterOpts类的具体用法?Golang CounterOpts怎么用?Golang CounterOpts使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CounterOpts类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: NewCounterVec1
func NewCounterVec1(opts prometheus.CounterOpts, labelName string, labelValues []string) *CounterVec1 {
v := CounterVec1{
desc: prometheus.NewDesc(
prometheus.BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
opts.Help,
[]string{labelName},
opts.ConstLabels,
),
counters: map[string]prometheus.Counter{},
}
for _, lvs := range labelValues {
labels := prometheus.Labels{}
for cl, cv := range opts.ConstLabels {
labels[cl] = cv
}
labels[labelName] = lvs
opts.ConstLabels = labels
v.counters[lvs] = prometheus.NewCounter(opts)
}
return &v
}
开发者ID:realzeitmedia,项目名称:promvec,代码行数:21,代码来源:countervec1.go
示例2: NewCounterVec2
// NewCounterVec2 creates a CounterVec2, given counter options and definitions
// of label names and label values.
func NewCounterVec2(opts prometheus.CounterOpts, labelNames [2]string, labelValues [][2]string) *CounterVec2 {
v := CounterVec2{
desc: prometheus.NewDesc(
prometheus.BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
opts.Help,
labelNames[:],
opts.ConstLabels,
),
counters: map[[2]string]prometheus.Counter{},
}
for _, lvs := range labelValues {
labels := prometheus.Labels{}
for cl, cv := range opts.ConstLabels {
labels[cl] = cv
}
for i, l := range labelNames {
labels[l] = lvs[i]
}
opts.ConstLabels = labels
v.counters[lvs] = prometheus.NewCounter(opts)
}
return &v
}
开发者ID:realzeitmedia,项目名称:promvec,代码行数:25,代码来源:countervec2.go
示例3: ExampleRegister
func ExampleRegister() {
// Imagine you have a worker pool and want to count the tasks completed.
taskCounter := prometheus.NewCounter(prometheus.CounterOpts{
Subsystem: "worker_pool",
Name: "completed_tasks_total",
Help: "Total number of tasks completed.",
})
// This will register fine.
if err := prometheus.Register(taskCounter); err != nil {
fmt.Println(err)
} else {
fmt.Println("taskCounter registered.")
}
// Don't forget to tell the HTTP server about the Prometheus handler.
// (In a real program, you still need to start the HTTP server...)
http.Handle("/metrics", prometheus.Handler())
// Now you can start workers and give every one of them a pointer to
// taskCounter and let it increment it whenever it completes a task.
taskCounter.Inc() // This has to happen somewhere in the worker code.
// But wait, you want to see how individual workers perform. So you need
// a vector of counters, with one element for each worker.
taskCounterVec := prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: "worker_pool",
Name: "completed_tasks_total",
Help: "Total number of tasks completed.",
},
[]string{"worker_id"},
)
// Registering will fail because we already have a metric of that name.
if err := prometheus.Register(taskCounterVec); err != nil {
fmt.Println("taskCounterVec not registered:", err)
} else {
fmt.Println("taskCounterVec registered.")
}
// To fix, first unregister the old taskCounter.
if prometheus.Unregister(taskCounter) {
fmt.Println("taskCounter unregistered.")
}
// Try registering taskCounterVec again.
if err := prometheus.Register(taskCounterVec); err != nil {
fmt.Println("taskCounterVec not registered:", err)
} else {
fmt.Println("taskCounterVec registered.")
}
// Bummer! Still doesn't work.
// Prometheus will not allow you to ever export metrics with
// inconsistent help strings or label names. After unregistering, the
// unregistered metrics will cease to show up in the /metrics HTTP
// response, but the registry still remembers that those metrics had
// been exported before. For this example, we will now choose a
// different name. (In a real program, you would obviously not export
// the obsolete metric in the first place.)
taskCounterVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: "worker_pool",
Name: "completed_tasks_by_id",
Help: "Total number of tasks completed.",
},
[]string{"worker_id"},
)
if err := prometheus.Register(taskCounterVec); err != nil {
fmt.Println("taskCounterVec not registered:", err)
} else {
fmt.Println("taskCounterVec registered.")
}
// Finally it worked!
// The workers have to tell taskCounterVec their id to increment the
// right element in the metric vector.
taskCounterVec.WithLabelValues("42").Inc() // Code from worker 42.
// Each worker could also keep a reference to their own counter element
// around. Pick the counter at initialization time of the worker.
myCounter := taskCounterVec.WithLabelValues("42") // From worker 42 initialization code.
myCounter.Inc() // Somewhere in the code of that worker.
// Note that something like WithLabelValues("42", "spurious arg") would
// panic (because you have provided too many label values). If you want
// to get an error instead, use GetMetricWithLabelValues(...) instead.
notMyCounter, err := taskCounterVec.GetMetricWithLabelValues("42", "spurious arg")
if err != nil {
fmt.Println("Worker initialization failed:", err)
}
if notMyCounter == nil {
fmt.Println("notMyCounter is nil.")
}
// A different (and somewhat tricky) approach is to use
// ConstLabels. ConstLabels are pairs of label names and label values
// that never change. You might ask what those labels are good for (and
// rightfully so - if they never change, they could as well be part of
// the metric name). There are essentially two use-cases: The first is
// if labels are constant throughout the lifetime of a binary execution,
//.........这里部分代码省略.........
开发者ID:eghobo,项目名称:kubedash,代码行数:101,代码来源:examples_test.go
注:本文中的github.com/prometheus/client_golang/prometheus.CounterOpts类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论