Skip to content

Commit

Permalink
build smaller binaries, add version subcommand (#333)
Browse files Browse the repository at this point in the history
A PR came in to add -s -w to build ldflags, which will reduce binary sizes. Since the PR only updated /Dockerfile, these will only impact locally-built binaries.

This PR adds the flags to goreleaser and github actions builds to be consistent.

Along the way I noticed that I never exposed the version information in otel-cli, so I added that too.

Also removed a content-length check in http header testing because it's not reliable and beside the point of the test it was breaking.

Detailed commitlog follows:

* set ldflags in goreleaser builds

-s -w to build smaller binaries
-X options to fill in version, commit, and date

* add version subcommand

* set -s -w and some -X ldflags on build in GHA

Ensures testing happens with a binary closer to what goreleaser will
build.

* update CHANGELOG.md for recent changes

* go mod tidy

* fix test failure due to gzip encoding

With gzip encoding enabled it's difficult to compare content-length
header because side-effects can easily break it.

The test harness now automatically ignores content-length in HTTP
headers unless the test fixture specifically sets an expected result.
  • Loading branch information
tobert committed May 13, 2024
1 parent f335711 commit 0512be6
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Expand Up @@ -17,6 +17,7 @@ jobs:
# otel-cli's main test needs the binary built ahead of time
# also this validates it can acutally build before we get there
- name: Build
run: go build -v
# build with -s -w to reduce binary size and verify that build in test
run: go build -v -ldflags="-s -w -X main.version=test -X main.commit=${{ github.sha }}"
- name: Test
run: go test -v ./...
3 changes: 3 additions & 0 deletions .goreleaser.yml
Expand Up @@ -39,6 +39,9 @@ builds:
goarch: 386
- goos: freebsd
goarch: arm64
mod_timestamp: "{{ .CommitTimestamp }}"
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{ .CommitDate }}

nfpms:
- package_name: otel-cli
Expand Down
16 changes: 16 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,19 @@
## [0.4.6] - 2024-05-13

Build smaller binaries and add version subcommand.

### Added

- `otel-cli version` will now report version information of the binary (if any)

### Changed

- linker flags now contain `-s -w` to reduce binary size
- change made in build Dockerfile, goreleaser, and GH Actions
- contribution from @Ipmi-13, thank you!
- goreleaser now does the -X flags so `otel-cli version` will work right
- removed Content-Length from functional tests bc it's not fixed with gzip in play

## [0.4.5] - 2024-01-01

Fix exec attributes, cleanups, and dependency updates.
Expand Down
1 change: 0 additions & 1 deletion data_for_test.go
Expand Up @@ -1302,7 +1302,6 @@ var suites = []FixtureSuite{
"Content-Type": "application/x-protobuf",
"Accept-Encoding": "gzip",
"User-Agent": "Go-http-client/1.1",
"Content-Length": "232",
"X-Otel-Cli-Otlpserver-Token": "abcdefgabcdefg",
},
Diagnostics: otelcli.Diagnostics{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -5,7 +5,6 @@ go 1.21
toolchain go1.21.1

require (
github.com/golang/protobuf v1.5.3
github.com/google/go-cmp v0.6.0
github.com/pterm/pterm v0.12.79
github.com/spf13/cobra v1.8.0
Expand All @@ -24,6 +23,7 @@ require (
github.com/containerd/console v1.0.3 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions main_test.go
Expand Up @@ -345,6 +345,12 @@ func checkSpanData(t *testing.T, fixture Fixture, results Results) {

// checkHeaders compares the expected and received headers.
func checkHeaders(t *testing.T, fixture Fixture, results Results) {
// gzip encoding makes automatically comparing values tricky, so ignore it
// unless the test actually specifies a length
if _, ok := fixture.Expect.Headers["Content-Length"]; !ok {
delete(results.Headers, "Content-Length")
}

injectMapVars(fixture.Endpoint, fixture.Expect.Headers, fixture.TlsData)
injectMapVars(fixture.Endpoint, results.Headers, fixture.TlsData)

Expand Down
1 change: 1 addition & 0 deletions otelcli/root.go
Expand Up @@ -71,6 +71,7 @@ func createRootCmd(config *Config) *cobra.Command {
rootCmd.AddCommand(execCmd(config))
rootCmd.AddCommand(statusCmd(config))
rootCmd.AddCommand(serverCmd(config))
rootCmd.AddCommand(versionCmd(config))
rootCmd.AddCommand(completionCmd(config))

return rootCmd
Expand Down
25 changes: 24 additions & 1 deletion otelcli/version.go
@@ -1,6 +1,29 @@
package otelcli

import "strings"
import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
)

// versionCmd prints the version and exits.
func versionCmd(_ *Config) *cobra.Command {
cmd := cobra.Command{
Use: "version",
Short: "print otel-cli's version, commit, release date to stdout",
Run: doVersion,
}

return &cmd
}

func doVersion(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
config := getConfig(ctx)
fmt.Fprintln(os.Stdout, config.Version)
}

// FormatVersion pretty-prints the global version, commit, and date values into
// a string to enable the --version flag. Public to be called from main.
Expand Down

0 comments on commit 0512be6

Please sign in to comment.