当前位置:网站首页>Gin framework: adding tracing Middleware

Gin framework: adding tracing Middleware

2022-06-24 01:53:00 Trespass

Introduce

Through a complete example , Based on Gin Add a call chain to the microservice of the framework (Tracing) middleware .

What is a call chain (Tracing) middleware ?

Call chain (Tracing) Middleware will be used for every API Request record Tracing data , Users can use something like Jaeger Tool View .

We will use rk-boot To start up Gin Microservices of the framework .

Please visit the following address for a complete tutorial :https://rkdocs.netlify.app/cn

install

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

Quick start

rk-boot By default OpenTelemetry-CNCF To deal with it Tracing.

1. establish boot.yaml

In order to verify , We launched the following options :

  • commonService:commonService It contains a series of general API. details
  • jaeger exporter:Gin The service will be delivered locally jaeger agent send data .
---
gin:
  - name: greeter                     # Required
    port: 8080                        # Required
    enabled: true                     # Required
    commonService:
      enabled: true                   # Optional, default: false
    interceptors:
      tracingTelemetry:
        enabled: true                 # Optional, Enable tracing interceptor/middleware
        exporter:
          jaeger:
            agent:
              enabled: true           # Optional, Export to jaeger agent

2. establish main.go

package main

import (
	"context"
	"github.com/rookie-ninja/rk-boot"
	_ "github.com/rookie-ninja/rk-gin/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. Folder structure

$ tree
.
├── boot.yaml
├── go.mod
├── go.sum
└── main.go

0 directories, 4 files

4. Local boot jaeger

$ 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

5. start-up main.go

$ go run main.go

6. verification

  • Send a request
$ curl -X GET localhost:8080/rk/v1/healthy
{"healthy":true}
  • visit jaeger Home page : http://localhost:16686/

rk-boot Will use go.mod In the document module Suffix to name Service. give an example : If your go.mod The contents of the document are as follows , be Service The name is rk-demo

module github.com/rookie-ninja/rk-demo

go 1.16

require (
	github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
	github.com/gin-gonic/gin v1.7.2
	github.com/rookie-ninja/rk-boot v1.4.0
	github.com/rookie-ninja/rk-gin v1.2.12
	github.com/swaggo/swag v1.7.0
)

Output to Stdout

It can be modified by boot.yaml File to modify the output path , such as STDOUT.

  • boot.yaml
---
gin:
  - name: greeter                     # Required
    port: 8080                        # Required
    enabled: true                     # Required
    commonService:
      enabled: true                   # Optional, default: false
    interceptors:
      tracingTelemetry:
        enabled: true                 # Optional, Enable tracing interceptor/middleware
        exporter:
          file:
            enabled: true
            outputPath: "stdout"      # Optional, Output to stdout

output to a file

It can be modified by boot.yaml File to save Tracing Information to file .

  • boot.yaml
---
gin:
  - name: greeter                             # Required
    port: 8080                                # Required
    enabled: true                             # Required
    commonService:
      enabled: true                           # Optional, default: false
    interceptors:
      tracingTelemetry:
        enabled: true                         # Optional, Enable tracing interceptor/middleware
        exporter:
          file:
            enabled: true
            outputPath: "logs/tracing.log"    # Optional, Log to files

Options

name

describe

type

The default value is

gin.interceptors.tracingTelemetry.enabled

Start the call chain interceptor

boolean

false

gin.interceptors.tracingTelemetry.exporter.file.enabled

Start file output

boolean

false

gin.interceptors.tracingTelemetry.exporter.file.outputPath

Output file path

string

stdout

gin.interceptors.tracingTelemetry.exporter.jaeger.agent.enabled

jaeger agent Output as data

boolean

false

gin.interceptors.tracingTelemetry.exporter.jaeger.agent.host

jaeger agent Address

string

localhost

gin.interceptors.tracingTelemetry.exporter.jaeger.agent.port

jaeger agent port

int

6831

gin.interceptors.tracingTelemetry.exporter.jaeger.collector.enabled

jaeger collector Output as data

boolean

false

gin.interceptors.tracingTelemetry.exporter.jaeger.collector.endpoint

jaeger collector Address

string

http://localhost:16368/api/trace

gin.interceptors.tracingTelemetry.exporter.jaeger.collector.username

jaeger collector user name

string

""

gin.interceptors.tracingTelemetry.exporter.jaeger.collector.password

jaeger collector password

string

""

原网站

版权声明
本文为[Trespass ]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211110191147121L.html