Skip to content

Commit

Permalink
New argument --max-packet-size
Browse files Browse the repository at this point in the history
  • Loading branch information
armsnyder committed Dec 10, 2022
1 parent b644781 commit 86b2b0f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ fix:
golangci-lint run --fix

build:
go mod tidy
go build ./...

test:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Flag | Variable | Default | Help
--path | A2S_EXPORTER_PATH | /metrics | Path for the metrics exporter.
--namespace | A2S_EXPORTER_NAMESPACE | a2s | Namespace prefix for all exported a2s metrics.
--a2s-only-metrics | A2S_EXPORTER_A2S_ONLY_METRICS | false | If true, excludes Go runtime and promhttp metrics.
--max-packet-size | A2S_EXPORTER_MAX_PACKET_SIZE | 0 | Advanced option to set a non-standard max packet size of the A2S query server. Set to 0 to use the default max packet size.

#### Special

Expand Down
20 changes: 12 additions & 8 deletions internal/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
)

type Collector struct {
addr string
client *a2s.Client
descs map[string]*prometheus.Desc
addr string
clientOptions []func(*a2s.Client) error
client *a2s.Client
descs map[string]*prometheus.Desc
}

type adder func(name string, value float64, labelValues ...string)

func New(namespace, addr string) *Collector {
func New(namespace, addr string, clientOptions ...func(*a2s.Client) error) *Collector {
descs := make(map[string]*prometheus.Desc)

fullDesc := func(name, help string, labels []string) {
Expand Down Expand Up @@ -53,8 +54,9 @@ func New(namespace, addr string) *Collector {
playerDesc("player_the_ship_money", "Player's money in a The Ship server.")

return &Collector{
addr: addr,
descs: descs,
addr: addr,
clientOptions: clientOptions,
descs: descs,
}
}

Expand Down Expand Up @@ -97,7 +99,7 @@ func (c *Collector) queryInfo() (serverInfo *a2s.ServerInfo, playerInfo *a2s.Pla

// Lazy initialization of UDP client.
if c.client == nil {
c.client, err = a2s.NewClient(c.addr)
c.client, err = a2s.NewClient(c.addr, c.clientOptions...)
if err != nil {
fmt.Println("Could not create A2S client:", err)
return
Expand All @@ -115,7 +117,9 @@ func (c *Collector) queryInfo() (serverInfo *a2s.ServerInfo, playerInfo *a2s.Pla
// constructed with The Ship App ID.
playerClient := c.client
if a2s.AppID(serverInfo.ID) == a2s.App_TheShip {
playerClient, err = a2s.NewClient(c.addr, a2s.SetAppID(int32(serverInfo.ID)))
options := []func(*a2s.Client) error{a2s.SetAppID(int32(serverInfo.ID))}
options = append(options, c.clientOptions...)
playerClient, err = a2s.NewClient(c.addr, options...)
if err != nil {
fmt.Println("Could not create A2S client for The Ship player query:", err)
return
Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rumblefrog/go-a2s"

"github.com/armsnyder/a2s-exporter/internal/collector"
)
Expand All @@ -24,6 +25,7 @@ func main() {
path := flag.String("path", envOrDefault("A2S_EXPORTER_PATH", "/metrics"), "Path for the metrics exporter.")
namespace := flag.String("namespace", envOrDefault("A2S_EXPORTER_NAMESPACE", "a2s"), "Namespace prefix for all exported a2s metrics.")
a2sOnlyMetrics := flag.Bool("a2s-only-metrics", envOrDefaultBool("A2S_EXPORTER_A2S_ONLY_METRICS", false), "If true, skips exporting Go runtime metrics.")
maxPacketSize := flag.Int("max-packet-size", envOrDefaultInt("A2S_EXPORTER_MAX_PACKET_SIZE", 0), "Advanced option to set a non-standard max packet size of the A2S query server. Set to 0 to use the default max packet size.")
help := flag.Bool("h", false, "Show help.")
version := flag.Bool("version", false, "Show build version.")

Expand Down Expand Up @@ -57,7 +59,11 @@ func main() {
}

// Register A2S metrics.
registry.MustRegister(collector.New(*namespace, *address))
var clientOptions []func(*a2s.Client) error
if *maxPacketSize > 0 {
clientOptions = append(clientOptions, a2s.SetMaxPacketSize(uint32(*maxPacketSize)))
}
registry.MustRegister(collector.New(*namespace, *address, clientOptions...))

// Set up http handler.
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
Expand Down

0 comments on commit 86b2b0f

Please sign in to comment.