spew

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2021 License: Apache-2.0 Imports: 13 Imported by: 0

README

krakend-spew

Spew exporter middleware for the KrakenD framework

Master Caution: running this module in production will kill your performance!!!

Usage

Add the ProxyFactory, BackendFactory, ClientFactory and/or RunServer functions in your factory stack as showed in the example:

import (
	...
	spew "github.com/devopsfaith/krakend-spew"
	spewhttp "github.com/devopsfaith/krakend-spew/http"
	...
)

...

df := spew.NewFileDumperFactory(ctx, *output, logger)

// spew http client factory wrapper
cf := spewhttp.ClientFactory(logger, client.NewHTTPClient, df)
// spew backend proxy wrapper
bf := spew.BackendFactory(logger, proxy.CustomHTTPProxyFactory(cf), df)
// spew proxy wrapper
pf := spew.ProxyFactory(logger, proxy.NewDefaultFactory(bf, logger), df)
// spew router wrapper
runServer := spewhttp.RunServer(logger, router.RunServer, df)

routerFactory := krakendgin.NewFactory(krakendgin.Config{
	Engine:         gin.Default(),
	ProxyFactory:   pf,
	Logger:         logger,
	HandlerFactory: krakendgin.EndpointHandler,
	RunServer:      krakendgin.RunServerFunc(runServer),
})

routerFactory.NewWithContext(ctx).Run(serviceConfig)

Build and start the example:

$ go build -o sample ./example
$ ./sample -c example/krakend.json -d -l DEBUG
[GIN-debug] [WARNING] Now Gin requires Go 1.6 or later and Go 1.7 will be required soon.

