Skip to content

Update golangci-lint version to v2.4.0 #1217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ jobs:
with:
go-version-file: 'go.mod'
cache: false
- name: run install-tools
run: make install-tools
- name: run lint
run: make lint
- name: Lint Go
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0
with:
version: v2.4.0

unit-test:
name: Unit Tests
Expand Down
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ linters:
- G110
- G111
- G112
- G113
- G114
- G201
- G202
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ build: ## Build agent executable

lint: ## Run linter
@$(GOVET) ./...
@$(GORUN) $(GOLANGCILINT) config verify -c ./.golangci.yml
@$(GORUN) $(GOLANGCILINT) run -c ./.golangci.yml
@cd api/grpc && $(GORUN) $(BUF) generate
@echo "🏯 Linting Done"
Expand Down
2 changes: 1 addition & 1 deletion Makefile.tools
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
OAPICODEGEN = github.com/deepmap/oapi-codegen/v2/cmd/[email protected]
LEFTHOOK = github.com/evilmartians/[email protected]
GOLANGCILINT = github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.2.1
GOLANGCILINT = github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.4.0
PROTOCGENGO = google.golang.org/protobuf/cmd/[email protected]
GOFUMPT = mvdan.cc/[email protected]
COUNTERFEITER = github.com/maxbrunsfeld/counterfeiter/[email protected]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package cgroup

import (
"bytes"
"context"
"errors"
"os/exec"
"path"
Expand Down Expand Up @@ -63,8 +64,8 @@ func NewCPUSource(basePath string) *CPUSource {
}
}

