调用链
启动调用链中间件。
安装#
go get github.com/rookie-ninja/rk-boot/v2
go get github.com/rookie-ninja/rk-fiber
选项#
名字 | 描述 | 类型 | 默认值 |
---|---|---|---|
fiber.middleware.trace.enabled | 启动调用链拦截器 | boolean | false |
fiber.middleware.trace.ignore | 局部选项,忽略 API 路径 | []string | [] |
fiber.middleware.trace.exporter.file.enabled | 启动文件输出 | boolean | false |
fiber.middleware.trace.exporter.file.outputPath | 输出文件路径 | string | stdout |
fiber.middleware.trace.exporter.otlp.enabled | otlp exporter | boolean | false |
fiber.middleware.trace.exporter.otlp.endpoint | OTLP 地址 | string | localhost |
快速开始#
1.创建 boot.yaml#
---
fiber:
- name: greeter
port: 8080
enabled: true
middleware:
trace:
enabled: true
exporter:
file:
enabled: true
outputPath: "stdout"
# ignore: [""]
# otlp:
# enabled: true
# endpoint: ""
2.创建 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"
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/rookie-ninja/rk-boot/v2"
"github.com/rookie-ninja/rk-fiber/boot"
"net/http"
)
// @title Swagger Example API
// @version 1.0
// @description This is a sample rk-demo server.
// @termsOfService http://swagger.io/terms/
// @securityDefinitions.basic BasicAuth
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
func main() {
// Create a new boot instance.
boot := rkboot.NewBoot()
// Bootstrap
boot.Bootstrap(context.TODO())
// Register handler
entry := rkfiber.GetFiberEntry("greeter")
entry.App.Get("/v1/greeter", Greeter)
// This is required!!!
entry.RefreshFiberRoutes()
boot.WaitForShutdownSig(context.TODO())
}
// Greeter handler
// @Summary Greeter
// @Id 1
// @Tags Hello
// @version 1.0
// @Param name query string true "name"
// @produce application/json
// @Success 200 {object} GreeterResponse
// @Router /v1/greeter [get]
func Greeter(ctx *fiber.Ctx) error {
ctx.Response().SetStatusCode(http.StatusOK)
return ctx.JSON(&GreeterResponse{
Message: fmt.Sprintf("Hello %s!", ctx.Query("name")),
})
}
type GreeterResponse struct {
Message string
}
3.验证#
发送请求
$ curl -vs -X GET "localhost:8080/v1/greeter?name=rk-dev"
{"Message":"Hello rk-dev!"}
{
"Name": "/v1/greeter",
"SpanContext": {
"TraceID": "507aaae07729a9f7cae7ec25ee7f0ede",
"SpanID": "ea556bd79f2f2732",
"TraceFlags": "01",
"TraceState": "",
"Remote": false
},
"Parent": {
"TraceID": "00000000000000000000000000000000",
"SpanID": "0000000000000000",
"TraceFlags": "00",
"TraceState": "",
"Remote": true
},
"SpanKind": 2,
"StartTime": "2022-04-15T23:28:34.054496+08:00",
"EndTime": "2022-04-15T23:28:34.054594239+08:00",
"Attributes": [
...
{
"Key": "http.status_code",
"Value": {
"Type": "INT64",
"Value": 200
}
}
],
...
"Resource": [
{
"Key": "service.entryName",
"Value": {
"Type": "STRING",
"Value": "greeter"
}
},
{
"Key": "service.entryType",
"Value": {
"Type": "STRING",
"Value": "FiberEntry"
}
},
],
"InstrumentationLibrary": {
"Name": "greeter",
"Version": "semver:1.4.0",
"SchemaURL": ""
}
}
Cheers#
4.输出到文件#
输出到文件的时候,会有几秒的延迟。
---
fiber:
- name: greeter
port: 8080
enabled: true
middleware:
trace:
enabled: true
exporter:
file:
enabled: true
outputPath: "logs/tracing.log"
Cheers#
5.输出到 jaeger#
本地启动 jaeger-all-in-one
$ docker run --rm --name jaeger \ -e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \ -p 6831:6831/udp \ -p 6832:6832/udp \ -p 5778:5778 \ -p 16686:16686 \ -p 4317:4317 \ -p 4318:4318 \ -p 14250:14250 \ -p 14268:14268 \ -p 14269:14269 \ -p 9411:9411 \ jaegertracing/all-in-one:1.50
---
fiber:
- name: greeter
port: 8080
enabled: true
middleware:
trace:
enabled: true
exporter:
otpl:
enabled: true
endpoint: "localhost:4317"
Jaeger: