Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

introduce

This article describes how to add API Tracing interceptors/middleware to gRPC microservices. That’s API monitoring that you can do in Jaeger.

What is API Tracing interceptor/middleware?

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

We will use RK-boot to start the gRPC service.

Please visit the following address for the full tutorial:

rkdev.info/cn

Rkdocs.net lilify. app/cn (standby)

The installation

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

Quick start

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

1. Create the boot. Yaml

To verify this, we started commonService, which contains a series of common apis such as /rk/v1/healthy.

---
grpc:
  - name: greeter                  # Name of grpc entry
    port: 8080                        # Port of grpc entry
    enabled: true                     # Enable grpc entry
    commonService:
      enabled: true                   # Enable common service for testing
    interceptors:
      tracingTelemetry:
        enabled: true                 # Enable tracing interceptor/middleware
        exporter:
          jaeger:
            agent:
              enabled: true           # Export to jaeger agent
Copy the code

2. Create a main. Go

package main

import (
	"context"
	"github.com/rookie-ninja/rk-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.tempo ├── go.tempo ├─ main. Go 0 directories, 4 filesCopy 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.23
Copy 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

Module github.com/rookie-ninja/rk-demo go 1.16 require (github.com/grpc-ecosystem/grpc-gateway/v2 v2.5.0 github.com/rookie-ninja/rk-boot v1.2.6 github.com/rookie-ninja/rk-grpc v1.2.6 google.golang.org/grpc v1.38.0 Google.golang.org/protobuf v1.26.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
---
grpc:
  - name: greeter                   # Name of grpc entry
    port: 8080                         # Port of grpc entry
    enabled: true                     # Enable grpc entry
    commonService:
      enabled: true                   # Enable common service for testing
    interceptors:
      tracingTelemetry:
        enabled: true
        exporter:
          file:
            enabled: true
            outputPath: "stdout"    # 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
---
grpc:
  - name: greeter                             # Name of grpc entry
    port: 8080                                   # Port of grpc entry
    enabled: true                               # Enable grpc entry
    commonService:
      enabled: true                             # Enable common service for testing
    interceptors:
      tracingTelemetry:
        enabled: true
        exporter:
          file:
            enabled: true
            outputPath: "logs/tracing.log"    # Log to files
Copy the code

options

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