Skip to content

Commit 0e2f06b

Browse files
authored
Merge pull request #1215 from nginx/add-nolinitlint-increase-complexity
Uncommented nolintlint, now comment is a must while adding a nolint directive.
2 parents 9c552a3 + fa0d157 commit 0e2f06b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+188
-230
lines changed

.golangci.yml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ linters:
3939
- nilnil
4040
- nlreturn
4141
- noctx
42-
# - nolintlint
42+
- nolintlint
4343
- nosprintfhostport
4444
- perfsprint
4545
- prealloc
@@ -66,7 +66,7 @@ linters:
6666
- zerologlint
6767
settings:
6868
cyclop:
69-
max-complexity: 10
69+
max-complexity: 12
7070
package-average: 0
7171
errorlint:
7272
errorf: true
@@ -216,19 +216,19 @@ linters:
216216
- chan
217217
nlreturn:
218218
block-size: 2
219-
# nolintlint:
220-
# # Disable to ensure that all nolint directives actually have an effect.
221-
# # Default: false
222-
# allow-unused: true
223-
# # Exclude following linters from requiring an explanation.
224-
# # Default: []
225-
# allow-no-explanation: [ ]
226-
# # Enable to require an explanation of nonzero length after each nolint directive.
227-
# # Default: false
228-
# require-explanation: true
229-
# # Enable to require nolint directives to mention the specific linter being suppressed.
230-
# # Default: false
231-
# require-specific: true
219+
nolintlint:
220+
# Disable to ensure that all nolint directives actually have an effect.
221+
# Default: false
222+
allow-unused: true
223+
# Exclude following linters from requiring an explanation.
224+
# Default: []
225+
allow-no-explanation: [ ]
226+
# Enable to require an explanation of nonzero length after each nolint directive.
227+
# Default: false
228+
require-explanation: true
229+
# Enable to require nolint directives to mention the specific linter being suppressed.
230+
# Default: false
231+
require-specific: true
232232
prealloc:
233233
simple: true
234234
range-loops: true
@@ -285,7 +285,7 @@ linters:
285285
disabled: false
286286
- name: cognitive-complexity
287287
arguments:
288-
- 10
288+
- 12
289289
severity: warning
290290
disabled: false
291291
- name: comment-spacings
@@ -310,7 +310,7 @@ linters:
310310
disabled: false
311311
- name: cyclomatic
312312
arguments:
313-
- 10
313+
- 12
314314
severity: warning
315315
disabled: false
316316
- name: datarace

