Compare commits

...

39 Commits

Author SHA1 Message Date
b42fcbc4b8 add bunrouterhelper; 2023-10-31 10:49:03 +08:00
91a82b5f48 add httpdata; 2023-10-31 10:21:45 +08:00
c2bf543f0a exegroup/修改HTTPListenAndServe支持tls并删除HTTPServer; 2023-06-28 10:54:39 +08:00
c82211c957 exegroup/HTTPListenAndServe移动到eghttp并增加HTTPServer; 2023-06-27 11:13:24 +08:00
a698a8135d log/删除logger.Logger.SetExit方法; 2023-06-16 14:29:39 +08:00
7bb09785f2 log/Panic改回调用panic; 2023-05-06 14:54:36 +08:00
a8534b8f5a log/细化退出函数, Panic不再调用panic, 调用栈排除runtime.main和之后的Frame; 2023-04-25 23:40:13 +08:00
c4439e938b logotel/WithDefaultSpan 设置当 span 没有在记录时创建新 span; 2023-04-25 16:50:29 +08:00
d83a012967 log/新增ParseLevel; 2023-04-25 15:03:33 +08:00
850b678c13 log/扩展Exit功能; 2023-04-25 13:17:10 +08:00
66e4001f0e cmd/retry/基于golangci-lint修改代码; 2023-04-14 23:37:56 +08:00
b5095448fc cmd/http-reflect-server/基于golangci-lint修改代码并且升级依赖; 2023-04-14 23:33:55 +08:00
13de8b9b6c wireexample/基于golangci-lint修改代码; 2023-04-14 23:28:06 +08:00
046bd80bf9 uptracehelper/基于golangci-lint修改代码; 2023-04-14 23:25:29 +08:00
0ad011081f timehelper/基于golangci-lint修改代码; 2023-04-14 23:22:39 +08:00
b483650651 synchelper/基于golangci-lint修改代码; 2023-04-14 23:18:56 +08:00
1467db05fb runtimehelper/基于golangci-lint修改代码; 2023-04-14 23:17:19 +08:00
6f50b2e715 logotel/基于golangci-lint修改代码; 2023-04-14 23:11:37 +08:00
6fd7badcb7 contexthelper/基于golangci-lint修改代码; 2023-04-14 23:03:59 +08:00
7c6fc1210c concurrentsafemapset/基于golangci-lint修改代码; 2023-04-14 23:00:54 +08:00
d9e5a3371d mapset/基于golangci-lint修改代码; 2023-04-14 22:57:38 +08:00
35ddffe6c6 exegroup/基于golangci-lint修改代码; 2023-04-14 22:27:29 +08:00
25bea8ca37 log/基于golangci-lint修改代码; 2023-04-14 19:35:21 +08:00
1cdffbd6ea logotel/依赖库log升级到v0.8.0; 2023-04-14 00:11:54 +08:00
b0180865fc log/EntryProcessor.Process新增ctx参数并且把ReadonlyEntry中的Context移除; 2023-04-14 00:10:13 +08:00
0f50bc0e73 logotel/依赖库log升级到v0.7.0; 2023-04-13 18:57:59 +08:00
216f3a1012 log/新增Logger.*ReportStackLevel; 2023-04-13 18:54:34 +08:00
f58f914826 log/升级对log的依赖; 2023-04-12 17:29:19 +08:00
f21f93b713 log/logjson提供NewSyncWriter和NewBytesBufferPool; 2023-04-12 17:25:07 +08:00
1f39ef8c3a log/删除所有的依赖库; 2023-04-12 17:09:05 +08:00
4e1fe211cb cmd/http-reflect-server/initialization; 2023-04-12 15:16:03 +08:00
8748299a96 log/修改 Field; 2023-04-12 15:07:11 +08:00
d12ff23ff9 uptracehelper/add Setup; rename InitTracer -> GoStop; 2023-04-10 15:21:30 +08:00
f032b6fbb3 logotel/initialization; 2023-04-10 14:27:32 +08:00
01792c8295 log/修复WithBufferPool; 2023-04-10 13:56:55 +08:00
1840681201 log/修改包名 jsonlog->logjson; 修改 logjson.config 的初始化方式; 2023-04-10 13:50:17 +08:00
625f12a793 修改注释中的错字; 2023-04-07 23:30:47 +08:00
179e00910d add log; 2023-04-07 22:51:52 +08:00
e484aaa1f9 add synchelper; 2023-04-07 22:41:19 +08:00
63 changed files with 3931 additions and 57 deletions

View File

@@ -0,0 +1,141 @@
## 基于 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- prealloc
- predeclared
- promlinter
- reassign
- revive
- rowserrcheck
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
rowserrcheck:
packages:
- github.com/jmoiron/sqlx
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

9
bunrouterhelper/go.mod Normal file
View File

@@ -0,0 +1,9 @@
module git.blauwelle.com/go/crate/bunrouterhelper
go 1.21.1
require (
git.blauwelle.com/go/crate/httpdata v0.0.1
git.blauwelle.com/go/crate/log v0.14.0
github.com/uptrace/bunrouter v1.0.20
)

14
bunrouterhelper/go.sum Normal file
View File

@@ -0,0 +1,14 @@
git.blauwelle.com/go/crate/httpdata v0.0.1 h1:kje/L7B1rkzxPKEURqORVXjxiCR2F90oVBTwzmdimDY=
git.blauwelle.com/go/crate/httpdata v0.0.1/go.mod h1:/NrE0so/hjc2Djj850I5iBM+Juzc4Cv48V74zj2tALE=
git.blauwelle.com/go/crate/log v0.14.0 h1:y7hJXP+ZPY/wD+wlEzKgakpki8/l0LwZWqxtJ92Wy58=
git.blauwelle.com/go/crate/log v0.14.0/go.mod h1:jfVfpRODZTA70A8IkApVeGsS1zfLk1D77sLWZM/w+L0=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/uptrace/bunrouter v1.0.20 h1:jNvYNcJxF+lSYBQAaQjnE6I11Zs0m+3M5Ek7fq/Tp4c=
github.com/uptrace/bunrouter v1.0.20/go.mod h1:TwT7Bc0ztF2Z2q/ZzMuSVkcb/Ig/d3MQeP2cxn3e1hI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -0,0 +1,48 @@
package bunrouterhelper
import (
"errors"
"fmt"
"net/http"
"git.blauwelle.com/go/crate/httpdata"
"git.blauwelle.com/go/crate/log"
"github.com/uptrace/bunrouter"
)
func MiddlewareRecover() bunrouter.MiddlewareFunc {
return func(next bunrouter.HandlerFunc) bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) (err error) {
defer func() {
if r := recover(); r != nil {
log.Error(req.Context(), r)
err = httpdata.NewUniverseError(httpdata.CodeUnexpectPanic, fmt.Sprintf("%v", r))
}
}()
return next(w, req)
}
}
}
func MiddlewareErrorHandler() bunrouter.MiddlewareFunc {
return func(next bunrouter.HandlerFunc) bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
err := next(w, req)
if err != nil {
var universeError httpdata.UniverseError
if errors.As(err, &universeError) {
_ = bunrouter.JSON(w, httpdata.Response{
Code: universeError.Code,
Message: universeError.Message,
})
} else {
_ = bunrouter.JSON(w, httpdata.Response{
Code: httpdata.CodeUnknown,
Message: err.Error(),
})
}
}
return err
}
}
}

View File

@@ -0,0 +1,138 @@
## 更新到 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

View File

@@ -0,0 +1,8 @@
module git.blauwelle.com/go/crate/cmd/http-reflect-server
go 1.20
require (
git.blauwelle.com/go/crate/exegroup v0.4.0
git.blauwelle.com/go/crate/log v0.9.0
)

View File

@@ -0,0 +1,4 @@
git.blauwelle.com/go/crate/exegroup v0.4.0 h1:hr9vhYDL+LidvoEBCabdUZ22oekUq0s2NK69tklb42g=
git.blauwelle.com/go/crate/exegroup v0.4.0/go.mod h1:DJoID54YI5WFHGHoTCjBao8oS3HFRzwbWMZW6P57AIQ=
git.blauwelle.com/go/crate/log v0.9.0 h1:H01AQIKcYybeCZGdReBzMoWhkXPQJAoY1t+K0J1asEk=
git.blauwelle.com/go/crate/log v0.9.0/go.mod h1:jfVfpRODZTA70A8IkApVeGsS1zfLk1D77sLWZM/w+L0=

View File

