Skip to content

Add extra linters #1160

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

Open
wants to merge 4 commits into
base: update-go-1.24
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ linters:
- copyloopvar
- cyclop
- dupl
- decorder
- errorlint
- exhaustive
- forcetypeassert
- funcorder
- gocheckcompilerdirectives
- gocognit
- goconst
Expand All @@ -21,8 +23,10 @@ linters:
- grouper
- importas
- inamedparam
- intrange
- ireturn
- lll
- loggercheck
- maintidx
- makezero
- mirror
Expand All @@ -35,7 +39,9 @@ linters:
- nilnil
- nlreturn
- noctx
# - nolintlint
- nosprintfhostport
- perfsprint
- prealloc
- predeclared
- promlinter
Expand All @@ -44,6 +50,7 @@ linters:
- revive
- rowserrcheck
- sloglint
- spancheck
- sqlclosecheck
- staticcheck
- tagalign
Expand All @@ -52,6 +59,7 @@ linters:
- thelper
- unconvert
- unparam
- usetesting
- usestdlibvars
- wastedassign
- whitespace
Expand Down Expand Up @@ -209,6 +217,19 @@ linters:
- chan
nlreturn:
block-size: 2
# nolintlint:
# # Disable to ensure that all nolint directives actually have an effect.
# # Default: false
# allow-unused: true
# # Exclude following linters from requiring an explanation.
# # Default: []
# allow-no-explanation: [ ]
# # Enable to require an explanation of nonzero length after each nolint directive.
# # Default: false
# require-explanation: true
# # Enable to require nolint directives to mention the specific linter being suppressed.
# # Default: false
# require-specific: true
prealloc:
simple: true
range-loops: true
Expand Down
20 changes: 10 additions & 10 deletions internal/bus/message_pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ func (p *MessagePipe) IsPluginRegistered(pluginName string) bool {
return isPluginRegistered
}

func (p *MessagePipe) Index(pluginName string, plugins []Plugin) int {
for index, plugin := range plugins {
if pluginName == plugin.Info().Name {
return index
}
}

return -1
}

