We have a series that covers the full practice of microservices from requirements to live, from code to K8S deployment, from logging to monitoring, and more.

The whole project uses the micro-services developed by GO-Zero, and basically includes some middleware developed by Go-Zero and related GO-Zero authors. The technology stack used is basically self-developed components of go-Zero project team, which is basically the whole Family of Go-Zero.

Actual combat project address: github.com/Mikaelemmmm…

1, an overview of the

If the error handling in my first two sections, the log collection configuration, we see through the log traceId can also complete an error when the overall link log, but not an error or want to convenient to check the individual business the whole link the execution time of the call is not very convenient to view, so it is better to add a link to track.

The go-Zero infrastructure has already written the code that connects us to link tracing

func startAgent(c Config) error {
  opts := []sdktrace.TracerProviderOption{
    // Set the sampling rate based on the parent span to 100%
    sdktrace.WithSampler(sdktrace.ParentBased(sdktrace.TraceIDRatioBased(c.Sampler))),
    // Record information about this application in an Resource.
    sdktrace.WithResource(resource.NewSchemaless(semconv.ServiceNameKey.String(c.Name))),
  }

  if len(c.Endpoint) > 0 {
    exp, err := createExporter(c)
    iferr ! =nil {
      logx.Error(err)
      return err
    }

    // Always be sure to batch in production.
    opts = append(opts, sdktrace.WithBatcher(exp))
  }

  tp := sdktrace.NewTracerProvider(opts...)
  otel.SetTracerProvider(tp)
  otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
    propagation.TraceContext{}, propagation.Baggage{}))
  otel.SetErrorHandler(otel.ErrorHandlerFunc(func(err error) {
    logx.Errorf("[otel] error: %v", err)
  }))

  return nil
}
Copy the code

Jaeger and Zinpink are supported by default

package trace

// TraceName represents the tracing name.
const TraceName = "go-zero"

// A Config is a opentelemetry config.
type Config struct {
  Name     string  `json:",optional"`
  Endpoint string  `json:",optional"`
  Sampler  float64 ` json: ". Default = 1.0 "`
  Batcher  string  `json:",default=jaeger,options=jaeger|zipkin"`
}

Copy the code

We just need to configure the parameters in our business code configuration, which is yamL for your business configuration.

2, implementation,

Go-zero-looklook is implemented with Jaeger

2.1 jaeger

Jaeger is configured in the docker-comement-env.yaml of the project

services:
  # Jaeger link tracing
  jaeger:
    image: jaegertracing/all-in-one:latest
    container_name: jaeger
    ports:
      - "5775:5775/udp"
      - "6831:6831/udp"
      - "6832:6832/udp"
      - "5778:5778"
      - "16686:16686"
      - "14268:14268"
      - "9411:9411"
    environment:
      - SPAN_STORAGE_TYPE=elasticsearch
      - ES_SERVER_URLS=http://elasticsearch:9200
      - LOG_LEVEL=debug
    networks:
      - looklook_net
      
   .
Copy the code

Jager_collector relies on ElasticSearch for storage, so install elasticSearch as we demonstrated in the previous section when collecting logs.

2.2 Service Configuration

Let’s take user services as an example

1) API configuration

app/usercenter/cmd/api/etc/usercenter.yaml

Name: usercenter-api
Host: 0.0. 0. 0
Port: 8002
Mode: dev
.

# Link tracing
Telemetry:
  Name: usercenter-api
  Endpoint: http://jaeger:14268/api/traces
  Sampler: 1.0
  Batcher: jaeger
Copy the code

2) RPC configuration

Name: usercenter-rpc
ListenOn: 0.0. 0. 0: 9002
Mode: dev

.

# Link tracing
Telemetry:
  Name: usercenter-rpc
  Endpoint: http://jaeger:14268/api/traces
  Sampler: 1.0
  Batcher: jaeger
Copy the code

2.3 Viewing Links

Request user service registration, login, access to login user information

The browser enter http://127.0.0.1:16686/search can look at it

3, summary

We have finished sorting out logs and link tracking. A good system must be able to monitor anomalies in time. The next step is to see service monitoring.

The project address

Github.com/zeromicro/g…

Welcome to Go-Zero and star support us!

Wechat communication group

Pay attention to the public account of “micro-service Practice” and click on the exchange group to obtain the QR code of the community group.