Repository: grpc/grpc-go
File: examples/features/opentelemetry/client/main.go (Go)

Code before:

var (
	addr               = flag.String("addr", ":50051", "the server address to connect to")
	prometheusEndpoint = flag.String("prometheus_endpoint", ":9465", "the Prometheus exporter endpoint for metrics")
)

func main() {
	exporter, err := prometheus.New()
	if err != nil {
		log.Fatalf("Failed to start prometheus exporter: %v", err)
	}
	// Configure meter provider for metrics
	meterProvider := otelmetric.NewMeterProvider(otelmetric.WithReader(exporter))
	// Configure exporter for traces
	traceExporter, err := otelstdouttrace.New(otelstdouttrace.WithPrettyPrint())
	if err != nil {
		log.Fatalf("Failed to create stdouttrace exporter: %v", err)
	}
	traceProvider := sdktrace.NewTracerProvider(sdktrace.WithBatcher(traceExporter), sdktrace.WithResource(otelresource.NewWithAttributes(semconv.SchemaURL, semconv.ServiceName("grpc-client"))))
	// Configure W3C Trace Context Propagator for traces
	textMapPropagator := otelpropagation.TraceContext{}
	do := opentelemetry.DialOption(opentelemetry.Options{
		MetricsOptions: opentelemetry.MetricsOptions{
			MeterProvider: meterProvider,
			Metrics: opentelemetry.DefaultMetrics().Add(
				"grpc.lb.wrr.rr_fallback",
				"grpc.lb.wrr.endpoint_weight_not_yet_usable",
				"grpc.lb.wrr.endpoint_weight_stale",
				"grpc.lb.wrr.endpoint_weights",
				"grpc.lb.pick_first.disconnections",
				"grpc.lb.pick_first.connection_attempts_succeeded",
				"grpc.lb.pick_first.connection_attempts_failed",
				"grpc.xds_client.connected",
				"grpc.xds_client.server_failure",
				"grpc.xds_client.resource_updates_valid",
				"grpc.xds_client.resource_updates_invalid",
				"grpc.xds_client.resources",
			),
		},
		TraceOptions: oteltracing.TraceOptions{TracerProvider: traceProvider, TextMapPropagator: textMapPropagator},
	})

	go http.ListenAndServe(*prometheusEndpoint, promhttp.Handler())

	cc, err := grpc.NewClient(*addr, grpc.WithTransportCredentials(insecure.NewCredentials()), do)
	if err != nil {
		log.Fatalf("grpc.NewClient() failed: %v", err)
	}
	defer cc.Close()
	c := echo.NewEchoClient(cc)
	ctx := context.Background()

Review comment (suggestion):
Keeping this list updates is not going to be easy. So we should avoid listing all the experimental metrics and instead add a couple of metrics with a  comment on top stating that these are experimental metrics and link to the list of [all possible metric values](https://grpc.io/docs/guides/opentelemetry-metrics/#instruments).

Code after:

var (
	addr               = flag.String("addr", ":50051", "the server address to connect to")
	prometheusEndpoint = flag.String("prometheus_endpoint", ":9465", "the Prometheus exporter endpoint for metrics")
)

func main() {
	exporter, err := prometheus.New()
	if err != nil {
		log.Fatalf("Failed to start prometheus exporter: %v", err)
	}
	// Configure meter provider for metrics
	meterProvider := otelmetric.NewMeterProvider(otelmetric.WithReader(exporter))
	// Configure exporter for traces
	traceExporter, err := otelstdouttrace.New(otelstdouttrace.WithPrettyPrint())
	if err != nil {
		log.Fatalf("Failed to create stdouttrace exporter: %v", err)
	}
	traceProvider := sdktrace.NewTracerProvider(sdktrace.WithBatcher(traceExporter), sdktrace.WithResource(otelresource.NewWithAttributes(semconv.SchemaURL, semconv.ServiceName("grpc-client"))))
	// Configure W3C Trace Context Propagator for traces
	textMapPropagator := otelpropagation.TraceContext{}
	do := opentelemetry.DialOption(opentelemetry.Options{
		MetricsOptions: opentelemetry.MetricsOptions{
			MeterProvider: meterProvider,
			// These are example experimental gRPC metrics, which are disabled
			// by default and must be explicitly enabled. For the full,
			// up-to-date list of metrics, see:
			// https://grpc.io/docs/guides/opentelemetry-metrics/#instruments
			Metrics: opentelemetry.DefaultMetrics().Add(
				"grpc.client.attempt.started",
				"grpc.client.attempt.duration",
			),
		},
		TraceOptions: oteltracing.TraceOptions{TracerProvider: traceProvider, TextMapPropagator: textMapPropagator},
	})

	go http.ListenAndServe(*prometheusEndpoint, promhttp.Handler())

	cc, err := grpc.NewClient(*addr, grpc.WithTransportCredentials(insecure.NewCredentials()), do)
	if err != nil {
		log.Fatalf("grpc.NewClient() failed: %v", err)
	}
	defer cc.Close()
	c := echo.NewEchoClient(cc)
	ctx := context.Background()

	// Make an RPC every second. This should trigger telemetry to be emitted from
	// the client and the server.
	for {
		r, err := c.UnaryEcho(ctx, &echo.EchoRequest{Message: "this is examples/opentelemetry"})
		if err != nil {