2016-05-24 15:04:28 +00:00
|
|
|
// package prometheuscollector allows to expose metrics for Prometheus.
|
|
|
|
//
|
|
|
|
// Using the provided collector, you can easily expose metrics for tusd in the
|
|
|
|
// Prometheus exposition format (https://prometheus.io/docs/instrumenting/exposition_formats/):
|
|
|
|
//
|
|
|
|
// handler, err := tusd.NewHandler(…)
|
|
|
|
// collector := prometheuscollector.New(handler.Metrics)
|
|
|
|
// prometheus.MustRegister(collector)
|
|
|
|
package prometheuscollector
|
|
|
|
|
|
|
|
import (
|
2017-02-28 19:39:25 +00:00
|
|
|
"strconv"
|
2016-05-24 15:04:28 +00:00
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"github.com/tus/tusd"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
requestsTotalDesc = prometheus.NewDesc(
|
|
|
|
"tusd_requests_total",
|
|
|
|
"Total number of requests served by tusd per method.",
|
|
|
|
[]string{"method"}, nil)
|
|
|
|
errorsTotalDesc = prometheus.NewDesc(
|
|
|
|
"tusd_errors_total",
|
2017-02-28 19:39:25 +00:00
|
|
|
"Total number of errors per status.",
|
|
|
|
[]string{"status", "message"}, nil)
|
2016-05-24 15:04:28 +00:00
|
|
|
bytesReceivedDesc = prometheus.NewDesc(
|
|
|
|
"tusd_bytes_received",
|
|
|
|
"Number of bytes received for uploads.",
|
|
|
|
nil, nil)
|
|
|
|
uploadsCreatedDesc = prometheus.NewDesc(
|
|
|
|
"tusd_uploads_created",
|
|
|
|
"Number of created uploads.",
|
|
|
|
nil, nil)
|
|
|
|
uploadsFinishedDesc = prometheus.NewDesc(
|
|
|
|
"tusd_uploads_finished",
|
|
|
|
"Number of finished uploads.",
|
|
|
|
nil, nil)
|
|
|
|
uploadsTerminatedDesc = prometheus.NewDesc(
|
|
|
|
"tusd_uploads_terminated",
|
2017-02-28 19:39:25 +00:00
|
|
|
"Number of terminated uploads.",
|
2016-05-24 15:04:28 +00:00
|
|
|
nil, nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
type Collector struct {
|
|
|
|
metrics tusd.Metrics
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new collector which read froms the provided Metrics struct.
|
|
|
|
func New(metrics tusd.Metrics) Collector {
|
|
|
|
return Collector{
|
|
|
|
metrics: metrics,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ Collector) Describe(descs chan<- *prometheus.Desc) {
|
|
|
|
descs <- requestsTotalDesc
|
|
|
|
descs <- errorsTotalDesc
|
|
|
|
descs <- bytesReceivedDesc
|
|
|
|
descs <- uploadsCreatedDesc
|
|
|
|
descs <- uploadsFinishedDesc
|
|
|
|
descs <- uploadsTerminatedDesc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Collector) Collect(metrics chan<- prometheus.Metric) {
|
|
|
|
for method, valuePtr := range c.metrics.RequestsTotal {
|
|
|
|
metrics <- prometheus.MustNewConstMetric(
|
|
|
|
requestsTotalDesc,
|
2017-03-30 12:51:02 +00:00
|
|
|
prometheus.CounterValue,
|
2016-05-24 15:04:28 +00:00
|
|
|
float64(atomic.LoadUint64(valuePtr)),
|
|
|
|
method,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-02-28 19:39:25 +00:00
|
|
|
for httpError, valuePtr := range c.metrics.ErrorsTotal.Load() {
|
2016-05-24 15:04:28 +00:00
|
|
|
metrics <- prometheus.MustNewConstMetric(
|
|
|
|
errorsTotalDesc,
|
2017-03-30 12:51:02 +00:00
|
|
|
prometheus.CounterValue,
|
2016-05-24 15:04:28 +00:00
|
|
|
float64(atomic.LoadUint64(valuePtr)),
|
2017-02-28 19:39:25 +00:00
|
|
|
strconv.Itoa(httpError.StatusCode()),
|
|
|
|
httpError.Error(),
|
2016-05-24 15:04:28 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics <- prometheus.MustNewConstMetric(
|
|
|
|
bytesReceivedDesc,
|
|
|
|
prometheus.CounterValue,
|
|
|
|
float64(atomic.LoadUint64(c.metrics.BytesReceived)),
|
|
|
|
)
|
|
|
|
|
|
|
|
metrics <- prometheus.MustNewConstMetric(
|
|
|
|
uploadsFinishedDesc,
|
|
|
|
prometheus.CounterValue,
|
|
|
|
float64(atomic.LoadUint64(c.metrics.UploadsFinished)),
|
|
|
|
)
|
|
|
|
|
|
|
|
metrics <- prometheus.MustNewConstMetric(
|
|
|
|
uploadsCreatedDesc,
|
|
|
|
prometheus.CounterValue,
|
|
|
|
float64(atomic.LoadUint64(c.metrics.UploadsCreated)),
|
|
|
|
)
|
|
|
|
|
|
|
|
metrics <- prometheus.MustNewConstMetric(
|
|
|
|
uploadsTerminatedDesc,
|
|
|
|
prometheus.CounterValue,
|
|
|
|
float64(atomic.LoadUint64(c.metrics.UploadsTerminated)),
|
|
|
|
)
|
|
|
|
}
|