func (p *MessagePipe) unsubscribePlugin(ctx context.Context, index int, plugin Plugin) error {
if index != -1 {
p.plugins = append(p.plugins[:index], p.plugins[index+1:]...)
Expand Down Expand Up @@ -181,16 +191,6 @@ func (p *MessagePipe) findPlugins(pluginNames []string) []Plugin {
return plugins
}

func (p *MessagePipe) Index(pluginName string, plugins []Plugin) int {
for index, plugin := range plugins {
if pluginName == plugin.Info().Name {
return index
}
}

return -1
}

func (p *MessagePipe) initPlugins(ctx context.Context) {
for index, plugin := range p.plugins {
err := plugin.Init(ctx, p)
Expand Down
32 changes: 16 additions & 16 deletions internal/collector/logsgzipprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ func (p *logsGzipProcessor) ConsumeLogs(ctx context.Context, ld plog.Logs) error
return p.nextConsumer.ConsumeLogs(ctx, ld)
}

func (p *logsGzipProcessor) Capabilities() consumer.Capabilities {
return consumer.Capabilities{
MutatesData: true,
}
}

func (p *logsGzipProcessor) Start(ctx context.Context, _ component.Host) error {
p.settings.Logger.Info("Starting logs gzip processor")
return nil
}

func (p *logsGzipProcessor) Shutdown(ctx context.Context) error {
p.settings.Logger.Info("Shutting down logs gzip processor")
return nil
}

func (p *logsGzipProcessor) processLogRecords(logRecords plog.LogRecordSlice) error {
var errs error
// Filter out unsupported data types in the log before processing
Expand Down Expand Up @@ -164,19 +180,3 @@ func (p *logsGzipProcessor) gzipCompress(data []byte) ([]byte, error) {

return buf.Bytes(), nil
}

func (p *logsGzipProcessor) Capabilities() consumer.Capabilities {
return consumer.Capabilities{
MutatesData: true,
}
}

func (p *logsGzipProcessor) Start(ctx context.Context, _ component.Host) error {
p.settings.Logger.Info("Starting logs gzip processor")
return nil
}

func (p *logsGzipProcessor) Shutdown(ctx context.Context) error {
p.settings.Logger.Info("Shutting down logs gzip processor")
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func generateLogs(numRecords, recordSize int) plog.Logs {
logs := plog.NewLogs()
rl := logs.ResourceLogs().AppendEmpty()
sl := rl.ScopeLogs().AppendEmpty()
for i := 0; i < numRecords; i++ {
for range numRecords {
lr := sl.LogRecords().AppendEmpty()
content, _ := randomString(recordSize)
lr.Body().SetStr(content)
Expand Down Expand Up @@ -64,7 +64,7 @@ func BenchmarkGzipProcessor(b *testing.B) {
logs := generateLogs(bm.numRecords, bm.recordSize)

b.ResetTimer()
for i := 0; i < b.N; i++ {
for range b.N {
_ = p.ConsumeLogs(context.Background(), logs)
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ func (nls *NginxLogScraper) Shutdown(_ context.Context) error {
return err
}

func (nls *NginxLogScraper) ConsumerCallback(_ context.Context, entries []*entry.Entry) {
nls.mut.Lock()
nls.entries = append(nls.entries, entries...)
nls.mut.Unlock()
}

func (nls *NginxLogScraper) initStanzaPipeline(
operators []operator.Config,
logger *zap.Logger,
Expand All @@ -248,12 +254,6 @@ func (nls *NginxLogScraper) initStanzaPipeline(
return pipe, err
}

func (nls *NginxLogScraper) ConsumerCallback(_ context.Context, entries []*entry.Entry) {
nls.mut.Lock()
nls.entries = append(nls.entries, entries...)
nls.mut.Unlock()
}

func (nls *NginxLogScraper) runConsumer(ctx context.Context) {
nls.logger.Info("Starting NGINX access log receiver's consumer")
defer nls.wg.Done()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ import (

const operatorType = "access_log_file_input"

// Config is the configuration of a file input operator
type Config struct {
helper.InputConfig `mapstructure:",squash"`
AccessLogFormat string `mapstructure:"access_log_format"`
fileconsumer.Config `mapstructure:",squash"`
}

func init() {
operator.Register(operatorType, func() operator.Builder { return NewConfig() })
}
Expand All @@ -38,13 +45,6 @@ func NewConfigWithID(operatorID string) *Config {
}
}

// Config is the configuration of a file input operator
type Config struct {
helper.InputConfig `mapstructure:",squash"`
AccessLogFormat string `mapstructure:"access_log_format"`
fileconsumer.Config `mapstructure:",squash"`
}

// Build will build a file input operator from the supplied configuration
// nolint: ireturn
func (c Config) Build(set component.TelemetrySettings) (operator.Operator, error) {
Expand Down
118 changes: 59 additions & 59 deletions internal/collector/otel_collector_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,63 @@ func (oc *Collector) Init(ctx context.Context, mp bus.MessagePipeInterface) erro
return nil
}

// Info the plugin.
func (oc *Collector) Info() *bus.Info {
return &bus.Info{
Name: "collector",
}
}

// Close the plugin.
func (oc *Collector) Close(ctx context.Context) error {
slog.InfoContext(ctx, "Closing OTel Collector plugin")

if !oc.stopped {
slog.InfoContext(ctx, "Shutting down OTel Collector", "state", oc.service.GetState())
oc.service.Shutdown()
oc.cancel()

settings := *oc.config.Client.Backoff
settings.MaxElapsedTime = maxTimeToWaitForShutdown
err := backoff.WaitUntil(ctx, &settings, func() error {
if oc.service.GetState() == otelcol.StateClosed {
return nil
}

return errors.New("OTel Collector not in a closed state yet")
})

if err != nil {
slog.ErrorContext(ctx, "Failed to shutdown OTel Collector", "error", err, "state", oc.service.GetState())
} else {
slog.InfoContext(ctx, "OTel Collector shutdown", "state", oc.service.GetState())
oc.stopped = true
}
}

return nil
}

// Process an incoming Message Bus message in the plugin
func (oc *Collector) Process(ctx context.Context, msg *bus.Message) {
switch msg.Topic {
case bus.NginxConfigUpdateTopic:
oc.handleNginxConfigUpdate(ctx, msg)
case bus.ResourceUpdateTopic:
oc.handleResourceUpdate(ctx, msg)
default:
slog.DebugContext(ctx, "OTel collector plugin unknown topic", "topic", msg.Topic)
}
}

// Subscriptions returns the list of topics the plugin is subscribed to
func (oc *Collector) Subscriptions() []string {
return []string{
bus.ResourceUpdateTopic,
bus.NginxConfigUpdateTopic,
}
}

// Process receivers and log warning for sub-optimal configurations
func (oc *Collector) processReceivers(ctx context.Context, receivers []config.OtlpReceiver) {
for _, receiver := range receivers {
Expand Down Expand Up @@ -167,7 +224,7 @@ func (oc *Collector) bootup(ctx context.Context) error {

go func() {
if oc.service == nil {
errChan <- fmt.Errorf("unable to start OTel collector: service is nil")
errChan <- errors.New("unable to start OTel collector: service is nil")
return
}

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

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

// Info the plugin.
func (oc *Collector) Info() *bus.Info {
return &bus.Info{
Name: "collector",
}
}

// Close the plugin.
func (oc *Collector) Close(ctx context.Context) error {
slog.InfoContext(ctx, "Closing OTel Collector plugin")

if !oc.stopped {
slog.InfoContext(ctx, "Shutting down OTel Collector", "state", oc.service.GetState())
oc.service.Shutdown()
oc.cancel()

settings := *oc.config.Client.Backoff
settings.MaxElapsedTime = maxTimeToWaitForShutdown
err := backoff.WaitUntil(ctx, &settings, func() error {
if oc.service.GetState() == otelcol.StateClosed {
return nil
}

return errors.New("OTel Collector not in a closed state yet")
})

if err != nil {
slog.ErrorContext(ctx, "Failed to shutdown OTel Collector", "error", err, "state", oc.service.GetState())
} else {
slog.InfoContext(ctx, "OTel Collector shutdown", "state", oc.service.GetState())
oc.stopped = true
}
}

return nil
}

// Process an incoming Message Bus message in the plugin
func (oc *Collector) Process(ctx context.Context, msg *bus.Message) {
switch msg.Topic {
case bus.NginxConfigUpdateTopic:
oc.handleNginxConfigUpdate(ctx, msg)
case bus.ResourceUpdateTopic:
oc.handleResourceUpdate(ctx, msg)
default:
slog.DebugContext(ctx, "OTel collector plugin unknown topic", "topic", msg.Topic)
}
}

// Subscriptions returns the list of topics the plugin is subscribed to
func (oc *Collector) Subscriptions() []string {
return []string{
bus.ResourceUpdateTopic,
bus.NginxConfigUpdateTopic,
}
}

func (oc *Collector) handleNginxConfigUpdate(ctx context.Context, msg *bus.Message) {
slog.DebugContext(ctx, "OTel collector plugin received nginx config update message")
oc.mu.Lock()
Expand Down
5 changes: 2 additions & 3 deletions internal/collector/otel_collector_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bytes"
"context"
"errors"
"fmt"
"path/filepath"
"testing"

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

if len(test.receivers.NginxPlusReceivers) == 1 {
apiDetails := config.APIDetails{
URL: fmt.Sprintf("%s/api", nginxPlusMock.URL),
URL: nginxPlusMock.URL + "/api",
Listen: "",
Location: "",
}
Expand All @@ -270,7 +269,7 @@ func TestCollector_ProcessNginxConfigUpdateTopic(t *testing.T) {
model.PlusAPI.Location = apiDetails.Location
} else {
apiDetails := config.APIDetails{
URL: fmt.Sprintf("%s/stub_status", nginxPlusMock.URL),
URL: nginxPlusMock.URL + "/stub_status",
Listen: "",
Location: "",
}
Expand Down
Loading