Skip to content

Commit 8003d94

Browse files
authored
chore: add golangci-lint (#156)
1 parent f641b0a commit 8003d94

Some content is hidden

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

58 files changed

+541
-721
lines changed

.github/workflows/check_golang_profiler_changes.yml

Lines changed: 0 additions & 42 deletions
This file was deleted.

.github/workflows/go.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ permissions:
1010
contents: read
1111

1212
jobs:
13+
lint:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
with:
19+
persist-credentials: false
20+
21+
- name: Install Go
22+
uses: actions/setup-go@v5
23+
with:
24+
go-version: '1.22'
25+
26+
- run: make lint
27+
1328
go:
1429
runs-on: ubuntu-latest
1530
strategy:
@@ -59,7 +74,7 @@ jobs:
5974
make gotip/fix
6075
6176
- name: Build example application
62-
run: go build example/main.go
77+
run: make examples
6378

6479
- name: Run tests
6580
run: |

.github/workflows/gotip_cron_test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
echo "$HOME/sdk/gotip/bin" >> "$GITHUB_PATH"
3232
make gotip/fix
3333
- name: Build example application
34-
run: go build example/main.go
34+
run: make examples
3535
- name: Run tests
3636
run: |
3737
which go

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.idea/
22
.vscode/
3+
.tools/
34

45
main
6+
timing

.golangci.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
version: "2"
2+
3+
formatters:
4+
enable:
5+
- gofmt
6+
- goimports
7+
settings:
8+
gofmt:
9+
simplify: true
10+
goimports:
11+
local-prefixes:
12+
- github.com/grafana/pyroscope-go
13+
14+
15+
run:
16+
timeout: 10m
17+
tests: true
18+
19+
linters:
20+
default: all
21+
disable:
22+
- wsl
23+
- wsl_v5
24+
- cyclop
25+
- depguard
26+
- funcorder
27+
- funlen
28+
- mnd
29+
- varnamelen
30+
- wrapcheck
31+
- exhaustruct
32+
- paralleltest
33+
- godot
34+
- godox
35+
- testpackage
36+
- canonicalheader
37+
- tagliatelle
38+
- noinlineerr
39+
- ireturn
40+
- gochecknoinits
41+
- nonamedreturns
42+
- gomoddirectives # TODO can this be re-enabled?
43+
44+
settings:
45+
revive:
46+
rules:
47+
- name: exported
48+
disabled: true # TODO this is nice to enable for an SDK
49+

Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ TEST_PACKAGES := ./... ./godeltaprof/compat/... ./godeltaprof/...
22
GO ?= go
33
GOTIP ?= gotip
44

5+
GOLANGCI_LINT_VERSION ?= v2.2.2
6+
TOOLS_DIR := $(CURDIR)/.tools
7+
GOLANGCI_LINT := $(TOOLS_DIR)/golangci-lint
8+
59
.PHONY: test
610
test:
711
$(GO) test -race $(shell $(GO) list $(TEST_PACKAGES) | grep -v /example)
@@ -22,3 +26,20 @@ gotip/fix:
2226
cd godeltaprof/compat/ && $(GOTIP) get -v golang.org/x/[email protected]
2327
git --no-pager diff
2428
! git diff | grep toolchain
29+
30+
.PHONY: install-lint
31+
install-lint:
32+
@ mkdir -p $(TOOLS_DIR)
33+
@ GOBIN=$(TOOLS_DIR) $(GO) install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
34+
35+
.PHONY: lint
36+
lint: install-lint
37+
$(GOLANGCI_LINT) run
38+
cd godeltaprof && $(GOLANGCI_LINT) run
39+
cd godeltaprof/compat && $(GOLANGCI_LINT) run
40+
41+
.PHONY: examples
42+
examples:
43+
go build example/http/main.go
44+
go build example/simple/main.go
45+
go build example/timing/timing.go

api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func Start(cfg Config) (*Profiler, error) {
102102
func (p *Profiler) Stop() error {
103103
p.session.Stop()
104104
p.uploader.Stop()
105+
105106
return nil
106107
}
107108

@@ -112,7 +113,7 @@ func (p *Profiler) Flush(wait bool) {
112113

113114
type LabelSet = pprof.LabelSet
114115

115-
var Labels = pprof.Labels
116+
var Labels = pprof.Labels //nolint:gochecknoglobals
116117

117118
func TagWrapper(ctx context.Context, labels LabelSet, cb func(context.Context)) {
118119
pprof.Do(ctx, labels, func(c context.Context) { cb(c) })

api_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package pyroscope
22

33
import (
44
"testing"
5+
6+
"github.com/stretchr/testify/require"
57
)
68

79
func TestProfilerStartStop(t *testing.T) {
810
profiler, err := Start(Config{
911
ApplicationName: "test",
1012
})
11-
if err != nil {
12-
t.Fatal(err)
13-
}
14-
profiler.Stop()
13+
require.NoError(t, err)
14+
err = profiler.Stop()
15+
require.NoError(t, err)
1516
}

collector.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package pyroscope
22

33
import (
44
"bytes"
5-
"fmt"
5+
"errors"
66
"io"
77
"time"
88

@@ -50,12 +50,14 @@ func newEvent(typ eventType) event {
5050

5151
func (e event) send(c chan<- event) error {
5252
c <- e
53+
5354
return <-e.done
5455
}
5556

5657
func newStartEvent(w io.Writer) event {
5758
e := newEvent(startEvent)
5859
e.w = w
60+
5961
return e
6062
}
6163

@@ -66,6 +68,7 @@ func newCPUProfileCollector(
6668
period time.Duration,
6769
) *cpuProfileCollector {
6870
buf := bytes.NewBuffer(make([]byte, 0, 1<<10))
71+
6972
return &cpuProfileCollector{
7073
name: name,
7174
dur: period,
@@ -105,6 +108,7 @@ func (c *cpuProfileCollector) Start() {
105108
d = c.dur
106109
}
107110
t.Reset(d)
111+
108112
continue
109113
}
110114
t.Reset(c.dur)
@@ -125,6 +129,7 @@ func (c *cpuProfileCollector) Start() {
125129
c.collector.StopCPUProfile()
126130
c.upload()
127131
close(c.done)
132+
128133
return
129134

130135
case e := <-c.events:
@@ -145,7 +150,7 @@ func (c *cpuProfileCollector) handleEvent(e event) {
145150
if c.started { // Misuse.
146151
// Just to avoid interruption of the background
147152
// profiling that will fail immediately.
148-
err = fmt.Errorf("cpu profiling already started")
153+
err = errAlreadyStarted
149154
} else {
150155
err = c.reset(e.w)
151156
c.started = err == nil
@@ -161,7 +166,7 @@ func (c *cpuProfileCollector) handleEvent(e event) {
161166
if c.started {
162167
// Flush can't be done if StartCPUProfile is called,
163168
// as we'd need stopping the foreground collector first.
164-
err = fmt.Errorf("flush rejected: cpu profiling is in progress")
169+
err = errFlushRejected
165170
} else {
166171
err = c.reset(nil)
167172
}
@@ -184,6 +189,7 @@ func (c *cpuProfileCollector) Stop() {
184189

185190
func (c *cpuProfileCollector) StartCPUProfile(w io.Writer) error {
186191
c.logger.Debugf("cpu profile collector interrupted with StartCPUProfile")
192+
187193
return newStartEvent(w).send(c.events)
188194
}
189195

@@ -206,12 +212,15 @@ func (c *cpuProfileCollector) reset(w io.Writer) error {
206212
d = io.MultiWriter(d, w)
207213
}
208214
c.timeStarted = time.Now()
215+
209216
if err := c.collector.StartCPUProfile(d); err != nil {
210217
c.logger.Errorf("failed to start CPU profiling: %v", err)
211218
c.timeStarted = time.Time{}
212219
c.buf.Reset()
220+
213221
return err
214222
}
223+
215224
return nil
216225
}
217226

@@ -236,3 +245,8 @@ func (c *cpuProfileCollector) upload() {
236245
})
237246
c.buf.Reset()
238247
}
248+
249+
var (
250+
errAlreadyStarted = errors.New("cpu profiling already started")
251+
errFlushRejected = errors.New("flush rejected: cpu profiling is in progress")
252+
)

collector_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func Test_StartCPUProfile_interrupts_background_profiling(t *testing.T) {
2929
// Try to interrupt it with StartCPUProfile.
3030
start := collector.waitStartCPUProfile()
3131
stop := collector.waitStopCPUProfile()
32+
3233
if err := c.StartCPUProfile(io.Discard); err != nil {
3334
t.Fatal("failed to start CPU profiling")
3435
}
@@ -77,6 +78,7 @@ func Test_StartCPUProfile_blocks_Stop(t *testing.T) {
7778
// Try to interrupt it with StartCPUProfile.
7879
start := collector.waitStartCPUProfile()
7980
stop := collector.waitStopCPUProfile()
81+
8082
if err := c.StartCPUProfile(io.Discard); err != nil {
8183
t.Fatal("failed to start CPU profiling")
8284
}
@@ -102,6 +104,7 @@ func Test_StartCPUProfile_blocks_Stop(t *testing.T) {
102104

103105
type mockCollector struct {
104106
sync.Mutex
107+
105108
start chan struct{}
106109
stop chan struct{}
107110
}
@@ -111,6 +114,7 @@ func (m *mockCollector) waitStartCPUProfile() <-chan struct{} {
111114
c := make(chan struct{})
112115
m.start = c
113116
m.Unlock()
117+
114118
return c
115119
}
116120

@@ -119,6 +123,7 @@ func (m *mockCollector) waitStopCPUProfile() <-chan struct{} {
119123
c := make(chan struct{})
120124
m.stop = c
121125
m.Unlock()
126+
122127
return c
123128
}
124129

@@ -129,6 +134,7 @@ func (m *mockCollector) StartCPUProfile(_ io.Writer) error {
129134
m.start = nil
130135
}
131136
m.Unlock()
137+
132138
return nil
133139
}
134140

0 commit comments

Comments
 (0)