Skip to content

Commit

Permalink
完成issue377 (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Nov 9, 2023
1 parent 9ad575c commit 511f37c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 14 deletions.
18 changes: 17 additions & 1 deletion debug/api.go
Expand Up @@ -25,7 +25,7 @@ func NoColor() Apply {
type file struct{ fileName string }

func (f *file) Write(p []byte) (n int, err error) {
fd, err := os.OpenFile(f.fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
fd, err := os.OpenFile(f.fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
if err != nil {
return
}
Expand Down Expand Up @@ -60,6 +60,22 @@ func OnlyTraceFlag() Apply {
})
}

// trace信息格式化成json输出至标准输出
func TraceJSON() Apply {
return TraceJSONToWriter(os.Stdout)
}

// trace信息格式化成json输出至w
func TraceJSONToWriter(w io.Writer) Apply {
return Func(func(o *Options) {
o.Color = false
o.Debug = false
o.Trace = true
o.FormatTraceJSON = true
o.Write = w
})
}

// 打开Trace()
func Trace() Apply {
return Func(func(o *Options) {
Expand Down
23 changes: 12 additions & 11 deletions debug/debug.go
Expand Up @@ -19,23 +19,24 @@ func ToBodyType(s string) color.BodyType {
case "json":
return color.JSONType
case "xml":
//return color.XmlType //TODO open
// return color.XmlType //TODO open
case "yaml":
//return color.YamlType //TODO open
// return color.YamlType //TODO open
}

return color.TxtType
}

// Options Debug mode core data structure
type Options struct {
EscapeHTML bool
Write io.Writer
Debug bool
Color bool
Trace bool
ReqBodyType string
RspBodyType string
EscapeHTML bool
Write io.Writer
Debug bool
Color bool
Trace bool
FormatTraceJSON bool
ReqBodyType string
RspBodyType string
TraceInfo
}

Expand Down Expand Up @@ -106,7 +107,7 @@ func (do *Options) debugPrint(req *http.Request, rsp *http.Response) error {
return err
}

var r = io.Reader(b)
r := io.Reader(b)
format := color.NewFormatEncoder(r, do.Color, ToBodyType(do.ReqBodyType), do.EscapeHTML)
if format != nil {
r = format
Expand All @@ -126,7 +127,7 @@ func (do *Options) debugPrint(req *http.Request, rsp *http.Response) error {

fmt.Fprintf(w, "\r\n\r\n")
// write rsp body
var r = io.Reader(rsp.Body)
r := io.Reader(rsp.Body)
format := color.NewFormatEncoder(r, do.Color, ToBodyType(do.RspBodyType), do.EscapeHTML)
if format != nil {
r = format
Expand Down
15 changes: 13 additions & 2 deletions debug/debug_trace.go
Expand Up @@ -2,6 +2,7 @@ package debug

import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -34,7 +35,6 @@ func (t *TraceInfo) StartTrace(opt *Options, needTrace bool, req *http.Request,
if needTrace {
startNow = time.Now()
trace := &httptrace.ClientTrace{

DNSStart: func(_ httptrace.DNSStartInfo) {
dnsStart = time.Now()
},
Expand Down Expand Up @@ -87,7 +87,18 @@ func (t *TraceInfo) StartTrace(opt *Options, needTrace bool, req *http.Request,
t.ResponseDuration = time.Since(respStart)
t.TotalDuration = time.Since(startNow)
t.w = w
t.output(opt)
// 格式化成json
if opt.FormatTraceJSON {
all, err := json.Marshal(t)
if err != nil {
return resp, err
}
if _, err := w.Write(all); err != nil {
return resp, err
}
} else {
t.output(opt)
}
}
return resp, err
}
Expand Down
36 changes: 36 additions & 0 deletions trace_test.go
@@ -0,0 +1,36 @@
package gout

import (
"bytes"
"testing"

"github.com/guonaihong/gout/dataflow"
"github.com/guonaihong/gout/debug"
)

// 测试trace的入口函数
func Test_Trace(t *testing.T) {
t.Run("TraceJSONToWriter", func(t *testing.T) {
//大约是这样的字符串
//{"DnsDuration":0,"ConnDuration":1490250,"TLSDuration":0,"RequestDuration":33166,"WaitResponeDuration":258084,"ResponseDuration":10708,"TotalDuration":1810834}
ts := createMethodEcho()
defer ts.Close()
var buf bytes.Buffer
err := dataflow.New().GET(ts.URL).Debug(debug.TraceJSONToWriter(&buf)).Do()
if err != nil {
t.Fatal(err)
}
pos := bytes.Index(buf.Bytes(), []byte("ConnDuration"))
if pos == -1 {
t.Fatal("not found ConnDuration")
}
})
t.Run("TraceJSON", func(t *testing.T) {
ts := createMethodEcho()
defer ts.Close()
err := dataflow.New().GET(ts.URL).Debug(debug.TraceJSON()).Do()
if err != nil {
t.Fatal(err)
}
})
}

0 comments on commit 511f37c

Please sign in to comment.