当前位置:网站首页>Echo framework: automatically add requestid
Echo framework: automatically add requestid
2022-06-24 00:58:00 【Trespass 】
Introduce
Through a complete example , Based on Echo In the frame , For every one API Automatic addition RequestId .
We will use rk-boot To start up Echo Microservices .
Please visit the following address for a complete tutorial :
install
go get github.com/rookie-ninja/rk-boot go get github.com/rookie-ninja/rk-echo
Quick start
Open the meta After middleware , Each request will automatically contain the following values .
Header key | details |
|---|---|
X-Request-Id | The middleware will automatically generate the request ID. |
X-Prefix-App | The service name . |
X-Prefix-App-Version | Service version . |
X-Prefix-App-Unix-Time | Current service Unix Time . |
X-Prefix-Request-Received-Time | Timestamp of the request received . |
1. establish boot.yaml
In order to verify , We started commonService,commonService Contains a series of commonly used API, for example /rk/v1/healthy.
---
echo:
- name: greeter # Required
port: 8080 # Required
enabled: true # Required
commonService:
enabled: true # Optional, enable common service
interceptors:
meta:
enabled: true # Optional, enable meta middleware2. establish main.go
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"github.com/rookie-ninja/rk-boot"
_ "github.com/rookie-ninja/rk-echo/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. start-up main.go
$ go run main.go
4. verification
$ curl -vs -X GET localhost:8080/rk/v1/healthy
...
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< X-Request-Id: 78c25a06-3b34-4ecb-b9dd-7197078873c7
< X-Rk-App-Name: rk-demo
< X-Rk-App-Unix-Time: 2021-11-21T00:24:49.662023+08:00
< X-Rk-App-Version: master-2c9c6fd
< X-Rk-Received-Time: 2021-11-21T00:24:49.662023+08:00
...
{"healthy":true}Cover requestId
If we want to customize requestId, Need to add a Header.
func Greeter(ctx echo.Context) error {
// Override request id
rkechoctx.SetHeaderToClient(ctx, rkechoctx.RequestIdKey, "request-id-override")
// We expect new request id attached to logger
rkechoctx.GetLogger(ctx).Info("Received request")
return ctx.JSON(http.StatusOK, &GreeterResponse{
Message: fmt.Sprintf("Hello %s!", ctx.QueryParam("name")),
})
}
// Response.
type GreeterResponse struct {
Message string
}RequestId Will be covered .
$ 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-11-21T00:37:16.514685+08:00
< X-Rk-App-Version: master-2c9c6fd
< X-Rk-Received-Time: 2021-11-21T00:37:16.514685+08:00
...
{"Message":"Hello rk-dev!"}If we start the logging middleware , Then we will see the following log .
2021-11-22T00:30:39.442+0800 INFO basic/main.go:46 Received request {"requestId": "request-id-override"}------------------------------------------------------------------------
endTime=2021-11-22T00:30:39.442963+08:00
startTime=2021-11-22T00:30:39.442746+08:00
elapsedNano=216828
timezone=CST
ids={"eventId":"request-id-override","requestId":"request-id-override"}
app={"appName":"rk-demo","appVersion":"master-5507ff5","entryName":"greeter","entryType":"EchoEntry"}
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:52446
operation=/v1/greeter
resCode=200
eventStatus=Ended
EOECover header Prefix
---
echo:
- name: greeter
...
interceptors:
meta:
enabled: true # Enable meta 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-11-21T00:40:15.761993+08:00
< X-Override-App-Version: master-2c9c6fd
< X-Override-Received-Time: 2021-11-21T00:40:15.761993+08:00
< X-Request-Id: 1449deb5-464d-4b65-8430-985413c2671b
...
{"healthy":true}边栏推荐
- 股票网上开户安全吗?需要满足什么条件?
- setfacl命令的基本用法
- 通达信股票开户是安全的吗?
- Theoretical analysis of countermeasure training: adaptive step size fast countermeasure training
- 苹果Iphone14搭载北斗导航系统,北斗VS GPS有哪些优势?
- WinSCP和PuTTY的安装和使用
- [technique of planting grass] spit blood and clean up, and take you to collect goose feathers in a fancy way! Do not spread!!!
- 13 `bs_duixiang.tag标签`得到一个tag对象
- 智能制造时代下,MES管理系统需要解决哪些问题
- Real time computing framework: Flink cluster construction and operation mechanism
猜你喜欢

Android Aidl: cross process call service (Aidl service), kotlininvoke function

kubernetes之常用核心资源对象

CVPR2022 | 可精简域适应

利用Scanorama高效整合异质单细胞转录组

Superscalar processor design yaoyongbin Chapter 3 virtual memory -- Excerpt from subsection 3.1~3.2

GNN upper edge distributor! Instead of trying to refine pills, you might as well give your GNN some tricks

【Redis进阶之ZipList】如果再有人问你什么是压缩列表?请把这篇文章直接甩给他。

Open source model library of flying propeller industry: accelerating the development and application of enterprise AI tasks

paddle使用指南

数字化工厂可以分为哪两类
随机推荐
The industrial Internet era will be realized by products, technologies and models derived from the industry itself
ICML'22 | ProGCL: 重新思考图对比学习中的难样本挖掘
[applet] realize the effect of double column commodities
ShardingSphere-proxy-5.0.0容量范围分片的实现(五)
Empty encoded password warning reason
使用递归形成多级目录树结构,附带可能是全网最详细注释。
[OSG] OSG development (04) - create multiple scene views
【小程序】编译预览小程序时,出现-80063错误提示
实时计算框架:Spark集群搭建与入门案例
纯js实现判断ip是否ping通
Application configuration management, basic principle analysis
【Flutter】如何使用Flutter包和插件
[iccv workshop 2021] small target detection based on density map: coarse-grained density map guided object detection in aerial images
JS language precision problem
【CVPR 2020】会议版本:A Physics-based Noise Formation Model for Extreme Low-light Raw Denoising
实时计算框架:Flink集群搭建与运行机制
Shardingsphere-proxy-5.0.0 implementation of capacity range partition (V)
Mip-NeRF:抗混叠的多尺度神经辐射场ICCV2021
version `ZLIB_ 1.2.9‘ not found (required by /lib64/libpng16.so.16)
股票网上开户安全吗?需要满足什么条件?