-
Notifications
You must be signed in to change notification settings - Fork 4.6k
examples/features/opentelemetry: demonstrate tracing using OpenTelemetry plugin #8056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 63 commits
7ddbe46
0486355
53fa9cd
4435b8a
a413555
4e203c3
e9ad552
61e15e3
4a988a6
58451b6
9ac7ce3
c0e8885
0b4231a
b8c0ef5
a33cc4b
6215e98
c68e570
02d9139
9db8ad6
26017c7
fd4c632
e8a9e48
5f849ae
1c55841
ebe37f3
99cc7d7
b10de6e
8664e40
8fcfc33
0ef715a
a2fd0c0
737c458
ad5c177
5e1d020
11f0d89
10821bb
c027805
1bb9bd4
23e0ff5
dcd525a
8693ecc
c5a1dcf
87e4352
545bf03
06f4309
1ed2c6b
b6035d5
ba00736
3fdcf97
e5a6a9e
3cfa4a9
9db8e81
fd85029
e00b308
e9e7796
932feb2
5c27cd0
86ce88b
d12b4f3
2ef0356
95398e1
e861fbe
1a36d58
f63f1f6
8c99dce
881f0e5
b129fa0
3855e04
7f2636c
2e59ff7
56cf9ef
b24b2fb
85c095b
f5828d3
ab19f69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,32 +1,39 @@ | ||||||||
| # OpenTelemetry | ||||||||
|
|
||||||||
| This example shows how to configure OpenTelemetry on a client and server, and shows | ||||||||
| what type of telemetry data it can produce for certain RPC's. | ||||||||
| This example shows how to configure OpenTelemetry on a client and server, and shows what type of telemetry data it can produce for certain RPC's. | ||||||||
|
|
||||||||
| ## Try it | ||||||||
|
|
||||||||
| ``` | ||||||||
| go run server/main.go | ||||||||
| ``` | ||||||||
| This section shows how to configure OpenTelemetry Metrics and Traces and visualize them. | ||||||||
|
|
||||||||
| ``` | ||||||||
| go run client/main.go | ||||||||
| ``` | ||||||||
| **1. Run the gRPC Applications:** | ||||||||
|
||||||||
|
|
||||||||
| ``` | ||||||||
| curl localhost:9464/metrics | ||||||||
purnesh42H marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| curl localhost:9465/metrics | ||||||||
| ``` | ||||||||
| * **Run the gRPC Server:** | ||||||||
|
|
||||||||
| ``` | ||||||||
| go run server/main.go | ||||||||
| ``` | ||||||||
| * **Run the gRPC Client:** | ||||||||
| ``` | ||||||||
| go run client/main.go | ||||||||
| ``` | ||||||||
| **2. View Telemetry Data:** | ||||||||
|
||||||||
| ``` | ||||||||
| curl localhost:9464/metrics | ||||||||
| ``` | ||||||||
| ``` | ||||||||
| curl localhost:9465/metrics | ||||||||
| ``` | ||||||||
| ## Explanation | ||||||||
| The client continuously makes RPC's to a server. The client and server both | ||||||||
| expose a prometheus exporter to listen and provide metrics. This defaults to | ||||||||
| :9464 for the server and :9465 for the client. | ||||||||
| The client continuously makes RPC's to a server. The client and server both expose a Prometheus exporter to listen and provide metrics. This defaults to :9464 for the server and :9465 for the client. The client and server are also configured to output traces directly to their standard output streams using stdouttrace. | ||||||||
|
||||||||
| OpenTelemetry is configured on both the client and the server, and exports to | ||||||||
| the Prometheus exporter. The exporter exposes metrics on the Prometheus ports | ||||||||
| described above. | ||||||||
| OpenTelemetry is configured on both the client and the server, and exports metrics to the Prometheus exporter. The exporter exposes metrics on the Prometheus ports described above. OpenTelemetry also exports traces using the stdouttrace exporter. These traces are printed as structured data to the console output of both the client and the server. | ||||||||
|
||||||||
| OpenTelemetry is configured on both the client and the server, and exports metrics to the Prometheus exporter. The exporter exposes metrics on the Prometheus ports described above. OpenTelemetry also exports traces using the stdouttrace exporter. These traces are printed as structured data to the console output of both the client and the server. | |
| OpenTelemetry is configured on both the client and the server, and exports metrics to the Prometheus exporter. The exporter exposes metrics on the Prometheus ports described above. | |
| OpenTelemetry exports traces using the stdouttrace exporter, which prints structured trace data to the console output of both the client and server. Each RPC call produces trace information that captures the execution flow and timing of operations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
purnesh42H marked this conversation as resolved.
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -27,38 +27,61 @@ import ( | |||||||
| "net/http" | ||||||||
| "time" | ||||||||
|
|
||||||||
| "github.com/prometheus/client_golang/prometheus/promhttp" | ||||||||
| "go.opentelemetry.io/otel/exporters/prometheus" | ||||||||
| otelstdouttrace "go.opentelemetry.io/otel/exporters/stdout/stdouttrace" | ||||||||
| otelpropagation "go.opentelemetry.io/otel/propagation" | ||||||||
| otelmetric "go.opentelemetry.io/otel/sdk/metric" | ||||||||
| otelresource "go.opentelemetry.io/otel/sdk/resource" | ||||||||
| sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||||||||
| semconv "go.opentelemetry.io/otel/semconv/v1.24.0" | ||||||||
| "google.golang.org/grpc" | ||||||||
| "google.golang.org/grpc/credentials/insecure" | ||||||||
| "google.golang.org/grpc/examples/features/proto/echo" | ||||||||
| oteltracing "google.golang.org/grpc/experimental/opentelemetry" | ||||||||
| "google.golang.org/grpc/stats/opentelemetry" | ||||||||
|
|
||||||||
| "github.com/prometheus/client_golang/prometheus/promhttp" | ||||||||
purnesh42H marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| "go.opentelemetry.io/otel/exporters/prometheus" | ||||||||
| "go.opentelemetry.io/otel/sdk/metric" | ||||||||
| ) | ||||||||
|
|
||||||||
| var ( | ||||||||
| addr = flag.String("addr", ":50051", "the server address to connect to") | ||||||||
| prometheusEndpoint = flag.String("prometheus_endpoint", ":9465", "the Prometheus exporter endpoint") | ||||||||
| prometheusEndpoint = flag.String("prometheus_endpoint", ":9465", "the Prometheus exporter endpoint for metrics") | ||||||||
| serviceName = "grpc-client" | ||||||||
|
||||||||
| ) | ||||||||
|
|
||||||||
| func main() { | ||||||||
| exporter, err := prometheus.New() | ||||||||
| flag.Parse() | ||||||||
|
||||||||
|
|
||||||||
| promExporter, err := prometheus.New() | ||||||||
| if err != nil { | ||||||||
| log.Fatalf("Failed to start prometheus exporter: %v", err) | ||||||||
| } | ||||||||
| provider := metric.NewMeterProvider(metric.WithReader(exporter)) | ||||||||
| go http.ListenAndServe(*prometheusEndpoint, promhttp.Handler()) | ||||||||
| // Initialize MeterProvider with Prometheus exporter. | ||||||||
|
||||||||
| meterProvider := otelmetric.NewMeterProvider(otelmetric.WithReader(promExporter)) | ||||||||
|
|
||||||||
| ctx := context.Background() | ||||||||
| do := opentelemetry.DialOption(opentelemetry.Options{MetricsOptions: opentelemetry.MetricsOptions{MeterProvider: provider}}) | ||||||||
| // Initialize stdouttrace exporter for traces | ||||||||
|
||||||||
| // Initialize stdouttrace exporter for traces | |
| // Configure exporter for traces |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nix new line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can remove this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| textMapPropagator := otelpropagation.TraceContext{} // Using W3C Trace Context Propagator. | |
| // Configure W3C Trace Context Propagator for traces | |
| textMapPropagator := otelpropagation.TraceContext{} // Using W3C Trace Context Propagator. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
purnesh42H marked this conversation as resolved.
Show resolved
Hide resolved
purnesh42H marked this conversation as resolved.
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,18 +27,24 @@ import ( | |
| "net" | ||
| "net/http" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus/promhttp" | ||
| "go.opentelemetry.io/otel/exporters/prometheus" | ||
| otelstdouttrace "go.opentelemetry.io/otel/exporters/stdout/stdouttrace" | ||
| otelpropagation "go.opentelemetry.io/otel/propagation" | ||
| otelmetric "go.opentelemetry.io/otel/sdk/metric" | ||
| otelresource "go.opentelemetry.io/otel/sdk/resource" | ||
| sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
| semconv "go.opentelemetry.io/otel/semconv/v1.24.0" | ||
| "google.golang.org/grpc" | ||
| pb "google.golang.org/grpc/examples/features/proto/echo" | ||
| oteltracing "google.golang.org/grpc/experimental/opentelemetry" | ||
| "google.golang.org/grpc/stats/opentelemetry" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus/promhttp" | ||
| "go.opentelemetry.io/otel/exporters/prometheus" | ||
| "go.opentelemetry.io/otel/sdk/metric" | ||
| ) | ||
|
|
||
| var ( | ||
| addr = flag.String("addr", ":50051", "the server address to connect to") | ||
| prometheusEndpoint = flag.String("prometheus_endpoint", ":9464", "the Prometheus exporter endpoint") | ||
| prometheusEndpoint = flag.String("prometheus_endpoint", ":9464", "the Prometheus exporter endpoint for metrics") | ||
| serviceName = "grpc-server" | ||
|
||
| ) | ||
|
|
||
| type echoServer struct { | ||
|
|
@@ -51,14 +57,30 @@ func (s *echoServer) UnaryEcho(_ context.Context, req *pb.EchoRequest) (*pb.Echo | |
| } | ||
|
|
||
| func main() { | ||
| exporter, err := prometheus.New() | ||
| flag.Parse() | ||
|
||
|
|
||
|
||
| promExporter, err := prometheus.New() | ||
| if err != nil { | ||
| log.Fatalf("Failed to start prometheus exporter: %v", err) | ||
| } | ||
| provider := metric.NewMeterProvider(metric.WithReader(exporter)) | ||
| go http.ListenAndServe(*prometheusEndpoint, promhttp.Handler()) | ||
| // Initialize MeterProvider with Prometheus exporter. | ||
|
||
| meterProvider := otelmetric.NewMeterProvider(otelmetric.WithReader(promExporter)) | ||
|
|
||
| so := opentelemetry.ServerOption(opentelemetry.Options{MetricsOptions: opentelemetry.MetricsOptions{MeterProvider: provider}}) | ||
| // Initialize stdouttrace exporter for traces | ||
| traceExporter, err := otelstdouttrace.New(otelstdouttrace.WithPrettyPrint()) | ||
| if err != nil { | ||
| log.Fatalf("Failed to create stdouttrace exporter: %v", err) | ||
| } | ||
|
|
||
| // Configure the tracer provider | ||
| tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(traceExporter), sdktrace.WithResource(otelresource.NewWithAttributes(semconv.SchemaURL, semconv.ServiceName(serviceName)))) | ||
| textMapPropagator := otelpropagation.TraceContext{} // Using W3C Trace Context Propagator. | ||
|
|
||
| so := opentelemetry.ServerOption(opentelemetry.Options{ | ||
purnesh42H marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| MetricsOptions: opentelemetry.MetricsOptions{MeterProvider: meterProvider}, | ||
| TraceOptions: oteltracing.TraceOptions{TracerProvider: tp, TextMapPropagator: textMapPropagator}}) | ||
|
|
||
| go http.ListenAndServe(*prometheusEndpoint, promhttp.Handler()) | ||
|
|
||
| lis, err := net.Listen("tcp", *addr) | ||
| if err != nil { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please restrict the sentences to 80 cols. What is the change here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reached 80 cols, so moved to the next line.