internal/backoff/backoff.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ func WaitUntil(
6565

6666
// WaitUntilWithData Implementation of backoff operations that increases the back off period for each retry
6767
// attempt using a randomization function that grows exponentially. This does not allow for parameters.
68-
// nolint: ireturn
68+
//
69+
//nolint:ireturn // must return an interface
6970
func WaitUntilWithData[T any](
7071
ctx context.Context,
7172
backoffSettings *config.BackOff,
@@ -76,7 +77,7 @@ func WaitUntilWithData[T any](
7677
return backoff.RetryWithData(operation, backoffWithContext)
7778
}
7879

79-
// nolint: ireturn
80+
//nolint:ireturn // must return an interface
8081
func Context(ctx context.Context, backoffSettings *config.BackOff) backoff.BackOffContext {
8182
eb := backoff.NewExponentialBackOff()
8283
eb.InitialInterval = backoffSettings.InitialInterval

internal/collector/containermetricsreceiver/factory.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/nginx/agent/v3/internal/collector/containermetricsreceiver/internal/metadata"
2222
)
2323

24-
// nolint: ireturn
24+
//nolint:ireturn // must return metrics interface
2525
func NewFactory() receiver.Factory {
2626
return receiver.NewFactory(
2727
metadata.Type,
@@ -33,7 +33,7 @@ func NewFactory() receiver.Factory {
3333
)
3434
}
3535

36-
// nolint: ireturn
36+
//nolint:ireturn // must return metrics interface
3737
func createMetricsReceiver(
3838
_ context.Context,
3939
params receiver.Settings,

internal/collector/containermetricsreceiver/internal/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Config struct {
1414
scraperhelper.ControllerConfig `mapstructure:",squash"`
1515
}
1616

17-
// nolint: ireturn
17+
//nolint:ireturn // must return a default metrics controller interface
1818
func CreateDefaultConfig() component.Config {
1919
cfg := scraperhelper.NewDefaultControllerConfig()
2020
return &Config{

internal/collector/containermetricsreceiver/internal/scraper/cpuscraper/factory.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import (
1515
"go.opentelemetry.io/collector/scraper"
1616
)
1717

18-
// NewFactory for CPU scraper.
19-
// nolint: ireturn
18+
//nolint:ireturn // must return a CPU scraper interface.
2019
func NewFactory() scraper.Factory {
2120
return scraper.NewFactory(
2221
metadata.Type,
@@ -25,16 +24,14 @@ func NewFactory() scraper.Factory {
2524
)
2625
}
2726

28-
// createDefaultConfig creates the default configuration for the Scraper.
29-
// nolint: ireturn
27+
//nolint:ireturn // must create the default configuration for the Scraper.
3028
func createDefaultConfig() component.Config {
3129
return &Config{
3230
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
3331
}
3432
}
3533

36-
// createMetricsScraper creates a scraper based on provided config.
37-
// nolint: ireturn
34+
//nolint:ireturn // must create and return a scraper based on provided config.
3835
func createMetricsScraper(
3936
ctx context.Context,
4037
settings scraper.Settings,

internal/collector/containermetricsreceiver/internal/scraper/cpuscraper/internal/cgroup/cpu.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ func (cs *CPUSource) Collect(ctx context.Context) (ContainerCPUStats, error) {
7373
return cpuStats, nil
7474
}
7575

76-
// nolint: mnd
7776
func (cs *CPUSource) collectCPUStats(ctx context.Context) (ContainerCPUStats, error) {
7877
clockTicks, err := clockTicks(ctx)
78+
const nanosecondsPerMillisecond = 1000
7979
if err != nil {
8080
return ContainerCPUStats{}, err
8181
}
@@ -85,7 +85,7 @@ func (cs *CPUSource) collectCPUStats(ctx context.Context) (ContainerCPUStats, er
8585
userKey := V2UserKey
8686
sysKey := V2SystemKey
8787
convertUsage := func(usage float64) float64 {
88-
return usage * 1000
88+
return usage * nanosecondsPerMillisecond
8989
}
9090

9191
if !cs.isCgroupV2 { // cgroup v1
@@ -162,7 +162,7 @@ func (cs *CPUSource) cpuUsageTimes(filePath, userKey, systemKey string) (*Contai
162162
return cpuTimes, nil
163163
}
164164

165-
// nolint: revive, gocritic
165+
//nolint:revive // cognitive complexity is 14
166166
func systemCPUUsage(clockTicks int) (float64, error) {
167167
lines, err := internal.ReadLines(CPUStatsPath)
168168
if err != nil {
@@ -171,8 +171,7 @@ func systemCPUUsage(clockTicks int) (float64, error) {
171171

172172
for _, line := range lines {
173173
parts := strings.Fields(line)
174-
switch parts[0] {
175-
case "cpu":
174+
if parts[0] == "cpu" {
176175
if len(parts) < CPUStatsFileLineLength {
177176
return 0, errors.New("unable to process " + CPUStatsPath + ". Invalid number of fields for cpu line")
178177
}

internal/collector/containermetricsreceiver/internal/scraper/memoryscraper/factory.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import (
1616
)
1717

1818
// NewFactory for CPU scraper.
19-
// nolint: ireturn
19+
//
20+
//nolint:ireturn // must return a CPU scraper
2021
func NewFactory() scraper.Factory {
2122
return scraper.NewFactory(
2223
metadata.Type,
@@ -26,15 +27,17 @@ func NewFactory() scraper.Factory {
2627
}
2728

2829
// createDefaultConfig creates the default configuration for the Scraper.
29-
// nolint: ireturn
30+
//
31+
//nolint:ireturn // must return a default configuration for scraper
3032
func createDefaultConfig() component.Config {
3133
return &Config{
3234
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
3335
}
3436
}
3537

3638
// createMetricsScraper creates a scraper based on provided config.
37-
// nolint: ireturn
39+
//
40+
//nolint:ireturn // must return a metric scraper interface
3841
func createMetricsScraper(
3942
ctx context.Context,
4043
settings scraper.Settings,

internal/collector/containermetricsreceiver/internal/scraper/memoryscraper/internal/cgroup/memory.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ func (ms *MemorySource) Collect() {
6464
}
6565
}
6666

67-
// nolint: unparam
6867
func (ms *MemorySource) VirtualMemoryStatWithContext(ctx context.Context) (*mem.VirtualMemoryStat, error) {
6968
var cgroupStat mem.VirtualMemoryStat
7069
var memoryStat MemoryStat
@@ -146,16 +145,16 @@ func MemoryLimitInBytes(ctx context.Context, filePath string) (uint64, error) {
146145
return strconv.ParseUint(memTotalString, 10, 64)
147146
}
148147

149-
// nolint: revive, mnd
150148
func CalculateMemoryStat(statFile, cachedKey, sharedKey string) (MemoryStat, error) {
149+
const requiredFields = 2
151150
memoryStat := MemoryStat{}
152151
lines, err := internal.ReadLines(statFile)
153152
if err != nil {
154153
return memoryStat, err
155154
}
156155
for _, line := range lines {
157156
fields := strings.Fields(line)
158-
if len(fields) != 2 {
157+
if len(fields) != requiredFields {
159158
return memoryStat, fmt.Errorf("%+v required 2 fields", fields)
160159
}
161160

internal/collector/containermetricsreceiver/internal/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func ReadLines(filename string) ([]string, error) {
2121
return ReadLinesOffsetN(filename, 0, -1)
2222
}
2323

24-
// nolint: revive
24+
//nolint:revive // cognitive complexity is 16
2525
func ReadLinesOffsetN(filename string, offset uint, n int) (lines []string, err error) {
2626
f, err := os.Open(filename)
2727
defer func() {

internal/collector/logsgzipprocessor/processor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"go.uber.org/zap"
2222
)
2323

24-
// nolint: ireturn
24+
//nolint:ireturn // must return factory processor interface for collector
2525
func NewFactory() processor.Factory {
2626
return processor.NewFactory(
2727
component.MustNewType("logsgzip"),
@@ -32,7 +32,7 @@ func NewFactory() processor.Factory {
3232
)
3333
}
3434

35-
// nolint: ireturn
35+
//nolint:ireturn // returns a zip processor interface
3636
func createLogsGzipProcessor(_ context.Context,
3737
settings processor.Settings,
3838
cfg component.Config,

0 commit comments

Comments
 (0)