@@ -0,0 +1,160 @@
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"git.blauwelle.com/go/crate/log"
)
const (
maxParseMemory = 16 * 1024 * 1024
)
func newHandler() http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
start := time.Now()
var response Response
response.Request.TransferEncoding = r.TransferEncoding
response.Request.Proto = r.Proto
response.Request.Host = r.Host
response.Request.Method = r.Method
response.Request.URL = r.URL.String()
response.Request.RemoteAddr = r.RemoteAddr
response.Request.RequestURI = r.RequestURI
response.Request.Header = r.Header
response.Request.ContentLength = r.ContentLength
ctx := r.Context()
duration := time.Since(start)
code := http.StatusOK
var message string
if r.ContentLength > 0 {
if body, err := readBody(ctx, r); err != nil {
response.Error = err.Error()
} else {
response.Request.Body = body
}
}
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(code)
end := time.Now()
response.ServeDuration = end.Sub(start).String()
encoder := json.NewEncoder(rw)
encoder.SetEscapeHTML(true)
encoder.SetIndent("", " ")
err := encoder.Encode(response)
if err != nil {
log.Errorf(ctx, "json marshal: %s", err.Error())
}
log.WithFields(
log.Field("code", code),
log.Field("duration", duration.String()),
).Info(ctx, message)
}
}
type Response struct {
Error string `json:"error,omitempty"`
ServeDuration string `json:"serveDuration"`
Request ResponseRequest `json:"request"`
}
type MultipartFormFileInfo struct {
MIMEHeader map[string][]string `json:"mimeHeader"`
Filename string `json:"filename"`
Size int64 `json:"size"`
}
type MultipartForm struct {
Values map[string][]string `json:"values"`
Files map[string][]MultipartFormFileInfo `json:"files"`
}
type ResponseRequest struct {
Header http.Header `json:"header"`
Body any `json:"body"`
Proto string `json:"proto"`
Host string `json:"host"`
Method string `json:"method"`
URL string `json:"url"`
RemoteAddr string `json:"remoteAddr"`
RequestURI string `json:"requestUri"`
TransferEncoding []string `json:"transferEncoding"`
ContentLength int64 `json:"contentLength"`
}
//nolint:cyclop
func readBody(ctx context.Context, r *http.Request) (any, error) {
contentType := r.Header.Get("Content-Type")
switch {
case contentType == "":
return nil, nil
case strings.HasPrefix(contentType, "application/json"):
b, err := io.ReadAll(r.Body)
if err != nil {
err = fmt.Errorf("read: %w", err)
log.Error(ctx, err.Error())
return nil, err
}
if err := json.Unmarshal(b, new(any)); err != nil {
log.Error(ctx, err.Error())
return nil, err
}
return json.RawMessage(b), nil
case contentType == "application/x-www-form-urlencoded":
fallthrough
case strings.HasPrefix(contentType, "application/xml"):
fallthrough
case strings.HasPrefix(contentType, "text/"):
b, err := io.ReadAll(r.Body)
if err != nil {
panic(err)
}
return string(b), nil
//case :
case strings.HasPrefix(contentType, "multipart/form-data"):
if err := r.ParseMultipartForm(maxParseMemory); err != nil {
log.Error(ctx, err.Error())
return nil, err
}
body := MultipartForm{
Values: make(map[string][]string),
Files: make(map[string][]MultipartFormFileInfo),
}
body.Values = r.MultipartForm.Value
for k, fs := range r.MultipartForm.File {
for _, f := range fs {
body.Files[k] = append(body.Files[k], MultipartFormFileInfo{
Filename: f.Filename,
MIMEHeader: f.Header,
Size: f.Size,
})
}
}
return body, nil
}
b, err := io.ReadAll(r.Body)
if err != nil {
err = fmt.Errorf("read: %w", err)
log.Error(ctx, err.Error())
return nil, err
}
const maxBodyByteSizeToReturn = 96
if len(b) > maxBodyByteSizeToReturn {
b = b[:maxBodyByteSizeToReturn]
}
return b, nil
}

View File

@@ -0,0 +1,26 @@
package main
import (
"context"
"flag"
"net/http"
"git.blauwelle.com/go/crate/exegroup"
"git.blauwelle.com/go/crate/log"
"git.blauwelle.com/go/crate/log/logsdk"
"git.blauwelle.com/go/crate/log/logsdk/logjson"
)
var port = flag.Int("port", 8080, "HTTP Port") //nolint:gomnd
func main() {
flag.Parse()
log.Logger().AddProcessor(logsdk.AllLevels, logjson.New())
g := exegroup.Default()
mux := http.NewServeMux()
var handler http.Handler = mux
mux.HandleFunc("/", newHandler())
g.New().WithGoStop(exegroup.HTTPListenAndServe(*port, handler))
log.Infof(context.Background(), "listening %d", *port)
log.Error(context.Background(), "exit: ", g.Run(context.Background()))
}

138
cmd/retry/.golangci.yaml Normal file
View File

@@ -0,0 +1,138 @@
## 更新到 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

View File

