Skip to content

Commit 81f80e6

Browse files
authored
fix golang ci yaml and improve makefile (#61)
* fix golang ci yaml and improve makefile
1 parent aa8d825 commit 81f80e6

24 files changed

+93
-135
lines changed

.golangci.yml

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
---
2+
run:
3+
issues-exit-code: 1
4+
tests: true
5+
skip-dirs:
6+
skip-dirs-use-default: false
7+
skip-files:
28
linters:
9+
disable:
10+
- errcheck
311
enable:
12+
- gofmt
413
- bodyclose
514
- goconst
615
- gocritic
7-
- goimports
8-
- maligned
916
- misspell
1017
- nakedret
1118
- prealloc
@@ -14,5 +21,13 @@ linters:
1421
- whitespace
1522
- ineffassign
1623
linters-settings:
24+
gci:
25+
local-prefixes: github.com/open-telemetry/opentelemetry-log-collection
1726
goconst:
18-
min-occurrences: 5
27+
min-occurrences: 5
28+
issues:
29+
exclude-rules:
30+
- path: operator/builtin/input/windows/xml\.go
31+
linters:
32+
- unused
33+
- deadcode

Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ listmod:
4545

4646
.PHONY: lint
4747
lint:
48-
golangci-lint run ./...
48+
golangci-lint run --allow-parallel-runners ./...
49+
50+
.PHONY: lint-fix
51+
lint-fix:
52+
golangci-lint run --fix --allow-parallel-runners ./...
4953

5054
.PHONY: vet
5155
vet:

operator/builtin/input/file/config_test.go

+6-11
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,7 @@ func TestConfig(t *testing.T) {
339339
false,
340340
func() *InputConfig {
341341
cfg := defaultCfg()
342-
var newMulti *MultilineConfig
343-
newMulti = new(MultilineConfig)
342+
newMulti := new(MultilineConfig)
344343
newMulti.LineStartPattern = "Start"
345344
cfg.Multiline = newMulti
346345
return cfg
@@ -351,8 +350,7 @@ func TestConfig(t *testing.T) {
351350
false,
352351
func() *InputConfig {
353352
cfg := defaultCfg()
354-
var newMulti *MultilineConfig
355-
newMulti = new(MultilineConfig)
353+
newMulti := new(MultilineConfig)
356354
newMulti.LineStartPattern = "%"
357355
cfg.Multiline = newMulti
358356
return cfg
@@ -363,8 +361,7 @@ func TestConfig(t *testing.T) {
363361
false,
364362
func() *InputConfig {
365363
cfg := defaultCfg()
366-
var newMulti *MultilineConfig
367-
newMulti = new(MultilineConfig)
364+
newMulti := new(MultilineConfig)
368365
newMulti.LineEndPattern = "Start"
369366
cfg.Multiline = newMulti
370367
return cfg
@@ -375,8 +372,7 @@ func TestConfig(t *testing.T) {
375372
false,
376373
func() *InputConfig {
377374
cfg := defaultCfg()
378-
var newMulti *MultilineConfig
379-
newMulti = new(MultilineConfig)
375+
newMulti := new(MultilineConfig)
380376
newMulti.LineEndPattern = "%"
381377
cfg.Multiline = newMulti
382378
return cfg
@@ -468,7 +464,7 @@ func TestConfig(t *testing.T) {
468464

469465
for _, tc := range cases {
470466
t.Run(tc.name, func(t *testing.T) {
471-
cfgFromYaml, yamlErr := configFromFileViaYaml(t, path.Join(".", "testdata", fmt.Sprintf("%s.yaml", tc.name)))
467+
cfgFromYaml, yamlErr := configFromFileViaYaml(path.Join(".", "testdata", fmt.Sprintf("%s.yaml", tc.name)))
472468
cfgFromMapstructure, mapErr := configFromFileViaMapstructure(path.Join(".", "testdata", fmt.Sprintf("%s.yaml", tc.name)))
473469
if tc.expectErr {
474470
require.Error(t, yamlErr)
@@ -483,7 +479,7 @@ func TestConfig(t *testing.T) {
483479
}
484480
}
485481

486-
func configFromFileViaYaml(t *testing.T, file string) (*InputConfig, error) {
482+
func configFromFileViaYaml(file string) (*InputConfig, error) {
487483
bytes, err := ioutil.ReadFile(file)
488484
if err != nil {
489485
return nil, fmt.Errorf("could not find config file: %s", err)
@@ -520,7 +516,6 @@ func configFromFileViaMapstructure(file string) (*InputConfig, error) {
520516
return nil, err
521517
}
522518
return cfg, nil
523-
524519
}
525520

526521
func defaultCfg() *InputConfig {

operator/builtin/input/file/file.go

+16-19
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ type InputOperator struct {
5757
encoding encoding.Encoding
5858

5959
wg sync.WaitGroup
60-
readerWg sync.WaitGroup
6160
firstCheck bool
6261
cancel context.CancelFunc
6362
}
@@ -111,25 +110,26 @@ func (f *InputOperator) startPoller(ctx context.Context) {
111110

112111
// poll checks all the watched paths for new entries
113112
func (f *InputOperator) poll(ctx context.Context) {
114-
115113
var matches []string
116114
if len(f.queuedMatches) > f.MaxConcurrentFiles {
117115
matches, f.queuedMatches = f.queuedMatches[:f.MaxConcurrentFiles], f.queuedMatches[f.MaxConcurrentFiles:]
118-
} else if len(f.queuedMatches) > 0 {
119-
matches, f.queuedMatches = f.queuedMatches, make([]string, 0)
120116
} else {
121-
// Increment the generation on all known readers
122-
// This is done here because the next generation is about to start
123-
for i := 0; i < len(f.knownFiles); i++ {
124-
f.knownFiles[i].generation++
125-
}
117+
if len(f.queuedMatches) > 0 {
118+
matches, f.queuedMatches = f.queuedMatches, make([]string, 0)
119+
} else {
120+
// Increment the generation on all known readers
121+
// This is done here because the next generation is about to start
122+
for i := 0; i < len(f.knownFiles); i++ {
123+
f.knownFiles[i].generation++
124+
}
126125

127-
// Get the list of paths on disk
128-
matches = getMatches(f.Include, f.Exclude)
129-
if f.firstCheck && len(matches) == 0 {
130-
f.Warnw("no files match the configured include patterns", "include", f.Include)
131-
} else if len(matches) > f.MaxConcurrentFiles {
132-
matches, f.queuedMatches = matches[:f.MaxConcurrentFiles], matches[f.MaxConcurrentFiles:]
126+
// Get the list of paths on disk
127+
matches = getMatches(f.Include, f.Exclude)
128+
if f.firstCheck && len(matches) == 0 {
129+
f.Warnw("no files match the configured include patterns", "include", f.Include)
130+
} else if len(matches) > f.MaxConcurrentFiles {
131+
matches, f.queuedMatches = matches[:f.MaxConcurrentFiles], matches[f.MaxConcurrentFiles:]
132+
}
133133
}
134134
}
135135

@@ -229,7 +229,6 @@ OUTER:
229229
// Empty file, don't read it until we can compare its fingerprint
230230
fps = append(fps[:i], fps[i+1:]...)
231231
filesCopy = append(filesCopy[:i], filesCopy[i+1:]...)
232-
233232
}
234233

235234
for j := 0; j < len(fps); j++ {
@@ -267,9 +266,7 @@ OUTER:
267266
// before clearing out readers that have existed for 3 generations.
268267
func (f *InputOperator) saveCurrent(readers []*Reader) {
269268
// Add readers from the current, completed poll interval to the list of known files
270-
for _, reader := range readers {
271-
f.knownFiles = append(f.knownFiles, reader)
272-
}
269+
f.knownFiles = append(f.knownFiles, readers...)
273270

274271
// Clear out old readers. They are sorted such that they are oldest first,
275272
// so we can just find the first reader whose generation is less than our

operator/builtin/input/file/file_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func TestBuild(t *testing.T) {
134134
}{
135135
{
136136
"Basic",
137-
func(f *InputConfig) { return },
137+
func(f *InputConfig) {},
138138
require.NoError,
139139
func(t *testing.T, f *InputOperator) {
140140
require.Equal(t, f.OutputOperators[0], fakeOutput)
@@ -802,7 +802,6 @@ func (rt rotationTest) run(tc rotationTest, copyTruncate, sequential bool) func(
802802
}
803803

804804
func TestRotation(t *testing.T) {
805-
806805
cases := []rotationTest{
807806
{
808807
name: "NoRotation",

operator/builtin/input/file/fingerprint_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
)
2424

2525
func TestNewFingerprintDoesNotModifyOffset(t *testing.T) {
26-
2726
fingerprint := "this is the fingerprint"
2827
next := "this comes after the fingerprint and is substantially longer than the fingerprint"
2928
extra := "fin"
@@ -61,7 +60,6 @@ func TestNewFingerprintDoesNotModifyOffset(t *testing.T) {
6160
}
6261

6362
func TestNewFingerprint(t *testing.T) {
64-
6563
cases := []struct {
6664
name string
6765
fingerprintSize int

operator/builtin/input/file/line_splitter_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,14 @@ func TestLineStartSplitFunc(t *testing.T) {
151151
data := []byte(`LOGSTART`)
152152

153153
t.Run("NotAtEOF", func(t *testing.T) {
154-
advance, token, err := splitFunc(data[:], false)
154+
advance, token, err := splitFunc(data, false)
155155
require.NoError(t, err)
156156
require.Equal(t, 0, advance)
157157
require.Nil(t, token)
158158
})
159159

160160
t.Run("AtEOF", func(t *testing.T) {
161-
advance, token, err := splitFunc(data[:], true)
161+
advance, token, err := splitFunc(data, true)
162162
require.NoError(t, err)
163163
require.Equal(t, 0, advance)
164164
require.Nil(t, token)

operator/builtin/input/syslog/syslog.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,14 @@ func (c *SyslogInputConfig) UnmarshalYAML(unmarshal func(interface{}) error) err
8787
}
8888
c.SyslogParserConfig = *parserCfg
8989

90-
base := &BaseSyslogInputConfig{
91-
}
92-
err = unmarshal(base)
90+
base := &BaseSyslogInputConfig{}
91+
err = unmarshal(base)
9392
if err != nil {
9493
return err
9594
}
9695

9796
c.InputConfig = base.InputConfig
98-
c.Tcp= base.Tcp
97+
c.Tcp = base.Tcp
9998
c.Udp = base.Udp
10099
return nil
101100
}

operator/builtin/input/syslog/syslog_test.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,9 @@ func SyslogInputTest(t *testing.T, cfg *SyslogInputConfig, tc syslog.Case) {
7171
require.NoError(t, err)
7272
}
7373

74-
switch tc.InputRecord.(type) {
75-
case string:
76-
_, err = conn.Write([]byte(tc.InputRecord.(string)))
77-
case []byte:
74+
if v, ok := tc.InputRecord.(string); ok {
75+
_, err = conn.Write([]byte(v))
76+
} else {
7877
_, err = conn.Write(tc.InputRecord.([]byte))
7978
}
8079

@@ -121,7 +120,7 @@ udp:
121120
var cfg SyslogInputConfig
122121
err := yaml.Unmarshal([]byte(base), &cfg)
123122
require.NoError(t, err)
124-
require.Equal(t, "rfc5424", cfg.Protocol)
123+
require.Equal(t, syslog.RFC5424, cfg.Protocol)
125124
require.Equal(t, "localhost:1234", cfg.Udp.ListenAddress)
126125

127126
base = `type: syslog_input
@@ -133,8 +132,7 @@ tcp:
133132
`
134133
err = yaml.Unmarshal([]byte(base), &cfg)
135134
require.NoError(t, err)
136-
require.Equal(t, "rfc5424", cfg.Protocol)
135+
require.Equal(t, syslog.RFC5424, cfg.Protocol)
137136
require.Equal(t, "localhost:1234", cfg.Tcp.ListenAddress)
138137
require.Equal(t, "/tmp/test.ca", cfg.Tcp.TLS.CAFile)
139-
140138
}

operator/builtin/input/tcp/tcp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (t *TCPInput) configureListener() error {
136136
return nil
137137
}
138138

139-
t.tls.Time = func() time.Time { return time.Now() }
139+
t.tls.Time = time.Now
140140
t.tls.Rand = rand.Reader
141141

142142
listener, err := tls.Listen("tcp", t.address, t.tls)

operator/builtin/input/tcp/tcp_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ func tcpInputTest(input []byte, expected []string) func(t *testing.T) {
132132

133133
func tlsTCPInputTest(input []byte, expected []string) func(t *testing.T) {
134134
return func(t *testing.T) {
135-
136135
f, err := os.Create("test.crt")
137136
require.NoError(t, err)
138137
defer f.Close()
@@ -335,5 +334,4 @@ func createTlsConfig(cert string, key string) *helper.TLSServerConfig {
335334
KeyFile: key,
336335
},
337336
})
338-
339337
}

operator/builtin/input/windows/security.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919
)
2020

2121
func parseSecurity(message string) (string, map[string]interface{}) {
22-
23-
subject, details := message, map[string]interface{}{}
22+
var subject string
23+
details := map[string]interface{}{}
2424

2525
mp := newMessageProcessor(message)
2626

operator/builtin/input/windows/security_test.go

-10
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
)
2525

2626
func TestParseSecurity(t *testing.T) {
27-
2827
testCases := []string{
2928
"account_name_changed",
3029
"audit_settings_changed",
@@ -51,7 +50,6 @@ func TestParseSecurity(t *testing.T) {
5150

5251
for _, tc := range testCases {
5352
t.Run(tc, func(t *testing.T) {
54-
5553
testDir := filepath.Join("testdata", "security", tc)
5654
messageBytes, err := ioutil.ReadFile(filepath.Join(testDir, "message.in"))
5755
require.NoError(t, err, "problem reading input file")
@@ -77,11 +75,3 @@ func TestParseSecurity(t *testing.T) {
7775
})
7876
}
7977
}
80-
81-
// Use this to initialize test results from a WEL security message
82-
// make sure to validate manually!
83-
func initTestResult(testDir, message string, details map[string]interface{}) {
84-
ioutil.WriteFile(filepath.Join(testDir, "message.out"), []byte(message), 0644)
85-
bytes, _ := json.MarshalIndent(details, "", " ")
86-
ioutil.WriteFile(filepath.Join(testDir, "details.out"), bytes, 0644)
87-
}

operator/builtin/parser/json/json_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ func TestJSONParser(t *testing.T) {
154154
}
155155

156156
func TestJSONParserWithEmbeddedTimeParser(t *testing.T) {
157-
158157
testTime := time.Unix(1136214245, 0)
159158

160159
cases := []struct {

operator/builtin/parser/severity/severity_test.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ type severityTestCase struct {
3838
}
3939

4040
func TestSeverityParser(t *testing.T) {
41-
4241
testCases := []severityTestCase{
4342
{
4443
name: "unknown",
@@ -304,17 +303,16 @@ func TestSeverityParser(t *testing.T) {
304303
t.Run(tc.name, func(t *testing.T) {
305304
rootCfg := parseSeverityTestConfig(rootField, tc.mappingSet, tc.mapping)
306305
rootEntry := makeTestEntry(rootField, tc.sample)
307-
t.Run("root", runSeverityParseTest(t, rootCfg, rootEntry, tc.buildErr, tc.parseErr, tc.expected))
306+
t.Run("root", runSeverityParseTest(rootCfg, rootEntry, tc.buildErr, tc.parseErr, tc.expected))
308307

309308
nonRootCfg := parseSeverityTestConfig(someField, tc.mappingSet, tc.mapping)
310309
nonRootEntry := makeTestEntry(someField, tc.sample)
311-
t.Run("non-root", runSeverityParseTest(t, nonRootCfg, nonRootEntry, tc.buildErr, tc.parseErr, tc.expected))
310+
t.Run("non-root", runSeverityParseTest(nonRootCfg, nonRootEntry, tc.buildErr, tc.parseErr, tc.expected))
312311
})
313312
}
314313
}
315314

316-
func runSeverityParseTest(t *testing.T, cfg *SeverityParserConfig, ent *entry.Entry, buildErr bool, parseErr bool, expected entry.Severity) func(*testing.T) {
317-
315+
func runSeverityParseTest(cfg *SeverityParserConfig, ent *entry.Entry, buildErr bool, parseErr bool, expected entry.Severity) func(*testing.T) {
318316
return func(t *testing.T) {
319317
buildContext := testutil.NewBuildContext(t)
320318

0 commit comments

Comments
 (0)