middleware

package module
v0.0.0-...-e57a87d Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2018 License: MIT Imports: 9 Imported by: 1

README

Echo Middlewares

Build Status Coverage Status GoDoc Go Report Card License

Middlewares for Echo Framework

Installation

Requires Go 1.9 or later.

go get github.com/dafiti/echo-middleware

Middlewares

  • New Relic
  • LoggerWithOutput (Retrieves Logger middleware with Output)
  • Logrus (Http request logs)

Usage Examples

package main

import (
    "bytes"
    "net/http"
	mw "github.com/dafiti/echo-middleware"
    "github.com/labstack/echo"
)

func main() {
    e := echo.New()
    e.Use(mw.NewRelic("app name", "license key"))

    // Default Logger middleware
    //buf := new(bytes.Buffer)
    //e.Use(mw.LoggerWithOutput(buf))

    // Logrus HTTP request logs
    e.Use(mw.Logrus())

    e.GET("/", func(c echo.Context) error {
        txn := c.Get("newrelic-txn").(newrelic.Transaction)
        defer newrelic.StartSegment(txn, "mySegmentName").End()

        return c.String(http.StatusOK, "Hello, World!")
    })

    e.Logger.Fatal(e.Start(":1323"))
}

Documentation

Read the full documentation at https://godoc.org/github.com/dafiti/echo-middleware.

License

This project is released under the MIT licence. See LICENCE for more details.

Documentation

Index

Constants

View Source
const (
	// NEWRELIC_TXN defines the context key used to save newrelic transaction
	NEWRELIC_TXN = "newrelic-txn"
)

Variables

View Source
var (
	// DefaultLogrusConfig is the default Logrus middleware config.
	DefaultLogrusConfig = LogrusConfig{
		FieldMap: map[string]string{
			"id":            "@id",
			"remote_ip":     "@remote_ip",
			"uri":           "@uri",
			"host":          "@host",
			"method":        "@method",
			"status":        "@status",
			"latency":       "@latency",
			"latency_human": "@latency_human",
			"bytes_in":      "@bytes_in",
			"bytes_out":     "@bytes_out",
		},
		Logger:  logrus.StandardLogger(),
		Skipper: mw.DefaultSkipper,
	}
)

Functions

func LoggerWithOutput

func LoggerWithOutput(w io.Writer) echo.MiddlewareFunc

LoggerWithOutput returns a Logger middleware with output. See: `Logger()`.

func Logrus

func Logrus() echo.MiddlewareFunc

Logrus returns a middleware that logs HTTP requests.

func LogrusWithConfig

func LogrusWithConfig(config LogrusConfig) echo.MiddlewareFunc

LogrusWithConfig returns a Logrus middleware with config. See: `Logrus()`.

func NewRelic

func NewRelic(appName string, licenseKey string) echo.MiddlewareFunc

NewRelic returns a middleware that collect request data for NewRelic

func NewRelicWithApplication

func NewRelicWithApplication(app nr.Application) echo.MiddlewareFunc

NewRelicWithApplication returns a NewRelic middleware with application. See: `NewRelic()`.

Types

type LogrusConfig

type LogrusConfig struct {
	// FieldMap set a list of fields with tags
	//
	// Tags to constructed the logger fields.
	//
	// - @id (Request ID)
	// - @remote_ip
	// - @uri
	// - @host
	// - @method
	// - @path
	// - @referer
	// - @user_agent
	// - @status
	// - @latency (In nanoseconds)
	// - @latency_human (Human readable)
	// - @bytes_in (Bytes received)
	// - @bytes_out (Bytes sent)
	// - @header:<NAME>
	// - @query:<NAME>
	// - @form:<NAME>
	// - @cookie:<NAME>
	FieldMap map[string]string

	// Logger it is a logrus logger
	Logger logrus.FieldLogger

	// Skipper defines a function to skip middleware.
	Skipper mw.Skipper
}

LogrusConfig defines the config for Logrus middleware.

Jump to

Keyboard shortcuts

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