diff --git a/pkg/tracer/tarcer_test.go b/pkg/tracer/tarcer_test.go new file mode 100644 index 0000000000000..87391b7f61ca7 --- /dev/null +++ b/pkg/tracer/tarcer_test.go @@ -0,0 +1,42 @@ +// Licensed to the LF AI & Data foundation under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tracer + +import ( + "testing" + + "github.com/milvus-io/milvus/pkg/util/paramtable" + "github.com/stretchr/testify/assert" +) + +func TestTracer_Init(t *testing.T) { + paramtable.Init() + paramtable.Get().Save(paramtable.Get().TraceCfg.Exporter.Key, "Unknown") + // init failed with unkown exporter + err := Init() + assert.Error(t, err) + + paramtable.Get().Save(paramtable.Get().TraceCfg.Exporter.Key, "stdout") + // init with stdout exporter + err = Init() + assert.NoError(t, err) + + paramtable.Get().Save(paramtable.Get().TraceCfg.Exporter.Key, "noop") + // init with noop exporter + err = Init() + assert.NoError(t, err) +} diff --git a/pkg/tracer/tracer.go b/pkg/tracer/tracer.go index c919144231875..c378445591db7 100644 --- a/pkg/tracer/tracer.go +++ b/pkg/tracer/tracer.go @@ -36,18 +36,19 @@ import ( "github.com/milvus-io/milvus/pkg/util/paramtable" ) -func Init() { +func Init() error { params := paramtable.Get() exp, err := CreateTracerExporter(params) if err != nil { log.Warn("Init tracer faield", zap.Error(err)) - return + return err } SetTracerProvider(exp, params.TraceCfg.SampleFraction.GetAsFloat()) otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})) log.Info("Init tracer finished", zap.String("Exporter", params.TraceCfg.Exporter.GetValue())) + return nil } func CloseTracerProvider() error { @@ -100,6 +101,8 @@ func CreateTracerExporter(params *paramtable.ComponentParam) (sdk.SpanExporter, exp, err = otlptracegrpc.New(context.Background(), opts...) case "stdout": exp, err = stdout.New() + case "noop": + return nil, nil default: err = errors.New("Empty Trace") } diff --git a/pkg/util/paramtable/component_param.go b/pkg/util/paramtable/component_param.go index 6694a600c806b..e8fa2e9949591 100644 --- a/pkg/util/paramtable/component_param.go +++ b/pkg/util/paramtable/component_param.go @@ -761,8 +761,8 @@ func (t *traceConfig) init(base *BaseTable) { Key: "trace.exporter", Version: "2.3.0", Doc: `trace exporter type, default is stdout, -optional values: ['stdout', 'jaeger', 'otlp']`, - DefaultValue: "stdout", +optional values: ['noop','stdout', 'jaeger', 'otlp']`, + DefaultValue: "noop", Export: true, } t.Exporter.Init(base.mgr)