[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:	export GIN_MODE=release
 - using code:	gin.SetMode(gin.ReleaseMode)

{{0xc000216480 [] 0x1555a60 0x1564500 {0 [KRAKEND]} 0x1564e40} 0xc0001bc0c0 0x1564e40}
[KRAKEND] DEBUG: Debug enabled
[GIN-debug] GET    /__debug/*param           --> github.com/devopsfaith/krakend-spew/vendor/github.com/devopsfaith/krakend/router/gin.DebugHandler.func1 (3 handlers)
[GIN-debug] POST   /__debug/*param           --> github.com/devopsfaith/krakend-spew/vendor/github.com/devopsfaith/krakend/router/gin.DebugHandler.func1 (3 handlers)
[GIN-debug] PUT    /__debug/*param           --> github.com/devopsfaith/krakend-spew/vendor/github.com/devopsfaith/krakend/router/gin.DebugHandler.func1 (3 handlers)
[GIN-debug] GET    /nick/:nick               --> github.com/devopsfaith/krakend-spew/vendor/github.com/devopsfaith/krakend/router/gin.CustomErrorEndpointHandler.func1 (3 handlers)
[GIN-debug] GET    /combination/:id          --> github.com/devopsfaith/krakend-spew/vendor/github.com/devopsfaith/krakend/router/gin.CustomErrorEndpointHandler.func1 (3 handlers)

After sending a test request to http://localhost:8000/nick/kpacha, you will see some log lines

[KRAKEND] DEBUG: spew: proxy request captured: proxy_L25pY2svOm5pY2s=
[KRAKEND] DEBUG: spew: proxy request captured: backend_L3VzZXJzL3t7Lk5pY2t9fQ==
[KRAKEND] DEBUG: spew: proxy request captured: backend_LzIuMC91c2Vycy97ey5OaWNrfX0=
[KRAKEND] DEBUG: spew: capturing http request and response at the backend layer
[KRAKEND] DEBUG: spew: capturing http request and response at the backend layer
[KRAKEND] DEBUG: spew: proxy executed
[KRAKEND] DEBUG: spew: proxy response captured: backend_LzIuMC91c2Vycy97ey5OaWNrfX0=
[KRAKEND] DEBUG: spew: capturing http request and response at the backend layer
[KRAKEND] DEBUG: spew: proxy executed
[KRAKEND] DEBUG: spew: proxy response captured: backend_L3VzZXJzL3t7Lk5pY2t9fQ==
[KRAKEND] DEBUG: spew: proxy executed
[KRAKEND] DEBUG: spew: proxy response captured: proxy_L25pY2svOm5pY2s=
[GIN] 2018/09/25 - 19:12:27 | 200 |   692.97048ms |             ::1 | GET      /nick/kpacha
[KRAKEND] DEBUG: spew: capturing http request and response at the router layer
...

You can stop the KrakenD and check the .txt files generated by the module:

2,0K 25 sep 19:12 backend_L3VzZXJzL3t7Lk5pY2t9fQ==_1537895547814979000.txt
1,8K 25 sep 19:12 backend_LzIuMC91c2Vycy97ey5OaWNrfX0=_1537895547800941000.txt
 92K 25 sep 19:12 client_aHR0cHM6Ly9hcGkuYml0YnVja2V0Lm9yZy8yLjAvdXNlcnMva3BhY2hh_1537895547798571000.txt
 92K 25 sep 19:12 client_aHR0cHM6Ly9hcGkuYml0YnVja2V0Lm9yZy8yLjAvdXNlcnMva3BhY2hh_1537895547800824000.txt
104K 25 sep 19:12 client_aHR0cHM6Ly9hcGkuZ2l0aHViLmNvbS91c2Vycy9rcGFjaGE=_1537895547814647000.txt
1,9K 25 sep 19:12 client_basic_aHR0cHM6Ly9hcGkuYml0YnVja2V0Lm9yZy8yLjAvdXNlcnMva3BhY2hh_1537895547796264000.txt
1,9K 25 sep 19:12 client_basic_aHR0cHM6Ly9hcGkuYml0YnVja2V0Lm9yZy8yLjAvdXNlcnMva3BhY2hh_1537895547798755000.txt
2,7K 25 sep 19:12 client_basic_aHR0cHM6Ly9hcGkuZ2l0aHViLmNvbS91c2Vycy9rcGFjaGE=_1537895547812621000.txt
2,3K 25 sep 19:12 proxy_L25pY2svOm5pY2s=_1537895547815244000.txt
 66K 25 sep 19:12 router_L25pY2sva3BhY2hh_1537895547816402000.txt

Documentation

Overview

Package spew provides a set of middlewares for the KrakenD framework ready to start dumping (pretty printed) request and response pairs into files

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BackendFactory

func BackendFactory(logger logging.Logger, factory proxy.BackendFactory, df DumperFactory) proxy.BackendFactory

BackendFactory returns a proxy.BackendFactory over the received proxy.BackendFactory with a spew middleware wrapping the generated backend

func New

func New(logger logging.Logger, name string, dumper Dumper) proxy.Middleware

New returns a proxy middleware ready to start dumping all the requests and responses it processes.

func ProxyFactory

func ProxyFactory(logger logging.Logger, factory proxy.Factory, df DumperFactory) proxy.FactoryFunc

ProxyFactory returns a proxy.FactoryFunc over the received proxy.FactoryFunc with a spew middleware wrapping the generated pipe

func SpewFormater

func SpewFormater(req interface{}, resp interface{}, err error) []byte

SpewFormater is a function that dumps the inspected data using the spew lib

Types

type Dumper

type Dumper interface {
	Dump(id string, req interface{}, resp interface{}, err error)
}

Dumper is the interface for the structs responsibles of persisting the inspected pair req/resp after formating them

type DumperFactory

type DumperFactory func(formater Formater) Dumper

DumperFactory is the signature of a function that returns a dumper with the received formater injected.

func NewFileDumperFactory

func NewFileDumperFactory(ctx context.Context, path string, l logging.Logger) DumperFactory

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

type Formater

type Formater func(req interface{}, resp interface{}, err error) []byte

Formater is the signature of a function that transform the inspected data into a byte array representation

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL