Skip to content

Commit

Permalink
add prometheus exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
egoroof committed Apr 18, 2024
1 parent c49e6d3 commit 55ff80e
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 2 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ jobs:
- uses: actions/setup-go@v5
with:
go-version: "1.22"
- run: go build wowPing.go
- run: ./wowPing -n=2
- run: go build cmd/wow-ping/wow-ping.go
- run: go build cmd/wow-ping-service/wow-ping-service.go
- run: ./wow-ping -n=2
98 changes: 98 additions & 0 deletions cmd/wow-ping-service/wow-ping-service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"context"
"errors"
"fmt"
"log"
"net/http"
"os"
"time"

"github.com/egoroof/wow-server-ping-go/pkg/ping"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

const port = 8090
const sleepBetweenRequestsMs = 500

var promRespTime = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "wow_server_response_time_ms",
Help: "WoW server response time in ms",
}, []string{"server"})

var promRespTimeout = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "wow_server_timeout_count",
Help: "WoW server timeout count",
}, []string{"server"})

var promRespErr = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "wow_server_error_count",
Help: "WoW server error count",
}, []string{"server"})

func recordMetrics() {
params := ping.ParseArguments(os.Args[1:])
responseChan := make(chan ping.ServerResponse)

for _, group := range ping.Servers {
if group.Name != params.ServerGroup {
continue
}

for _, server := range group.List {
promRespTimeout.WithLabelValues(server.Name).Add(0)
promRespErr.WithLabelValues(server.Name).Add(0)
}
}

for {
connectionCount := 0

for _, group := range ping.Servers {
if group.Name != params.ServerGroup {
continue
}

for _, server := range group.List {
connectionCount++
go ping.OpenConnection(server.Name, server.Host, server.Port, params.Timeout, responseChan)
}
}

for i := 0; i < connectionCount; i++ {
response := <-responseChan

if response.Error == nil {
promRespTime.WithLabelValues(response.Name).Set(float64(response.Duration))
} else {
promRespTime.DeleteLabelValues(response.Name)
if errors.Is(response.Error, context.DeadlineExceeded) || errors.Is(response.Error, os.ErrDeadlineExceeded) {
promRespTimeout.WithLabelValues(response.Name).Inc()
} else {
log.Printf("%v %v\n", response.Name, response.Error)
promRespErr.WithLabelValues(response.Name).Inc()
}
}
}

time.Sleep(time.Millisecond * sleepBetweenRequestsMs)
}
}

func main() {
promReg := prometheus.NewRegistry()
promReg.MustRegister(promRespTime)
promReg.MustRegister(promRespTimeout)
promReg.MustRegister(promRespErr)

handler := promhttp.HandlerFor(promReg, promhttp.HandlerOpts{})
http.Handle("/metrics", handler)

go recordMetrics()

log.Printf("Listening port %v\n", port)
err := http.ListenAndServe(fmt.Sprintf(":%v", port), nil)
log.Fatal(err)
}
File renamed without changes.
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
module github.com/egoroof/wow-server-ping-go

go 1.22.1

require github.com/prometheus/client_golang v1.19.0

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
golang.org/x/sys v0.16.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
)
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU=
github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k=
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE=
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=

0 comments on commit 55ff80e

Please sign in to comment.