@@ -14,7 +14,7 @@ var (
)
func main() {
flag.IntVar(&count, "c", 5, "maximum execution times")
flag.IntVar(&count, "c", 5, "maximum execution times") //nolint:gomnd
flag.DurationVar(&interval, "i", time.Second, "retry interval")
flag.Parse()
if count < 1 {
@@ -34,7 +34,7 @@ func main() {
fmt.Printf("retry %d after %s...\n", i, interval)
time.Sleep(interval)
}
cmd := exec.Command(args[0], args[1:]...)
cmd := exec.Command(args[0], args[1:]...) // #nosec G204
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()

View File

@@ -0,0 +1,138 @@
## 更新到 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

View File

@@ -0,0 +1,138 @@
## 更新到 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

View File

@@ -8,6 +8,7 @@ func WithNoCancel(ctx context.Context) context.Context {
return &noCancelCtx{Context: ctx}
}
//nolint:containedctx
type noCancelCtx struct {
context.Context
}

141
exegroup/.golangci.yaml Normal file
View File

@@ -0,0 +1,141 @@
## 基于 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- prealloc
- predeclared
- promlinter
- reassign
- revive
- rowserrcheck
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
rowserrcheck:
packages:
- github.com/jmoiron/sqlx
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

95
exegroup/eghttp/actor.go Normal file
View File

@@ -0,0 +1,95 @@
package eghttp
import (
"context"
"net"
"net/http"
"strconv"
"sync/atomic"
"time"
)
const (
DefaultAddr = ":8080"
DefaultReadHeaderTimeout = time.Second
)
type Option interface {
apply(cfg *config)
}
func WithPort(port int) Option {
return optionFunc(func(cfg *config) {
cfg.server.Addr = net.JoinHostPort("", strconv.Itoa(port))
})
}
func WithHandler(handler http.Handler) Option {
return optionFunc(func(cfg *config) {
cfg.server.Handler = handler
})
}
func WithServer(server *http.Server) Option {
return optionFunc(func(cfg *config) {
cfg.server = server
})
}
func WithStartFn(fn func(server *http.Server) error) Option {
return optionFunc(func(cfg *config) {
cfg.startFn = fn
})
}
// WithServerOption 使用 fn 配置 [http.Server];
func WithServerOption(fn func(server *http.Server)) Option {
return optionFunc(func(cfg *config) {
fn(cfg.server)
})
}
type config struct {
server *http.Server
startFn func(server *http.Server) error
}
func newDefaultConfig() *config {
return &config{
server: &http.Server{
Addr: DefaultAddr,
ReadHeaderTimeout: DefaultReadHeaderTimeout,
},
startFn: func(server *http.Server) error {
return server.ListenAndServe()
},
}
}
type optionFunc func(cfg *config)
func (fn optionFunc) apply(cfg *config) {
fn(cfg)
}
// HTTPListenAndServe 创建 [http.Server] 并提供启动和停止函数;
func HTTPListenAndServe(opts ...Option) (func(ctx context.Context) error, func(ctx context.Context)) {
cfg := newDefaultConfig()
for _, opt := range opts {
opt.apply(cfg)
}
inShutdown := &atomic.Bool{}
c := make(chan error, 1)
goFunc := func(_ context.Context) error {
err := cfg.startFn(cfg.server)
if inShutdown.Load() {
err = <-c
}
return err
}
stopFunc := func(ctx context.Context) {
inShutdown.Store(true)
c <- cfg.server.Shutdown(ctx)
}
return goFunc, stopFunc
}

View File

@@ -123,8 +123,8 @@ func (g *Group) start(ctx context.Context, c chan error) {
}
}
func (g *Group) wait(c chan error, cancel context.CancelFunc) (err error) {
err = <-c
func (g *Group) wait(c chan error, cancel context.CancelFunc) error {
err := <-c
cancel()
ctx := context.Background()
if g.cfg.stopTimeout > 0 {
@@ -144,10 +144,10 @@ func (g *Group) wait(c chan error, cancel context.CancelFunc) (err error) {
select {
case <-c:
case <-ctx.Done():
return
return err
}
}
return
return err
}
type config struct {

View File

@@ -2,8 +2,9 @@ package exegroup_test
import (
"context"
"git.blauwelle.com/go/crate/exegroup"
"log"
"git.blauwelle.com/go/crate/exegroup"
)
func Example_defaultGroup() {
@@ -13,4 +14,5 @@ func Example_defaultGroup() {
return ctx.Err()
})
log.Println("exit:", g.Run(context.Background()))
// Output:
}

View File

@@ -1,27 +0,0 @@
package exegroup
import (
"context"
"fmt"
"net/http"
"sync/atomic"
)
// HttpListenAndServe 提供 [http.Server] 的启动和停止函数;
func HttpListenAndServe(port int, handler http.Handler) (func(ctx context.Context) error, func(ctx context.Context)) {
server := &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: handler}
inShutdown := &atomic.Bool{}
c := make(chan error, 1)
goFunc := func(_ context.Context) error {
err := server.ListenAndServe()
if inShutdown.Load() {
err = <-c
}
return err
}
stopFunc := func(ctx context.Context) {
inShutdown.Store(true)
c <- server.Shutdown(ctx)
}
return goFunc, stopFunc
}

141
httpdata/.golangci.yaml Normal file
View File

@@ -0,0 +1,141 @@
## 基于 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- prealloc
- predeclared
- promlinter
- reassign
- revive
- rowserrcheck
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
rowserrcheck:
packages:
- github.com/jmoiron/sqlx
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

14
httpdata/code.go Normal file
View File

@@ -0,0 +1,14 @@
package httpdata
type Code string
const (
CodeOK Code = "ok"
CodeError Code = "error"
CodeBadRequest Code = "bad_request"
CodeInternal Code = "internal"
CodeInternalUpstream Code = "internal.upstream"
CodeUnexpect Code = "unexpect"
CodeUnexpectPanic Code = "unexpect.panic"
CodeUnknown Code = "unknown"
)

22
httpdata/data.go Normal file
View File

@@ -0,0 +1,22 @@
package httpdata
type Response struct {
Data any `json:"data,omitempty"`
Code Code `json:"code"`
Message string `json:"message,omitempty"`
}
type PageData struct {
List []any `json:"list"`
PageIndex int `json:"pageIndex"` // >=1
PageSize int `json:"pageSize"` // >=1
Total int `json:"total"` // maybe 0
}
func NewOkResponse(data any) Response {
return Response{
Code: CodeOK,
Message: "",
Data: data,
}
}

42
httpdata/error.go Normal file
View File

@@ -0,0 +1,42 @@
package httpdata
import (
"errors"
)
type UniverseError struct {
Code Code
Message string
}
func NewUniverseError(code Code, message string) error {
return UniverseError{
Code: code,
Message: message,
}
}
func NewBadRequestError(message string) error {
return UniverseError{
Code: CodeBadRequest,
Message: message,
}
}
func (err UniverseError) Error() string {
if err.Message == "" {
return string(err.Code)
}
return string(err.Code) + ": " + err.Message
}
func IsUniverseError(err error) bool {
return errors.As(err, &UniverseError{})
}
func ToUniverseError(err error, code Code, message string) error {
if err == nil || IsUniverseError(err) {
return err
}
return NewUniverseError(code, message)
}

3
httpdata/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module git.blauwelle.com/go/crate/httpdata
go 1.21.1

141
log/.golangci.yaml Normal file
View File

@@ -0,0 +1,141 @@
## 基于 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- prealloc
- predeclared
- promlinter
- reassign
- revive
- rowserrcheck
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
rowserrcheck:
packages:
- github.com/jmoiron/sqlx
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

100
log/README.md Normal file
View File

@@ -0,0 +1,100 @@
# log 日志
安装: `go get git.blauwelle.com/go/crate/log`
简单使用方式
```go
package main
import (
"context"
"git.blauwelle.com/go/crate/log"
"git.blauwelle.com/go/crate/log/logsdk"
"git.blauwelle.com/go/crate/log/logsdk/logjson"
)
func main() {
log.Logger().AddProcessor(logsdk.AllLevels, logjson.New()) // 添加日志处理器, 默认没有处理器(日志生成后会被忽略)
log.Info(context.Background(), "hello world") // 打印日志, Context 会被传递给日志处理器
}
```
`log` 模块包含日志处理的代码, 由 3 个包组成:
1. [logsdk](./logsdk): 日志实现;
2. [logjson](./logsdk/logjson): 控制台 JSON 日志处理器(`Processor`);
3. [log](.): 根目录, 提供全局 `Logger`, 把 `Logger` / `Entry` 相关的方法封装成函数.
## 基本概念
`logsdk``Logger`, `Entry`, `Processor` 3 部分组成, 其中只有 `Processor` 是接口类型.
`Logger` 主要负责全局的日志配置, `Logger` 可以通过内嵌的 `Entry` 值对象实现日志处理.
`Entry` 负责保存局部的的日志配置, 比如覆盖 `Logger``Caller` 开关或设置日志的时间属性; `Entry` 生成单条日志(`ReadonlyEntry`), 并调用 `Processor` 输出日志.
## 使用方式
### 获取全局 `Logger` 对象
一般只用来配置全局 `Logger` 对象(通过 `Set*``AddProcessor` 方法)
```go
log.Logger()
```
### 修改 `Logger` 配置
`Logger` 提供以下方法用来修改全局配置
- `AddProcessor` 新增处理器;
- `SetLevel` 设置全局日志等级, 只有小于 `Logger` 上设置的日志等级的日志才会被生成;
- `SetCallerSkip` 设置从 `runtime` 包获取调用信息时的 `skip` 参数, 为了方便使用, 0 表示调用 `Logger` / `Entry` 的日志方法处;
- `SetReportCaller` 设置生成调用信息;
- `SetReportStack` 设置生成调用栈;
- `SetReportStackLevel` 当日志等级小于设定值时强制生成调用栈;
- `Reset` 把 Logger 恢复到初始状态;
- `AddBeforeExit` 增加 Exit 在调用 [os.Exit] 前执行的函数, 先增加的后执行;
### 日志生成
`Entry` 负责生成日志, `Logger` 通过内嵌 `Entry` 值对象的方式获得 `Entry` 的方法.
`Entry` 提供以下方法来修改局部配置, `log` 包提供了对应的函数调用
- `AddCallerSkip` 增加通过 `runtime` 获取调用信息时的 `skip` 值, `skip` 可以是负值;
- `WithField` 添加1组键值对;
- `WithFields` 添加键值对;
- `WithTime` 设置生成的日志的时间;
- `WithReportCaller` 覆盖 `Logger` 上的生成调用信息的设置;
- `WithReportStack` 覆盖 `Logger` 上的生成调用栈的设置;
`Entry` 提供以下方法来生成日志, 所有方法的第 1 个参数都是 `context.Context`, `Log` 方法的参数包含日志等级和日志消息,
其他方法的参数是日志消息.
所有方法除了 `Log` 外日志等级从高到底(严重程度从低到高), 这些方法有对应的格式化方法(如 `Info``Infof`).
`log` 包提供了对应的函数调用
- `Log`
- `Trace`
- `Debug`
- `Info`
- `Warn`
- `Error`
- `Fatal`
- `Panic`
### 其他用法
关闭日志生成, `log.Logger().SetLevel(logsdk.LevelDisabled)`;
关闭强制生成调用栈, `log.Logger().SetReportStackLevel(logsdk.LevelDisabled)`;
记录 panic, 在 `defer v = recover()` 后执行 `log.Z(context.Background(), v)`, 其中 `Z` 需要是附加调用栈的等级;
mock, 实现 mock 日志处理器对生成的日志进行处理;
---
## 日志处理器
- [logsdk/logjson](logsdk/logjson) 控制台 JSON 日志
- [go get git.blauwelle.com/go/crate/logotel](../logotel) OpenTelemetry 日志

3
log/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module git.blauwelle.com/go/crate/log
go 1.20

163
log/log.go Normal file
View File

@@ -0,0 +1,163 @@
// log 实现了全局的日志处理
//
// 这个包里定义的函数都是对全局的 [logsdk.Logger] 对象的封装.
// 全局的 Logger 不能重新赋值,
// Logger 的配置方法和日志方法可以并发调用.
package log
import (
"context"
"fmt"
"time"
"git.blauwelle.com/go/crate/log/logsdk"
)
type (
Level = logsdk.Level
)
const (
LevelPanic = logsdk.LevelPanic
LevelFatal = logsdk.LevelFatal
LevelError = logsdk.LevelError
LevelWarn = logsdk.LevelWarn
LevelInfo = logsdk.LevelInfo
LevelDebug = logsdk.LevelDebug
LevelTrace = logsdk.LevelTrace
)
// Field 返回键值对
func Field(key string, value any) logsdk.KV {
return logsdk.KV{
Value: value,
Key: key,
}
}
// AddCallerSkip 增加调用 [runtime.Callers] 时的 skip 参数,
// 当通过装饰器等方式封装 Entry 导致增加调用 Entry 方法的深度时使用 AddCallerSkip 调整 skip,
// 直接在需要日志的地方调用 Entry 的方法时不需要 AddCallerSkip.
func AddCallerSkip(n int) logsdk.Entry {
return globalLogger.AddCallerSkip(n)
}
// WithField 增加1组键值对
func WithField(key string, value any) logsdk.Entry {
return globalLogger.WithField(key, value)
}
// WithFields 增加键值对
func WithFields(fields ...logsdk.KV) logsdk.Entry {
return globalLogger.WithFields(fields...)
}
// WithTime 设置日志的时间,
// Entry 默认使用调用 Log 等最终方法的时间作为日志的时间.
func WithTime(t time.Time) logsdk.Entry {
return globalLogger.WithTime(t)
}
// WithReportCaller 设置 [logsdk.Entry] 是否收集调用记录
func WithReportCaller(reportCaller bool) logsdk.Entry {
return globalLogger.WithReportCaller(reportCaller)
}
// WithReportStack 设置 [logsdk.Entry] 是否收集调用栈
func WithReportStack(reportStack bool) logsdk.Entry {
return globalLogger.WithReportStack(reportStack)
}
// Log 输出日志
func Log(ctx context.Context, level Level, args ...any) {
globalLogger.AddCallerSkip(1).Log(ctx, level, fmt.Sprint(args...))
}
// Trace 输出 LevelTrace 等级的日志
func Trace(ctx context.Context, args ...any) {
globalLogger.AddCallerSkip(1).Trace(ctx, args...)
}
// Debug 输出 LevelDebug 等级的日志
func Debug(ctx context.Context, args ...any) {
globalLogger.AddCallerSkip(1).Debug(ctx, args...)
}
// Info 输出 LevelInfo 等级的日志
func Info(ctx context.Context, args ...any) {
globalLogger.AddCallerSkip(1).Info(ctx, args...)
}
// Warn 输出 LevelWarn 等级的日志
func Warn(ctx context.Context, args ...any) {
globalLogger.AddCallerSkip(1).Warn(ctx, args...)
}
// Error 输出 LevelError 等级的日志
func Error(ctx context.Context, args ...any) {
globalLogger.AddCallerSkip(1).Error(ctx, args...)
}
// Fatal 输出 LevelFatal 等级的日志并调用 Logger.Exit
func Fatal(ctx context.Context, args ...any) {
globalLogger.AddCallerSkip(1).Fatal(ctx, args...)
}
// Panic 输出 LevelPanic 等级的日志后执行 panic,
// 即使 Logger 日志等级高于 LevelPanic 也会 panic.
func Panic(ctx context.Context, args ...any) {
globalLogger.AddCallerSkip(1).Panic(ctx, args...)
}
// Logf 格式化输出日志
func Logf(ctx context.Context, level Level, format string, args ...any) {
globalLogger.AddCallerSkip(1).Logf(ctx, level, format, args...)
}
// Tracef 格式化输出 LevelTrace 等级的日志
func Tracef(ctx context.Context, format string, args ...any) {
globalLogger.AddCallerSkip(1).Tracef(ctx, format, args...)
}
// Debugf 格式化输出 LevelDebug 等级的日志
func Debugf(ctx context.Context, format string, args ...any) {
globalLogger.AddCallerSkip(1).Debugf(ctx, format, args...)
}
// Infof 格式化输出 LevelInfo 等级的日志
func Infof(ctx context.Context, format string, args ...any) {
globalLogger.AddCallerSkip(1).Infof(ctx, format, args...)
}
// Warnf 格式化输出 LevelWarn 等级的日志
func Warnf(ctx context.Context, format string, args ...any) {
globalLogger.AddCallerSkip(1).Warnf(ctx, format, args...)
}
// Errorf 格式化输出 LevelError 等级的日志
func Errorf(ctx context.Context, format string, args ...any) {
globalLogger.AddCallerSkip(1).Errorf(ctx, format, args...)
}
// Fatalf 格式化输出 LevelFatal 等级的日志并调用 Logger.Exit
func Fatalf(ctx context.Context, format string, args ...any) {
globalLogger.AddCallerSkip(1).Fatalf(ctx, format, args...)
}
// Panicf 格式化输出 LevelPanic 等级的日志,
// 即使 Logger 日志等级高于 LevelPanic 也会 panic.
func Panicf(ctx context.Context, format string, args ...any) {
globalLogger.AddCallerSkip(1).Panicf(ctx, format, args...)
}
// Exit 调用全局 logger 的 Exit
func Exit(code int) {
globalLogger.Exit(code)
}
// Logger 返回全局 logger, 通常用来在程序启动时对全局 logger 进行配置,
// 业务代码处理日志时直接使用这个包里定义的日志函数.
func Logger() *logsdk.Logger {
return globalLogger
}

216
log/logsdk/entry.go Normal file
View File

@@ -0,0 +1,216 @@
package logsdk
import (
"context"
"fmt"
"time"
)
// Entry 包含日志所需的全部中间信息并负责输出日志
type Entry struct {
logger *Logger
time time.Time
fields []KV
callerSkip int
reportCaller bool
isReportCallerSet bool
reportStack bool
isReportStackSet bool
initialized bool
}
func (entry Entry) copy() Entry {
if entry.initialized {
return entry
}
return entry.logger.newEntry()
}
// AddCallerSkip 增加调用 [runtime.Callers] 时的 skip 参数,
// 当通过装饰器等方式封装 Entry 导致增加调用 Entry 方法的深度时使用 AddCallerSkip 调整 skip,
// 直接在需要日志的地方调用 Entry 的方法时不需要 AddCallerSkip.
func (entry Entry) AddCallerSkip(n int) Entry {
newEntry := entry.copy()
newEntry.callerSkip += n
return newEntry
}
// WithField 增加1组键值对
func (entry Entry) WithField(key string, value any) Entry {
return entry.WithFields(Field(key, value))
}
// WithFields 增加键值对
func (entry Entry) WithFields(fields ...KV) Entry {
newEntry := entry.copy()
newEntry.fields = make([]KV, len(entry.fields)+len(fields))
copy(newEntry.fields, entry.fields)
copy(newEntry.fields, fields)
return newEntry
}
// WithTime 设置日志的时间,
// Entry 默认使用调用 Log 等最终方法的时间作为日志的时间.
func (entry Entry) WithTime(t time.Time) Entry {
newEntry := entry.copy()
newEntry.time = t
return newEntry
}
// GetReportCaller 获取是否收集调用记录,
// 默认使用 Logger 上的对应配置.
func (entry Entry) GetReportCaller() bool {
if entry.isReportCallerSet {
return entry.reportCaller
}
return entry.logger.GetReportCaller()
}
// WithReportCaller 设置是否收集调用记录
func (entry Entry) WithReportCaller(reportCaller bool) Entry {
newEntry := entry.copy()
newEntry.reportCaller = reportCaller
newEntry.isReportCallerSet = true
return newEntry
}
// GetReportStack 获取是否收集调用栈,
// 默认使用 Logger 上的对应配置.
func (entry Entry) GetReportStack() bool {
if entry.isReportStackSet {
return entry.reportStack
}
return entry.logger.GetReportStack()
}
// WithReportStack 设置是否收集调用栈
func (entry Entry) WithReportStack(reportStack bool) Entry {
newEntry := entry.copy()
newEntry.reportStack = reportStack
newEntry.isReportStackSet = true
return newEntry
}
// Log 输出日志
func (entry Entry) Log(ctx context.Context, level Level, args ...any) {
entry.log(ctx, level, fmt.Sprint(args...))
}
// Trace 输出 LevelTrace 等级的日志
func (entry Entry) Trace(ctx context.Context, args ...any) {
entry.log(ctx, LevelTrace, fmt.Sprint(args...))
}
// Debug 输出 LevelDebug 等级的日志
func (entry Entry) Debug(ctx context.Context, args ...any) {
entry.log(ctx, LevelDebug, fmt.Sprint(args...))
}
// Info 输出 LevelInfo 等级的日志
func (entry Entry) Info(ctx context.Context, args ...any) {
entry.log(ctx, LevelInfo, fmt.Sprint(args...))
}
// Warn 输出 LevelWarn 等级的日志
func (entry Entry) Warn(ctx context.Context, args ...any) {
entry.log(ctx, LevelWarn, fmt.Sprint(args...))
}
// Error 输出 LevelError 等级的日志
func (entry Entry) Error(ctx context.Context, args ...any) {
entry.log(ctx, LevelError, fmt.Sprint(args...))
}
// Fatal 输出 LevelFatal 等级的日志并调用 Logger.Exit
func (entry Entry) Fatal(ctx context.Context, args ...any) {
entry.log(ctx, LevelFatal, fmt.Sprint(args...))
entry.logger.Exit(1)
}
// Panic 输出 LevelPanic 等级的日志后执行 panic,
// 即使 Logger 日志等级高于 LevelPanic 也会 panic.
func (entry Entry) Panic(ctx context.Context, args ...any) {
entry.log(ctx, LevelPanic, fmt.Sprint(args...))
}
// Logf 格式化输出日志
func (entry Entry) Logf(ctx context.Context, level Level, format string, args ...any) {
entry.log(ctx, level, fmt.Sprintf(format, args...))
}
// Tracef 格式化输出 LevelTrace 等级的日志
func (entry Entry) Tracef(ctx context.Context, format string, args ...any) {
entry.log(ctx, LevelTrace, fmt.Sprintf(format, args...))
}
// Debugf 格式化输出 LevelDebug 等级的日志
func (entry Entry) Debugf(ctx context.Context, format string, args ...any) {
entry.log(ctx, LevelDebug, fmt.Sprintf(format, args...))
}
// Infof 格式化输出 LevelInfo 等级的日志
func (entry Entry) Infof(ctx context.Context, format string, args ...any) {
entry.log(ctx, LevelInfo, fmt.Sprintf(format, args...))
}
// Warnf 格式化输出 LevelWarn 等级的日志
func (entry Entry) Warnf(ctx context.Context, format string, args ...any) {
entry.log(ctx, LevelWarn, fmt.Sprintf(format, args...))
}
// Errorf 格式化输出 LevelError 等级的日志
func (entry Entry) Errorf(ctx context.Context, format string, args ...any) {
entry.log(ctx, LevelError, fmt.Sprintf(format, args...))
}
// Fatalf 格式化输出 LevelFatal 等级的日志并调用 Logger.Exit
func (entry Entry) Fatalf(ctx context.Context, format string, args ...any) {
entry.log(ctx, LevelFatal, fmt.Sprintf(format, args...))
entry.logger.Exit(1)
}
// Panicf 格式化输出 LevelPanic 等级的日志,
// 即使 Logger 日志等级高于 LevelPanic 也会 panic.
func (entry Entry) Panicf(ctx context.Context, format string, args ...any) {
entry.log(ctx, LevelPanic, fmt.Sprintf(format, args...))
}
func (entry Entry) log(ctx context.Context, level Level, message string) {
defer func() {
if level == LevelPanic {
panic(message)
}
}()
newEntry := entry.copy()
if newEntry.logger.GetLevel() < level {
return
}
readonlyEntry := ReadonlyEntry{
Fields: newEntry.fields,
Message: message,
Time: newEntry.time,
Level: level,
}
if readonlyEntry.Time.IsZero() {
readonlyEntry.Time = time.Now()
}
if newEntry.GetReportCaller() {
readonlyEntry.Caller = getCaller(newEntry.callerSkip)
}
if newEntry.GetReportStack() || level <= newEntry.logger.GetReportStackLevel() {
readonlyEntry.Stack = getStack(newEntry.callerSkip, maximumFrames)
}
for _, processor := range newEntry.logger.getLevelProcessors(level) {
processor.Process(ctx, readonlyEntry)
}
}
// ReadonlyEntry 是日志系统收集到1条日志记录
type ReadonlyEntry struct {
Caller Frame
Stack []Frame
Time time.Time
Message string
Fields []KV
Level Level
}

17
log/logsdk/field.go Normal file
View File

@@ -0,0 +1,17 @@
package logsdk
// Field 返回键值对
func Field(key string, value any) KV {
return KV{
Key: key,
Value: value,
}
}
// KV 是日志记录中的键值对
//
//nolint:govet
type KV struct {
Key string `json:"k"`
Value any `json:"v"`
}

104
log/logsdk/level.go Normal file
View File

@@ -0,0 +1,104 @@
package logsdk
import (
"fmt"
)
// Level 日志等级
type Level int
func (level Level) String() string {
if b, err := level.MarshalText(); err == nil {
return string(b)
}
return "unknown"
}
// ParseLevel 把 level 字符串解析成 Level,
// 支持的字符串: panic, fatal, error, warn, info, debug, trace,
// 传入不支持的字符串返回 LevelInfo.
func ParseLevel(s string) Level {
switch s {
case LevelPanicValue:
return LevelPanic
case LevelFatalValue:
return LevelFatal
case LevelErrorValue:
return LevelError
case LevelWarnValue:
return LevelWarn
case LevelInfoValue:
return LevelInfo
case LevelDebugValue:
return LevelDebug
case LevelTraceValue:
return LevelTrace
default:
return LevelInfo
}
}
func (level Level) MarshalText() ([]byte, error) {
switch level {
case LevelPanic:
return []byte(LevelPanicValue), nil
case LevelFatal:
return []byte(LevelFatalValue), nil
case LevelError:
return []byte(LevelErrorValue), nil
case LevelWarn:
return []byte(LevelWarnValue), nil
case LevelInfo:
return []byte(LevelInfoValue), nil
case LevelDebug:
return []byte(LevelDebugValue), nil
case LevelTrace:
return []byte(LevelTraceValue), nil
case LevelDisabled:
return []byte(levelDisabledValue), nil
}
return nil, fmt.Errorf("not a valid log level %d", level)
}
const (
// LevelCount 日志等级的个数
LevelCount = 7
// levelOffset 是把 LevelInfo 等级的日志值偏移到零值的偏移量
levelOffset = 4
)
const (
// LevelDisabled 表示不处理任何等级的日志,
// 其他日志等级的值越小表示日志严重程度越高.
LevelDisabled Level = iota - levelOffset - 1
LevelPanic
LevelFatal
LevelError
LevelWarn
LevelInfo
LevelDebug
LevelTrace
)
const (
LevelPanicValue = "panic"
LevelFatalValue = "fatal"
LevelErrorValue = "error"
LevelWarnValue = "warn"
LevelInfoValue = "info"
LevelDebugValue = "debug"
LevelTraceValue = "trace"
levelDisabledValue = "disabled"
)
var AllLevels = []Level{
LevelPanic,
LevelFatal,
LevelError,
LevelWarn,
LevelInfo,
LevelDebug,
LevelTrace,
}

154
log/logsdk/logger.go Normal file
View File

@@ -0,0 +1,154 @@
package logsdk
import (
"os"
"sync"
"sync/atomic"
)
// New 返回初始未经过参数调整的 Logger
func New() *Logger {
logger := &Logger{}
logger.entry.logger = logger
logger.Reset()
return logger
}
// entry 仅被用来嵌入 Logger
type entry = Entry
// Logger 中保存了日志所需的全局配置,
// 使用 Logger 处理日志.
type Logger struct {
beforeExitFns []func() // protected by lock
levelProcessors levelProcessors // protected by lock
entry
lock sync.RWMutex
exitOnce sync.Once
level atomic.Int32
reportStackLevel atomic.Int32
callerSkip atomic.Int32
reportCaller atomic.Bool
reportStack atomic.Bool
}
// AddProcessor 把日志处理器增加到 Logger
func (logger *Logger) AddProcessor(levels []Level, processor EntryProcessor) {
logger.lock.Lock()
for _, level := range levels {
logger.levelProcessors[level+levelOffset] = append(logger.levelProcessors[level+levelOffset], processor)
}
logger.lock.Unlock()
}
// GetLevel 返回日志系统的等级, 严重程度低于返回等级的日志不会被处理.
func (logger *Logger) GetLevel() Level {
return Level(logger.level.Load())
}
// SetLevel 设置日志系统的等级
func (logger *Logger) SetLevel(level Level) {
logger.level.Store(int32(level))
}
// GetCallerSkip 获取调用 [runtime.Callers] 时的 skip 参数,
// skip 已经被偏移到从调用 Logger 相关方法处获取调用信息.
// 0 表示从调用 Logger 相关方法处获取调用信息.
func (logger *Logger) GetCallerSkip() int {
return int(logger.callerSkip.Load())
}
// SetCallerSkip 设置调用 [runtime.Callers] 时的 skip 参数,
// skip 已经被偏移到从调用 Logger 相关方法处获取调用信息.
// 0 表示从调用 Logger 相关方法处获取调用信息.
func (logger *Logger) SetCallerSkip(callerSkip int) {
logger.callerSkip.Store(int32(callerSkip))
}
// GetReportCaller 返回是否收集调用信息
func (logger *Logger) GetReportCaller() bool {
return logger.reportCaller.Load()
}
// SetReportCaller 设置是否收集调用信息
func (logger *Logger) SetReportCaller(reportCaller bool) {
logger.reportCaller.Store(reportCaller)
}
// GetReportStack 返回是否收集调用栈信息
func (logger *Logger) GetReportStack() bool {
return logger.reportStack.Load()
}
// SetReportStack 设置是否收集调用栈信息
func (logger *Logger) SetReportStack(reportStack bool) {
logger.reportStack.Store(reportStack)
}
// GetReportStackLevel 获取自动添加调用栈对应的日志等级
func (logger *Logger) GetReportStackLevel() Level {
return Level(logger.reportStackLevel.Load())
}
// SetReportStackLevel 设置日志等级小于等于 level 时自动添加调用栈
func (logger *Logger) SetReportStackLevel(level Level) {
logger.reportStackLevel.Store(int32(level))
}
// Reset 把 Logger 重置到初始状态
func (logger *Logger) Reset() {
logger.lock.Lock()
logger.beforeExitFns = nil
logger.levelProcessors = levelProcessors{}
logger.lock.Unlock()
logger.SetLevel(LevelInfo)
logger.SetReportStackLevel(LevelWarn)
logger.SetCallerSkip(0)
logger.SetReportCaller(false)
logger.SetReportStack(false)
}
// BeforeExit 按照先添加后执行的顺序执行 AddBeforeExit 添加的函数,
// AddBeforeExit 只会执行1次.
func (logger *Logger) BeforeExit() {
logger.exitOnce.Do(func() {
logger.lock.RLock()
beforeExitFns := make([]func(), len(logger.beforeExitFns))
copy(beforeExitFns, logger.beforeExitFns)
logger.lock.RUnlock()
for i := len(beforeExitFns) - 1; i >= 0; i-- {
beforeExitFns[i]()
}
})
}
// Exit 执行 BeforeExit 后调用 [os.Exit]退出程序.
func (logger *Logger) Exit(code int) {
logger.BeforeExit()
os.Exit(code)
}
// AddBeforeExit 增加 Exit 在调用 [os.Exit] 前执行的函数,
// 先增加的后执行.
func (logger *Logger) AddBeforeExit(fn ...func()) {
logger.lock.Lock()
logger.beforeExitFns = append(logger.beforeExitFns, fn...)
logger.lock.Unlock()
}
func (logger *Logger) getLevelProcessors(level Level) []EntryProcessor {
logger.lock.RLock()
defer logger.lock.RUnlock()
return logger.levelProcessors[level+levelOffset]
}
func (logger *Logger) newEntry() Entry {
return Entry{
logger: logger,
callerSkip: logger.GetCallerSkip() + entrySkipOffset,
reportCaller: logger.GetReportCaller(),
reportStack: logger.GetReportStack(),
initialized: true,
}
}

View File

@@ -0,0 +1,97 @@
package logjson
import (
"io"
"os"
"time"
)
// Option 配置日志处理对象
type Option interface {
apply(cfg *config)
}
// WithBufferPool 配置缓冲池
func WithBufferPool(pool BytesBufferPool) Option {
return optionFunc(func(cfg *config) {
cfg.bytesBufferPool = pool
cfg.hasPool = true
})
}
// WithOutput 配置输出
func WithOutput(w io.Writer) Option {
return optionFunc(func(cfg *config) {
cfg.output = w
})
}
// WithTimeFormat 配置时间格式
func WithTimeFormat(format string) Option {
return optionFunc(func(cfg *config) {
cfg.timestampFormat = format
})
}
// WithDisableTime 配置仅用时间输出
func WithDisableTime(disable bool) Option {
return optionFunc(func(cfg *config) {
cfg.disableTime = disable
})
}
// WithDisableHTMLEscape 配置禁止 HTML 转义
func WithDisableHTMLEscape(disable bool) Option {
return optionFunc(func(cfg *config) {
cfg.disableHTMLEscape = disable
})
}
// WithPrettyPrint 配置 JSON 多行缩进输出
func WithPrettyPrint(pretty bool) Option {
return optionFunc(func(cfg *config) {
cfg.prettyPrint = pretty
})
}
func newConfig(opts ...Option) *config {
cfg := defaultConfig()
for _, opt := range opts {
opt.apply(cfg)
}
if !cfg.hasPool {
cfg.bytesBufferPool = NewBytesBufferPool(bytesBufferInitialSize, bytesBufferMaximumSize)
}
if cfg.output == nil {
cfg.output = NewSyncWriter(os.Stderr)
}
if cfg.timestampFormat == "" {
cfg.timestampFormat = time.RFC3339Nano
}
return cfg
}
func defaultConfig() *config {
return &config{
hasPool: false,
disableTime: false,
disableHTMLEscape: false,
prettyPrint: false,
}
}
type config struct {
bytesBufferPool BytesBufferPool
output io.Writer
timestampFormat string
hasPool bool
disableTime bool
disableHTMLEscape bool
prettyPrint bool
}
type optionFunc func(cfg *config)
func (fn optionFunc) apply(cfg *config) {
fn(cfg)
}

View File

@@ -0,0 +1,44 @@
package logjson
import (
"bytes"
"sync"
)
const (
bytesBufferInitialSize = 512
bytesBufferMaximumSize = 4096
)
type BytesBufferPool interface {
Get() *bytes.Buffer
Put(buffer *bytes.Buffer)
}
// NewBytesBufferPool 创建并返回 BytesBufferPool
func NewBytesBufferPool(initialSize, maximumSize int) BytesBufferPool {
return &bytesBufferPool{
pool: sync.Pool{
New: func() any {
return bytes.NewBuffer(make([]byte, 0, initialSize))
},
},
maximumSize: maximumSize,
}
}
type bytesBufferPool struct {
pool sync.Pool
maximumSize int
}
func (pool *bytesBufferPool) Get() *bytes.Buffer {
return pool.pool.Get().(*bytes.Buffer)
}
func (pool *bytesBufferPool) Put(buf *bytes.Buffer) {
if buf.Cap() > pool.maximumSize {
return
}
pool.pool.Put(buf)
}

View File

@@ -0,0 +1,88 @@
package logjson
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"git.blauwelle.com/go/crate/log/logsdk"
)
var _ logsdk.EntryProcessor = &Processor{}
// New 返回日志处理对象,
// 返回的对象是 [logsdk.EntryProcessor].
func New(opts ...Option) *Processor {
cfg := newConfig(opts...)
return &Processor{
bytesBufferPool: cfg.bytesBufferPool,
output: cfg.output,
timeFormat: cfg.timestampFormat,
disableTime: cfg.disableTime,
disableHTMLEscape: cfg.disableHTMLEscape,
prettyPrint: cfg.prettyPrint,
}
}
// Processor 日志处理对象, 把日志处理成 JSON.
type Processor struct {
bytesBufferPool BytesBufferPool
output io.Writer
timeFormat string
disableTime bool
disableHTMLEscape bool
prettyPrint bool
}
// Process 处理日志
func (processor *Processor) Process(_ context.Context, entry logsdk.ReadonlyEntry) {
m := Entry{
Stack: entry.Stack,
Fields: entry.Fields,
Level: entry.Level,
Message: entry.Message,
}
if !processor.disableTime {
m.Time = entry.Time.Format(processor.timeFormat) // 1次分配
}
if entry.Caller.IsValid() {
// 1次分配
// 直接取 &entry.Caller 会增加堆内存分配
m.Caller = &logsdk.Frame{
Function: entry.Caller.Function,
File: entry.Caller.File,
Line: entry.Caller.Line,
}
}
buf := processor.bytesBufferPool.Get()
buf.Reset()
defer processor.bytesBufferPool.Put(buf)
encoder := json.NewEncoder(buf)
if processor.prettyPrint {
encoder.SetIndent("", " ")
}
encoder.SetEscapeHTML(!processor.disableHTMLEscape)
// Encode 2次分配
if err := encoder.Encode(m); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "JSON processor cannot encode log %#v: %s\n", m, err.Error())
}
if _, err := buf.WriteTo(processor.output); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "JSON processor cannot write log: %s\n", err.Error())
}
}
// Entry 被用来 JSON 序列化
type Entry struct {
Message string `json:"msg"`
Time string `json:"time,omitempty"`
Caller *logsdk.Frame `json:"caller,omitempty"`
Stack []logsdk.Frame `json:"stack,omitempty"`
Fields []logsdk.KV `json:"fields,omitempty"`
Level logsdk.Level `json:"level"`
}

