Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
Signed-off-by: aoiasd <[email protected]>
  • Loading branch information
aoiasd committed Apr 16, 2024
1 parent 686c97b commit 2450154
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
42 changes: 42 additions & 0 deletions pkg/tracer/tarcer_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 5 additions & 2 deletions pkg/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/paramtable/component_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2450154

Please sign in to comment.