Repository: grpc/grpc-go
File: stats/opentelemetry/e2e_test.go (Go)

Code before:
			},
		},
		{
			Name:        "grpc.subchannel.connection_attempts_failed",
			Description: "EXPERIMENTAL. Number of failed connection attempts.",
			Unit:        "{attempt}",
			Data: metricdata.Sum[int64]{
				DataPoints: []metricdata.DataPoint[int64]{
					{
						Attributes: attribute.NewSet(targetAttr, backendServiceAttr, localityAttr),
						Value:      1, // It will try to reconnect at least once
					},
				},
				Temporality: metricdata.CumulativeTemporality,
				IsMonotonic: true,
			},
		},
	}

	if err := pollForWantMetrics(ctx, t, reader, disconnectionWantMetrics); err != nil {
		t.Fatal(err)
	}
}

func (s) TestHealthStreamNoOtelErrorLog(t *testing.T) {
	// Setup a minimal, non-recursive logger to capture the specific error log.
	tl := &testLogReader{}
	grpclog.SetLoggerV2(tl)

	mo, _ := defaultMetricsOptions(t, nil)
	to, _ := defaultTraceOptions(t)
	otelOptions := opentelemetry.Options{
		MetricsOptions: *mo,
		TraceOptions:   *to,
	}

	// Start a vanilla backend.
	backend := stubserver.StartTestService(t, &stubserver.StubServer{
		EmptyCallF: func(_ context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
			return &testpb.Empty{}, nil
		},
	})
	defer backend.Stop()

	// Dial with healthCheckConfig to trigger the internal health stream.
	sc := `{"healthCheckConfig": {"serviceName": ""}}`
	dopts := []grpc.DialOption{
		grpc.WithTransportCredentials(insecure.NewCredentials()),
		opentelemetry.DialOption(otelOptions),
		grpc.WithDefaultServiceConfig(sc),
	}

Review comment (bug):
The test is already passing on the `master` branch. After investigation, I found that it's because health checks are still. To enable health checks:
1. The health package needs to be imported.
```go
import (
	_ "google.golang.org/grpc/health"
)
```
2. The server needs to have the health service running.
```go
import (
	healthgrpc "google.golang.org/grpc/health/grpc_health_v1"
	healthpb "google.golang.org/grpc/health/grpc_health_v1"
)
```
```go
func (s) TestHealthStreamNoOtelErrorLog(t *testing.T) {
    // omitted.
	defer backend.Stop()
	healthcheck := health.NewServer()
	healthgrpc.RegisterHealthServer(backend.S, healthcheck)
	healthcheck.SetServingStatus("", healthpb.HealthCheckResponse_SERVING)
```
3. The Load Balancing policy needs to be something other than `pick_first`, as health checks are explicitly disallowed when using the `pick_first` policy.
```go
	sc := `{"loadBalancingConfig": [{"round_robin":{}}], "healthCheckConfig": {"serviceName": ""}}`
```

With these changes, the test should fail on the master branch due to the error log from the Otel stats handler and it should pass with changes in this PR.

Code after:
				IsMonotonic: true,
			},
		},
		{
			Name:        "grpc.subchannel.connection_attempts_failed",
			Description: "EXPERIMENTAL. Number of failed connection attempts.",
			Unit:        "{attempt}",
			Data: metricdata.Sum[int64]{
				DataPoints: []metricdata.DataPoint[int64]{
					{
						Attributes: attribute.NewSet(targetAttr, backendServiceAttr, localityAttr),
						Value:      1, // It will try to reconnect at least once
					},
				},
				Temporality: metricdata.CumulativeTemporality,
				IsMonotonic: true,
			},
		},
	}

	if err := pollForWantMetrics(ctx, t, reader, disconnectionWantMetrics); err != nil {
		t.Fatal(err)
	}
}

func (s) TestHealthStreamNoOtelErrorLog(t *testing.T) {
	mo, _ := defaultMetricsOptions(t, nil)
	to, _ := defaultTraceOptions(t)
	otelOptions := opentelemetry.Options{
		MetricsOptions: *mo,
		TraceOptions:   *to,
	}

	healthcheck := health.NewServer()

	backend := stubserver.StartTestService(t, &stubserver.StubServer{
		EmptyCallF: func(_ context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
			return &testpb.Empty{}, nil
		},
	}, stubserver.RegisterServiceServerOption(func(s grpc.ServiceRegistrar) {
		healthgrpc.RegisterHealthServer(s, healthcheck)
	}))
	defer backend.Stop()

	healthcheck.SetServingStatus("", healthpb.HealthCheckResponse_SERVING)

	// Dial with healthCheckConfig to trigger the internal health stream.
	sc := `{"loadBalancingConfig": [{"round_robin":{}}], "healthCheckConfig": {"serviceName": ""}}`
	dopts := []grpc.DialOption{
		grpc.WithTransportCredentials(insecure.NewCredentials()),
		opentelemetry.DialOption(otelOptions),