View File

@@ -0,0 +1,25 @@
package logjson
import (
"io"
"sync"
)
// NewSyncWriter 返回写互斥的 io.Writer
func NewSyncWriter(writer io.Writer) io.Writer {
return &syncWriter{
writer: writer,
lock: sync.Mutex{},
}
}
type syncWriter struct {
writer io.Writer
lock sync.Mutex
}
func (w *syncWriter) Write(p []byte) (int, error) {
w.lock.Lock()
defer w.lock.Unlock()
return w.writer.Write(p)
}

10
log/logsdk/processor.go Normal file
View File

@@ -0,0 +1,10 @@
package logsdk
import "context"
// EntryProcessor 处理日志记录
type EntryProcessor interface {
Process(ctx context.Context, entry ReadonlyEntry)
}
type levelProcessors [LevelCount][]EntryProcessor

63
log/logsdk/runtime.go Normal file
View File

@@ -0,0 +1,63 @@
package logsdk
import (
"runtime"
)
const (
maximumFrames = 32
getCallerSkipOffset = 2
entrySkipOffset = 2
runtimeMain = "runtime.main"
)
// Frame 调用相关信息
type Frame struct {
Function string `json:"func"`
File string `json:"file"`
Line int `json:"line"`
}
// IsValid 表示是否有效
func (frame Frame) IsValid() bool {
return frame.Line > 0
}
func getCaller(skip int) Frame {
pc := make([]uintptr, 1)
n := runtime.Callers(skip+getCallerSkipOffset, pc)
frame, _ := runtime.CallersFrames(pc[:n]).Next()
if frame.PC == 0 {
return Frame{}
}
return Frame{
Function: frame.Function,
File: frame.File,
Line: frame.Line,
}
}
func getStack(skip, maximumFrames int) []Frame {
pc := make([]uintptr, maximumFrames)
n := runtime.Callers(skip+getCallerSkipOffset, pc)
stack := make([]Frame, 0, n)
frames := runtime.CallersFrames(pc[:n])
for {
frame, more := frames.Next()
if frame.PC != 0 {
if frame.Function == runtimeMain {
break
}
stack = append(stack, Frame{
Function: frame.Function,
File: frame.File,
Line: frame.Line,
})
}
if !more {
break
}
}
return stack
}

