Skip to content

Commit

Permalink
Add golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacome committed Dec 3, 2024
1 parent 3acce45 commit fd07b8d
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 20 deletions.
90 changes: 90 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
linters-settings:
misspell:
locale: US
revive:
ignore-generated-header: true
rules:
- name: blank-imports
- name: context-as-argument
- name: context-keys-type
- name: dot-imports
- name: empty-block
- name: error-naming
- name: error-return
- name: error-strings
- name: errorf
- name: exported
- name: increment-decrement
- name: indent-error-flow
- name: package-comments
- name: range
- name: receiver-naming
- name: redefines-builtin-id
- name: superfluous-else
- name: time-naming
- name: unexported-return
- name: unreachable-code
- name: var-declaration
- name: var-naming
govet:
enable-all: true
linters:
enable:
- asasalint
- asciicheck
- bidichk
- contextcheck
- copyloopvar
- dupword
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- fatcontext
- forcetypeassert
- gocheckcompilerdirectives
- gochecksumtype
- gocritic
- godot
- gofmt
- gofumpt
- goimports
- gosec
- gosimple
- gosmopolitan
- govet
- ineffassign
- intrange
- makezero
- misspell
- musttag
- nilerr
- noctx
- nolintlint
- paralleltest
- perfsprint
- prealloc
- predeclared
- reassign
- revive
- staticcheck
- stylecheck
- tagalign
- tenv
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- wastedassign
- whitespace
# - wrapcheck
disable-all: true
issues:
max-issues-per-linter: 0
max-same-issues: 0
run:
timeout: 5m
6 changes: 4 additions & 2 deletions ginhttp/server.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build go1.7
// +build go1.7

// This is the middleware from github.com/opentracing-contrib/go-stdlib
Expand Down Expand Up @@ -63,7 +64,8 @@ func MWURLTagFunc(f func(u *url.URL) string) MWOption {
}

// Middleware is a gin native version of the equivalent middleware in:
// https://github.com/opentracing-contrib/go-stdlib/
//
// https://github.com/opentracing-contrib/go-stdlib/
func Middleware(tr opentracing.Tracer, options ...MWOption) gin.HandlerFunc {
opts := mwOptions{
opNameFunc: func(r *http.Request) string {
Expand Down Expand Up @@ -98,7 +100,7 @@ func Middleware(tr opentracing.Tracer, options ...MWOption) gin.HandlerFunc {

c.Next()

ext.HTTPStatusCode.Set(sp, uint16(c.Writer.Status()))
ext.HTTPStatusCode.Set(sp, uint16(c.Writer.Status())) //nolint:gosec // can't have integer overflow with status code
sp.Finish()
}
}
58 changes: 40 additions & 18 deletions ginhttp/server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ginhttp

import (
"context"
"net/http"
"net/http/httptest"
"net/url"
Expand All @@ -12,33 +13,40 @@ import (
)

func TestOperationNameOption(t *testing.T) {

t.Parallel()
fn := func(r *http.Request) string {
return "HTTP " + r.Method + ": /root"
}

tests := []struct {
options []MWOption
opName string
options []MWOption
}{
{nil, "HTTP GET"},
{[]MWOption{OperationNameFunc(fn)}, "HTTP GET: /root"},
{"HTTP GET", nil},
{"HTTP GET: /root", []MWOption{OperationNameFunc(fn)}},
}

for _, tt := range tests {
testCase := tt
t.Run(testCase.opName, func(t *testing.T) {
t.Parallel()
tr := &mocktracer.MockTracer{}
mw := Middleware(tr, testCase.options...)
r := gin.New()
r.Use(mw)
srv := httptest.NewServer(r)
defer srv.Close()

_, err := http.Get(srv.URL)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL, nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}

resp, err := srv.Client().Do(req)
if err != nil {
t.Fatalf("server returned error: %v", err)
}
defer resp.Body.Close()

spans := tr.FinishedSpans()
if got, want := len(spans), 1; got != want {
Expand All @@ -53,7 +61,7 @@ func TestOperationNameOption(t *testing.T) {
}

func TestSpanObserverOption(t *testing.T) {

t.Parallel()
opNamefn := func(r *http.Request) string {
return "HTTP " + r.Method + ": /root"
}
Expand All @@ -63,30 +71,37 @@ func TestSpanObserverOption(t *testing.T) {
wantTags := map[string]interface{}{"http.uri": "/"}

tests := []struct {
options []MWOption
opName string
Tags map[string]interface{}
opName string
options []MWOption
}{
{nil, "HTTP GET", nil},
{[]MWOption{OperationNameFunc(opNamefn)}, "HTTP GET: /root", nil},
{[]MWOption{MWSpanObserver(spanObserverfn)}, "HTTP GET", wantTags},
{[]MWOption{OperationNameFunc(opNamefn), MWSpanObserver(spanObserverfn)}, "HTTP GET: /root", wantTags},
{nil, "HTTP GET: /root", []MWOption{OperationNameFunc(opNamefn)}},
{wantTags, "HTTP GET", []MWOption{MWSpanObserver(spanObserverfn)}},
{wantTags, "HTTP GET: /root", []MWOption{OperationNameFunc(opNamefn), MWSpanObserver(spanObserverfn)}},
}

for _, tt := range tests {
testCase := tt
t.Run(testCase.opName, func(t *testing.T) {
t.Parallel()
tr := &mocktracer.MockTracer{}
mw := Middleware(tr, testCase.options...)
r := gin.New()
r.Use(mw)
srv := httptest.NewServer(r)
defer srv.Close()

_, err := http.Get(srv.URL)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL, nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}

resp, err := srv.Client().Do(req)
if err != nil {
t.Fatalf("server returned error: %v", err)
}
defer resp.Body.Close()

spans := tr.FinishedSpans()
if got, want := len(spans), 1; got != want {
Expand All @@ -102,7 +117,7 @@ func TestSpanObserverOption(t *testing.T) {
t.Fatalf("got tag length %d, expected %d", len(spans[0].Tags()), len(testCase.Tags))
}
for k, v := range testCase.Tags {
if tag := spans[0].Tag(k); v != tag.(string) {
if tag, ok := spans[0].Tag(k).(string); !ok || v != tag {
t.Fatalf("got %v tag, expected %v", tag, v)
}
}
Expand All @@ -111,35 +126,42 @@ func TestSpanObserverOption(t *testing.T) {
}

func TestURLTagOption(t *testing.T) {

t.Parallel()
fn := func(u *url.URL) string {
// Log path only (no query parameters etc)
return u.Path
}

tests := []struct {
options []MWOption
url string
tag string
options []MWOption
}{
{[]MWOption{}, "/root?token=123", "/root?token=123"},
{[]MWOption{MWURLTagFunc(fn)}, "/root?token=123", "/root"},
{"/root?token=123", "/root?token=123", []MWOption{}},
{"/root?token=123", "/root", []MWOption{MWURLTagFunc(fn)}},
}

for _, tt := range tests {
testCase := tt
t.Run(testCase.tag, func(t *testing.T) {
t.Parallel()
tr := &mocktracer.MockTracer{}
mw := Middleware(tr, testCase.options...)
r := gin.New()
r.Use(mw)
srv := httptest.NewServer(r)
defer srv.Close()

_, err := http.Get(srv.URL + testCase.url)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL+testCase.url, nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}

resp, err := srv.Client().Do(req)
if err != nil {
t.Fatalf("server returned error: %v", err)
}
defer resp.Body.Close()

spans := tr.FinishedSpans()
if got, want := len(spans), 1; got != want {
Expand Down

0 comments on commit fd07b8d

Please sign in to comment.