ginrequestid

package module
v0.0.0-...-7ce47d2 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2019 License: GPL-3.0 Imports: 2 Imported by: 0

README

Gingonic X-Request-Id middleware

Basic middleware to add and expose an UUID4 for each request based on the code posted on Dan Sosedoff development and experiments website.

It will set the variable RequestId or order to use it from within the application for logging or propagation.

If X-Request-Id header is sent in the request, it will set that value as RequestId.

Installation

Use go get to fetch the package:

$ go get github.com/atarantini/ginrequestid

Usage

Import it and use it in your router:

import (
    ...
    "github.com/gin-gonic/gin"
    "github.com/atarantini/ginrequestid"
    ...
)

func main() {
    // Initialize router
    r := gin.Default()

    // Load middleware
    r.Use(ginrequestid.RequestId())

    // Your routes
    r.GET("/ping", func(c *gin.Context) {
        c.String(200, "pong")
    })

    // Start server
    r.Run()
}

Check if it's working:

$ curl --verbose localhost:8080/ping
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /ping HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/plain; charset=utf-8
< X-Request-Id: 3528d29d-f5c6-4758-ae05-eee5a56c27ea
< Date: Wed, 25 Jan 2017 12:42:30 GMT
< Content-Length: 4
<
* Connection #0 to host localhost left intact

Use it for logging::

r.GET("/ping", func(c *gin.Context) {
    fmt.Println(fmt.Sprintf("[request-id:%s][event:ping]", c.MustGet("RequestId")))
    c.String(200, "pong")
})

Changelog

0.0.2 - 2018-03-06

  • Fix for change in signature in the main dependecy of the package. In order to use the last version of github.com/satori/go.uuid update to 0.0.2 version.

0.0.1 - 2017-02-07

  • Initial release, basic middleware. It works!

Author

Andres Tarantini (atarantini@gmail.com)

License

GNU GPL v3. See LICENSE file.

Documentation

Overview

ginrequestid

Set an UUID4 string as Request ID into response headers ("X-Request-Id") and expose that value as "RequestId" in order to use it inside the application for logging or propagation to other systems.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RequestId

func RequestId() gin.HandlerFunc

Types

This section is empty.

Jump to

Keyboard shortcuts

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