preamble
Opentelemetry
Distributed Link Trace (Distributed Tracing
) concept was first proposed by Google, the development of technology has been relatively mature, there are also some protocol standards can be referred to. Currently inTracing
Two of the more influential open-source technology frameworks are Netflix's open-sourceOpenTracing
and Google's open sourceOpenCensus
. Both frameworks have a relatively high developer base. In order to form a unified technical standard, the two frameworks eventually merged to form theOpenTelemetry
The project, referred to asotel
The otel has two main sections, link tracing and monitoring alarms, for monitoring alarms, check out another article:Getting Started with Go Monitoring and Alerting Opentelemetry
Jaeger
Jaeger\ˈyā-gər\
is Uber's open-source distributed tracking system, which is the first of its kind to support theOpenTelemetry
One of the systems that is alsoCNCF Project.
Installing Jaeger
Jaeger has prepared a Docker image for us that we can easily install.
docker run --rm --name jaeger \
-p 16686:16686 \
-p 4317:4317 \
-p 4318:4318
As of 2024-04-18, the latest version of Jaeger is 1.56.
Here's a quick overview of the three ports: 16686 is used as the web dashboard for the Jaeger service, which we'll be able to access in a browser in a moment; 4317 and 4318 are both used for uploading tracking data, with the difference being that the former is a gRPC protocol and the latter is an HTTP protocol.
Jaeger has many other available ports, this article only covers the otel related ones, check out theJaeger Official Documentation Oh.
After installation, enter IP:16686 in your browser:
A cute picture of a gopher detective following a trail means that Jaeger has been successfully installed.
Writing Go Code
Install the dependencies:
go get "/otel" \
"/otel/exporters/stdout/stdoutmetric" \
"/otel/exporters/stdout/stdouttrace" \
"/otel/propagation" \
"/otel/sdk/metric" \
"/otel/sdk/resource" \
"/otel/sdk/trace" \
"/otel/semconv/v1.24.0" \
"/contrib/instrumentation/net/http/otelhttp"
I'll post the entire code for HTTP and gRPC here, just copy it and change it to your own address:
HTTP
func TestTraceHttp(t *) {
ctx := ()
// Create an OTLP HTTP exporter that connects to the Jaeger
exporter, err := (ctx,
(":4318/v1/traces"))
if err ! = nil {
("Failed to create exporter: %v", err)
}
// Create the resource
res, err := (ctx,
(
("otel-traces-demo-http"), (
), ), ("otel-traces-demo-http")
)
if err ! = nil {
("Failed to create resource: %v", err)
}
// Create the Tracer Provider
tp := (
(exporter), (res), (tp), (tp), (exporter), (tp)
(res), )
)
// Set the global Tracer provider
(tp)
// Create a new trace
tracer := ("example-tracer")
ctx, span := (ctx, "root-span")
// Pause for 100ms
(100 * )
// end span
()
// Create the child span
_, childSpan := (ctx, "child-span")
// Pause for 50ms
(50 * )
()
// Make sure all spans are sent
if err := (ctx); err ! = nil {
("Failed to close Tracer provider: %v", err)
}
}
gRPC
func TestTraceGrpc(t *) {
ctx := ()
// Create an OTLP gRPC exporter to connect to Jaeger.
exporter, err := (ctx,
(":4317"), (), err := (ctx, ctx, ctx, ctx
(), )
)
if err ! = nil {
("Failed to create exporter: %v", err)
}
// Create the resource
res, err := (ctx,
(
("otel-traces-demo-grpc"), (
), .
)
if err ! = nil {
("Failed to create resource: %v", err)
}
// Create the Tracer Provider
tp := (
(exporter), (res), (tp), (tp), (exporter), (tp)
(res), )
)
// Set the global Tracer provider
(tp)
// Create a new trace
tracer := ("example-tracer")
ctx, span := (ctx, "root-span")
// Pause for 100ms
(100 * )
// end span
()
// Create the child span
_, childSpan := (ctx, "child-span")
// Pause for 50ms
(50 * )
()
// Make sure all spans are sent
if err := (ctx); err ! = nil {
("Failed to close Tracer provider: %v", err)
}
}
effect
After execution, you can see our uploaded data in the panel.
You can see that our two spans have been uploaded to theJaeger
It's in, it's that simple! The code in the article is open source atGithub。