Sitemap

GoFrame 101: Add tracing middleware

3 min readJan 1, 2022

--

We will use rk-boot to enable tracing middleware in gogf/gf framework.

Introduce rk-boot

rk-boot is a library used to create goLang micro-service with gogf/gf framework easily.

Please visit official site for doc details.

Install rk-boot

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

Quick start

Please visit rkdev.info for detailed document.

I. Create boot.yaml

boot.yaml tells rk-boot how to start gogf/gf server.

Get mofcloud’s stories in your inbox

Join Medium for free to get updates from this writer.

CommonService was enabled which contains couple of utility functions such as /rk/v1/healthy.

II. Create main.go

Add /v1/greeter API.

III. Directory hierarchy

$ tree
.
├── boot.yaml
├── go.mod
├── go.sum
└── main.go0 directories, 4 files

IV. Start jaeger locally

$ docker run -d --name jaeger \
-e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
-p 5775:5775/udp \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 14268:14268 \
-p 14250:14250 \
-p 9411:9411 \
jaegertracing/all-in-one:1.23

V. Start main.go

$ go run main.go

VI. Validate

  • Send request.
$ curl -X GET localhost:8080/rk/v1/healthy
{"healthy":true}
$ curl -X GET "localhost:8080/v1/greeter?name=rk-dev" {"Message":"Hello rk-dev!"}

rk-boot will export service name as rk if rkentry.GlobalAppCtx.GetAppInfoEntry().AppName was not set in code.

Press enter or click to view image in full size

Export to Stdout

We can modify boot.yaml to export tracing info to stdout.

Export to file

We can modify boot.yaml to export tracing info to files.

Options

Press enter or click to view image in full size

Introduction of rk-boot

rk-boot is a library which support bootstrapping server at runtime. It is a little like spring boot way.

Why do I want it?

  • Build application with unified project layout at enterprise level .
  • Build API with the unified format of logging, metrics, tracing, authorization at enterprise level.
  • Make application replace core dependencies quickly.
  • Save learning time of writing initializing procedure of popular frameworks and libraries.
  • User defined Entry for customization.
Press enter or click to view image in full size

Key feature

User can bootstrap multiple Web Service with different framework with same layout of YAML. Hers is simple example where we start Gin, gRPC, Echo and GoFrame web framework in the same process.

  • Install dependencies
go get github.com/rookie-ninja/rk-boot/grpc 
go get github.com/rookie-ninja/rk-boot/gin
go get github.com/rookie-ninja/rk-boot/echo
go get github.com/rookie-ninja/rk-boot/gf
  • boot.yaml
  • main.go
  • Validation
# gRPC through grpc-gateway
$ curl localhost:8080/rk/v1/healthy
{"healthy":true}

# Gin
$ curl localhost:8081/rk/v1/healthy
{"healthy":true}

# Echo
$ curl localhost:8082/rk/v1/healthy
{"healthy":true}

# GoFrame
$ curl localhost:8083/rk/v1/healthy
{"healthy":true}

Supported web frameworks

Press enter or click to view image in full size

Introduction of rk-gf

Interceptor & bootstrapper designed for gogf/gf web framework.

This belongs to rk-boot family. We suggest use this lib from rk-boot.

Press enter or click to view image in full size

Supported instances

All instances could be configured via YAML or Code.

User can enable anyone of those as needed! No mandatory binding!

Press enter or click to view image in full size

Supported middleware

All middlewares could be configured via YAML or Code.

User can enable anyone of those as needed! No mandatory binding!

Press enter or click to view image in full size

Full YAML Options

--

--