Skip to content

Commit a308df0

Browse files
committed
Add golangci-lint
1 parent 7ad4d37 commit a308df0

File tree

8 files changed

+197
-27
lines changed

8 files changed

+197
-27
lines changed

.github/workflows/lint.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Lint
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
defaults:
12+
run:
13+
shell: bash
14+
15+
concurrency:
16+
group: ${{ github.ref_name }}-lint
17+
cancel-in-progress: true
18+
19+
jobs:
20+
lint:
21+
name: Go Lint
22+
runs-on: ubuntu-24.04
23+
steps:
24+
- name: Checkout Repository
25+
uses: actions/checkout@v4
26+
27+
- name: Setup Golang Environment
28+
uses: actions/setup-go@v5
29+
with:
30+
go-version: stable
31+
32+
- name: Lint Go
33+
uses: golangci/golangci-lint-action@v6
34+
35+
actionlint:
36+
name: Actionlint
37+
runs-on: ubuntu-24.04
38+
steps:
39+
- name: Checkout Repository
40+
uses: actions/checkout@v4
41+
42+
- name: Lint Actions
43+
uses: reviewdog/action-actionlint@v1
44+
with:
45+
actionlint_flags: -shellcheck ""

.golangci.yml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
linters-settings:
2+
misspell:
3+
locale: US
4+
revive:
5+
ignore-generated-header: true
6+
rules:
7+
- name: blank-imports
8+
- name: context-as-argument
9+
- name: context-keys-type
10+
- name: dot-imports
11+
- name: empty-block
12+
- name: error-naming
13+
- name: error-return
14+
- name: error-strings
15+
- name: errorf
16+
- name: exported
17+
- name: increment-decrement
18+
- name: indent-error-flow
19+
- name: package-comments
20+
- name: range
21+
- name: receiver-naming
22+
- name: redefines-builtin-id
23+
- name: superfluous-else
24+
- name: time-naming
25+
- name: unexported-return
26+
- name: unreachable-code
27+
- name: var-declaration
28+
- name: var-naming
29+
govet:
30+
enable-all: true
31+
linters:
32+
enable:
33+
- asasalint
34+
- asciicheck
35+
- bidichk
36+
- contextcheck
37+
- copyloopvar
38+
- dupword
39+
- durationcheck
40+
- errcheck
41+
- errchkjson
42+
- errname
43+
- errorlint
44+
- fatcontext
45+
- forcetypeassert
46+
- gocheckcompilerdirectives
47+
- gochecksumtype
48+
- gocritic
49+
- godot
50+
- gofmt
51+
- gofumpt
52+
- goimports
53+
- gosec
54+
- gosimple
55+
- gosmopolitan
56+
- govet
57+
- ineffassign
58+
- intrange
59+
- makezero
60+
- misspell
61+
- musttag
62+
- nilerr
63+
- noctx
64+
- nolintlint
65+
- paralleltest
66+
- perfsprint
67+
- prealloc
68+
- predeclared
69+
- reassign
70+
- revive
71+
- staticcheck
72+
- stylecheck
73+
- tagalign
74+
- thelper
75+
- tparallel
76+
- typecheck
77+
- unconvert
78+
- unparam
79+
- unused
80+
- usestdlibvars
81+
- usetesting
82+
- wastedassign
83+
- whitespace
84+
# - wrapcheck
85+
disable-all: true
86+
issues:
87+
max-issues-per-linter: 0
88+
max-same-issues: 0
89+
run:
90+
timeout: 5m

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ between the producers and the consumers.
6767
)
6868
defer sp.Finish()
6969

70-
// Update the context with the span for the subsequent reference.
70+
// Update the context with the span for the subsequent reference.
7171
ctx = opentracing.ContextWithSpan(ctx, sp)
7272

7373
// Actual message processing.

