-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
83 lines (71 loc) · 2.86 KB
/
main.go
File metadata and controls
83 lines (71 loc) · 2.86 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package goprometheus
import (
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
// Prometheus struct represents a params needed to using all metrics
type Prometheus struct {
Method string
Path string
Code int
StartTime time.Time
}
// Prometheus Metric
var (
// DefaultLatencyBuckets are used for latency histograms when no custom bucket
// configuration is provided.
DefaultLatencyBuckets = []float64{0.1, 0.3, 0.5, 0.7, 0.9}
// appHttpRequest is a counter metric to record total number of application request
// with labels of method, path, and code
appHttpRequest = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "app_http_request_totals",
Help: "The total number of application request http",
}, []string{"method", "path", "code"})
// appHttpLatency is a histogram metric to record latency of application request
// using buckets in range of 0.1, 0.3, 0.5, 0.7, and 0.9
// with labels of method and path
appHttpLatency = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "app_http_request_latency_seconds",
Help: "Latency of HTTP requests.",
// Define the desired histogram buckets.
Buckets: DefaultLatencyBuckets,
},
[]string{"method", "path"},
)
)
// prometheusBackend persists metrics into the default in-memory Prometheus registry.
type prometheusBackend struct{}
func (prometheusBackend) RecordHttpRequest(method string, path string, code int) {
appHttpRequest.WithLabelValues(method, path, strconv.Itoa(code)).Inc()
}
func (prometheusBackend) RecordLatency(method string, path string, elapsedSeconds float64) {
appHttpLatency.WithLabelValues(method, path).Observe(elapsedSeconds)
}
// RecordHttpRequest records an HTTP request.
//
// This function takes the HTTP method, path, and response code of an HTTP request
// as input parameters. It increments the appHttpRequest metric with the provided
// method, path, and code labels.
func RecordHttpRequest(method string, path string, code int) {
currentBackend().RecordHttpRequest(method, path, code)
}
// RecordLatency records the latency of an HTTP request.
//
// This function takes the HTTP method, path, and the start time of the request
// as input parameters. It calculates the elapsed time since the start time
// and records the latency using the appHttpLatency metric.
func RecordLatency(method string, path string, start time.Time) {
elapsed := time.Since(start).Seconds()
currentBackend().RecordLatency(method, path, elapsed)
}
// RecordMetric is a function that records all metric
//
// It calls the RecordHttpRequest function with the HTTP method, path, and response code,
// and also calls the RecordLatency function with the HTTP method, path, and start time.
func (p *Prometheus) RecordMetric() {
RecordHttpRequest(p.Method, p.Path, p.Code)
RecordLatency(p.Method, p.Path, p.StartTime)
}