File: request.go

package info (click to toggle)
golang-github-canonical-candid 1.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,016 kB
  • sloc: python: 1,903; sh: 235; makefile: 81
file content (40 lines) | stat: -rw-r--r-- 852 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package monitoring

import (
	"time"

	"github.com/prometheus/client_golang/prometheus"
	"gopkg.in/httprequest.v1"
)

var (
	requestDuration = prometheus.NewSummaryVec(prometheus.SummaryOpts{
		Namespace: "candid",
		Subsystem: "handler",
		Name:      "request_duration",
		Help:      "The duration of a web request.",
	}, []string{"path_pattern"})
)

func init() {
	prometheus.MustRegister(requestDuration)
}

type Request struct {
	startTime time.Time
	params    *httprequest.Params
}

func NewRequest(p *httprequest.Params) Request {
	return Request{
		startTime: time.Now(),
		params:    p,
	}
}

func (r Request) ObserveMetric() {
	requestDuration.WithLabelValues(r.params.PathPattern).Observe(float64(time.Since(r.startTime)) / float64(time.Second))
}