@@ -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+
125155func 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
288318func (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 }
0 commit comments