-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_counters.go
100 lines (86 loc) · 3.21 KB
/
server_counters.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package msmtpd
import (
"fmt"
"io"
"net/http"
"sync/atomic"
)
type wrapper struct {
Original io.ReadWriteCloser
server *Server
}
func (w *wrapper) Read(p []byte) (n int, err error) {
n, err = w.Original.Read(p)
atomic.AddUint64(&w.server.bytesRead, uint64(n))
return
}
func (w *wrapper) Write(p []byte) (n int, err error) {
n, err = w.Original.Write(p)
atomic.AddUint64(&w.server.bytesWritten, uint64(n))
return
}
func (w *wrapper) Close() error {
return w.Original.Close()
}
func (srv *Server) wrapWithCounters(stream io.ReadWriteCloser) (wrapped io.ReadWriteCloser) {
return &wrapper{Original: stream, server: srv}
}
// GetBytesWritten returns number of bytes written
func (srv *Server) GetBytesWritten() uint64 {
return srv.bytesWritten
}
// GetBytesRead returns number of bytes written
func (srv *Server) GetBytesRead() uint64 {
return srv.bytesRead
}
// GetTransactionsCount returns number of all transactions this server processed
func (srv *Server) GetTransactionsCount() uint64 {
return srv.transactionsAll
}
// GetActiveTransactionsCount returns number of active transactions this server is processing
func (srv *Server) GetActiveTransactionsCount() int32 {
return srv.transactionsActive
}
// GetSuccessfulTransactionsCount returns number of successful transactions this server processed
func (srv *Server) GetSuccessfulTransactionsCount() uint64 {
return srv.transactionsSuccess
}
// GetFailedTransactionsCount returns number of failed transactions this server processed
func (srv *Server) GetFailedTransactionsCount() uint64 {
return srv.transactionsFail
}
// ResetCounters resets counters
func (srv *Server) ResetCounters() {
srv.bytesRead = 0
srv.bytesWritten = 0
srv.transactionsAll = 0
}
// StartPrometheusScrapperEndpoint starts prometheus scrapper endpoint with data
// in this format https://prometheus.io/docs/instrumenting/exposition_formats/
func (srv *Server) StartPrometheusScrapperEndpoint(address, path string) (err error) {
httpServ := http.Server{
Addr: address,
Handler: http.DefaultServeMux,
}
http.HandleFunc(path, func(res http.ResponseWriter, req *http.Request) {
res.Header().Add("Content-Type", "text/plain; version=0.0.4")
res.WriteHeader(http.StatusOK)
fmt.Fprintf(res, "bytes_read{hostname=\"%s\"} %v %v\n",
srv.Hostname, srv.GetBytesRead(), srv.lastTransactionStartedAt.UnixMilli())
fmt.Fprintf(res, "bytes_written{hostname=\"%s\"} %v %v\n",
srv.Hostname, srv.GetBytesWritten(), srv.lastTransactionStartedAt.UnixMilli())
fmt.Fprintf(res, "active_transactions_count{hostname=\"%s\"} %v %v\n",
srv.Hostname, srv.GetActiveTransactionsCount(), srv.lastTransactionStartedAt.UnixMilli())
fmt.Fprintf(res, "all_transactions_count{hostname=\"%s\"} %v %v\n",
srv.Hostname, srv.GetTransactionsCount(), srv.lastTransactionStartedAt.UnixMilli())
fmt.Fprintf(res, "successfull_transactions_count{hostname=\"%s\"} %v %v\n",
srv.Hostname, srv.GetSuccessfulTransactionsCount(), srv.lastTransactionStartedAt.UnixMilli())
fmt.Fprintf(res, "failed_transactions_count{hostname=\"%s\"} %v %v\n",
srv.Hostname, srv.GetFailedTransactionsCount(), srv.lastTransactionStartedAt.UnixMilli())
})
go func() {
<-srv.Context.Done()
httpServ.Close()
}()
return httpServ.ListenAndServe()
}