Skip to content

Commit 371dff5

Browse files
committed
Add golangci-lint
1 parent f79ab66 commit 371dff5

File tree

4 files changed

+188
-44
lines changed

4 files changed

+188
-44
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: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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: unused-parameter
28+
- name: var-declaration
29+
- name: var-naming
30+
govet:
31+
enable-all: true
32+
linters:
33+
enable:
34+
- asasalint
35+
- asciicheck
36+
- bidichk
37+
# - contextcheck
38+
- copyloopvar
39+
- dupword
40+
- durationcheck
41+
- errcheck
42+
- errchkjson
43+
- errname
44+
- errorlint
45+
- fatcontext
46+
- forcetypeassert
47+
- gocheckcompilerdirectives
48+
- gochecksumtype
49+
- gocritic
50+
- godot
51+
- gofmt
52+
- gofumpt
53+
- goimports
54+
- gosec
55+
- gosimple
56+
- gosmopolitan
57+
- govet
58+
- ineffassign
59+
- intrange
60+
- makezero
61+
- misspell
62+
- musttag
63+
- nilerr
64+
- noctx
65+
- nolintlint
66+
- paralleltest
67+
- perfsprint
68+
- prealloc
69+
- predeclared
70+
- reassign
71+
- revive
72+
- staticcheck
73+
- stylecheck
74+
- tagalign
75+
- thelper
76+
- tparallel
77+
- typecheck
78+
- unconvert
79+
- unparam
80+
- unused
81+
- usestdlibvars
82+
- usetesting
83+
- wastedassign
84+
- whitespace
85+
# - wrapcheck
86+
disable-all: true
87+
issues:
88+
max-issues-per-linter: 0
89+
max-same-issues: 0
90+
run:
91+
timeout: 5m

README.md

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,61 @@
11
# echo
2-
a middleware for the echov4 web framework to use opentracing
2+
3+
[![CI](https://github.com/opentracing-contrib/echo/actions/workflows/ci.yml/badge.svg)](https://github.com/opentracing-contrib/echo/actions/workflows/ci.yml)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/opentracing-contrib/echo)](https://goreportcard.com/report/github.com/opentracing-contrib/echo)
5+
[![GoDoc](https://godoc.org/github.com/opentracing-contrib/echo?status.svg)](https://pkg.go.dev/github.com/opentracing-contrib/echo)
6+
[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/opentracing-contrib/echo?logo=github&sort=semver)](https://github.com/opentracing-contrib/echo/releases/latest)
7+
8+
A middleware for the echov4 web framework to use OpenTracing
39

410
```go
511
package main
612

713
import (
8-
"github.com/labstack/echo/v4"
9-
apmecho "github.com/opentracing-contrib/echo"
10-
"github.com/opentracing-contrib/echo/examples/tracer"
11-
"github.com/opentracing/opentracing-go"
12-
"net/http"
13-
"os"
14+
"github.com/labstack/echo/v4"
15+
apmecho "github.com/opentracing-contrib/echo"
16+
"github.com/opentracing-contrib/echo/examples/tracer"
17+
"github.com/opentracing/opentracing-go"
18+
"net/http"
19+
"os"
1420
)
1521

1622
const (
17-
DefaultComponentName = "echo-demo"
23+
DefaultComponentName = "echo-demo"
1824
)
1925

2026
func main() {
2127

22-
flag := os.Getenv("JAEGER_ENABLED")
23-
if flag == "true" {
24-
// 1. init tracer
25-
tracer, closer := tracer.Init(DefaultComponentName)
26-
if closer != nil {
27-
defer closer.Close()
28-
}
29-
// 2. ste the global tracer
30-
if tracer != nil {
31-
opentracing.SetGlobalTracer(tracer)
32-
}
33-
}
34-
35-
e := echo.New()
36-
37-
if flag == "true" {
38-
// 3. use the middleware
39-
e.Use(apmecho.Middleware(DefaultComponentName))
40-
}
41-
42-
e.GET("/", func(c echo.Context) error {
43-
return c.String(http.StatusOK, "Hello, World!")
44-
})
45-
46-
e.Logger.Fatal(e.Start(":1323"))
28+
flag := os.Getenv("JAEGER_ENABLED")
29+
if flag == "true" {
30+
// 1. init tracer
31+
tracer, closer := tracer.Init(DefaultComponentName)
32+
if closer != nil {
33+
defer closer.Close()
34+
}
35+
// 2. ste the global tracer
36+
if tracer != nil {
37+
opentracing.SetGlobalTracer(tracer)
38+
}
39+
}
40+
41+
e := echo.New()
42+
43+
if flag == "true" {
44+
// 3. use the middleware
45+
e.Use(apmecho.Middleware(DefaultComponentName))
46+
}
47+
48+
e.GET("/", func(c echo.Context) error {
49+
return c.String(http.StatusOK, "Hello, World!")
50+
})
51+
52+
e.Logger.Fatal(e.Start(":1323"))
4753
}
4854

4955
```
5056

5157
Example: [echo-example](./examples)
5258

53-
![](./examples/imgs/img1.jpg)
59+
![Echo tracing example screenshot 1](./examples/imgs/img1.jpg)
5460

55-
![](./examples/imgs/img2.jpg)
61+
![Echo tracing example screenshot 2](./examples/imgs/img2.jpg)

middleware.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package apm
22

33
import (
4+
"net/http"
5+
"net/url"
6+
47
"github.com/labstack/echo/v4"
58
"github.com/opentracing/opentracing-go"
69
"github.com/opentracing/opentracing-go/ext"
7-
"net/http"
8-
"net/url"
910
)
1011

1112
type mwOptions struct {
@@ -18,17 +19,15 @@ type mwOptions struct {
1819
func Middleware(componentName string) echo.MiddlewareFunc {
1920
return func(next echo.HandlerFunc) echo.HandlerFunc {
2021
return func(c echo.Context) error {
21-
22-
r := c.Request();
22+
r := c.Request()
2323
tracer := opentracing.GlobalTracer()
2424

2525
opts := mwOptions{
2626
componentName: componentName,
2727
opNameFunc: func(r *http.Request) string {
2828
return "HTTP " + r.Method + " " + r.URL.Path
2929
},
30-
spanObserver: func(span opentracing.Span, r *http.Request) {
31-
30+
spanObserver: func(span opentracing.Span, r *http.Request) { //nolint:revive
3231
},
3332
urlTagFunc: func(u *url.URL) string {
3433
return u.String()
@@ -50,7 +49,6 @@ func Middleware(componentName string) echo.MiddlewareFunc {
5049
c.SetRequest(r)
5150

5251
err := tracer.Inject(sp.Context(), opentracing.HTTPHeaders, carrier)
53-
5452
if err != nil {
5553
panic("SpanContext Inject Error!")
5654
}
@@ -61,10 +59,14 @@ func Middleware(componentName string) echo.MiddlewareFunc {
6159
}
6260

6361
sp.SetTag("error", false)
64-
ext.HTTPStatusCode.Set(sp, uint16(c.Response().Status))
62+
if status := c.Response().Status; status >= 0 && status <= 65535 {
63+
ext.HTTPStatusCode.Set(sp, uint16(status))
64+
} else {
65+
// Either use a default value or log the issue
66+
ext.HTTPStatusCode.Set(sp, 0) // Using 0 to indicate invalid status
67+
}
6568

6669
return nil
67-
6870
}
6971
}
7072
}

0 commit comments

Comments
 (0)