func (cs *CPUSource) Collect() (ContainerCPUStats, error) {
cpuStats, err := cs.collectCPUStats()
func (cs *CPUSource) Collect(ctx context.Context) (ContainerCPUStats, error) {
cpuStats, err := cs.collectCPUStats(ctx)
if err != nil {
return ContainerCPUStats{}, err
}
Expand All @@ -73,8 +74,8 @@ func (cs *CPUSource) Collect() (ContainerCPUStats, error) {
}

// nolint: mnd
func (cs *CPUSource) collectCPUStats() (ContainerCPUStats, error) {
clockTicks, err := clockTicks()
func (cs *CPUSource) collectCPUStats(ctx context.Context) (ContainerCPUStats, error) {
clockTicks, err := clockTicks(ctx)
if err != nil {
return ContainerCPUStats{}, err
}
Expand Down Expand Up @@ -191,8 +192,8 @@ func systemCPUUsage(clockTicks int) (float64, error) {
return 0, errors.New("unable to process " + CPUStatsPath + ". No cpu found")
}

func clockTicks() (int, error) {
cmd := exec.Command("getconf", "CLK_TCK")
func clockTicks(ctx context.Context) (int, error) {
cmd := exec.CommandContext(ctx, "getconf", "CLK_TCK")
out := new(bytes.Buffer)
cmd.Stdout = out

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package cgroup

import (
"context"
"os"
"path"
"runtime"
Expand All @@ -19,6 +20,8 @@ func TestCollectCPUStats(t *testing.T) {
_, filename, _, _ := runtime.Caller(0)
localDirectory := path.Dir(filename)

ctx := context.Background()

tests := []struct {
errorType error
name string
Expand Down Expand Up @@ -73,7 +76,7 @@ func TestCollectCPUStats(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
cgroupCPUSource := NewCPUSource(test.basePath)
cpuStat, err := cgroupCPUSource.collectCPUStats()
cpuStat, err := cgroupCPUSource.collectCPUStats(ctx)

// Assert error
assert.IsType(tt, test.errorType, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ func (s *CPUScraper) Start(_ context.Context, _ component.Host) error {
return nil
}

func (s *CPUScraper) Scrape(context.Context) (pmetric.Metrics, error) {
func (s *CPUScraper) Scrape(ctx context.Context) (pmetric.Metrics, error) {
s.settings.Logger.Debug("Scraping container CPU metrics")

now := pcommon.NewTimestampFromTime(time.Now())

stats, err := s.cpuSource.Collect()
stats, err := s.cpuSource.Collect(ctx)
if err != nil {
return pmetric.NewMetrics(), err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ func (s *NginxStubStatusScraper) Start(_ context.Context, _ component.Host) erro

if strings.HasPrefix(s.cfg.APIDetails.Listen, "unix:") {
httpClient.Transport = &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", strings.TrimPrefix(s.cfg.APIDetails.Listen, "unix:"))
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
dialer := &net.Dialer{}
return dialer.DialContext(ctx, "unix", strings.TrimPrefix(s.cfg.APIDetails.Listen, "unix:"))
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Reading: 6 Writing: 179 Waiting: 106

func TestStubStatusScraperUnixSocket(t *testing.T) {
// Create a test server with a Unix domain socket
ctx := context.Background()
handler := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/status" {
rw.WriteHeader(http.StatusOK)
Expand All @@ -115,7 +116,8 @@ Reading: 6 Writing: 179 Waiting: 106
os.Remove(socketPath)

// Create a listener for the Unix socket
listener, err := net.Listen("unix", socketPath)
listenConfig := &net.ListenConfig{}
listener, err := listenConfig.Listen(ctx, "unix", socketPath)
require.NoError(t, err, "Failed to create Unix socket listener")

// Create a test server with our custom listener
Expand Down
9 changes: 5 additions & 4 deletions internal/collector/nginxplusreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (nps *NginxPlusScraper) ID() component.ID {
return component.NewID(metadata.Type)
}

func (nps *NginxPlusScraper) Start(_ context.Context, _ component.Host) error {
func (nps *NginxPlusScraper) Start(ctx context.Context, _ component.Host) error {
endpoint := strings.TrimPrefix(nps.cfg.APIDetails.URL, "unix:")
httpClient := http.DefaultClient
caCertLocation := nps.cfg.APIDetails.Ca
Expand Down Expand Up @@ -110,7 +110,7 @@ func (nps *NginxPlusScraper) Start(_ context.Context, _ component.Host) error {
httpClient.Timeout = nps.cfg.ClientConfig.Timeout

if strings.HasPrefix(nps.cfg.APIDetails.Listen, "unix:") {
httpClient = socketClient(strings.TrimPrefix(nps.cfg.APIDetails.Listen, "unix:"))
httpClient = socketClient(ctx, strings.TrimPrefix(nps.cfg.APIDetails.Listen, "unix:"))
}

plusClient, err := plusapi.NewNginxClient(endpoint,
Expand Down Expand Up @@ -1208,11 +1208,12 @@ func (nps *NginxPlusScraper) recordCacheMetrics(stats *plusapi.Stats, now pcommo
}
}

func socketClient(socketPath string) *http.Client {
func socketClient(ctx context.Context, socketPath string) *http.Client {
return &http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", socketPath)
dialer := &net.Dialer{}
return dialer.DialContext(ctx, "unix", socketPath)
},
},
}
Expand Down
19 changes: 11 additions & 8 deletions internal/collector/otel_collector_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ func (oc *Collector) checkForNewReceivers(ctx context.Context, nginxConfigContex
}

if oc.config.IsFeatureEnabled(pkgConfig.FeatureLogsNap) {
tcplogReceiversFound := oc.updateNginxAppProtectTcplogReceivers(nginxConfigContext)
tcplogReceiversFound := oc.updateNginxAppProtectTcplogReceivers(ctx, nginxConfigContext)
if tcplogReceiversFound {
reloadCollector = true
}
Expand Down Expand Up @@ -546,14 +546,16 @@ func (oc *Collector) updateExistingNginxOSSReceiver(
return nginxReceiverFound, reloadCollector
}

func (oc *Collector) updateNginxAppProtectTcplogReceivers(nginxConfigContext *model.NginxConfigContext) bool {
func (oc *Collector) updateNginxAppProtectTcplogReceivers(
ctx context.Context, nginxConfigContext *model.NginxConfigContext,
) bool {
newTcplogReceiverAdded := false

if oc.config.Collector.Receivers.TcplogReceivers == nil {
oc.config.Collector.Receivers.TcplogReceivers = make(map[string]*config.TcplogReceiver)
}

napSysLogServer := oc.findAvailableSyslogServers(nginxConfigContext.NAPSysLogServers)
napSysLogServer := oc.findAvailableSyslogServers(ctx, nginxConfigContext.NAPSysLogServers)

if napSysLogServer != "" {
if !oc.doesTcplogReceiverAlreadyExist(napSysLogServer) {
Expand Down Expand Up @@ -680,7 +682,7 @@ func (oc *Collector) updateResourceAttributes(
return actionUpdated
}

func (oc *Collector) findAvailableSyslogServers(napSyslogServers []string) string {
func (oc *Collector) findAvailableSyslogServers(ctx context.Context, napSyslogServers []string) string {
napSyslogServersMap := make(map[string]bool)
for _, server := range napSyslogServers {
napSyslogServersMap[server] = true
Expand All @@ -693,19 +695,20 @@ func (oc *Collector) findAvailableSyslogServers(napSyslogServers []string) strin
}

for _, napSyslogServer := range napSyslogServers {
ln, err := net.Listen("tcp", napSyslogServer)
listenConfig := &net.ListenConfig{}
ln, err := listenConfig.Listen(ctx, "tcp", napSyslogServer)
if err != nil {
slog.Debug("NAP syslog server is not reachable", "address", napSyslogServer,
slog.DebugContext(ctx, "NAP syslog server is not reachable", "address", napSyslogServer,
"error", err)

continue
}
closeError := ln.Close()
if closeError != nil {
slog.Debug("Failed to close syslog server", "address", napSyslogServer, "error", closeError)
slog.DebugContext(ctx, "Failed to close syslog server", "address", napSyslogServer, "error", closeError)
}

slog.Debug("Found valid NAP syslog server", "address", napSyslogServer)
slog.DebugContext(ctx, "Found valid NAP syslog server", "address", napSyslogServer)

return napSyslogServer
}
Expand Down
15 changes: 9 additions & 6 deletions internal/collector/otel_collector_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ func TestCollector_updateResourceAttributes(t *testing.T) {
}

func TestCollector_updateNginxAppProtectTcplogReceivers(t *testing.T) {
ctx := context.Background()
conf := types.OTelConfig(t)
conf.Collector.Log.Path = ""
conf.Collector.Processors.Batch = nil
Expand All @@ -745,7 +746,7 @@ func TestCollector_updateNginxAppProtectTcplogReceivers(t *testing.T) {
assert.Empty(t, conf.Collector.Receivers.TcplogReceivers)

t.Run("Test 1: NewCollector TcplogReceiver added", func(tt *testing.T) {
tcplogReceiverAdded := collector.updateNginxAppProtectTcplogReceivers(nginxConfigContext)
tcplogReceiverAdded := collector.updateNginxAppProtectTcplogReceivers(ctx, nginxConfigContext)

assert.True(tt, tcplogReceiverAdded)
assert.Len(tt, conf.Collector.Receivers.TcplogReceivers, 1)
Expand All @@ -756,21 +757,21 @@ func TestCollector_updateNginxAppProtectTcplogReceivers(t *testing.T) {
// Calling updateNginxAppProtectTcplogReceivers shouldn't update the TcplogReceivers slice
// since there is already a receiver with the same ListenAddress
t.Run("Test 2: TcplogReceiver already exists", func(tt *testing.T) {
tcplogReceiverAdded := collector.updateNginxAppProtectTcplogReceivers(nginxConfigContext)
tcplogReceiverAdded := collector.updateNginxAppProtectTcplogReceivers(ctx, nginxConfigContext)
assert.False(t, tcplogReceiverAdded)
assert.Len(t, conf.Collector.Receivers.TcplogReceivers, 1)
assert.Equal(t, "localhost:15632", conf.Collector.Receivers.TcplogReceivers["nginx_app_protect"].ListenAddress)
assert.Len(t, conf.Collector.Receivers.TcplogReceivers["nginx_app_protect"].Operators, 6)
})

t.Run("Test 3: TcplogReceiver deleted", func(tt *testing.T) {
tcplogReceiverDeleted := collector.updateNginxAppProtectTcplogReceivers(&model.NginxConfigContext{})
tcplogReceiverDeleted := collector.updateNginxAppProtectTcplogReceivers(ctx, &model.NginxConfigContext{})
assert.True(t, tcplogReceiverDeleted)
assert.Empty(t, conf.Collector.Receivers.TcplogReceivers)
})

t.Run("Test 4: NewCollector tcplogReceiver added and deleted another", func(tt *testing.T) {
tcplogReceiverDeleted := collector.updateNginxAppProtectTcplogReceivers(
tcplogReceiverDeleted := collector.updateNginxAppProtectTcplogReceivers(ctx,
&model.NginxConfigContext{
NAPSysLogServers: []string{"localhost:1555"},
},
Expand All @@ -784,6 +785,7 @@ func TestCollector_updateNginxAppProtectTcplogReceivers(t *testing.T) {
}

func TestCollector_findAvailableSyslogServers(t *testing.T) {
ctx := context.Background()
conf := types.OTelConfig(t)
conf.Collector.Log.Path = ""
conf.Collector.Processors.Batch = nil
Expand Down Expand Up @@ -840,12 +842,13 @@ func TestCollector_findAvailableSyslogServers(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
if test.portInUse {
ln, listenError := net.Listen("tcp", "localhost:15632")
listenConfig := &net.ListenConfig{}
ln, listenError := listenConfig.Listen(ctx, "tcp", "localhost:15632")
require.NoError(t, listenError)
defer ln.Close()
}

actual := collector.findAvailableSyslogServers(test.syslogServers)
actual := collector.findAvailableSyslogServers(ctx, test.syslogServers)
assert.Equal(tt, test.expectedSyslogServer, actual)
})
}
Expand Down
5 changes: 3 additions & 2 deletions internal/datasource/config/nginx_config_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,8 +801,9 @@ func (ncp *NginxConfigParser) socketClient(socketPath string) *http.Client {
return &http.Client{
Timeout: ncp.agentConfig.Client.Grpc.KeepAlive.Timeout,
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", socketPath)
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
dialer := &net.Dialer{}
return dialer.DialContext(ctx, "unix", socketPath)
},
},
}
Expand Down
Loading