Skip to content

Commit a4918b3

Browse files
authored
Add extra linters (#1160)
1 parent bc48941 commit a4918b3

Some content is hidden

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

49 files changed

+437
-411
lines changed

.golangci.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ linters:
66
- copyloopvar
77
- cyclop
88
- dupl
9+
- decorder
910
- errorlint
1011
- exhaustive
1112
- forcetypeassert
13+
- funcorder
1214
- gocheckcompilerdirectives
1315
- gocognit
1416
- goconst
@@ -21,8 +23,10 @@ linters:
2123
- grouper
2224
- importas
2325
- inamedparam
26+
- intrange
2427
- ireturn
2528
- lll
29+
- loggercheck
2630
- maintidx
2731
- makezero
2832
- mirror
@@ -35,7 +39,9 @@ linters:
3539
- nilnil
3640
- nlreturn
3741
- noctx
42+
# - nolintlint
3843
- nosprintfhostport
44+
- perfsprint
3945
- prealloc
4046
- predeclared
4147
- promlinter
@@ -44,6 +50,7 @@ linters:
4450
- revive
4551
- rowserrcheck
4652
- sloglint
53+
- spancheck
4754
- sqlclosecheck
4855
- staticcheck
4956
- tagalign
@@ -52,6 +59,7 @@ linters:
5259
- thelper
5360
- unconvert
5461
- unparam
62+
- usetesting
5563
- usestdlibvars
5664
- wastedassign
5765
- whitespace
@@ -209,6 +217,19 @@ linters:
209217
- chan
210218
nlreturn:
211219
block-size: 2
220+
# nolintlint:
221+
# # Disable to ensure that all nolint directives actually have an effect.
222+
# # Default: false
223+
# allow-unused: true
224+
# # Exclude following linters from requiring an explanation.
225+
# # Default: []
226+
# allow-no-explanation: [ ]
227+
# # Enable to require an explanation of nonzero length after each nolint directive.
228+
# # Default: false
229+
# require-explanation: true
230+
# # Enable to require nolint directives to mention the specific linter being suppressed.
231+
# # Default: false
232+
# require-specific: true
212233
prealloc:
213234
simple: true
214235
range-loops: true

internal/bus/message_pipe.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ func (p *MessagePipe) IsPluginRegistered(pluginName string) bool {
147147
return isPluginRegistered
148148
}
149149

150+
func (p *MessagePipe) Index(pluginName string, plugins []Plugin) int {
151+
for index, plugin := range plugins {
152+
if pluginName == plugin.Info().Name {
153+
return index
154+
}
155+
}
156+
157+
return -1
158+
}
159+
150160
func (p *MessagePipe) unsubscribePlugin(ctx context.Context, index int, plugin Plugin) error {
151161
if index != -1 {
152162
p.plugins = append(p.plugins[:index], p.plugins[index+1:]...)
@@ -181,16 +191,6 @@ func (p *MessagePipe) findPlugins(pluginNames []string) []Plugin {
181191
return plugins
182192
}
183193

184-
func (p *MessagePipe) Index(pluginName string, plugins []Plugin) int {
185-
for index, plugin := range plugins {
186-
if pluginName == plugin.Info().Name {
187-
return index
188-
}
189-
}
190-
191-
return -1
192-
}
193-
194194
func (p *MessagePipe) initPlugins(ctx context.Context) {
195195
for index, plugin := range p.plugins {
196196
err := plugin.Init(ctx, p)

internal/collector/logsgzipprocessor/processor.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,22 @@ func (p *logsGzipProcessor) ConsumeLogs(ctx context.Context, ld plog.Logs) error
9595
return p.nextConsumer.ConsumeLogs(ctx, ld)
9696
}
9797

98+
func (p *logsGzipProcessor) Capabilities() consumer.Capabilities {
99+
return consumer.Capabilities{
100+
MutatesData: true,
101+
}
102+
}
103+
104+
func (p *logsGzipProcessor) Start(ctx context.Context, _ component.Host) error {
105+
p.settings.Logger.Info("Starting logs gzip processor")
106+
return nil
107+
}
108+
109+
func (p *logsGzipProcessor) Shutdown(ctx context.Context) error {
110+
p.settings.Logger.Info("Shutting down logs gzip processor")
111+
return nil
112+
}
113+
98114
func (p *logsGzipProcessor) processLogRecords(logRecords plog.LogRecordSlice) error {
99115
var errs error
100116
// Filter out unsupported data types in the log before processing
@@ -164,19 +180,3 @@ func (p *logsGzipProcessor) gzipCompress(data []byte) ([]byte, error) {
164180

165181
return buf.Bytes(), nil
166182
}
167-
168-
func (p *logsGzipProcessor) Capabilities() consumer.Capabilities {
169-
return consumer.Capabilities{
170-
MutatesData: true,
171-
}
172-
}
173-
174-
func (p *logsGzipProcessor) Start(ctx context.Context, _ component.Host) error {
175-
p.settings.Logger.Info("Starting logs gzip processor")
176-
return nil
177-
}
178-
179-
func (p *logsGzipProcessor) Shutdown(ctx context.Context) error {
180-
p.settings.Logger.Info("Shutting down logs gzip processor")
181-
return nil
182-
}

internal/collector/logsgzipprocessor/processor_benchmark_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func generateLogs(numRecords, recordSize int) plog.Logs {
2020
logs := plog.NewLogs()
2121
rl := logs.ResourceLogs().AppendEmpty()
2222
sl := rl.ScopeLogs().AppendEmpty()
23-
for i := 0; i < numRecords; i++ {
23+
for range numRecords {
2424
lr := sl.LogRecords().AppendEmpty()
2525
content, _ := randomString(recordSize)
2626
lr.Body().SetStr(content)
@@ -64,7 +64,7 @@ func BenchmarkGzipProcessor(b *testing.B) {
6464
logs := generateLogs(bm.numRecords, bm.recordSize)
6565

6666
b.ResetTimer()
67-
for i := 0; i < b.N; i++ {
67+
for range b.N {
6868
_ = p.ConsumeLogs(context.Background(), logs)
6969
}
7070
})

internal/collector/nginxossreceiver/internal/scraper/accesslog/nginx_log_scraper.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ func (nls *NginxLogScraper) Shutdown(_ context.Context) error {
224224
return err
225225
}
226226

227+
func (nls *NginxLogScraper) ConsumerCallback(_ context.Context, entries []*entry.Entry) {
228+
nls.mut.Lock()
229+
nls.entries = append(nls.entries, entries...)
230+
nls.mut.Unlock()
231+
}
232+
227233
func (nls *NginxLogScraper) initStanzaPipeline(
228234
operators []operator.Config,
229235
logger *zap.Logger,
@@ -248,12 +254,6 @@ func (nls *NginxLogScraper) initStanzaPipeline(
248254
return pipe, err
249255
}
250256

251-
func (nls *NginxLogScraper) ConsumerCallback(_ context.Context, entries []*entry.Entry) {
252-
nls.mut.Lock()
253-
nls.entries = append(nls.entries, entries...)
254-
nls.mut.Unlock()
255-
}
256-
257257
func (nls *NginxLogScraper) runConsumer(ctx context.Context) {
258258
nls.logger.Info("Starting NGINX access log receiver's consumer")
259259
defer nls.wg.Done()

internal/collector/nginxossreceiver/internal/scraper/accesslog/operator/input/file/config.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ import (
2121

2222
const operatorType = "access_log_file_input"
2323

24+
// Config is the configuration of a file input operator
25+
type Config struct {
26+
helper.InputConfig `mapstructure:",squash"`
27+
AccessLogFormat string `mapstructure:"access_log_format"`
28+
fileconsumer.Config `mapstructure:",squash"`
29+
}
30+
2431
func init() {
2532
operator.Register(operatorType, func() operator.Builder { return NewConfig() })
2633
}
@@ -38,13 +45,6 @@ func NewConfigWithID(operatorID string) *Config {
3845
}
3946
}
4047

41-
// Config is the configuration of a file input operator
42-
type Config struct {
43-
helper.InputConfig `mapstructure:",squash"`
44-
AccessLogFormat string `mapstructure:"access_log_format"`
45-
fileconsumer.Config `mapstructure:",squash"`
46-
}
47-
4848
// Build will build a file input operator from the supplied configuration
4949
// nolint: ireturn
5050
func (c Config) Build(set component.TelemetrySettings) (operator.Operator, error) {

internal/collector/otel_collector_plugin.go

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,63 @@ func (oc *Collector) Init(ctx context.Context, mp bus.MessagePipeInterface) erro
136136
return nil
137137
}
138138

139+
// Info the plugin.
140+
func (oc *Collector) Info() *bus.Info {
141+
return &bus.Info{
142+
Name: "collector",
143+
}
144+
}
145+
146+
// Close the plugin.
147+
func (oc *Collector) Close(ctx context.Context) error {
148+
slog.InfoContext(ctx, "Closing OTel Collector plugin")
149+
150+
if !oc.stopped {
151+
slog.InfoContext(ctx, "Shutting down OTel Collector", "state", oc.service.GetState())
152+
oc.service.Shutdown()
153+
oc.cancel()
154+
155+
settings := *oc.config.Client.Backoff
156+
settings.MaxElapsedTime = maxTimeToWaitForShutdown
157+
err := backoff.WaitUntil(ctx, &settings, func() error {
158+
if oc.service.GetState() == otelcol.StateClosed {
159+
return nil
160+
}
161+
162+
return errors.New("OTel Collector not in a closed state yet")
163+
})
164+
165+
if err != nil {
166+
slog.ErrorContext(ctx, "Failed to shutdown OTel Collector", "error", err, "state", oc.service.GetState())
167+
} else {
168+
slog.InfoContext(ctx, "OTel Collector shutdown", "state", oc.service.GetState())
169+
oc.stopped = true
170+
}
171+
}
172+
173+
return nil
174+
}
175+
176+
// Process an incoming Message Bus message in the plugin
177+
func (oc *Collector) Process(ctx context.Context, msg *bus.Message) {
178+
switch msg.Topic {
179+
case bus.NginxConfigUpdateTopic:
180+
oc.handleNginxConfigUpdate(ctx, msg)
181+
case bus.ResourceUpdateTopic:
182+
oc.handleResourceUpdate(ctx, msg)
183+
default:
184+
slog.DebugContext(ctx, "OTel collector plugin unknown topic", "topic", msg.Topic)
185+
}
186+
}
187+
188+
// Subscriptions returns the list of topics the plugin is subscribed to
189+
func (oc *Collector) Subscriptions() []string {
190+
return []string{
191+
bus.ResourceUpdateTopic,
192+
bus.NginxConfigUpdateTopic,
193+
}
194+
}
195+
139196
// Process receivers and log warning for sub-optimal configurations
140197
func (oc *Collector) processReceivers(ctx context.Context, receivers []config.OtlpReceiver) {
141198
for _, receiver := range receivers {
@@ -167,7 +224,7 @@ func (oc *Collector) bootup(ctx context.Context) error {
167224

168225
go func() {
169226
if oc.service == nil {
170-
errChan <- fmt.Errorf("unable to start OTel collector: service is nil")
227+
errChan <- errors.New("unable to start OTel collector: service is nil")
171228
return
172229
}
173230

@@ -184,7 +241,7 @@ func (oc *Collector) bootup(ctx context.Context) error {
184241
return err
185242
default:
186243
if oc.service == nil {
187-
return fmt.Errorf("unable to start otel collector: service is nil")
244+
return errors.New("unable to start otel collector: service is nil")
188245
}
189246

190247
state := oc.service.GetState()
@@ -205,63 +262,6 @@ func (oc *Collector) bootup(ctx context.Context) error {
205262
}
206263
}
207264

208-
// Info the plugin.
209-
func (oc *Collector) Info() *bus.Info {
210-
return &bus.Info{
211-
Name: "collector",
212-
}
213-
}
214-
215-
// Close the plugin.
216-
func (oc *Collector) Close(ctx context.Context) error {
217-
slog.InfoContext(ctx, "Closing OTel Collector plugin")
218-
219-
if !oc.stopped {
220-
slog.InfoContext(ctx, "Shutting down OTel Collector", "state", oc.service.GetState())
221-
oc.service.Shutdown()
222-
oc.cancel()
223-
224-
settings := *oc.config.Client.Backoff
225-
settings.MaxElapsedTime = maxTimeToWaitForShutdown
226-
err := backoff.WaitUntil(ctx, &settings, func() error {
227-
if oc.service.GetState() == otelcol.StateClosed {
228-
return nil
229-
}
230-
231-
return errors.New("OTel Collector not in a closed state yet")
232-
})
233-
234-
if err != nil {
235-
slog.ErrorContext(ctx, "Failed to shutdown OTel Collector", "error", err, "state", oc.service.GetState())
236-
} else {
237-
slog.InfoContext(ctx, "OTel Collector shutdown", "state", oc.service.GetState())
238-
oc.stopped = true
239-
}
240-
}
241-
242-
return nil
243-
}
244-
245-
// Process an incoming Message Bus message in the plugin
246-
func (oc *Collector) Process(ctx context.Context, msg *bus.Message) {
247-
switch msg.Topic {
248-
case bus.NginxConfigUpdateTopic:
249-
oc.handleNginxConfigUpdate(ctx, msg)
250-
case bus.ResourceUpdateTopic:
251-
oc.handleResourceUpdate(ctx, msg)
252-
default:
253-
slog.DebugContext(ctx, "OTel collector plugin unknown topic", "topic", msg.Topic)
254-
}
255-
}
256-
257-
// Subscriptions returns the list of topics the plugin is subscribed to
258-
func (oc *Collector) Subscriptions() []string {
259-
return []string{
260-
bus.ResourceUpdateTopic,
261-
bus.NginxConfigUpdateTopic,
262-
}
263-
}
264-
265265
func (oc *Collector) handleNginxConfigUpdate(ctx context.Context, msg *bus.Message) {
266266
slog.DebugContext(ctx, "OTel collector plugin received nginx config update message")
267267
oc.mu.Lock()

internal/collector/otel_collector_plugin_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"context"
1010
"errors"
11-
"fmt"
1211
"path/filepath"
1312
"testing"
1413

@@ -252,7 +251,7 @@ func TestCollector_ProcessNginxConfigUpdateTopic(t *testing.T) {
252251

253252
if len(test.receivers.NginxPlusReceivers) == 1 {
254253
apiDetails := config.APIDetails{
255-
URL: fmt.Sprintf("%s/api", nginxPlusMock.URL),
254+
URL: nginxPlusMock.URL + "/api",
256255
Listen: "",
257256
Location: "",
258257
}
@@ -270,7 +269,7 @@ func TestCollector_ProcessNginxConfigUpdateTopic(t *testing.T) {
270269
model.PlusAPI.Location = apiDetails.Location
271270
} else {
272271
apiDetails := config.APIDetails{
273-
URL: fmt.Sprintf("%s/stub_status", nginxPlusMock.URL),
272+
URL: nginxPlusMock.URL + "/stub_status",
274273
Listen: "",
275274
Location: "",
276275
}

0 commit comments

Comments
 (0)