Skip to content

Commit

Permalink
[cmd] Add new make targets with ldflags to build lite version binaries (
Browse files Browse the repository at this point in the history
open-telemetry#34698)

**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->

We can use ldflags `-w` and `-s` to downscale the binary size. The
official docs explanation of the two options:
```
-s
	Omit the symbol table and debug information.
-w
	Omit the DWARF symbol table.
```

After building with the above options, the binary size scales down 28%:
```
-rwxr-xr-x  1 fraps  staff   249M Aug 15 14:23 otelcontribcol_darwin_arm64
-rwxr-xr-x  1 fraps  staff   347M Aug  8 18:20 otelcontribcol_darwin_arm64-old
```

I think it's worth adding them. The only degradation is we can't use GDB
to debug however it rare use case.

**Link to tracking Issue:** <Issue number if applicable>

**Testing:** <Describe what testing was performed and which tests were
added.>

**Documentation:** <Describe the documentation added.>

---------

Co-authored-by: Curtis Robert <[email protected]>
  • Loading branch information
2 people authored and jriguera committed Oct 4, 2024
1 parent 4504382 commit 2bd1ead
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ otelcontribcol:
cd ./cmd/otelcontribcol && GO111MODULE=on CGO_ENABLED=0 $(GOCMD) build -trimpath -o ../../bin/otelcontribcol_$(GOOS)_$(GOARCH)$(EXTENSION) \
-tags $(GO_BUILD_TAGS) .

# Build the Collector executable without the symbol table, debug information, and the DWARF symbol table.
.PHONY: otelcontribcollite
otelcontribcollite:
cd ./cmd/otelcontribcol && GO111MODULE=on CGO_ENABLED=0 $(GOCMD) build -trimpath -o ../../bin/otelcontribcol_$(GOOS)_$(GOARCH)$(EXTENSION) \
-tags $(GO_BUILD_TAGS) -ldflags $(GO_BUILD_LDFLAGS) .

.PHONY: genoteltestbedcol
genoteltestbedcol: $(BUILDER)
$(BUILDER) --skip-compilation --config cmd/oteltestbedcol/builder-config.yaml --output-path cmd/oteltestbedcol
Expand All @@ -349,12 +355,22 @@ oteltestbedcol:
cd ./cmd/oteltestbedcol && GO111MODULE=on CGO_ENABLED=0 $(GOCMD) build -trimpath -o ../../bin/oteltestbedcol_$(GOOS)_$(GOARCH)$(EXTENSION) \
-tags $(GO_BUILD_TAGS) .

.PHONY: oteltestbedcollite
oteltestbedcollite:
cd ./cmd/oteltestbedcol && GO111MODULE=on CGO_ENABLED=0 $(GOCMD) build -trimpath -o ../../bin/oteltestbedcol_$(GOOS)_$(GOARCH)$(EXTENSION) \
-tags $(GO_BUILD_TAGS) -ldflags $(GO_BUILD_LDFLAGS) .

# Build the telemetrygen executable.
.PHONY: telemetrygen
telemetrygen:
cd ./cmd/telemetrygen && GO111MODULE=on CGO_ENABLED=0 $(GOCMD) build -trimpath -o ../../bin/telemetrygen_$(GOOS)_$(GOARCH)$(EXTENSION) \
-tags $(GO_BUILD_TAGS) .

.PHONY: telemetrygenlite
telemetrygenlite:
cd ./cmd/telemetrygen && GO111MODULE=on CGO_ENABLED=0 $(GOCMD) build -trimpath -o ../../bin/telemetrygen_$(GOOS)_$(GOARCH)$(EXTENSION) \
-tags $(GO_BUILD_TAGS) -ldflags $(GO_BUILD_LDFLAGS) .

# helper function to update the core packages in builder-config.yaml
# input parameters are
# $(1) = path/to/versions.yaml (where it greps the relevant packages)
Expand Down
2 changes: 2 additions & 0 deletions Makefile.Common
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ SRC_PARENT_DIR := $(shell dirname $(SRC_ROOT))

# build tags required by any component should be defined as an independent variables and later added to GO_BUILD_TAGS below
GO_BUILD_TAGS=""
# These ldflags allow the build tool to omit the symbol table, debug information, and the DWARF symbol table to downscale binary size.
GO_BUILD_LDFLAGS="-s -w"
GOTEST_TIMEOUT?= 600s
GOTEST_OPT?= -race -timeout $(GOTEST_TIMEOUT) -parallel 4 --tags=$(GO_BUILD_TAGS)
GOTEST_INTEGRATION_OPT?= -race -timeout 360s -parallel 4
Expand Down

0 comments on commit 2bd1ead

Please sign in to comment.