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),
	}

	cc, err := grpc.NewClient(backend.Address, dopts...)

Review comment (bug):
A custom logger should not be necessary here. When running tests through `grpctester` (indicated by the `(s)` receiver in the test name), the test is automatically failed if any error is logged.

Code after:
		},
		{
			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),
		grpc.WithDefaultServiceConfig(sc),
	}