Skip to content

Commit

Permalink
trace集成,go升级,golangci-lint升级
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongyu mao committed Feb 21, 2024
1 parent 158cb31 commit e3a931f
Show file tree
Hide file tree
Showing 25 changed files with 906 additions and 143 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ jobs:
- 8500:8500
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.22

- name: Build
run: go build -v ./...
Expand Down
14 changes: 14 additions & 0 deletions docker/docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,17 @@ docker run --restart=always -d -p 8500:8500 -p 8300:8300 -p 8301:8301 -p 8302:83
# 验证
# dig @192.168.3.109 -p 8600 consul.service.consul SRV

docker run -d --name jaeger \
-e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
-e COLLECTOR_OTLP_ENABLED=true \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 4317:4317 \
-p 4318:4318 \
-p 14250:14250 \
-p 14268:14268 \
-p 14269:14269 \
-p 9411:9411 \
jaegertracing/all-in-one:1.54
54 changes: 33 additions & 21 deletions errors/error_usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,32 @@ func Controller1() error {
测试Wrap 函数
=== RUN TestWrap
error_user_test.go:21: call funcB failed
error_user_test.go:21: call funcB failed
=============
error_user_test.go:25: func called error
logtest.funcB
/Users/maozhongyu/code/logtest/error_user_test.go:17
logtest.funcA
/Users/maozhongyu/code/logtest/error_user_test.go:10
logtest.TestWrap
/Users/maozhongyu/code/logtest/error_user_test.go:25
testing.tRunner
/Users/maozhongyu/go1.18.5/src/testing/testing.go:1439
runtime.goexit
/Users/maozhongyu/go1.18.5/src/runtime/asm_amd64.s:1571
call funcB failed
logtest.funcA
/Users/maozhongyu/code/logtest/error_user_test.go:11
logtest.TestWrap
/Users/maozhongyu/code/logtest/error_user_test.go:25
testing.tRunner
/Users/maozhongyu/go1.18.5/src/testing/testing.go:1439
runtime.goexit
/Users/maozhongyu/go1.18.5/src/runtime/asm_amd64.s:1571
error_user_test.go:25: func called error
logtest.funcB
/Users/maozhongyu/code/logtest/error_user_test.go:17
logtest.funcA
/Users/maozhongyu/code/logtest/error_user_test.go:10
logtest.TestWrap
/Users/maozhongyu/code/logtest/error_user_test.go:25
testing.tRunner
/Users/maozhongyu/go1.18.5/src/testing/testing.go:1439
runtime.goexit
/Users/maozhongyu/go1.18.5/src/runtime/asm_amd64.s:1571
call funcB failed
logtest.funcA
/Users/maozhongyu/code/logtest/error_user_test.go:11
logtest.TestWrap
/Users/maozhongyu/code/logtest/error_user_test.go:25
testing.tRunner
/Users/maozhongyu/go1.18.5/src/testing/testing.go:1439
runtime.goexit
/Users/maozhongyu/go1.18.5/src/runtime/asm_amd64.s:1571
--- PASS: TestWrap (0.00s)
PASS
*/
Expand Down Expand Up @@ -117,13 +121,21 @@ func TestIs(t *testing.T) {
error 1
Service has error
logtest.Controller1
/Users/maozhongyu/code/logtest/error_user_test.go:56
logtest.TestAs
/Users/maozhongyu/code/logtest/error_user_test.go:116
testing.tRunner
/Users/maozhongyu/go1.18.5/src/testing/testing.go:1439
runtime.goexit
/Users/maozhongyu/go1.18.5/src/runtime/asm_amd64.s:1571
-----------
AS error
*/
Expand Down
74 changes: 37 additions & 37 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,89 @@
//
// The traditional error handling idiom in Go is roughly akin to
//
// if err != nil {
// return err
// }
// if err != nil {
// return err
// }
//
// which when applied recursively up the call stack results in error reports
// without context or debugging information. The errors package allows
// programmers to add context to the failure path in their code in a way
// that does not destroy the original value of the error.
//
// Adding context to an error
// # Adding context to an error
//
// The errors.Wrap function returns a new error that adds context to the
// original error by recording a stack trace at the point Wrap is called,
// together with the supplied message. For example
//
// _, err := ioutil.ReadAll(r)
// if err != nil {
// return errors.Wrap(err, "read failed")
// }
// _, err := ioutil.ReadAll(r)
// if err != nil {
// return errors.Wrap(err, "read failed")
// }
//
// If additional control is required, the errors.WithStack and
// errors.WithMessage functions destructure errors.Wrap into its component
// operations: annotating an error with a stack trace and with a message,
// respectively.
//
// Retrieving the cause of an error
// # Retrieving the cause of an error
//
// Using errors.Wrap constructs a stack of errors, adding context to the
// preceding error. Depending on the nature of the error it may be necessary
// to reverse the operation of errors.Wrap to retrieve the original error
// for inspection. Any error value which implements this interface
//
// type causer interface {
// Cause() error
// }
// type causer interface {
// Cause() error
// }
//
// can be inspected by errors.Cause. errors.Cause will recursively retrieve
// the topmost error that does not implement causer, which is assumed to be
// the original cause. For example:
//
// switch err := errors.Cause(err).(type) {
// case *MyError:
// // handle specifically
// default:
// // unknown error
// }
// switch err := errors.Cause(err).(type) {
// case *MyError:
// // handle specifically
// default:
// // unknown error
// }
//
// Although the causer interface is not exported by this package, it is
// considered a part of its stable public interface.
//
// Formatted printing of errors
// # Formatted printing of errors
//
// All error values returned from this package implement fmt.Formatter and can
// be formatted by the fmt package. The following verbs are supported:
//
// %s print the error. If the error has a Cause it will be
// printed recursively.
// %v see %s
// %+v extended format. Each Frame of the error's StackTrace will
// be printed in detail.
// %s print the error. If the error has a Cause it will be
// printed recursively.
// %v see %s
// %+v extended format. Each Frame of the error's StackTrace will
// be printed in detail.
//
// Retrieving the stack trace of an error or wrapper
// # Retrieving the stack trace of an error or wrapper
//
// New, Errorf, Wrap, and Wrapf record a stack trace at the point they are
// invoked. This information can be retrieved with the following interface:
//
// type stackTracer interface {
// StackTrace() errors.StackTrace
// }
// type stackTracer interface {
// StackTrace() errors.StackTrace
// }
//
// The returned errors.StackTrace type is defined as
//
// type StackTrace []Frame
// type StackTrace []Frame
//
// The Frame type represents a call site in the stack trace. Frame supports
// the fmt.Formatter interface that can be used for printing information about
// the stack trace of this error. For example:
//
// if err, ok := err.(stackTracer); ok {
// for _, f := range err.StackTrace() {
// fmt.Printf("%+s:%d\n", f, f)
// }
// }
// if err, ok := err.(stackTracer); ok {
// for _, f := range err.StackTrace() {
// fmt.Printf("%+s:%d\n", f, f)
// }
// }
//
// Although the stackTracer interface is not exported by this package, it is
// considered a part of its stable public interface.
Expand Down Expand Up @@ -340,9 +340,9 @@ func (w *withCode) Unwrap() error { return w.cause }
// An error value has a cause if it implements the following
// interface:
//
// type causer interface {
// Cause() error
// }
// type causer interface {
// Cause() error
// }
//
// If the error does not implement Cause, the original error will
// be returned. If the error is nil, nil will be returned without further
Expand Down
29 changes: 16 additions & 13 deletions errors/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,26 @@ type formatInfo struct {
// Format implements fmt.Formatter. https://golang.org/pkg/fmt/#hdr-Printing
//
// Verbs:
// %s - Returns the user-safe error string mapped to the error code or
// ┊ the error message if none is specified.
// %v Alias for %s
//
// %s - Returns the user-safe error string mapped to the error code or
// ┊ the error message if none is specified.
// %v Alias for %s
//
// Flags:
// # JSON formatted output, useful for logging
// - Output caller details, useful for troubleshooting
// + Output full error stack details, useful for debugging
//
// # JSON formatted output, useful for logging
// - Output caller details, useful for troubleshooting
// + Output full error stack details, useful for debugging
//
// Examples:
// %s: error for internal read B
// %v: error for internal read B
// %-v: error for internal read B - #0 [/xxx/main.go:12 (main.main)] (#100102) Internal Server Error
// %+v: error for internal read B - #0 [/xxx/main.go:12 (main.main)] (#100102) Internal Server Error; error for internal read A - #1 [/xxx/main.go:35 (main.newErrorB)] (#100104) Validation failed
// %#v: [{"error":"error for internal read B"}]
// %#-v: [{"caller":"#0 /xxx/main.go:12 (main.main)","error":"error for internal read B","message":"(#100102) Internal Server Error"}]
// %#+v: [{"caller":"#0 /xxx/main.go:12 (main.main)","error":"error for internal read B","message":"(#100102) Internal Server Error"},{"caller":"#1 /xxx/main.go:35 (main.newErrorB)","error":"error for internal read A","message":"(#100104) Validation failed"}]
//
// %s: error for internal read B
// %v: error for internal read B
// %-v: error for internal read B - #0 [/xxx/main.go:12 (main.main)] (#100102) Internal Server Error
// %+v: error for internal read B - #0 [/xxx/main.go:12 (main.main)] (#100102) Internal Server Error; error for internal read A - #1 [/xxx/main.go:35 (main.newErrorB)] (#100104) Validation failed
// %#v: [{"error":"error for internal read B"}]
// %#-v: [{"caller":"#0 /xxx/main.go:12 (main.main)","error":"error for internal read B","message":"(#100102) Internal Server Error"}]
// %#+v: [{"caller":"#0 /xxx/main.go:12 (main.main)","error":"error for internal read B","message":"(#100102) Internal Server Error"},{"caller":"#1 /xxx/main.go:35 (main.newErrorB)","error":"error for internal read A","message":"(#100104) Validation failed"}]
func (w *withCode) Format(state fmt.State, verb rune) {
switch verb {
case 'v':
Expand Down
20 changes: 10 additions & 10 deletions errors/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ func (f Frame) name() string {

// Format formats the frame according to the fmt.Formatter interface.
//
// %s source file
// %d source line
// %n function name
// %v equivalent to %s:%d
// %s source file
// %d source line
// %n function name
// %v equivalent to %s:%d
//
// Format accepts flags that alter the printing of some verbs, as follows:
//
// %+s function name and path of source file relative to the compile time
// GOPATH separated by \n\t (<funcname>\n\t<path>)
// %+v equivalent to %+s:%d
// %+s function name and path of source file relative to the compile time
// GOPATH separated by \n\t (<funcname>\n\t<path>)
// %+v equivalent to %+s:%d
func (f Frame) Format(s fmt.State, verb rune) {
switch verb {
case 's':
Expand Down Expand Up @@ -98,12 +98,12 @@ type StackTrace []Frame

// Format formats the stack of Frames according to the fmt.Formatter interface.
//
// %s lists source files for each Frame in the stack
// %v lists the source file and line number for each Frame in the stack
// %s lists source files for each Frame in the stack
// %v lists the source file and line number for each Frame in the stack
//
// Format accepts flags that alter the printing of some verbs, as follows:
//
// %+v Prints filename, function, and line number for each Frame in the stack.
// %+v Prints filename, function, and line number for each Frame in the stack.
func (st StackTrace) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
Expand Down
18 changes: 16 additions & 2 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
## example

### 启动consul (如果是基于consul服务注册发现)

### 安装依赖consul、jaeger
```shell
docker run --restart=always -d -p 8500:8500 -p 8300:8300 -p 8301:8301 -p 8302:8302 -p 8600:8600/udp bitnami/consul:latest consul agent -dev -client=0.0.0.0

docker run -d --name jaeger \
-e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
-e COLLECTOR_OTLP_ENABLED=true \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 4317:4317 \
-p 4318:4318 \
-p 14250:14250 \
-p 14268:14268 \
-p 14269:14269 \
-p 9411:9411 \
jaegertracing/all-in-one:1.54
```


17 changes: 15 additions & 2 deletions example/app_server_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/**
/*
*
User: cr-mao
Date: 2024/2/19 12:48
Email: crmao@qq.com
Expand All @@ -14,6 +15,7 @@ import (
"github.com/cr-mao/lori/log"
"github.com/cr-mao/lori/pprof_server"
"github.com/cr-mao/lori/registry/consul"
"github.com/cr-mao/lori/trace"
"github.com/hashicorp/consul/api"
"strconv"
"testing"
Expand All @@ -38,6 +40,14 @@ func registerServer(server *grpc.Server) {
}

func TestAppServer(t *testing.T) {

trace.InitAgent(trace.Options{
Name: "lori_example",
Endpoint: "http://127.0.0.1:14268/api/traces",
Sampler: 1.0,
Batcher: "jaeger",
})

c := api.DefaultConfig()
c.Address = "127.0.0.1:8500"
c.Scheme = "http"
Expand All @@ -46,7 +56,10 @@ func TestAppServer(t *testing.T) {
panic(err)
}
r := consul.New(cli, consul.WithHealthCheck(true))
grpcServer := grpc.NewServer(grpc.WithAddress("0.0.0.0:8081"))
grpcServer := grpc.NewServer(
grpc.WithAddress("0.0.0.0:8081"),
grpc.WithEnableTrace(true),
)
pprofPort, _ := netlib.AssignRandPort()
pprofServer := pprof_server.NewPProf("0.0.0.0:" + strconv.Itoa(pprofPort))
registerServer(grpcServer)
Expand Down
Loading

0 comments on commit e3a931f

Please sign in to comment.