Skip to content

Commit 41cdcef

Browse files
committed
Add GET /telegraf for admin socket
1 parent e07aa5b commit 41cdcef

2 files changed

Lines changed: 64 additions & 5 deletions

File tree

pkg/server/server.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,36 @@ func (s *Server) snapshotConnections() []ConnectionInfo {
122122
return out
123123
}
124124

125+
func (s *Server) connectionCounts() (active, queued int) {
126+
for _, info := range s.snapshotConnections() {
127+
if info.QueuePos > 0 {
128+
queued++
129+
continue
130+
}
131+
active++
132+
}
133+
return active, queued
134+
}
135+
136+
func escapeInfluxTagValue(value string) string {
137+
replacer := strings.NewReplacer(",", `\,`, " ", `\ `, "=", `\=`)
138+
return replacer.Replace(value)
139+
}
140+
141+
func formatTelegrafLine(host string, active, queued int, timestamp time.Time) string {
142+
return fmt.Sprintf("git-queue,host=%s active=%di,queued=%di %d\n",
143+
escapeInfluxTagValue(host), active, queued, timestamp.UnixNano())
144+
}
145+
146+
func (s *Server) telegrafMetrics() string {
147+
host, err := os.Hostname()
148+
if err != nil || host == "" {
149+
host = "<unknown>"
150+
}
151+
active, queued := s.connectionCounts()
152+
return formatTelegrafLine(host, active, queued, time.Now())
153+
}
154+
125155
func remoteParts(attrs map[string]string, fallback net.Addr) (string, string) {
126156
host := attrs["REMOTE_ADDR"]
127157
port := attrs["REMOTE_PORT"]
@@ -287,16 +317,16 @@ func (s *Server) acceptLoop() {
287317

288318
func (s *Server) adminServeLoop() {
289319
mux := http.NewServeMux()
290-
mux.HandleFunc("/connections", func(w http.ResponseWriter, r *http.Request) {
291-
if r.Method != http.MethodGet {
292-
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
293-
return
294-
}
320+
mux.HandleFunc("GET /connections", func(w http.ResponseWriter, r *http.Request) {
295321
w.Header().Set("Content-Type", "application/json")
296322
if err := json.NewEncoder(w).Encode(s.snapshotConnections()); err != nil {
297323
log.Printf("Admin write: %v", err)
298324
}
299325
})
326+
mux.HandleFunc("GET /telegraf", func(w http.ResponseWriter, r *http.Request) {
327+
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
328+
_, _ = io.WriteString(w, s.telegrafMetrics())
329+
})
300330
if err := http.Serve(s.adminL, mux); err != nil && !errors.Is(err, net.ErrClosed) {
301331
log.Printf("Admin serve: %v", err)
302332
}

pkg/server/server_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package server
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestConnectionCounts(t *testing.T) {
12+
s := &Server{
13+
conns: map[uint64]ConnectionInfo{
14+
1: {Index: 1, QueuePos: 0},
15+
2: {Index: 2, QueuePos: 3},
16+
3: {Index: 3, QueuePos: 0},
17+
4: {Index: 4, QueuePos: 1},
18+
},
19+
}
20+
21+
active, queued := s.connectionCounts()
22+
require.Equal(t, 2, active)
23+
assert.Equal(t, 2, queued)
24+
}
25+
26+
func TestFormatTelegrafLine(t *testing.T) {
27+
line := formatTelegrafLine("host name,prod=1", 10, 15, time.Unix(0, 1778837100123456789))
28+
assert.Equal(t, "git-queue,host=host\\ name\\,prod\\=1 active=10i,queued=15i 1778837100123456789\n", line)
29+
}

0 commit comments

Comments
 (0)