-
Notifications
You must be signed in to change notification settings - Fork 2
/
spew.go
174 lines (147 loc) · 4.74 KB
/
spew.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Package spew provides a set of middlewares for the KrakenD framework ready to start dumping
// (pretty printed) request and response pairs into files
//
package spew
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"path"
"sync"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/luraproject/lura/config"
"github.com/luraproject/lura/logging"
"github.com/luraproject/lura/proxy"
)
// New returns a proxy middleware ready to start dumping all the requests and responses
// it processes.
func New(logger logging.Logger, name string, dumper Dumper) proxy.Middleware {
return func(next ...proxy.Proxy) proxy.Proxy {
switch len(next) {
case 0:
panic(proxy.ErrNotEnoughProxies)
case 1:
default:
panic(proxy.ErrTooManyProxies)
}
return func(ctx context.Context, req *proxy.Request) (*proxy.Response, error) {
resp, err := next[0](ctx, req)
logger.Debug("spew: capturing request and response at the", name, "layer")
dumper.Dump(name, req, resp, err)
return resp, err
}
}
}
// ProxyFactory returns a proxy.FactoryFunc over the received proxy.FactoryFunc with a spew middleware wrapping
// the generated pipe
func ProxyFactory(logger logging.Logger, factory proxy.Factory, df DumperFactory) proxy.FactoryFunc {
dumper := df(SpewFormater)
return func(cfg *config.EndpointConfig) (proxy.Proxy, error) {
p, err := factory.New(cfg)
if err != nil {
return p, err
}
name := "proxy_" + base64.URLEncoding.EncodeToString([]byte(cfg.Endpoint))
mw := New(logger, name, dumper)
return mw(p), nil
}
}
// BackendFactory returns a proxy.BackendFactory over the received proxy.BackendFactory with a spew middleware wrapping
// the generated backend
func BackendFactory(logger logging.Logger, factory proxy.BackendFactory, df DumperFactory) proxy.BackendFactory {
dumper := df(SpewFormater)
return func(backend *config.Backend) proxy.Proxy {
name := "backend_" + base64.URLEncoding.EncodeToString([]byte(backend.URLPattern))
mw := New(logger, name, dumper)
return mw(factory(backend))
}
}
// Dumper is the interface for the structs responsibles of persisting the inspected pair req/resp after formating them
type Dumper interface {
Dump(id string, req interface{}, resp interface{}, err error)
}
// DumperFactory is the signature of a function that returns a dumper with the received formater injected.
type DumperFactory func(formater Formater) Dumper
// NewFileDumperFactory creates a DumperFactory for building dumpers writing the intercepted data as txt into the filesystem.
//
// The txt files will be stored in the path defined by the output argument, using the name argument
// as a prefix, so a pair request and response named "xxx" will be stored in the output folder as xxx_{timestamp}.txt
func NewFileDumperFactory(ctx context.Context, path string, l logging.Logger) DumperFactory {
once.Do(func() {
fileFlusher = flusher{in: make(chan dumpedItem, 100)}
for i := 0; i < fileWriterWorkers; i++ {
go fileFlusher.consume(ctx, l)
}
})
return func(formater Formater) Dumper {
return &fileDumper{
path: path,
l: l,
formater: formater,
out: fileFlusher.in,
}
}
}
type fileDumper struct {
path string
l logging.Logger
formater Formater
out chan dumpedItem
}
func (f *fileDumper) Dump(name string, req interface{}, resp interface{}, err error) {
d := dumpedItem{
path: fmt.Sprintf("%s_%d.txt", path.Join(f.path, name), time.Now().UnixNano()),
content: f.formater(req, resp, err),
l: f.l,
}
select {
case f.out <- d:
default:
}
}
// Formater is the signature of a function that transform the inspected data into a byte array representation
type Formater func(req interface{}, resp interface{}, err error) []byte
// SpewFormater is a function that dumps the inspected data using the spew lib
func SpewFormater(req interface{}, resp interface{}, err error) []byte {
bf := new(bytes.Buffer)
writeHeader(bf, "Request")
spew.Fdump(bf, req)
writeHeader(bf, "Response")
spew.Fdump(bf, resp)
writeHeader(bf, "error")
spew.Fdump(bf, err)
return bf.Bytes()
}
const lineSeparation = "\n*************************************************************\n"
func writeHeader(w io.Writer, msg string) {
w.Write([]byte(lineSeparation + msg + lineSeparation))
}
type dumpedItem struct {
path string
content []byte
l logging.Logger
}
var (
fileFlusher flusher
once = new(sync.Once)
fileWriterWorkers = 5
)
type flusher struct {
in chan dumpedItem
}
func (f flusher) consume(ctx context.Context, l logging.Logger) {
for {
select {
case <-ctx.Done():
return
case i := <-f.in:
if err := ioutil.WriteFile(i.path, i.content, 0666); err != nil && i.l != nil {
l.Error("spew: writing the captured data:", err.Error())
}
}
}
}