This is the fifth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

introduce

As a complete example, add Tracing middleware to a microservice based on the Echo framework.

What is call chain middleware?

Tracing middleware records Tracing data for each API request, which users can view using tools like Jaeger.

We will use RK-boot to start the Echo framework microservice.

Please visit rkdocs.net lilify. app/cn for the full tutorial

The installation

go get github.com/rookie-ninja/rk-boot
go get github.com/rookie-ninja/rk-echo
Copy the code

Quick start

Rk-boot defaults to using OpenTelemetry-CNCF to process Tracing.

1. Create the boot. Yaml

To verify, we enabled the following options:

  • CommonService: commonService contains a set of common apis. details
  • Jaeger exporter: The Echo service sends data to the local Jaeger Agent.
---
echo:
  - 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
Copy the code

2. Create a main. Go

package main

import (
	"context"
	"github.com/rookie-ninja/rk-boot"
        _ "github.com/rookie-ninja/rk-echo/boot"
)

// Application entrance.
func main(a) {
	// Create a new boot instance.
	boot := rkboot.NewBoot()

	// Bootstrap
	boot.Bootstrap(context.Background())

	// Wait for shutdown sig
	boot.WaitForShutdownSig(context.Background())
}
Copy the code

3. Folder structure

$tree. └── Go. ├─ go. ├─ goCopy the code

4. 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.23Copy the code

5. Start the main. Go

$ go run main.go
Copy the code

6. Verify

  • Send the request
$ curl -X GET localhost:8080/rk/v1/healthy
{"healthy":true}
Copy the code
  • Visit jaeger’s homepage at http://localhost:16686/

Rk-boot names Service with the module suffix in the go.mod file.

For example, if your go.mod file is as follows, the Service name is rk-demo

1.16 the require module github.com/rookie-ninja/rk-demo go (github.com/alecthomas/template V0.0.0-20190718012654 - fb15b899a751 github.com/labstack/echo/v4 v4.6.1 github.com/rookie-ninja/rk-boot v1.4.0 github.com/rookie-ninja/rk-echo v0.0.5 github.com/swaggo/swag v1.7.0)Copy the code

The output to Stdout

You can modify the output path by modifying the boot.yaml file, such as STDOUT.

  • boot.yaml
---
echo:
  - 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
Copy the code

Output to file

You can modify the boot.yaml file to save Tracing information to the file.

  • boot.yaml
---
echo:
  - 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
Copy the code

options

The name describe type The default value
echo.interceptors.tracingTelemetry.enabled Start the call chain interceptor boolean false
echo.interceptors.tracingTelemetry.exporter.file.enabled Start file output boolean false
echo.interceptors.tracingTelemetry.exporter.file.outputPath Output file path string stdout
echo.interceptors.tracingTelemetry.exporter.jaeger.agent.enabled Jaeger Agent as data output boolean false
echo.interceptors.tracingTelemetry.exporter.jaeger.agent.host Jaeger agent address string localhost
echo.interceptors.tracingTelemetry.exporter.jaeger.agent.port Jaeger agent port int 6831
echo.interceptors.tracingTelemetry.exporter.jaeger.collector.enabled Jaeger Collector as data output boolean false
echo.interceptors.tracingTelemetry.exporter.jaeger.collector.endpoint Jaeger collector address string http://localhost:16368/api/trace
echo.interceptors.tracingTelemetry.exporter.jaeger.collector.username Jaeger collector user name string “”
echo.interceptors.tracingTelemetry.exporter.jaeger.collector.password Jaeger collector password string “”