8
log/state.go Normal file
View File

@@ -0,0 +1,8 @@
package log
import (
"git.blauwelle.com/go/crate/log/logsdk"
)
// globalLogger 不能重新赋值
var globalLogger = logsdk.New()

141
logotel/.golangci.yaml Normal file
View File

@@ -0,0 +1,141 @@
## 基于 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- prealloc
- predeclared
- promlinter
- reassign
- revive
- rowserrcheck
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
rowserrcheck:
packages:
- github.com/jmoiron/sqlx
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

9
logotel/go.mod Normal file
View File

@@ -0,0 +1,9 @@
module git.blauwelle.com/go/crate/logotel
go 1.20
require (
git.blauwelle.com/go/crate/log v0.9.0
go.opentelemetry.io/otel v1.13.0
go.opentelemetry.io/otel/trace v1.13.0
)

11
logotel/go.sum Normal file
View File

@@ -0,0 +1,11 @@
git.blauwelle.com/go/crate/log v0.9.0 h1:H01AQIKcYybeCZGdReBzMoWhkXPQJAoY1t+K0J1asEk=
git.blauwelle.com/go/crate/log v0.9.0/go.mod h1:jfVfpRODZTA70A8IkApVeGsS1zfLk1D77sLWZM/w+L0=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
go.opentelemetry.io/otel v1.13.0 h1:1ZAKnNQKwBBxFtww/GwxNUyTf0AxkZzrukO8MeXqe4Y=
go.opentelemetry.io/otel v1.13.0/go.mod h1:FH3RtdZCzRkJYFTCsAKDy9l/XYjMdNv6QrkFFB8DvVg=
go.opentelemetry.io/otel/trace v1.13.0 h1:CBgRZ6ntv+Amuj1jDsMhZtlAPT6gbyIRdaIzFhfBSdY=
go.opentelemetry.io/otel/trace v1.13.0/go.mod h1:muCvmmO9KKpvuXSf3KKAXXB2ygNYHQ+ZfI5X08d3tds=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

