Documentation ¶
Overview ¶
Package mux defines a set of basic building blocks for instrumenting KrakenD gateways built using the mux router
Example (Backend) ¶
ctx := context.Background() buf := bytes.NewBuffer(make([]byte, 1024)) l, _ := logging.NewLogger("DEBUG", buf, "") metricProducer := New(ctx, defaultCfg, l) bf := metricProducer.BackendFactory("backend_layer", proxy.CustomHTTPProxyFactory(client.NewHTTPClient)) p := bf(&config.Backend{URLPattern: "/some/{url}", Decoder: encoding.JSONDecoder}) h := metricProducer.NewHTTPHandler("test", http.HandlerFunc(dummyHTTPHandler)) ts1 := httptest.NewServer(h) defer ts1.Close() parsedURL, _ := url.Parse(ts1.URL) req := proxy.Request{URL: parsedURL, Method: "GET", Body: ioutil.NopCloser(strings.NewReader(""))} for i := 0; i < 10; i++ { resp, err := p(ctx, &req) if err != nil { fmt.Println("Error:", err) } if !resp.IsComplete { fmt.Println("Unexpected response:", *resp) } } ts2 := httptest.NewServer(metricProducer.NewExpHandler()) defer ts2.Close() resp, err := http.Get(ts2.URL) if err != nil { fmt.Println("Error:", err) } data, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("Error:", err) } resp.Body.Close() if len(data) < 5000 { fmt.Println("unexpected response size:", len(data)) } // Uncomment to see what's been logged // fmt.Println(buf.String()) // Uncomment the next line and remove the # to see the stats report as the test fails // fmt.Println(string(data)) // #Output: golly
Output:
Example (Proxy) ¶
ctx := context.Background() buf := bytes.NewBuffer(make([]byte, 1024)) l, _ := logging.NewLogger("DEBUG", buf, "") cfg := &config.EndpointConfig{ Endpoint: "/test/endpoint", } metricProducer := New(ctx, defaultCfg, l) response := proxy.Response{Data: map[string]interface{}{}, IsComplete: true} fakeFactory := proxy.FactoryFunc(func(_ *config.EndpointConfig) (proxy.Proxy, error) { return func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) { return &response, nil }, nil }) pf := metricProducer.ProxyFactory("proxy_layer", fakeFactory) p, err := pf.New(cfg) if err != nil { fmt.Println("Error:", err) } req := proxy.Request{} for i := 0; i < 10; i++ { resp, err := p(ctx, &req) if err != nil { fmt.Println("Error:", err) } if resp != &response { fmt.Println("Unexpected response:", *resp) } } ts := httptest.NewServer(metricProducer.NewExpHandler()) defer ts.Close() resp, err := http.Get(ts.URL) if err != nil { fmt.Println("Error:", err) } data, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("Error:", err) } resp.Body.Close() if len(data) < 5000 { fmt.Println("unexpected response size:", len(data)) } // Uncomment to see what's been logged // fmt.Println(buf.String()) // Uncomment the next line and remove the # to see the stats report as the test fails // fmt.Println(string(data)) // #Output: golly
Output:
Example (Router) ¶
ctx, cancel := context.WithCancel(context.Background()) defer cancel() buf := bytes.NewBuffer(make([]byte, 1024)) l, _ := logging.NewLogger("DEBUG", buf, "") metricProducer := New(ctx, defaultCfg, l) h := metricProducer.NewHTTPHandler("test", http.HandlerFunc(dummyHTTPHandler)) ts1 := httptest.NewServer(h) defer ts1.Close() for i := 0; i < 10; i++ { resp, err := http.Get(ts1.URL) if err != nil { fmt.Println(err) } if resp.Header.Get("x-test") != "ok" { fmt.Println("unexpected header:", resp.Header.Get("x-test")) } if resp.StatusCode != 200 { fmt.Println("unexpected status code:", resp.StatusCode) } } metricProducer.Router.Aggregate() ts2 := httptest.NewServer(metricProducer.NewExpHandler()) defer ts2.Close() resp, err := http.Get(ts2.URL) if err != nil { fmt.Println(err) } data, _ := ioutil.ReadAll(resp.Body) resp.Body.Close() if len(data) < 5000 { fmt.Println("unexpected response size:", len(data)) } // Uncomment to see what's been logged // fmt.Println(buf.String()) // Uncomment the next line and remove the # to see the stats report as the test fails // fmt.Println(string(data)) // #Output: golly
Output:
Index ¶
- func NewExpHandler(parent *metrics.Registry) http.Handler
- func NewHTTPHandler(name string, h http.Handler, rm *krakendmetrics.RouterMetrics) http.HandlerFunc
- type Metrics
- func (m *Metrics) NewEngine() *http.ServeMux
- func (m *Metrics) NewExpHandler() http.Handler
- func (m *Metrics) NewHTTPHandler(name string, h http.Handler) http.HandlerFunc
- func (m *Metrics) NewHTTPHandlerFactory(defaultHandlerFactory mux.HandlerFactory) mux.HandlerFactory
- func (m *Metrics) RunEndpoint(ctx context.Context, s *http.ServeMux, l logging.Logger)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewExpHandler ¶
NewExpHandler creates an http.Handler ready to expose all the collected metrics as a JSON
func NewHTTPHandler ¶
func NewHTTPHandler(name string, h http.Handler, rm *krakendmetrics.RouterMetrics) http.HandlerFunc
NewHTTPHandler wraps an http.Handler adding some simple instrumentation to the handler
Types ¶
type Metrics ¶
type Metrics struct {
*krakendmetrics.Metrics
}
Metrics is the component that manages all the metrics for the mux-based gateways
func (*Metrics) NewExpHandler ¶
NewExpHandler creates an http.Handler ready to expose all the collected metrics as a JSON
func (*Metrics) NewHTTPHandler ¶
NewHTTPHandler wraps an http.Handler adding some simple instrumentation to the handler
func (*Metrics) NewHTTPHandlerFactory ¶
func (m *Metrics) NewHTTPHandlerFactory(defaultHandlerFactory mux.HandlerFactory) mux.HandlerFactory
Click to show internal directories.
Click to hide internal directories.