当前位置:网站首页>Echo framework: implementing distributed log tracing
Echo framework: implementing distributed log tracing
2022-06-24 01:12:00 【Trespass 】
Introduce
Through a complete example , be based on Echo Framework for distributed log tracing .
What is? API Log tracking ?
One API Requests span multiple microservices , We hope to pass a unique ID Retrieve the log of the whole link .
We will use rk-boot To start up Echo Microservices of the framework .
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
We will create /v1/greeter API To verify , At the same time open logging, meta and tracing Middleware to achieve the purpose .
1. establish bootA.yaml & serverA.go
ServerA monitor 1949 port , And send a request to ServerB.
We go through rkechoctx.InjectSpanToNewContext() Method to Tracing Information is injected into Context in , Send to ServerB.
---
echo:
- name: greeter # Required
port: 1949 # Required
enabled: true # Required
interceptors:
loggingZap:
enabled: true # Optional, enable logging interceptor
meta:
enabled: true # Optional, enable meta interceptor
tracingTelemetry:
enabled: true # Optional, enable tracing interceptor// 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/labstack/echo/v4"
"github.com/rookie-ninja/rk-boot"
"github.com/rookie-ninja/rk-echo/boot"
"github.com/rookie-ninja/rk-echo/interceptor/context"
"net/http"
)
// Application entrance.
func main() {
// Create a new boot instance.
boot := rkboot.NewBoot(rkboot.WithBootConfigPath("bootA.yaml"))
// Register handler
boot.GetEntry("greeter").(*rkecho.EchoEntry).Echo.GET("/v1/greeter", GreeterA)
// Bootstrap
boot.Bootstrap(context.Background())
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}
// GreeterA will add trace info into context and call serverB
func GreeterA(ctx echo.Context) error {
// Call serverB at 2008
req, _ := http.NewRequest(http.MethodGet, "http://localhost:2008/v1/greeter", nil)
// Inject current trace information into context
rkechoctx.InjectSpanToHttpRequest(ctx, req)
// Call server
http.DefaultClient.Do(req)
// Respond to request
return ctx.String(http.StatusOK, "Hello from serverA!")
}2. establish bootB.yaml & serverB.go
ServerB monitor 2008 port .
---
echo:
- name: greeter # Required
port: 2008 # Required
enabled: true # Required
interceptors:
loggingZap:
enabled: true # Optional, enable logging interceptor
meta:
enabled: true # Optional, enable meta interceptor
tracingTelemetry:
enabled: true # Optional, enable tracing interceptor// 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/labstack/echo/v4"
"github.com/rookie-ninja/rk-boot"
"github.com/rookie-ninja/rk-echo/boot"
"net/http"
)
// Application entrance.
func main() {
// Create a new boot instance.
boot := rkboot.NewBoot(rkboot.WithBootConfigPath("bootB.yaml"))
// Register handler
boot.GetEntry("greeter").(*rkecho.EchoEntry).Echo.GET("/v1/greeter", GreeterB)
// Bootstrap
boot.Bootstrap(context.Background())
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}
// GreeterB will add trace info into context and call serverB
func GreeterB(ctx echo.Context) error {
// Respond to request
return ctx.String(http.StatusOK, "Hello from serverB!")
}3. Folder structure
. ├── bootA.yaml ├── bootB.yaml ├── go.mod ├── go.sum ├── serverA.go └── serverB.go 0 directories, 6 files
4. start-up ServerA & ServerB
$ go run serverA.go $ go run serverB.go
5. Go to ServerA Send a request
$ curl localhost:1949/v1/greeter Hello from serverA!
6. Verify the log
In the logs of the two services , There will be the same traceId, Different requestId.
We can go through grep traceId To track RPC.
- ServerA
------------------------------------------------------------------------
endTime=2021-11-19T23:56:47.681644+08:00
...
ids={"eventId":"e2670cdb-9a3c-42e9-ae8f-e01de3d8fbfa","requestId":"e2670cdb-9a3c-42e9-ae8f-e01de3d8fbfa","traceId":"eb466c6e0c46538027d8b8c2efc08baa"}
...
operation=/v1/greeter
resCode=200
eventStatus=Ended
EOE- ServerB
------------------------------------------------------------------------
endTime=2021-11-19T23:56:47.681362+08:00
...
ids={"eventId":"3c72b929-78bd-4ff1-b48c-3ad699429c45","requestId":"3c72b929-78bd-4ff1-b48c-3ad699429c45","traceId":"eb466c6e0c46538027d8b8c2efc08baa"}
...
operation=/v1/greeter
resCode=200
eventStatus=Ended
EOEConcept
When we don't use, for example jaeger When calling the chain service , We hope to track the data in the distributed system through logs RPC request .
rk-boot The middleware will pass openTelemetry Library to write... To the log traceId To track RPC.
When the logging middleware is started , Original data middleware , When calling the chain middleware , The middleware will write the following three types into the log ID.
EventId
When the logging middleware is started ,EventId It will generate automatically .
---
echo:
- name: greeter # Required
port: 1949 # Required
enabled: true # Required
interceptors:
loggingZap:
enabled: true------------------------------------------------------------------------
...
ids={"eventId":"cd617f0c-2d93-45e1-bef0-95c89972530d"}
...RequestId
When the log middleware and the original data middleware are started ,RequestId and EventId It will generate automatically , And these two ID Will be consistent .
---
echo:
- name: greeter # Required
port: 1949 # Required
enabled: true # Required
interceptors:
loggingZap:
enabled: true
meta:
enabled: true------------------------------------------------------------------------
...
ids={"eventId":"8226ba9b-424e-4e19-ba63-d37ca69028b3","requestId":"8226ba9b-424e-4e19-ba63-d37ca69028b3"}
...Even if the user overwrites RequestId,EventId Will be consistent .
rkechoctx.AddHeaderToClient(ctx, rkechoctx.RequestIdKey, "overridden-request-id")
------------------------------------------------------------------------
...
ids={"eventId":"overridden-request-id","requestId":"overridden-request-id"}
...TraceId
When the call chain middleware is started ,traceId It will generate automatically .
---
echo:
- name: greeter # Required
port: 1949 # Required
enabled: true # Required
interceptors:
loggingZap:
enabled: true
meta:
enabled: true
tracingTelemetry:
enabled: true------------------------------------------------------------------------
...
ids={"eventId":"dd19cf9a-c7be-486c-b29d-7af777a78ebe","requestId":"dd19cf9a-c7be-486c-b29d-7af777a78ebe","traceId":"316a7b475ff500a76bfcd6147036951c"}
...边栏推荐
- 数据管理:业务数据清洗,落地实现方案
- Container JVM that has to be picked up
- MIP nerf: anti aliasing multiscale neural radiation field iccv2021
- Cvpr2022 𞓜 thin domain adaptation
- 一次 MySQL 误操作导致的事故,「高可用」都顶不住了!
- Real time preview of RTSP video based on webrtc
- Attack and defense world PyC trade
- Icml'22 | progcl: rethinking difficult sample mining in graph contrast learning
- 杂乱的知识点
- Esp8266 OTA remote and wireless upgrade
猜你喜欢

