Middleware meta

Enable RPC meta interceptor/middleware for server.

Overview

Meta interceptor/middleware will attach bellow headers to server response.

Header key Description
X-Request-Id Request id generated by the interceptor.
X-[Prefix]-App Application name.
X-[Prefix]-App-Version Version of application.
X-[Prefix]-App-Unix-Time Unix time of current application.
X-[Prefix]-Request-Received-Time Time of current request received by application.

Installation

go get github.com/rookie-ninja/rk-boot

General options

These are general options to start a gin server with rk-boot

name description type default value
gin.name The name of gin server string N/A
gin.port The port of gin server integer nil, server won’t start
gin.enabled Enable gin entry bool false
gin.description Description of gin entry. string ""

Meta options

name description type default value
gin.interceptors.meta.enabled Enable meta interceptor boolean false
gin.interceptors.meta.prefix Header key was formed as X--XXX string RK

Quick start

1.Create boot.yaml

---
gin:
  - name: greeter
    port: 8080
    enabled: true
    commonService:
      enabled: true          # Enable common service for testing
    interceptors:
      meta:
        enabled: true        # Enable meta interceptor/middleware

2.Create main.go

package main

import (
	"context"
	"github.com/rookie-ninja/rk-boot"
)

// Application entrance.
func main() {
	// Create a new boot instance.
	boot := rkboot.NewBoot()

	// Bootstrap
	boot.Bootstrap(context.Background())

	// Wait for shutdown sig
	boot.WaitForShutdownSig(context.Background())
}

3.Validate

Send request

$ curl -vs -X GET localhost:8080/rk/v1/healthy
  ...
  < X-Request-Id: c2ec237e-e141-45af-a4ff-89ce8a9d0636
  < X-Rk-App-Name: rk-demo
  < X-Rk-App-Unix-Time: 2021-07-06T02:46:31.977902+08:00
  < X-Rk-App-Version: master-f414049
  < X-Rk-Received-Time: 2021-07-06T02:46:31.977902+08:00
  ...
  {"healthy":true}

Cheers

4.Override requestId

func Greeter(ctx *gin.Context) {
    // Override request id
	rkginctx.SetHeaderToClient(ctx, rkginctx.RequestIdKey, "request-id-override")
    // We expect new request id attached to logger
	rkginctx.GetLogger(ctx).Info("Received request")

	ctx.JSON(http.StatusOK, &GreeterResponse{
		Message: fmt.Sprintf("Hello %s!", ctx.Query("name")),
	})
}
$ curl -vs -X GET "localhost:8080/v1/greeter?name=rk-dev"
...
< X-Request-Id: request-id-override
< X-Rk-App-Name: rk-demo
< X-Rk-App-Unix-Time: 2021-07-06T02:49:34.27756+08:00
< X-Rk-App-Version: master-f414049
< X-Rk-Received-Time: 2021-07-06T02:49:34.27756+08:00
...
{"Message":"Hello rk-dev!"}

If we enabled logging interceptor/middleware, then we expect request as bellow

2021-07-06T02:49:34.277+0800    INFO    basic/main.go:41        Received request        {"requestId": "request-id-override"}
------------------------------------------------------------------------
endTime=2021-07-06T02:49:34.27773+08:00
startTime=2021-07-06T02:49:34.277544+08:00
elapsedNano=185412
timezone=CST
ids={"eventId":"request-id-override","requestId":"request-id-override"}
app={"appName":"rk-demo","appVersion":"master-f414049","entryName":"greeter","entryType":"GinEntry"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.2","os":"darwin","realm":"*","region":"*"}
payloads={"apiMethod":"GET","apiPath":"/v1/greeter","apiProtocol":"HTTP/1.1","apiQuery":"name=rk-dev","userAgent":"curl/7.64.1"}
error={}
counters={}
pairs={}
timing={}
remoteAddr=localhost:57600
operation=/v1/greeter
resCode=200
eventStatus=Ended
EOE

Cheers

5.Override header prefix

---
gin:
  - name: greeter
    ...
    interceptors:
      meta:
        enabled: true        # Enable meta interceptor/middleware
        prefix: "Override"   # Override prefix which formed as X-[Prefix]-xxx
$ curl -vs -X GET localhost:8080/rk/v1/healthy
...
< X-Override-App-Name: rk-demo
< X-Override-App-Unix-Time: 2021-07-06T02:52:53.035941+08:00
< X-Override-App-Version: master-f414049
< X-Override-Received-Time: 2021-07-06T02:52:53.035941+08:00
< X-Request-Id: c3097361-1833-4bba-9867-e8d1f2fb2207
...
{"healthy":true}

Cheers

Last modified September 24, 2021 : Update based on rk-boot:v1.2.4 (357cf4c)