From 26c2f78657ae965a1070375df174e874f36cf13f Mon Sep 17 00:00:00 2001 From: Adam Snyder Date: Sun, 1 Aug 2021 22:24:19 -0700 Subject: [PATCH] Add build version flag --- .github/workflows/ci.yaml | 2 ++ Dockerfile | 3 ++- README.md | 11 +++++++++-- main.go | 16 +++++++++++++--- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 00483e3..99d032d 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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 }} @@ -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 }} diff --git a/Dockerfile b/Dockerfile index 968bf84..d3b07ff 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/README.md b/README.md index 3f2a297..d8dc4ec 100644 --- a/README.md +++ b/README.md @@ -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_`). @@ -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 @@ -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 diff --git a/main.go b/main.go index e924e07..3b2b47a 100644 --- a/main.go +++ b/main.go @@ -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).") @@ -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. @@ -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 {