Skip to content

Commit

Permalink
fix: flusher_otlp stop nil log/metric/trace client (#1994)
Browse files Browse the repository at this point in the history
* fix nil log/metric/trace otlp client stop issue

Change-Id: I8948322d1e54a61c7b17968a5bcaf5590d9a437c

* add ut

Change-Id: Ib0003a387cb9d23592f28fd8097bc49ff3ae2351
  • Loading branch information
shunjiazhu authored Dec 30, 2024
1 parent 2dc7605 commit dd5b59e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 3 additions & 3 deletions plugins/flusher/opentelemetry/flusher_otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions plugins/flusher/opentelemetry/flusher_otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
})
})
}
Expand Down

0 comments on commit dd5b59e

Please sign in to comment.