59
logotel/option.go Normal file
View File

@@ -0,0 +1,59 @@
package logotel
import (
"git.blauwelle.com/go/crate/log/logsdk/logjson"
)
const (
bytesBufferInitialSize = 512
bytesBufferMaximumSize = 4096
)
func newConfig(opts ...Option) *config {
cfg := defaultConfig()
for _, opt := range opts {
opt.apply(cfg)
}
if !cfg.hasPool {
cfg.bytesBufferPool = logjson.NewBytesBufferPool(bytesBufferInitialSize, bytesBufferMaximumSize)
}
return cfg
}
func defaultConfig() *config {
return &config{
hasPool: false,
defaultSpan: false,
}
}
// WithBufferPool 指定缓冲池
func WithBufferPool(pool logjson.BytesBufferPool) Option {
return optionFunc(func(cfg *config) {
cfg.bytesBufferPool = pool
cfg.hasPool = true
})
}
// WithDefaultSpan 设置当 span 没有在记录时创建新 span
func WithDefaultSpan(defaultSpan bool) Option {
return optionFunc(func(cfg *config) {
cfg.defaultSpan = defaultSpan
})
}
// Option 配置 Processor
type Option interface {
apply(cfg *config)
}
type config struct {
bytesBufferPool logjson.BytesBufferPool
hasPool bool
defaultSpan bool
}
type optionFunc func(cfg *config)
func (fn optionFunc) apply(cfg *config) {
fn(cfg)
}

