本文整理汇总了Golang中github.com/prometheus/client_golang/prometheus.NewSummary函数的典型用法代码示例。如果您正苦于以下问题:Golang NewSummary函数的具体用法?Golang NewSummary怎么用?Golang NewSummary使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewSummary函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: newMetrics
func newMetrics(r prometheus.Registerer) *metrics {
m := &metrics{}
m.gcDuration = prometheus.NewSummary(prometheus.SummaryOpts{
Name: "alertmanager_silences_gc_duration_seconds",
Help: "Duration of the last silence garbage collection cycle.",
})
m.snapshotDuration = prometheus.NewSummary(prometheus.SummaryOpts{
Name: "alertmanager_silences_snapshot_duration_seconds",
Help: "Duration of the last silence snapshot.",
})
m.queriesTotal = prometheus.NewCounter(prometheus.CounterOpts{
Name: "alertmanager_silences_queries_total",
Help: "How many silence queries were received.",
})
m.queryErrorsTotal = prometheus.NewCounter(prometheus.CounterOpts{
Name: "alertmanager_silences_query_errors_total",
Help: "How many silence received queries did not succeed.",
})
m.queryDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "alertmanager_silences_query_duration_seconds",
Help: "Duration of silence query evaluation.",
})
if r != nil {
r.MustRegister(
m.gcDuration,
m.snapshotDuration,
m.queriesTotal,
m.queryErrorsTotal,
m.queryDuration,
)
}
return m
}
示例2: InstrumentHandlerFuncWithOpts
// InstrumentHandlerFuncWithOpts works like InstrumentHandlerFunc but provides
// more flexibility (at the cost of a more complex call syntax).
//
// As InstrumentHandlerFunc, this function registers four metric collectors, but it
// uses the provided SummaryOpts to create them. However, the fields "Name" and
// "Help" in the SummaryOpts are ignored. "Name" is replaced by
// "requests_total", "request_duration_microseconds", "request_size_bytes", and
// "response_size_bytes", respectively. "Help" is replaced by an appropriate
// help string. The names of the variable labels of the http_requests_total
// CounterVec are "method" (get, post, etc.), and "code" (HTTP status code).
//
// If InstrumentHandlerWithOpts is called as follows, it mimics exactly the
// behavior of InstrumentHandler:
//
// prometheus.InstrumentHandlerWithOpts(
// prometheus.SummaryOpts{
// Subsystem: "http",
// ConstLabels: prometheus.Labels{"handler": handlerName},
// },
// handler,
// )
//
// Technical detail: "requests_total" is a CounterVec, not a SummaryVec, so it
// cannot use SummaryOpts. Instead, a CounterOpts struct is created internally,
// and all its fields are set to the equally named fields in the provided
// SummaryOpts.
func InstrumentHandlerFuncWithOpts(opts prometheus.SummaryOpts, handlerFunc gin.HandlerFunc) gin.HandlerFunc {
reqCnt := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: opts.Namespace,
Subsystem: opts.Subsystem,
Name: "requests_total",
Help: "Total number of HTTP requests made.",
ConstLabels: opts.ConstLabels,
},
instLabels,
)
opts.Name = "request_duration_microseconds"
opts.Help = "The HTTP request latencies in microseconds."
reqDur := prometheus.NewSummary(opts)
opts.Name = "request_size_bytes"
opts.Help = "The HTTP request sizes in bytes."
reqSz := prometheus.NewSummary(opts)
opts.Name = "response_size_bytes"
opts.Help = "The HTTP response sizes in bytes."
resSz := prometheus.NewSummary(opts)
regReqCnt := prometheus.MustRegisterOrGet(reqCnt).(*prometheus.CounterVec)
regReqDur := prometheus.MustRegisterOrGet(reqDur).(prometheus.Summary)
regReqSz := prometheus.MustRegisterOrGet(reqSz).(prometheus.Summary)
regResSz := prometheus.MustRegisterOrGet(resSz).(prometheus.Summary)
return func(c *gin.Context) {
now := time.Now()
r := c.Request
out := make(chan int)
urlLen := 0
if r.URL != nil {
urlLen = len(r.URL.String())
}
go computeApproximateRequestSize(r, out, urlLen)
handlerFunc(c)
elapsed := float64(time.Since(now)) / float64(time.Microsecond)
method := sanitizeMethod(r.Method)
code := sanitizeCode(c.Writer.Status())
regReqCnt.WithLabelValues(method, code).Inc()
regReqDur.Observe(elapsed)
regResSz.Observe(float64(c.Writer.Size()))
regReqSz.Observe(float64(<-out))
}
}
示例3: TestSensorRecordSummary
func TestSensorRecordSummary(t *testing.T) {
testServer := httptest.NewServer(prometheus.UninstrumentedHandler())
defer testServer.Close()
sensor := &Sensor{
Type: "summary",
collector: prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: "telemetry",
Subsystem: "sensors",
Name: "TestSensorRecordSummary",
Help: "help",
})}
prometheus.MustRegister(sensor.collector)
patt := `telemetry_sensors_TestSensorRecordSummary{quantile="([\.0-9]*)"} ([0-9\.]*)`
// need a bunch of metrics to make quantiles make any sense
for i := 1; i <= 10; i++ {
sensor.record(fmt.Sprintf("%v", i))
}
resp := getFromTestServer(t, testServer)
expected := [][]string{{"0.5", "5"}, {"0.9", "9"}, {"0.99", "9"}}
if !checkBuckets(resp, patt, expected) {
t.Fatalf("Failed to get match for sensor in response")
}
for i := 1; i <= 5; i++ {
// add a new record for each one in the bottom half
sensor.record(fmt.Sprintf("%v", i))
}
resp = getFromTestServer(t, testServer)
expected = [][]string{{"0.5", "4"}, {"0.9", "8"}, {"0.99", "9"}}
if !checkBuckets(resp, patt, expected) {
t.Fatalf("Failed to get match for sensor in response")
}
}
示例4: NewWorkDurationMetric
func (_ prometheusMetricsProvider) NewWorkDurationMetric(name string) workqueue.SummaryMetric {
workDuration := prometheus.NewSummary(prometheus.SummaryOpts{
Subsystem: name,
Name: "work_duration",
Help: "How long processing an item from workqueue" + name + " takes.",
})
prometheus.Register(workDuration)
return workDuration
}
示例5: NewSensors
// NewSensors creates new sensors from a raw config
func NewSensors(raw []interface{}) ([]*Sensor, error) {
var sensors []*Sensor
if err := utils.DecodeRaw(raw, &sensors); err != nil {
return nil, fmt.Errorf("Sensor configuration error: %v", err)
}
for _, s := range sensors {
check, err := commands.NewCommand(s.CheckExec, s.Timeout)
if err != nil {
return nil, fmt.Errorf("could not parse check in sensor %s: %s", s.Name, err)
}
check.Name = fmt.Sprintf("%s.sensor", s.Name)
s.checkCmd = check
// the prometheus client lib's API here is baffling... they don't expose
// an interface or embed their Opts type in each of the Opts "subtypes",
// so we can't share the initialization.
switch {
case s.Type == "counter":
s.collector = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: s.Namespace,
Subsystem: s.Subsystem,
Name: s.Name,
Help: s.Help,
})
case s.Type == "gauge":
s.collector = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: s.Namespace,
Subsystem: s.Subsystem,
Name: s.Name,
Help: s.Help,
})
case s.Type == "histogram":
s.collector = prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: s.Namespace,
Subsystem: s.Subsystem,
Name: s.Name,
Help: s.Help,
})
case s.Type == "summary":
s.collector = prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: s.Namespace,
Subsystem: s.Subsystem,
Name: s.Name,
Help: s.Help,
})
default:
return nil, fmt.Errorf("invalid sensor type: %s", s.Type)
}
// we're going to unregister before every attempt to register
// so that we can reload config
prometheus.Unregister(s.collector)
if err := prometheus.Register(s.collector); err != nil {
return nil, err
}
}
return sensors, nil
}
示例6: NewLatencyMetric
func (_ prometheusMetricsProvider) NewLatencyMetric(name string) workqueue.SummaryMetric {
latency := prometheus.NewSummary(prometheus.SummaryOpts{
Subsystem: name,
Name: "queue_latency",
Help: "How long an item stays in workqueue" + name + " before being requested.",
})
prometheus.Register(latency)
return latency
}
示例7: NewStorageQueueManager
// NewStorageQueueManager builds a new StorageQueueManager.
func NewStorageQueueManager(tsdb StorageClient, queueCapacity int) *StorageQueueManager {
constLabels := prometheus.Labels{
"type": tsdb.Name(),
}
return &StorageQueueManager{
tsdb: tsdb,
queue: make(chan *clientmodel.Sample, queueCapacity),
sendSemaphore: make(chan bool, maxConcurrentSends),
drained: make(chan bool),
samplesCount: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "sent_samples_total",
Help: "Total number of processed samples to be sent to remote storage.",
ConstLabels: constLabels,
},
[]string{result},
),
sendLatency: prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "sent_latency_milliseconds",
Help: "Latency quantiles for sending sample batches to the remote storage.",
ConstLabels: constLabels,
}),
sendErrors: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "sent_errors_total",
Help: "Total number of errors sending sample batches to the remote storage.",
ConstLabels: constLabels,
}),
queueLength: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "queue_length",
Help: "The number of processed samples queued to be sent to the remote storage.",
ConstLabels: constLabels,
}),
queueCapacity: prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "queue_capacity"),
"The capacity of the queue of samples to be sent to the remote storage.",
nil,
constLabels,
),
prometheus.GaugeValue,
float64(queueCapacity),
),
}
}
示例8: registerMetrics
func (p *Prometheus) registerMetrics(subsystem string) {
p.reqCnt = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: subsystem,
Name: "requests_total",
Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
},
[]string{"code", "method", "handler"},
)
prometheus.MustRegister(p.reqCnt)
p.reqDur = prometheus.NewSummary(
prometheus.SummaryOpts{
Subsystem: subsystem,
Name: "request_duration_seconds",
Help: "The HTTP request latencies in seconds.",
},
)
prometheus.MustRegister(p.reqDur)
p.reqSz = prometheus.NewSummary(
prometheus.SummaryOpts{
Subsystem: subsystem,
Name: "request_size_bytes",
Help: "The HTTP request sizes in bytes.",
},
)
prometheus.MustRegister(p.reqSz)
p.resSz = prometheus.NewSummary(
prometheus.SummaryOpts{
Subsystem: subsystem,
Name: "response_size_bytes",
Help: "The HTTP response sizes in bytes.",
},
)
prometheus.MustRegister(p.resSz)
}
示例9: New
// NewHandler constructs a new Handler.
func New(o *HandlerOptions) *Handler {
ctx, cancel := context.WithCancel(context.Background())
return &Handler{
queue: make(model.Alerts, 0, o.QueueCapacity),
ctx: ctx,
cancel: cancel,
more: make(chan struct{}, 1),
opts: o,
latency: prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "latency_seconds",
Help: "Latency quantiles for sending alert notifications (not including dropped notifications).",
}),
errors: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "errors_total",
Help: "Total number of errors sending alert notifications.",
}),
sent: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "sent_total",
Help: "Total number of alerts successfully sent.",
}),
dropped: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "dropped_total",
Help: "Total number of alerts dropped due to alert manager missing in configuration.",
}),
queueLength: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "queue_length",
Help: "The number of alert notifications in the queue.",
}),
queueCapacity: prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "queue_capacity"),
"The capacity of the alert notifications queue.",
nil, nil,
),
prometheus.GaugeValue,
float64(o.QueueCapacity),
),
}
}
示例10: newQueueMetrics
func newQueueMetrics(name string) queueMetrics {
var ret *defaultQueueMetrics
if len(name) == 0 {
return ret
}
ret = &defaultQueueMetrics{
depth: prometheus.NewGauge(prometheus.GaugeOpts{
Subsystem: name,
Name: "depth",
Help: "Current depth of workqueue: " + name,
}),
adds: prometheus.NewCounter(prometheus.CounterOpts{
Subsystem: name,
Name: "adds",
Help: "Total number of adds handled by workqueue: " + name,
}),
latency: prometheus.NewSummary(prometheus.SummaryOpts{
Subsystem: name,
Name: "queue_latency",
Help: "How long an item stays in workqueue" + name + " before being requested.",
}),
workDuration: prometheus.NewSummary(prometheus.SummaryOpts{
Subsystem: name,
Name: "work_duration",
Help: "How long processing an item from workqueue" + name + " takes.",
}),
addTimes: map[t]time.Time{},
processingStartTimes: map[t]time.Time{},
}
prometheus.Register(ret.depth)
prometheus.Register(ret.adds)
prometheus.Register(ret.latency)
prometheus.Register(ret.workDuration)
return ret
}
示例11: GetSummary
func (group *Group) GetSummary(name string, description string) prometheus.Summary {
summary := group.Summaries[name]
if summary == nil {
summary = prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: "mongodb",
Subsystem: group.Name,
Name: name,
Help: description,
})
group.Summaries[name] = summary
}
return summary
}
示例12: AddSample
func (p *PrometheusSink) AddSample(parts []string, val float32) {
p.mu.Lock()
defer p.mu.Unlock()
key := p.flattenKey(parts)
g, ok := p.summaries[key]
if !ok {
g = prometheus.NewSummary(prometheus.SummaryOpts{
Name: key,
Help: key,
MaxAge: 10 * time.Second,
})
prometheus.MustRegister(g)
p.summaries[key] = g
}
g.Observe(float64(val))
}
示例13: Get
func (c *SummaryContainer) Get(metricName string, labels prometheus.Labels) prometheus.Summary {
hash := hashNameAndLabels(metricName, labels)
summary, ok := c.Elements[hash]
if !ok {
summary = prometheus.NewSummary(
prometheus.SummaryOpts{
Name: metricName,
Help: defaultHelp,
ConstLabels: labels,
})
c.Elements[hash] = summary
if err := prometheus.Register(summary); err != nil {
log.Fatalf(regErrF, metricName, err)
}
}
return summary
}
示例14: NewNotificationHandler
// NewNotificationHandler constructs a new NotificationHandler.
func NewNotificationHandler(o *NotificationHandlerOptions) *NotificationHandler {
return &NotificationHandler{
alertmanagerURL: strings.TrimRight(o.AlertmanagerURL, "/"),
pendingNotifications: make(chan NotificationReqs, o.QueueCapacity),
httpClient: httputil.NewDeadlineClient(o.Deadline, nil),
notificationLatency: prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "latency_milliseconds",
Help: "Latency quantiles for sending alert notifications (not including dropped notifications).",
}),
notificationErrors: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "errors_total",
Help: "Total number of errors sending alert notifications.",
}),
notificationDropped: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "dropped_total",
Help: "Total number of alert notifications dropped due to alert manager missing in configuration.",
}),
notificationsQueueLength: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "queue_length",
Help: "The number of alert notifications in the queue.",
}),
notificationsQueueCapacity: prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "queue_capacity"),
"The capacity of the alert notifications queue.",
nil, nil,
),
prometheus.GaugeValue,
float64(o.QueueCapacity),
),
stopped: make(chan struct{}),
}
}
示例15: ExampleSummary
func ExampleSummary() {
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))
// Output:
// summary: <
// sample_count: 1000
// sample_sum: 29969.50000000001
// quantile: <
// quantile: 0.5
// value: 31.1
// >
// quantile: <
// quantile: 0.9
// value: 41.3
// >
// quantile: <
// quantile: 0.99
// value: 41.9
// >
// >
}