Skip to content

Commit 86db4f8

Browse files
authored
fix(daemon): enable go vet check on pull requests (#1005)
Address issues spotted by `go vet`: - fix syntax of `kubernetes.KubernetesServiceHost` struct tags - ensure `context.cancel()` is always called to avoid context leak Add go vet check for pull requests.
1 parent 5b320e8 commit 86db4f8

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

.github/workflows/test-agent.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,39 @@ jobs:
4444
echo "$GOFMT_REPORTED_FILES" >> $GITHUB_STEP_SUMMARY
4545
exit 1
4646
fi
47+
govet:
48+
runs-on: ubuntu-latest
49+
continue-on-error: true
50+
steps:
51+
- name: Checkout newrelic-php-agent code
52+
uses: actions/checkout@v4
53+
with:
54+
path: php-agent
55+
- name: Get go version
56+
id: get-go-version
57+
run: |
58+
echo "go toolchain version required to build the daemon:"
59+
toolchain="$(awk '/^toolchain */ {gsub(/^go/, "", $2); print $2}' ./php-agent/daemon/go.mod)"
60+
echo "[${toolchain}]"
61+
echo "go-toolchain-version=${toolchain}" >> $GITHUB_OUTPUT
62+
- name: Setup go
63+
uses: actions/setup-go@v5
64+
with:
65+
go-version: ${{ steps.get-go-version.outputs.go-toolchain-version }}
66+
cache-dependency-path: "**/*.sum"
67+
- name: Verify go version
68+
run: |
69+
echo "Verify correct go toolchain version is used"
70+
actual="$(go version)"
71+
echo "Actual: [$actual]"
72+
expected="go version go${{ steps.get-go-version.outputs.go-toolchain-version }} linux/amd64"
73+
echo "Expected: [$expected]"
74+
if [ "$actual" != "$expected" ]; then
75+
exit 1
76+
fi
77+
- name: Run go vet
78+
run: go vet -C ./php-agent/daemon ./...
79+
shell: bash
4780
daemon-unit-tests:
4881
runs-on: ubuntu-latest
4982
env:

daemon/cmd/daemon/worker.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ func runWorker(cfg *Config) {
124124
hasProgenitor := !(cfg.Foreground || cfg.WatchdogForeground)
125125

126126
ctx, cancel := context.WithCancel(context.Background())
127+
defer cancel() // Ensure that the context is always cancelled when the worker exits, not only when signal is caught.
127128

128129
select {
129130
case <-listenAndServe(ctx, cfg.BindAddr, errorChan, p, hasProgenitor):
@@ -140,7 +141,8 @@ func runWorker(cfg *Config) {
140141
case caught := <-signalChan:
141142
// Close the listener before sending remaining data. This ensures that the socket
142143
// connection is closed as soon as possible and other processes can start listening
143-
// the socket while remaining data is sent.
144+
// the socket while remaining data is sent. Earlier defer cancel() will be a no-op
145+
// because cancel() is called here explicitly.
144146
cancel()
145147
log.Infof("worker received signal %d - sending remaining data", caught)
146148
p.CleanExit()

daemon/internal/newrelic/utilization/kubernetes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
type kubernetes struct {
14-
KubernetesServiceHost string `json:"kubernetes_service_host",omitempty`
14+
KubernetesServiceHost string `json:"kubernetes_service_host,omitempty"`
1515

1616
// Having a custom getter allows the unit tests to mock os.Getenv().
1717
environmentVariableGetter func(key string) string

0 commit comments

Comments
 (0)