Skip to content

Commit

Permalink
Add build version flag
Browse files Browse the repository at this point in the history
  • Loading branch information
armsnyder committed Aug 2, 2021
1 parent 8d88c1f commit 26c2f78
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
with:
context: .
push: false
build-args: BUILD_VERSION=${{ steps.meta.outputs.version }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

Expand All @@ -63,6 +64,7 @@ jobs:
with:
context: .
push: true
build-args: BUILD_VERSION=${{ steps.meta.outputs.version }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ COPY go.mod go.sum ./
RUN go mod download
COPY internal internal
COPY *.go .
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-w -s" -o /bin/a2s-exporter .
ARG BUILD_VERSION=development
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-w -s -X 'main.buildVersion=$BUILD_VERSION'" -o /bin/a2s-exporter .

FROM scratch
COPY --from=builder /bin/a2s-exporter /bin/a2s-exporter
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ Flag | Variable | Default | Help
--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.

#### Special

Flag | Help
--- | ---
-h | Show help.
--version | Show build version.

## Exported Metrics

Metrics names are prefixed with a namespace (default `a2s_`).
Expand All @@ -50,7 +57,7 @@ player_duration | Time (in seconds) player has been connected to the server. | s
player_score | Player's score (usually \"frags\" or \"kills\"). | server_name player_name player_index
player_the_ship_deaths | Player's deaths in a The Ship server. | server_name player_name player_index
player_the_ship_money | Player's money in a The Ship server. | server_name player_name player_index
player_up | Was the last player info query successful. |
player_up | Was the last player info query successful. |
server_bots | Number of bots on the server. | server_name
server_info | Non-numerical server info, including server_steam_id and version. The value is 1, and info is in the labels. | server_name map folder game server_type server_os version server_id keywords server_game_id server_steam_id the_ship_mode source_tv_name
server_max_players | Maximum number of players the server reports it can hold. | server_name
Expand All @@ -60,7 +67,7 @@ server_protocol | Protocol version used by the server. | server_name
server_source_tv_port | Spectator port number for SourceTV. | server_name
server_the_ship_duration | Time (in seconds) before a player is arrested while being witnessed in a The Ship server. | server_name
server_the_ship_witnesses | The number of witnesses necessary to have a player arrested in a The Ship server. | server_name
server_up | Was the last server info query successful. |
server_up | Was the last server info query successful. |
server_vac | Specifies whether the server uses VAC (0 for unsecured, 1 for secured). | server_name
server_visibility | Indicates whether the server requires a password (0 for public, 1 for private). | server_name

Expand Down
16 changes: 13 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
"github.com/armsnyder/a2s-exporter/internal/collector"
)

// buildVersion variable is set at build time.
var buildVersion = "development"

func main() {
// Flags.
address := flag.String("address", envOrDefault("A2S_EXPORTER_QUERY_ADDRESS", ""), "Address of the A2S query server as host:port (This is a separate port from the main server port).")
Expand All @@ -22,22 +25,27 @@ func main() {
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.")
help := flag.Bool("h", false, "Show help.")
version := flag.Bool("version", false, "Show build version.")

flag.Parse()

defer os.Exit(1)
// Show version.
if *version || flag.Arg(0) == "version" {
fmt.Println(buildVersion)
os.Exit(0)
}

// Show help.
if *help || flag.NArg() > 0 {
flag.Usage()
return
os.Exit(1)
}

// Check required arguments.
if *address == "" {
fmt.Println("address argument is required")
flag.Usage()
return
os.Exit(1)
}

// Set up prometheus metrics registry.
Expand All @@ -62,6 +70,8 @@ func main() {
// Run http server.
fmt.Printf("Serving metrics at http://127.0.0.1:%d%s\n", *port, *path)
fmt.Println(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))

os.Exit(1)
}

func envOrDefault(key, def string) string {
Expand Down

0 comments on commit 26c2f78

Please sign in to comment.