diff --git a/plugins/flusher/opentelemetry/flusher_otlp.go b/plugins/flusher/opentelemetry/flusher_otlp.go index fce944339e..ed747e9c25 100644 --- a/plugins/flusher/opentelemetry/flusher_otlp.go +++ b/plugins/flusher/opentelemetry/flusher_otlp.go @@ -160,21 +160,21 @@ func (f *FlusherOTLP) IsReady(projectName string, logstoreName string, logstoreK func (f *FlusherOTLP) Stop() error { var err error - if f.logClient.grpcConn != nil { + if f.logClient != nil && f.logClient.grpcConn != nil { err = f.logClient.grpcConn.Close() if err != nil { logger.Error(f.context.GetRuntimeContext(), "FLUSHER_STOP_ALARM", "stop otlp logs flusher fail, error", err) } } - if f.metricClient.grpcConn != nil { + if f.metricClient != nil && f.metricClient.grpcConn != nil { err = f.metricClient.grpcConn.Close() if err != nil { logger.Error(f.context.GetRuntimeContext(), "FLUSHER_STOP_ALARM", "stop otlp metrics flusher fail, error", err) } } - if f.traceClient.grpcConn != nil { + if f.traceClient != nil && f.traceClient.grpcConn != nil { err = f.traceClient.grpcConn.Close() if err != nil { logger.Error(f.context.GetRuntimeContext(), "FLUSHER_STOP_ALARM", "stop otlp traces flusher fail, error", err) diff --git a/plugins/flusher/opentelemetry/flusher_otlp_test.go b/plugins/flusher/opentelemetry/flusher_otlp_test.go index 45637a4085..24f70bd52f 100644 --- a/plugins/flusher/opentelemetry/flusher_otlp_test.go +++ b/plugins/flusher/opentelemetry/flusher_otlp_test.go @@ -50,6 +50,13 @@ func (t *TestOtlpLogService) Export(ctx context.Context, request *otlpv1.ExportL } func Test_Flusher_Init(t *testing.T) { + convey.Convey("When config is invalid", t, func() { + convey.Convey("When FlusherOTLP is not initialized", func() { + f := &FlusherOTLP{Version: v1, Logs: &helper.GrpcClientConfig{Endpoint: ":8080"}} + err := f.Stop() + convey.So(err, convey.ShouldBeNil) + }) + }) convey.Convey("When init grpc service", t, func() { _, server := newTestGrpcService(t, ":8080", time.Nanosecond) defer func() { @@ -61,6 +68,8 @@ func Test_Flusher_Init(t *testing.T) { f := &FlusherOTLP{Version: v1, Logs: &helper.GrpcClientConfig{Endpoint: ":8080"}} err := f.Init(logCtx) convey.So(err, convey.ShouldBeNil) + err = f.Stop() + convey.So(err, convey.ShouldBeNil) }) }) }