101
logotel/processor.go Normal file
View File

@@ -0,0 +1,101 @@
// otellog 提供 git.blauwelle.com/go/crate/log 的 opentelemetry 处理功能
package logotel
import (
"context"
"fmt"
"strconv"
"git.blauwelle.com/go/crate/log/logsdk"
"git.blauwelle.com/go/crate/log/logsdk/logjson"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
"go.opentelemetry.io/otel/trace"
)
// New 创建 log/opentelemetry 处理器
func New(opts ...Option) *Processor {
cfg := newConfig(opts...)
return &Processor{
bufferPool: cfg.bytesBufferPool,
defaultSpan: cfg.defaultSpan,
}
}
var _ logsdk.EntryProcessor = &Processor{}
// Processor 用于把日志和 opentelemetry 对接
type Processor struct {
bufferPool logjson.BytesBufferPool
defaultSpan bool
}
func (processor *Processor) Process(ctx context.Context, entry logsdk.ReadonlyEntry) {
span := trace.SpanFromContext(ctx)
if !span.IsRecording() {
if processor.defaultSpan {
ctx, span = otel.Tracer("git.blauwelle.com/go/crate/logotel").Start(ctx, "default") //nolint:ineffassign,staticcheck,wastedassign
defer span.End()
}
}
if !span.IsRecording() {
return
}
const entryInitSize = 6
attrs := make([]attribute.KeyValue, 0, len(entry.Fields)+entryInitSize)
attrs = append(attrs, attribute.String("log.severity", entry.Level.String()))
attrs = append(attrs, attribute.String("log.message", entry.Message))
if entry.Caller.IsValid() {
attrs = append(attrs, semconv.CodeFunctionKey.String(entry.Caller.Function))
attrs = append(attrs, semconv.CodeFilepathKey.String(entry.Caller.File))
attrs = append(attrs, semconv.CodeLineNumberKey.Int(entry.Caller.Line))
}
if len(entry.Stack) > 0 {
buf := processor.bufferPool.Get()
for _, frame := range entry.Stack {
buf.WriteString(frame.Function)
buf.WriteByte('\n')
buf.WriteByte('\t')
buf.WriteString(frame.File)
buf.WriteByte(':')
buf.WriteString(strconv.Itoa(frame.Line))
buf.WriteByte('\n')
}
processor.bufferPool.Put(buf)
attrs = append(attrs, attribute.String("zz.stack", buf.String()))
}
for _, field := range entry.Fields {
attrs = append(attrs, fieldToKV(field))
}
span.AddEvent("log", trace.WithTimestamp(entry.Time), trace.WithAttributes(attrs...))
if entry.Level <= logsdk.LevelError {
span.SetStatus(codes.Error, entry.Message)
}
}
func fieldToKV(field logsdk.KV) attribute.KeyValue {
switch value := field.Value.(type) {
case nil:
return attribute.String(field.Key, "<nil>")
case string:
return attribute.String(field.Key, value)
case int:
return attribute.Int(field.Key, value)
case int64:
return attribute.Int64(field.Key, value)
case float64:
return attribute.Float64(field.Key, value)
case bool:
return attribute.Bool(field.Key, value)
case error:
return attribute.String(field.Key, value.Error())
case fmt.Stringer:
return attribute.String(field.Key, value.String())
}
return attribute.String(field.Key, fmt.Sprint(field.Value))
}

138
mapset/.golangci.yaml Normal file
View File

@@ -0,0 +1,138 @@
## 更新到 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

View File

