baggage transmits custom information between links (between services).

Example code address: https://github.com/gogf/gf/tree/master/example/trace/http

Client

  1. package main
  2. import (
  3. "github.com/gogf/gf/contrib/trace/otlphttp/v2"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/net/gtrace"
  6. "github.com/gogf/gf/v2/os/gctx"
  7. )
  8. const (
  9. serviceName = "otlp-http-client"
  10. endpoint = "tracing-analysis-dc-hz.aliyuncs.com"
  11. path = "adapt_******_******/api/otlp/traces"
  12. )
  13. func main() {
  14. var ctx = gctx.New()
  15. shutdown, err := otlphttp.Init(serviceName, endpoint, path)
  16. if err != nil {
  17. g.Log().Fatal(ctx, err)
  18. }
  19. defer shutdown()
  20. StartRequests()
  21. }
  22. func StartRequests() {
  23. ctx, span := gtrace.NewSpan(gctx.New(), "StartRequests")
  24. defer span.End()
  25. ctx = gtrace.SetBaggageValue(ctx, "name", "john")
  26. content := g.Client().GetContent(ctx, "http://127.0.0.1:8199/hello")
  27. g.Log().Print(ctx, content)
  28. }

Brief explanation of client code:

  1. First, the client also needs to initialize Jaeger through the jaeger.Init method.
  2. Then, using gtrace.SetBaggageValue(ctx, "name", "john"), a baggage is set, which will be transmitted in all links of the request. However, in this example, there are only two nodes, so the baggage data will only be transmitted to the server. This method will return a new context.Context variable, which we will need to pass in the subsequent call chain.
  3. Here, an HTTP client request object is created using g.Client(), which automatically enables tracing features without requiring developers to explicitly call any methods or settings.
  4. Finally, g.Log().Print(ctx, content) is used to print the server’s return content, where ctx is used to pass link information to the logging component. If the ctx context object contains link information, the logging component will automatically include the TraceId in the log content.

Server

  1. package main
  2. import (
  3. "github.com/gogf/gf/contrib/trace/otlphttp/v2"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/net/ghttp"
  6. "github.com/gogf/gf/v2/net/gtrace"
  7. "github.com/gogf/gf/v2/os/gctx"
  8. )
  9. const (
  10. serviceName = "otlp-http-server"
  11. endpoint = "tracing-analysis-dc-hz.aliyuncs.com"
  12. path = "adapt_******_******/api/otlp/traces" )
  13. func main() {
  14. var ctx = gctx.New()
  15. shutdown, err := otlphttp.Init(serviceName, endpoint, path)
  16. if err != nil {
  17. g.Log().Fatal(ctx, err)
  18. }
  19. defer shutdown()
  20. s := g.Server()
  21. s.Group("/", func(group *ghttp.RouterGroup) {
  22. group.GET("/hello", HelloHandler)
  23. })
  24. s.SetPort(8199)
  25. s.Run()
  26. }
  27. func HelloHandler(r *ghttp.Request) {
  28. ctx, span := gtrace.NewSpan(r.Context(), "HelloHandler")
  29. defer span.End()
  30. value := gtrace.GetBaggageVar(ctx, "name").String()
  31. r.Response.Write("hello:", value)
  32. }

Brief explanation of server code:

  1. Of course, the server also needs to initialize Jaeger through the jaeger.Init method.
  2. The server starts with tracing enabled, and developers do not need to call any methods or configure any settings explicitly.
  3. The server uses gtrace.GetBaggageVar(ctx, "name").String() to obtain the baggage information submitted by the client and converts it to a string for return.

View the Effects

Start the server:

Tracing HTTP - Baggage - 图1

Start the client:

Tracing HTTP - Baggage - 图2

As you can see, the baggage submitted by the client has been successfully received and printed by the server. Also, the client outputs the TraceId information when logging. TraceId is a unique ID for a link, and it can be used to retrieve all log information for that link as well as query the detailed call chain on the Jaeger system.

View the link information on Jaeger:

Tracing HTTP - Baggage - 图3

As seen here, there are two service names: tracing-http-client and tracing-http-server, indicating that this request involves two services, specifically the HTTP request client and server, and each service involves 2 span link nodes.

When clicking on the details of this trace, you can see the hierarchical relationship of the call chain. Additionally, you can find the client’s request address, the server’s received route, and the server route function names. Let’s introduce the Attributes and Events information of the client, or the Tags and Process information shown in Jaeger.

HTTP Client Attributes

Tracing HTTP - Baggage - 图4

Attribute/TagDescription
otel.instrumentation_library.nameCurrent instrument name, usually the component name of the current span operation
otel.instrumentation_library.versionCurrent instrument component version
span.kindType of the current span, generally written by the component automatically. Common span types include:
TypeDescription
clientClient
serverServer
producerProducer, commonly used in MQ
consumerConsumer, commonly used in MQ
internalInternal method, generally used in business
undefinedUndefined, rarely used
status.codeCurrent span status, 0 is normal, non-zero indicates failure
status.messageCurrent span status information, often contains error information when failed
hostnameHostname of the current node
ip.intranetIntranet address list of the current node
http.address.localLocal address and port of HTTP communication
http.address.remoteTarget address and port of HTTP communication
http.dns.startDomain name address to resolve when the target address of the request contains a domain name
http.dns.doneIP address after domain name resolution when the request target address contains a domain name
http.connect.startType and address where connection creation starts
http.connect.doneType and address after successful connection creation

HTTP Client Events

Tracing HTTP - Baggage - 图5

Event/LogDescription
http.request.headersHeader information submitted by the HTTP client request, which may be large.
http.request.baggageBaggage information submitted by the HTTP client request for inter-service link information transmission.
http.request.bodyBody data submitted by the HTTP client request, which may be large. Only records up to 512KB, exceeding which will be ignored.
http.response.headersHeader information returned by the HTTP client request, which may be large.
http.response.bodyBody data returned by the HTTP client request, which may be large. Only records up to 512KB, exceeding which will be ignored.

HTTP Server Attributes

Tracing HTTP - Baggage - 图6

The Attributes of the HTTP Server side are the same as those of the HTTP Client, and similar data is printed in the same request.

HTTP Server Events

Tracing HTTP - Baggage - 图7

The Events of the HTTP Server side are the same as those of the HTTP Client, and similar data is printed in the same request.