Skip to content

Commit

Permalink
changes according to the reviews
Browse files Browse the repository at this point in the history
Signed-off-by: tifayuki <tifayuki@gmail.com>
  • Loading branch information
tifayuki committed Feb 2, 2018
1 parent 5ef4ea1 commit be4fb1c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
10 changes: 5 additions & 5 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// HttpHandlerOpts describes a set of configurable options of http metrics
type HttpHandlerOpts struct {
// HTTPHandlerOpts describes a set of configurable options of http metrics
type HTTPHandlerOpts struct {
DurationBuckets []float64
RequestSizeBuckets []float64
ResponseSizeBuckets []float64
Expand All @@ -22,7 +22,7 @@ const (
InstrumentHandlerInFlight
)

type HttpMetric struct {
type HTTPMetric struct {
prometheus.Collector
handlerType int
}
Expand All @@ -39,11 +39,11 @@ func Handler() http.Handler {
return promhttp.Handler()
}

func InstrumentHandler(metrics []*HttpMetric, handler http.Handler) http.HandlerFunc {
func InstrumentHandler(metrics []*HTTPMetric, handler http.Handler) http.HandlerFunc {
return InstrumentHandlerFunc(metrics, handler.ServeHTTP)
}

func InstrumentHandlerFunc(metrics []*HttpMetric, handlerFunc http.HandlerFunc) http.HandlerFunc {
func InstrumentHandlerFunc(metrics []*HTTPMetric, handlerFunc http.HandlerFunc) http.HandlerFunc {
var handler http.Handler
handler = http.HandlerFunc(handlerFunc)
for _, metric := range metrics {
Expand Down
66 changes: 36 additions & 30 deletions namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,115 +180,121 @@ func makeName(name string, unit Unit) string {
return fmt.Sprintf("%s_%s", name, unit)
}

func (n *Namespace) NewDefaultHttpMetrics() []*HttpMetric {
return n.NewHttpMetricsWithOpts(HttpHandlerOpts{
func (n *Namespace) NewDefaultHttpMetrics(handlerName string) []*HTTPMetric {
return n.NewHttpMetricsWithOpts(handlerName, HTTPHandlerOpts{
DurationBuckets: defaultDurationBuckets,
RequestSizeBuckets: defaultResponseSizeBuckets,
ResponseSizeBuckets: defaultResponseSizeBuckets,
})
}

func (n *Namespace) NewHttpMetrics(durationBuckets, requestSizeBuckets, responseSizeBuckets []float64) []*HttpMetric {
return n.NewHttpMetricsWithOpts(HttpHandlerOpts{
func (n *Namespace) NewHttpMetrics(handlerName string, durationBuckets, requestSizeBuckets, responseSizeBuckets []float64) []*HTTPMetric {
return n.NewHttpMetricsWithOpts(handlerName, HTTPHandlerOpts{
DurationBuckets: durationBuckets,
RequestSizeBuckets: requestSizeBuckets,
ResponseSizeBuckets: responseSizeBuckets,
})
}

func (n *Namespace) NewHttpMetricsWithOpts(opts HttpHandlerOpts) []*HttpMetric {
if _, ok := n.labels["handler"]; !ok {
panic("label \"handler\" must be provided in the namespace")
}

var httpMetrics []*HttpMetric
inFlightMetric := n.NewInFlightGaugeMetric()
requestTotalMetric := n.NewRequestTotalMetric()
requestDurationMetric := n.NewRequestDurationMetric(opts.DurationBuckets)
requestSizeMetric := n.NewRequestSizeMetric(opts.RequestSizeBuckets)
responseSizeMetric := n.NewResponseSizeMetric(opts.ResponseSizeBuckets)
func (n *Namespace) NewHttpMetricsWithOpts(handlerName string, opts HTTPHandlerOpts) []*HTTPMetric {
var httpMetrics []*HTTPMetric
inFlightMetric := n.NewInFlightGaugeMetric(handlerName)
requestTotalMetric := n.NewRequestTotalMetric(handlerName)
requestDurationMetric := n.NewRequestDurationMetric(handlerName, opts.DurationBuckets)
requestSizeMetric := n.NewRequestSizeMetric(handlerName, opts.RequestSizeBuckets)
responseSizeMetric := n.NewResponseSizeMetric(handlerName, opts.ResponseSizeBuckets)
httpMetrics = append(httpMetrics, inFlightMetric, requestDurationMetric, requestTotalMetric, requestSizeMetric, responseSizeMetric)
return httpMetrics
}

func (n *Namespace) NewInFlightGaugeMetric() *HttpMetric {
func (n *Namespace) NewInFlightGaugeMetric(handlerName string) *HTTPMetric {
labels := prometheus.Labels(n.labels)
labels["handler"] = handlerName
metric := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: n.name,
Subsystem: n.subsystem,
Name: "in_flight_requests",
Help: "The in-flight HTTP requests",
ConstLabels: prometheus.Labels(n.labels),
ConstLabels: prometheus.Labels(labels),
})
httpMetric := &HttpMetric{metric, InstrumentHandlerInFlight}
httpMetric := &HTTPMetric{metric, InstrumentHandlerInFlight}
n.Add(httpMetric)
return httpMetric
}

func (n *Namespace) NewRequestTotalMetric() *HttpMetric {
func (n *Namespace) NewRequestTotalMetric(handlerName string) *HTTPMetric {
labels := prometheus.Labels(n.labels)
labels["handler"] = handlerName
metric := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: n.name,
Subsystem: n.subsystem,
Name: "requests_total",
Help: "Total number of HTTP requests made.",
ConstLabels: prometheus.Labels(n.labels),
ConstLabels: prometheus.Labels(labels),
},
[]string{"code", "method"},
)
httpMetric := &HttpMetric{metric, InstrumentHandlerCounter}
httpMetric := &HTTPMetric{metric, InstrumentHandlerCounter}
n.Add(httpMetric)
return httpMetric
}
func (n *Namespace) NewRequestDurationMetric(buckets []float64) *HttpMetric {
func (n *Namespace) NewRequestDurationMetric(handlerName string, buckets []float64) *HTTPMetric {
if len(buckets) == 0 {
panic("DurationBuckets must be provided")
}
labels := prometheus.Labels(n.labels)
labels["handler"] = handlerName
opts := prometheus.HistogramOpts{
Namespace: n.name,
Subsystem: n.subsystem,
Name: "request_duration_seconds",
Help: "The HTTP request latencies in seconds.",
Buckets: buckets,
ConstLabels: prometheus.Labels(n.labels),
ConstLabels: prometheus.Labels(labels),
}
metric := prometheus.NewHistogramVec(opts, []string{"method"})
httpMetric := &HttpMetric{metric, InstrumentHandlerDuration}
httpMetric := &HTTPMetric{metric, InstrumentHandlerDuration}
n.Add(httpMetric)
return httpMetric
}

func (n *Namespace) NewRequestSizeMetric(buckets []float64) *HttpMetric {
func (n *Namespace) NewRequestSizeMetric(handlerName string, buckets []float64) *HTTPMetric {
if len(buckets) == 0 {
panic("RequestSizeBuckets must be provided")
}
labels := prometheus.Labels(n.labels)
labels["handler"] = handlerName
opts := prometheus.HistogramOpts{
Namespace: n.name,
Subsystem: n.subsystem,
Name: "request_size_bytes",
Help: "The HTTP request sizes in bytes.",
Buckets: buckets,
ConstLabels: prometheus.Labels(n.labels),
ConstLabels: prometheus.Labels(labels),
}
metric := prometheus.NewHistogramVec(opts, []string{})
httpMetric := &HttpMetric{metric, InstrumentHandlerRequestSize}
httpMetric := &HTTPMetric{metric, InstrumentHandlerRequestSize}
n.Add(httpMetric)
return httpMetric
}

func (n *Namespace) NewResponseSizeMetric(buckets []float64) *HttpMetric {
func (n *Namespace) NewResponseSizeMetric(handlerName string, buckets []float64) *HTTPMetric {
if len(buckets) == 0 {
panic("ResponseSizeBuckets must be provided")
}
labels := prometheus.Labels(n.labels)
labels["handler"] = handlerName
opts := prometheus.HistogramOpts{
Namespace: n.name,
Subsystem: n.subsystem,
Name: "response_size_bytes",
Help: "The HTTP response sizes in bytes.",
Buckets: buckets,
ConstLabels: prometheus.Labels(n.labels),
ConstLabels: prometheus.Labels(labels),
}
metrics := prometheus.NewHistogramVec(opts, []string{})
httpMetric := &HttpMetric{metrics, InstrumentHandlerResponseSize}
httpMetric := &HTTPMetric{metrics, InstrumentHandlerResponseSize}
n.Add(httpMetric)
return httpMetric
}

0 comments on commit be4fb1c

Please sign in to comment.