Explorar o código

fix(trace): grpc exporter should use nonblock option (#2744)

* fix(trace): grpc exporter should use nonblock option

* chore: sort imports
cong %!s(int64=2) %!d(string=hai) anos
pai
achega
deefc1a8eb
Modificáronse 2 ficheiros con 13 adicións e 7 borrados
  1. 8 4
      core/trace/agent.go
  2. 5 3
      core/trace/agent_test.go

+ 8 - 4
core/trace/agent.go

@@ -5,8 +5,6 @@ import (
 	"fmt"
 	"sync"
 
-	"github.com/zeromicro/go-zero/core/lang"
-	"github.com/zeromicro/go-zero/core/logx"
 	"go.opentelemetry.io/otel"
 	"go.opentelemetry.io/otel/exporters/jaeger"
 	"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
@@ -14,7 +12,9 @@ import (
 	"go.opentelemetry.io/otel/sdk/resource"
 	sdktrace "go.opentelemetry.io/otel/sdk/trace"
 	semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
-	"google.golang.org/grpc"
+
+	"github.com/zeromicro/go-zero/core/lang"
+	"github.com/zeromicro/go-zero/core/logx"
 )
 
 const (
@@ -60,11 +60,15 @@ func createExporter(c Config) (sdktrace.SpanExporter, error) {
 	case kindZipkin:
 		return zipkin.New(c.Endpoint)
 	case kindGrpc:
+		// Always treat trace exporter as optional component, so we use nonblock here,
+		// otherwise this would slow down app start up even set a dial timeout here when
+		// endpoint can not reach.
+		// If the connection not dial success, the global otel ErrorHandler will catch error
+		// when reporting data like other exporters.
 		return otlptracegrpc.New(
 			context.Background(),
 			otlptracegrpc.WithInsecure(),
 			otlptracegrpc.WithEndpoint(c.Endpoint),
-			otlptracegrpc.WithDialOption(grpc.WithBlock()),
 		)
 	default:
 		return nil, fmt.Errorf("unknown exporter: %s", c.Batcher)

+ 5 - 3
core/trace/agent_test.go

@@ -4,6 +4,7 @@ import (
 	"testing"
 
 	"github.com/stretchr/testify/assert"
+
 	"github.com/zeromicro/go-zero/core/logx"
 )
 
@@ -13,6 +14,7 @@ func TestStartAgent(t *testing.T) {
 	const (
 		endpoint1 = "localhost:1234"
 		endpoint2 = "remotehost:1234"
+		endpoint3 = "localhost:1235"
 	)
 	c1 := Config{
 		Name: "foo",
@@ -29,12 +31,12 @@ func TestStartAgent(t *testing.T) {
 	}
 	c4 := Config{
 		Name:     "bla",
-		Endpoint: endpoint1,
+		Endpoint: endpoint3,
 		Batcher:  "otlp",
 	}
 	c5 := Config{
 		Name:     "grpc",
-		Endpoint: endpoint1,
+		Endpoint: endpoint3,
 		Batcher:  "grpc",
 	}
 
@@ -49,7 +51,7 @@ func TestStartAgent(t *testing.T) {
 	defer lock.Unlock()
 
 	// because remotehost cannot be resolved
-	assert.Equal(t, 2, len(agents))
+	assert.Equal(t, 3, len(agents))
 	_, ok := agents[""]
 	assert.True(t, ok)
 	_, ok = agents[endpoint1]