@@ -7,6 +7,10 @@ import (
"strings"
)
const (
maximumItemToPrint = 64
)
// New 返回 [MapSet]
func New[T comparable](keys ...T) MapSet[T] {
s := make(MapSet[T], len(keys))
@@ -14,7 +18,7 @@ func New[T comparable](keys ...T) MapSet[T] {
return s
}
// MapSet 是集合的范型实现
// MapSet 是集合的 generics 实现
type MapSet[T comparable] map[T]struct{}
// Cardinality 返回集合的元素个数
@@ -267,8 +271,8 @@ func (s *MapSet[T]) UnmarshalJSON(b []byte) error {
func (s MapSet[T]) String() string {
size := s.Cardinality()
if size > 64 {
size = 64
if size > maximumItemToPrint {
size = maximumItemToPrint
}
keys := make([]string, 0, size)
for key := range s {

View File

@@ -0,0 +1,138 @@
## 更新到 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

View File

@@ -6,6 +6,11 @@ import (
"runtime"
)
const (
callerSkipOffset = 2
maximumFrames = 32
)
// Frame 调用相关信息
type Frame struct {
Function string `json:"func"`
@@ -38,7 +43,7 @@ func (frame Frame) SplitFunction() (string, string) {
// skip=0 表示调用 Caller 处.
func Caller(skip int) Frame {
pc := make([]uintptr, 1)
n := runtime.Callers(skip+2, pc)
n := runtime.Callers(skip+callerSkipOffset, pc)
frame, _ := runtime.CallersFrames(pc[:n]).Next()
if frame.PC == 0 {
return Frame{}
@@ -54,7 +59,7 @@ func Caller(skip int) Frame {
// skip=0 表示调用 Stack 处.
func Stack(skip, maximumFrames int) []Frame {
pc := make([]uintptr, maximumFrames)
n := runtime.Callers(skip+2, pc)
n := runtime.Callers(skip+callerSkipOffset, pc)
stack := make([]Frame, 0, n)
frames := runtime.CallersFrames(pc[:n])
for {
@@ -82,7 +87,7 @@ func Stack(skip, maximumFrames int) []Frame {
// 可以使用 [runtime.Frame.PC] != 0 判断 runtime.Frame 有效
func CallerFrame(skip int) runtime.Frame {
pc := make([]uintptr, 1)
n := runtime.Callers(skip+2, pc)
n := runtime.Callers(skip+callerSkipOffset, pc)
frame, _ := runtime.CallersFrames(pc[:n]).Next()
return frame
}
@@ -95,14 +100,14 @@ func CallerFrame(skip int) runtime.Frame {
// - 0: 调用 CallersFrames 处
func CallersFrames(skip, maximumFrames int) *runtime.Frames {
pc := make([]uintptr, maximumFrames)
n := runtime.Callers(skip+2, pc)
n := runtime.Callers(skip+callerSkipOffset, pc)
return runtime.CallersFrames(pc[:n])
}
// PrintCallersFrames 打印函数调用栈,
// 从调用 PrintCallersFrames 的地方开始打印
func PrintCallersFrames() {
frames := CallersFrames(3, 32)
frames := CallersFrames(callerSkipOffset+1, maximumFrames)
for {
frame, more := frames.Next()
if frame.PC != 0 {

138
synchelper/.golangci.yaml Normal file
View File

@@ -0,0 +1,138 @@
## 更新到 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

View File

@@ -0,0 +1,18 @@
package synchelper
import "bytes"
// NewBytesBufferPool 返回新的 BytesBufferPool
func NewBytesBufferPool(initialSize, maximumSize int) BytesBufferPool {
return NewPool(
func() any {
return bytes.NewBuffer(make([]byte, 0, initialSize))
},
func(v *bytes.Buffer) bool {
return v.Cap() <= maximumSize
},
)
}
// BytesBufferPool 是 [*bytes.Buffer] 的资源池
type BytesBufferPool = Pool[*bytes.Buffer]

3
synchelper/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module git.blauwelle.com/go/crate/synchelper
go 1.20

25
synchelper/io_writer.go Normal file
View File

@@ -0,0 +1,25 @@
package synchelper
import (
"io"
"sync"
)
// NewSyncWriter 返回写互斥的 writer
func NewSyncWriter(writer io.Writer) io.Writer {
return &syncWriter{
writer: writer,
lock: sync.Mutex{},
}
}
type syncWriter struct {
writer io.Writer
lock sync.Mutex
}
func (w *syncWriter) Write(p []byte) (int, error) {
w.lock.Lock()
defer w.lock.Unlock()
return w.writer.Write(p)
}

34
synchelper/pool.go Normal file
View File

@@ -0,0 +1,34 @@
package synchelper
import (
"sync"
)
// NewPool 初始化 Pool,
// newFn 是资源的构造函数,
// putCond 返回 true 时表示资源可以被放回 Pool 中.
func NewPool[T any](newFn func() any, putCond func(v T) bool) Pool[T] {
return Pool[T]{
pool: &sync.Pool{New: newFn},
putCond: putCond,
}
}
// Pool 是通用的资源池
type Pool[T any] struct {
pool *sync.Pool
putCond func(v T) bool
}
// Get 获取资源
func (pool Pool[T]) Get() T {
return pool.pool.Get().(T)
}
// Put 放回资源
func (pool Pool[T]) Put(v T) {
if !pool.putCond(v) {
return
}
pool.pool.Put(v)
}

138
timehelper/.golangci.yaml Normal file
View File

@@ -0,0 +1,138 @@
## 更新到 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

View File

@@ -4,5 +4,5 @@ import "time"
// SetupCST 把本地时区固定到CST
func SetupCST() {
time.Local = time.FixedZone("CST", 8*3600)
time.Local = time.FixedZone("CST", 8*3600) //nolint:gomnd,reassign
}

View File

@@ -14,20 +14,20 @@ func ExampleRFC3339Nano() {
// Output: 2023-01-15T15:16:17.123456789+08:00
}
func ExampleRFC3339Nano2Sec() {
func ExampleRFC3339_nano2Sec() {
t, _ := time.Parse(time.RFC3339Nano, "2023-01-15T15:16:17.123456789+08:00")
fmt.Println(t.Format(time.RFC3339))
// Output: 2023-01-15T15:16:17+08:00
}
// 使用 RFC3339 没有丢失纳秒精度
func ExampleRFC3339Sec2Nano() {
func ExampleRFC3339_sec2Nano() {
t, _ := time.Parse(time.RFC3339, "2023-01-15T15:16:17.123456789+08:00")
fmt.Println(t.Format(time.RFC3339Nano))
// Output: 2023-01-15T15:16:17.123456789+08:00
}
func ExampleRFC3339NanoOtherZone() {
func ExampleRFC3339_nanoOtherZone() {
t, _ := time.Parse(time.RFC3339Nano, "2023-01-15T15:16:17.123456789+09:00")
fmt.Println(t.Format(time.RFC3339Nano))
// Output: 2023-01-15T15:16:17.123456789+09:00

View File

@@ -0,0 +1,138 @@
## 更新到 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

View File

@@ -23,7 +23,7 @@ require (
go.opentelemetry.io/otel/sdk/metric v0.36.0 // indirect
go.opentelemetry.io/otel/trace v1.13.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
golang.org/x/net v0.6.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/genproto v0.0.0-20230202175211-008b39050e57 // indirect

View File

@@ -250,6 +250,8 @@ golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=

View File

@@ -2,6 +2,7 @@ package uptracehelper
import (
"context"
"github.com/uptrace/uptrace-go/uptrace"
)
@@ -11,12 +12,7 @@ type Config struct {
DeploymentEnvironment string
}
// InitTracer 初始化并等待 uptrace
// 配合 [git.blauwelle.com/go/crate/exegroup] 使用
// env
// - UPTRACE_DISABLED 存在就不再初始化
// - UPTRACE_DSN 服务端地址
func InitTracer(cfg Config) (func(ctx context.Context) error, func(ctx context.Context)) {
func Setup(cfg Config) {
opts := []uptrace.Option{
uptrace.WithServiceName(cfg.ServiceName),
uptrace.WithServiceVersion(cfg.ServiceVersion),
@@ -25,7 +21,16 @@ func InitTracer(cfg Config) (func(ctx context.Context) error, func(ctx context.C
opts = append(opts, uptrace.WithDeploymentEnvironment(cfg.DeploymentEnvironment))
}
uptrace.ConfigureOpentelemetry(opts...)
shutdownErr := make(chan error)
}
// GoStop 初始化并等待 uptrace
// 配合 [git.blauwelle.com/go/crate/exegroup] 使用
// env
// - UPTRACE_DISABLED 存在就不再初始化
// - UPTRACE_DSN 服务端地址
func GoStop(cfg Config) (func(ctx context.Context) error, func(ctx context.Context)) {
Setup(cfg)
shutdownErr := make(chan error, 1)
goFunc := func(context.Context) error {
return <-shutdownErr
}

138
wireexample/.golangci.yaml Normal file
View File

@@ -0,0 +1,138 @@
## 更新到 golangci-lint@v1.52.2
run:
timeout: 1m
build-tags: [ ]
skip-dirs: [ ]
skip-files: [ ]
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- dupl
- durationcheck
- errname
- errorlint
- exhaustive
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- goimports
- gomnd
- goprintffuncname
- gosec
- lll
- loggercheck
- makezero
- nakedret
- nestif
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- revive
- stylecheck
- tenv
- testableexamples
- testpackage
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace
linters-settings:
errcheck:
check-type-assertions: true
exclude-functions: [ ]
govet:
enable-all: true
disable: [ ]
cyclop:
max-complexity: 10
package-average: 0.0
dupl:
threshold: 150
exhaustive:
check:
- switch
- map
funlen:
lines: 100
statements: 60
gocritic:
disabled-checks:
- commentFormatting
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false
gocyclo:
min-complexity: 20
gomnd:
ignored-functions:
- os.Chmod
- os.Mkdir
- os.MkdirAll
- os.OpenFile
- os.WriteFile
- prometheus.ExponentialBuckets
- prometheus.ExponentialBucketsRange
- prometheus.LinearBuckets
lll:
line-length: 240
nakedret:
max-func-lines: 10
nestif:
min-complexity: 5
predeclared:
ignore: ""
q: false
reassign:
patterns:
- ".*"
tenv:
all: true
usestdlibvars:
time-month: true
time-layout: true
crypto-hash: true
default-rpc-path: true
os-dev-null: true
sql-isolation-level: true
tls-signature-scheme: true
constant-kind: true
syslog-priority: true
issues:
max-same-issues: 10
exclude-rules:
- source: "//noinspection"
linters: [ gocritic ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx

View File

@@ -16,7 +16,7 @@ func approachA() {
rander := &MockRander{}
guess := InjectGuessWithoutMock(rander)
rander.Value = 1
fmt.Println("approachA:", guess.Guess(10))
fmt.Println("approachA:", guess.Guess(10)) //nolint:gomnd
}
// approach B:
@@ -25,5 +25,5 @@ func approachA() {
func approachB() {
guessWithMock := InjectMockGuess()
guessWithMock.Mock.Value = 1
fmt.Println("approachB:", guessWithMock.Guess.Guess(10))
fmt.Println("approachB:", guessWithMock.Guess.Guess(10)) //nolint:gomnd
}

View File

@@ -14,7 +14,7 @@ type Rander interface {
type DefaultRander struct{}
func (r *DefaultRander) Rand() int {
return rand.Int()
return rand.Int() //nolint:gosec
}
func NewRander() *DefaultRander {