amqptracer/propagation.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ package amqptracer
44
//
55
// Example usage for server side:
66
//
7-
// carrier := amqpHeadersCarrier(amqp.Table)
8-
// clientContext, err := tracer.Extract(
9-
// opentracing.TextMap,
10-
// carrier)
7+
// carrier := amqpHeadersCarrier(amqp.Table)
8+
// clientContext, err := tracer.Extract(
9+
// opentracing.TextMap,
10+
// carrier)
1111
//
1212
// Example usage for client side:
1313
//
14-
// carrier := amqpHeadersCarrier(amqp.Table)
15-
// err := tracer.Inject(
16-
// span.Context(),
17-
// opentracing.TextMap,
18-
// carrier)
19-
//
14+
// carrier := amqpHeadersCarrier(amqp.Table)
15+
// err := tracer.Inject(
16+
// span.Context(),
17+
// opentracing.TextMap,
18+
// carrier)
2019
type amqpHeadersCarrier map[string]interface{}
2120

2221
// ForeachKey conforms to the TextMapReader interface.

amqptracer/propagation_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@ import (
88
)
99

1010
func TestAMQPHeaderInject(t *testing.T) {
11+
t.Parallel()
1112
h := map[string]interface{}{}
1213
h["NotOT"] = "blah"
1314
h["opname"] = "AlsoNotOT"
1415
tracer := testTracer{}
1516
span := tracer.StartSpan("someSpan")
16-
fakeID := span.Context().(testSpanContext).FakeID
17+
18+
spanCtx, ok := span.Context().(testSpanContext)
19+
if !ok {
20+
t.Fatalf("Expected span.Context() to be of type testSpanContext")
21+
}
22+
fakeID := spanCtx.FakeID
1723

1824
// Use amqpHeadersCarrier to wrap around `h`.
1925
carrier := amqpHeadersCarrier(h)
@@ -32,6 +38,7 @@ func TestAMQPHeaderInject(t *testing.T) {
3238
}
3339

3440
func TestAMQPHeaderExtract(t *testing.T) {
41+
t.Parallel()
3542
h := map[string]interface{}{}
3643
h["NotOT"] = "blah"
3744
h["opname"] = "AlsoNotOT"
@@ -45,7 +52,11 @@ func TestAMQPHeaderExtract(t *testing.T) {
4552
t.Fatal(err)
4653
}
4754

48-
if spanContext.(testSpanContext).FakeID != 42 {
55+
testContext, ok := spanContext.(testSpanContext)
56+
if !ok {
57+
t.Fatalf("Expected spanContext to be of type testSpanContext")
58+
}
59+
if testContext.FakeID != 42 {
4960
t.Errorf("Failed to read testprefix-fakeid correctly")
5061
}
5162
}

amqptracer/testtracer_test.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ type testSpanContext struct {
3030
func (n testSpanContext) ForeachBaggageItem(handler func(k, v string) bool) {}
3131

3232
type testSpan struct {
33-
spanContext testSpanContext
34-
OperationName string
3533
StartTime time.Time
3634
Tags map[string]interface{}
35+
OperationName string
36+
spanContext testSpanContext
3737
}
3838

3939
func (n testSpan) Equal(os opentracing.Span) bool {
@@ -63,7 +63,6 @@ func (n testSpan) Equal(os opentracing.Span) bool {
6363
return true
6464
}
6565

66-
// testSpan:
6766
func (n testSpan) Context() opentracing.SpanContext { return n.spanContext }
6867
func (n testSpan) SetTag(key string, value interface{}) opentracing.Span { return n }
6968
func (n testSpan) Finish() {}
@@ -90,7 +89,9 @@ func (n testTracer) StartSpan(operationName string, opts ...opentracing.StartSpa
9089
func (n testTracer) startSpanWithOptions(name string, opts opentracing.StartSpanOptions) opentracing.Span {
9190
fakeID := nextFakeID()
9291
if len(opts.References) > 0 {
93-
fakeID = opts.References[0].ReferencedContext.(testSpanContext).FakeID
92+
if ctx, ok := opts.References[0].ReferencedContext.(testSpanContext); ok {
93+
fakeID = ctx.FakeID
94+
}
9495
}
9596

9697
return testSpan{
@@ -106,25 +107,37 @@ func (n testTracer) startSpanWithOptions(name string, opts opentracing.StartSpan
106107

107108
// Inject belongs to the Tracer interface.
108109
func (n testTracer) Inject(sp opentracing.SpanContext, format interface{}, carrier interface{}) error {
109-
spanContext := sp.(testSpanContext)
110+
spanContext, ok := sp.(testSpanContext)
111+
if !ok {
112+
return opentracing.ErrInvalidSpanContext
113+
}
114+
110115
switch format {
111116
case opentracing.HTTPHeaders, opentracing.TextMap:
112-
carrier.(opentracing.TextMapWriter).Set(testHTTPHeaderPrefix+"fakeid", strconv.Itoa(spanContext.FakeID))
117+
writer, ok := carrier.(opentracing.TextMapWriter)
118+
if !ok {
119+
return opentracing.ErrInvalidCarrier
120+
}
121+
writer.Set(testHTTPHeaderPrefix+"fakeid", strconv.Itoa(spanContext.FakeID))
113122
return nil
114123
}
115124
return opentracing.ErrUnsupportedFormat
116125
}
117126

118127
// Extract belongs to the Tracer interface.
119128
func (n testTracer) Extract(format interface{}, carrier interface{}) (opentracing.SpanContext, error) {
120-
switch format {
121-
case opentracing.HTTPHeaders, opentracing.TextMap:
129+
if format == opentracing.HTTPHeaders || format == opentracing.TextMap {
122130
// Just for testing purposes... generally not a worthwhile thing to
123131
// propagate.
124132
sm := testSpanContext{}
125-
err := carrier.(opentracing.TextMapReader).ForeachKey(func(key, val string) error {
126-
switch strings.ToLower(key) {
127-
case testHTTPHeaderPrefix + "fakeid":
133+
reader, ok := carrier.(opentracing.TextMapReader)
134+
if !ok {
135+
return nil, opentracing.ErrInvalidCarrier
136+
}
137+
138+
err := reader.ForeachKey(func(key, val string) error {
139+
lowerKey := strings.ToLower(key)
140+
if lowerKey == testHTTPHeaderPrefix+"fakeid" {
128141
i, err := strconv.Atoi(val)
129142
if err != nil {
130143
return err

amqptracer/tracer_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@ import (
88
)
99

1010
func TestInject(t *testing.T) {
11+
t.Parallel()
1112
h := map[string]interface{}{}
1213
h["NotOT"] = "blah"
1314
h["opname"] = "AlsoNotOT"
1415
tracer := testTracer{}
1516
sp := tracer.StartSpan("someSpan")
16-
fakeID := sp.Context().(testSpanContext).FakeID
17+
18+
spanCtx, ok := sp.Context().(testSpanContext)
19+
if !ok {
20+
t.Fatalf("Expected sp.Context() to be of type testSpanContext")
21+
}
22+
fakeID := spanCtx.FakeID
1723

1824
// Inject the tracing context to the AMQP header.
1925
if err := Inject(sp, h); err != nil {
@@ -31,6 +37,7 @@ func TestInject(t *testing.T) {
3137
}
3238

3339
func TestExtract(t *testing.T) {
40+
t.Parallel()
3441
h := map[string]interface{}{}
3542
h["NotOT"] = "blah"
3643
h["opname"] = "AlsoNotOT"
@@ -44,7 +51,12 @@ func TestExtract(t *testing.T) {
4451
if err != nil {
4552
t.Fatal(err)
4653
}
47-
if ctx.(testSpanContext).FakeID != 42 {
54+
55+
testCtx, ok := ctx.(testSpanContext)
56+
if !ok {
57+
t.Fatalf("Expected ctx to be of type testSpanContext")
58+
}
59+
if testCtx.FakeID != 42 {
4860
t.Errorf("Failed to read testprefix-fakeid correctly")
4961
}
5062
}

renovate.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
33
"extends": [
44
"github>opentracing-contrib/common",
5-
"schedule:daily"
5+
"schedule:weekly"
66
]
77
}

0 commit comments

Comments
 (0)