Skip to content

Commit

Permalink
New metrics to count total bytes sent and received
Browse files Browse the repository at this point in the history
  • Loading branch information
dmachard committed Dec 10, 2021
1 parent a34e8d2 commit a3c169f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion loggers/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ func (s *Webserver) metricsHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "# TYPE %s_build_info gauge\n", suffix)
fmt.Fprintf(w, "%s_build_info{version=\"%s\"} 1\n", suffix, s.ver)

// bytes
fmt.Fprintf(w, "# HELP %s_received_bytes_total Total bytes received\n", suffix)
fmt.Fprintf(w, "# TYPE %s_received_bytes_total counter\n", suffix)
fmt.Fprintf(w, "# HELP %s_sent_bytes_total Total bytes sent\n", suffix)
fmt.Fprintf(w, "# TYPE %s_sent_bytes_total counter\n", suffix)

// docs
fmt.Fprintf(w, "# HELP %s_requesters_total Number of clients\n", suffix)
fmt.Fprintf(w, "# TYPE %s_requesters_total counter\n", suffix)
Expand Down Expand Up @@ -319,12 +325,16 @@ func (s *Webserver) metricsHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s_reply_len_max_total{stream=\"%s\"} %v\n", suffix, stream, counters.ReplyLengthMax)
fmt.Fprintf(w, "%s_reply_len_min_total{stream=\"%s\"} %v\n", suffix, stream, counters.ReplyLengthMin)

// add in v0.13.0
// malformed
fmt.Fprintf(w, "%s_packets_malformed_total{stream=\"%s\"} %d\n", suffix, stream, counters.PacketsMalformed)
fmt.Fprintf(w, "%s_clients_suspicious_total{stream=\"%s\"} %d\n", suffix, stream, totalSuspiciousClients)
for _, v := range topSuspiciousClients {
fmt.Fprintf(w, "%s_clients_suspicious_top_total{stream=\"%s\",ip=\"%s\"} %d\n", suffix, stream, v.Name, v.Hit)
}

// bytes
fmt.Fprintf(w, "%s_received_bytes_total{stream=\"%s\"} %d\n", suffix, stream, counters.ReceivedBytesTotal)
fmt.Fprintf(w, "%s_sent_bytes_total{stream=\"%s\"} %d\n", suffix, stream, counters.SentBytesTotal)
}
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
Expand Down
7 changes: 7 additions & 0 deletions subprocessors/statsperstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type Counters struct {
ReplyLength500_Inf int
ReplyLengthMax int
ReplyLengthMin int

ReceivedBytesTotal int
SentBytesTotal int
}

type StatsPerStream struct {
Expand Down Expand Up @@ -144,6 +147,8 @@ func (c *StatsPerStream) Record(dm dnsutils.DnsMessage) {

// packet size repartition
if dm.Type == "query" {
c.total.ReceivedBytesTotal += dm.Length

if c.total.QueryLengthMin == 0 {
c.total.QueryLengthMin = dm.Length
}
Expand All @@ -170,6 +175,8 @@ func (c *StatsPerStream) Record(dm dnsutils.DnsMessage) {
c.total.QueryLength500_Inf++
}
} else {
c.total.SentBytesTotal += dm.Length

if c.total.ReplyLengthMin == 0 {
c.total.ReplyLengthMin = dm.Length
}
Expand Down

0 comments on commit a3c169f

Please sign in to comment.