CVPR2022 | 可精简域适应

An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!

【小程序】编译预览小程序时,出现-80063错误提示

Kitten paw: FOC control 15-mras method of PMSM

Handwritten digit recognition using SVM, Bayesian classification, binary tree and CNN

Shardingsphere-proxy-5.0.0 implementation of capacity range partition (V)

牛学长周年庆活动:软件大促限时抢,注册码免费送!

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

JS input / output statements, variables

这不会又是一个Go的BUG吧?
随机推荐
Basic usage of setfacl command
机器学习中 TP FP TN FN的概念
7 tips for preventing DDoS Attacks
Is it safe to open a stock account online now? Select a state-owned securities firm, and the fastest time to open an account is 8 minutes
Gin framework: automatically add requestid
Application configuration management, basic principle analysis
DML操作
[CVPR 2020 oral] a physics based noise formation model for extreme low light raw denoising
[redis advanced ziplist] if someone asks you what is a compressed list? Please dump this article directly to him.
Handwritten digit recognition using SVM, Bayesian classification, binary tree and CNN
If you want to open an account for stock trading, is it safe to open an account online-
Shengdun technology joined dragon lizard community to build a new open source ecosystem
[Hongke case] how can 3D data become operable information Object detection and tracking
Graduation project - thesis writing notes [design topic type, thesis writing details, design materials]
version `ZLIB_1.2.9‘ not found (required by /lib64/libpng16.so.16)
An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!
CODING CD
用一个软件纪念自己故去的母亲,这或许才是程序员最大的浪漫吧
Skywalking installation and deployment practice
一次 MySQL 误操作导致的事故,「高可用」都顶不住了!