We use a series to explain the complete practice of microservices from requirements to online, from code to k8s deployment, from logging to monitoring, etc.
The whole project uses microservices developed by go-zero, which basically includes go-zero and some middleware developed by related go-zero authors. The technology stack used is basically the self-developed components of the go-zero project team, basically go -zero the whole family bucket.
Actual project address: https://github.com/Mikaelemmmm/go-zero-looklook
1 Overview
If you follow the error handling and log collection configuration in the previous two sections, we can also see the overall link log when an error is reported through the traceId in the log, but when no error is reported or you want to easily view the call of the entire link of a single business The execution time is not very convenient to view, so it is better to add link tracking.
The bottom layer of go-zero has already written the code to help us connect the code with the link tracking
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)
if err != 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
}
Default support for jaeger, zinpink
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"`
}
We only need to configure the parameters in our business code configuration, that is, the yaml of your business configuration.
2. Realize
go-zero-looklook is implemented by jaeger
2.1 Jaeger
jaeger is configured in the project's docker-compose-env.yaml
services:
#jaeger链路追踪
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
........
Among them, jager_collector relies on elasticsearch for storage, so elasticsearch should be installed, as we have demonstrated in the previous section when collecting logs.
2.2 Business Configuration
Let's take user service 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
......
#链路追踪
Telemetry:
Name: usercenter-api
Endpoint: http://jaeger:14268/api/traces
Sampler: 1.0
Batcher: jaeger
2) rpc configuration
Name: usercenter-rpc
ListenOn: 0.0.0.0:9002
Mode: dev
.....
#链路追踪
Telemetry:
Name: usercenter-rpc
Endpoint: http://jaeger:14268/api/traces
Sampler: 1.0
Batcher: jaeger
2.3 View link
Request user service registration, login, and obtain login user information
Browser input http://127.0.0.1:16686/search to view
3. Summary
We have finished sorting out logs and link tracking. A good system must be able to monitor abnormalities in time. Next, we will look at service monitoring.
project address
https://github.com/zeromicro/go-zero
Welcome go-zero
and star support us!
WeChat exchange group
Follow the official account of " Microservice Practice " and click on the exchange group to get the QR code of the community group.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。