Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
j-bro committed Nov 2, 2023
1 parent c233ed5 commit 7372f33
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 48 deletions.
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

go-svrquery is a [Go](http://golang.org/) client for talking to game servers using various query protocols.

Features
Features
--------
* Support for various game server query protocol's including:
** SQP, TF2E
** SQP, TF2E, Prometheus

Installation
------------
Expand All @@ -22,7 +22,6 @@ package main

import (
"log"
"time"

"github.com/multiplay/go-svrquery/lib/svrquery"
)
Expand All @@ -42,6 +41,29 @@ func main() {
}
```

### Prometheus

To scape metrics exposed in Prometheus/OpenMetrics format, use the protocol `prom`, and the URL of the HTTP metrics
endpoint. For example:

```go
client, err := svrquery.NewClient("prom", "http://127.0.0.1:9100/metrics")
```

The library will read the following metrics:

```text
# HELP gameserver_current_players Number of players currently connected to the server.
# TYPE gameserver_current_players gauge
gameserver_current_players 1
# HELP gameserver_max_players Maximum number of players that can connect to the server.
# TYPE gameserver_max_players gauge
gameserver_max_players 2
# HELP gameserver_server_info Server status info.
# TYPE gameserver_server_info gauge
gameserver_server_info{game_type="Game Type",map_name="Map",port="1000",server_name="Name"} 1
```

CLI
-------------
A cli is available in github releases and also at https://github.com/multiplay/go-svrquery/tree/master/cmd/cli
Expand Down Expand Up @@ -71,7 +93,7 @@ This enables you make queries to servers using the specified protocol, and retur

This tool also provides the ability to start a very basic sample server using a given protocol.

Currently, only `sqp` is supported
Currently, only `sqp` and `prom` are supported

```
./go-svrquery -server :12121 -proto sqp
Expand Down
15 changes: 7 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@ module github.com/multiplay/go-svrquery
go 1.20

require (
github.com/prometheus/client_golang v1.15.1
github.com/prometheus/common v0.44.0
github.com/stretchr/testify v1.7.0
github.com/prometheus/client_golang v1.17.0
github.com/prometheus/common v0.45.0
github.com/stretchr/testify v1.8.4
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/stretchr/objx v0.3.0 // indirect
golang.org/x/sys v0.8.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/stretchr/objx v0.5.0 // indirect
golang.org/x/sys v0.13.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
37 changes: 17 additions & 20 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,37 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg=
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.15.1 h1:8tXpTmJbyH5lydzFPoxSIJ0J46jdh3tylbvM1xCv0LI=
github.com/prometheus/client_golang v1.15.1/go.mod h1:e9yaBhRPU2pPNsZwE+JdQl0KEt1N9XgF6zxWmaC0xOk=
github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q=
github.com/prometheus/client_golang v1.17.0/go.mod h1:VeL+gMmOAxkS2IqfCq0ZmHSL+LjWfWDUmp1mBz9JgUY=
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 h1:v7DLqVdK4VrYkVD5diGdl4sxJurKJEMnODWRJlxV9oM=
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU=
github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdOOfY=
github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY=
github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI=
github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY=
github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM=
github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY=
github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwaUuI=
github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
25 changes: 16 additions & 9 deletions lib/svrquery/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,21 @@ func NewClient(proto, addr string, options ...Option) (*Client, error) {
}
c.Queryer = f(c)

var t transport
switch proto {
case "prom":
t = &httpTransport{address: addr}
c.transport = newHTTPTransport(addr)
default:
// defaulting to udp
t = &udpTransport{address: addr}
c.transport = newUDPTransport(addr)
}
c.transport = t

for _, o := range options {
if err := o(c); err != nil {
return nil, err
}
}

if err := t.Setup(); err != nil {
if err := c.transport.Setup(); err != nil {
return nil, fmt.Errorf("setup client transport: %w", err)
}

Expand All @@ -110,21 +108,24 @@ type udpTransport struct {
udpAddress *net.UDPAddr
}

func newUDPTransport(address string) *udpTransport {
return &udpTransport{address: address}
}

// Address implements transport.Address.
func (u *udpTransport) Address() string {
return u.address
}

// Setup implements transport.Setup.
func (u *udpTransport) Setup() error {
udpNet := "udp"
udpAddr, err := net.ResolveUDPAddr(udpNet, u.address)
udpAddr, err := net.ResolveUDPAddr(DefaultNetwork, u.address)
if err != nil {
return err
}
u.udpAddress = udpAddr

if u.connection, err = net.DialUDP(udpNet, nil, u.udpAddress); err != nil {
if u.connection, err = net.DialUDP(DefaultNetwork, nil, u.udpAddress); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -180,8 +181,14 @@ type httpTransport struct {
httpClient *http.Client
}

func newHTTPTransport(address string) *httpTransport {
t := &httpTransport{address: address}
t.httpClient = &http.Client{}
return t
}

func (h *httpTransport) Setup() error {
h.httpClient = &http.Client{}
// no-op
return nil
}

Expand Down
8 changes: 8 additions & 0 deletions lib/svrquery/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func TestNewClient(t *testing.T) {
name: "tf2e",
protocol: "tf2e",
},
{
name: "prometheus",
protocol: "prom",
},
{
name: "invalid-protocol",
protocol: "my-protocol",
Expand All @@ -41,6 +45,10 @@ func TestNewClient(t *testing.T) {
}
}

func TestRead(t *testing.T) {
t.Fatal("unimplemented")
}

func TestQuery(t *testing.T) {
addr := os.Getenv("TEST_QUERY_ADDR")
if addr == "" {
Expand Down
2 changes: 1 addition & 1 deletion lib/svrquery/protocol/prom/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import (
)

func init() {
protocol.MustRegister("prom", newCreator)
protocol.MustRegister(protocol.Prometheus, newCreator)
}
11 changes: 11 additions & 0 deletions lib/svrquery/protocol/protocols.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package protocol

const (
SQP = "sqp"
TF2 = "tf2e"
TF2v7 = "tf2e-v7"
TF2v8 = "tf2e-v8"
TF2v9 = "tf2e-v9"
TF2v10 = "tf2e-v10"
Prometheus = "prom"
)
2 changes: 1 addition & 1 deletion lib/svrquery/protocol/sqp/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import (
)

func init() {
protocol.MustRegister("sqp", newCreator)
protocol.MustRegister(protocol.SQP, newCreator)
}
10 changes: 5 additions & 5 deletions lib/svrquery/protocol/titanfall/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (

func init() {
// TODO(steve): add support for tf2.
protocol.MustRegister("tf2e", newQueryer(3))
protocol.MustRegister("tf2e-v7", newQueryer(7))
protocol.MustRegister("tf2e-v8", newQueryer(8))
protocol.MustRegister("tf2e-v9", newQueryer(9))
protocol.MustRegister("tf2e-v10", newQueryer(10))
protocol.MustRegister(protocol.TF2, newQueryer(3))
protocol.MustRegister(protocol.TF2v7, newQueryer(7))
protocol.MustRegister(protocol.TF2v8, newQueryer(8))
protocol.MustRegister(protocol.TF2v9, newQueryer(9))
protocol.MustRegister(protocol.TF2v10, newQueryer(10))
}

0 comments on commit 7372f33

Please sign in to comment.