2021-04-19 13:42:47 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed 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.
|
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
package trace
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-12-27 20:06:05 +08:00
|
|
|
"errors"
|
2021-02-23 09:58:06 +08:00
|
|
|
"fmt"
|
2021-09-29 22:02:00 +08:00
|
|
|
"os"
|
2021-02-23 09:58:06 +08:00
|
|
|
"testing"
|
|
|
|
|
2021-09-03 15:41:25 +08:00
|
|
|
"github.com/opentracing/opentracing-go"
|
2021-02-23 09:58:06 +08:00
|
|
|
oplog "github.com/opentracing/opentracing-go/log"
|
2021-09-03 15:41:25 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-23 09:58:06 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type simpleStruct struct {
|
|
|
|
name string
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
2021-09-29 22:02:00 +08:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
closer := InitTracing("test")
|
|
|
|
defer closer.Close()
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2021-09-03 15:41:25 +08:00
|
|
|
func TestInit(t *testing.T) {
|
2021-09-17 16:29:51 +08:00
|
|
|
cfg := initFromEnv("test")
|
2021-09-03 15:41:25 +08:00
|
|
|
assert.NotNil(t, cfg)
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
func TestTracing(t *testing.T) {
|
|
|
|
// context normally can be propagated through func params
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
//start span
|
|
|
|
//default use function name for operation name
|
|
|
|
sp, ctx := StartSpanFromContext(ctx)
|
2021-09-03 15:41:25 +08:00
|
|
|
id, sampled, found := InfoFromContext(ctx)
|
|
|
|
fmt.Printf("traceID = %s, sampled = %t, found = %t", id, sampled, found)
|
2021-02-23 09:58:06 +08:00
|
|
|
sp.SetTag("tag1", "tag1")
|
|
|
|
// use self-defined operation name for span
|
|
|
|
// sp, ctx := StartSpanFromContextWithOperationName(ctx, "self-defined name")
|
|
|
|
defer sp.Finish()
|
|
|
|
|
|
|
|
ss := &simpleStruct{
|
|
|
|
name: "name",
|
|
|
|
value: "value",
|
|
|
|
}
|
|
|
|
sp.LogFields(oplog.String("key", "value"), oplog.Object("key", ss))
|
|
|
|
|
|
|
|
err := caller(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
LogError(sp, err) //LogError do something error log in trace and returns origin error.
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func caller(ctx context.Context) error {
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
// if span starts in a loop, defer is not allowed.
|
|
|
|
// manually call span.Finish() if error occurs or one loop ends
|
|
|
|
sp, _ := StartSpanFromContextWithOperationName(ctx, fmt.Sprintf("test:%d", i))
|
|
|
|
sp.SetTag(fmt.Sprintf("tags:%d", i), fmt.Sprintf("tags:%d", i))
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if i == 1 {
|
|
|
|
err = errors.New("test")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2021-10-06 07:04:02 +08:00
|
|
|
LogError(sp, err)
|
2021-02-23 09:58:06 +08:00
|
|
|
sp.Finish()
|
2021-10-06 07:04:02 +08:00
|
|
|
return nil
|
2021-02-23 09:58:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sp.Finish()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-03 15:41:25 +08:00
|
|
|
|
|
|
|
func TestInject(t *testing.T) {
|
|
|
|
// context normally can be propagated through func params
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
//start span
|
|
|
|
//default use function name for operation name
|
|
|
|
sp, ctx := StartSpanFromContext(ctx)
|
|
|
|
id, sampled, found := InfoFromContext(ctx)
|
|
|
|
fmt.Printf("traceID = %s, sampled = %t, found = %t", id, sampled, found)
|
|
|
|
pp := PropertiesReaderWriter{PpMap: map[string]string{}}
|
|
|
|
InjectContextToPulsarMsgProperties(sp.Context(), pp.PpMap)
|
|
|
|
tracer := opentracing.GlobalTracer()
|
|
|
|
sc, _ := tracer.Extract(opentracing.TextMap, pp)
|
|
|
|
assert.NotNil(t, sc)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTraceError(t *testing.T) {
|
|
|
|
// context normally can be propagated through func params
|
|
|
|
sp, ctx := StartSpanFromContext(nil)
|
|
|
|
assert.Nil(t, ctx)
|
|
|
|
assert.NotNil(t, sp)
|
|
|
|
|
|
|
|
sp, ctx = StartSpanFromContextWithOperationName(nil, "test")
|
|
|
|
assert.Nil(t, ctx)
|
|
|
|
assert.NotNil(t, sp)
|
|
|
|
|
|
|
|
//Will Cause span log error
|
|
|
|
StartSpanFromContextWithOperationNameWithSkip(context.Background(), "test", 10000)
|
|
|
|
|
|
|
|
//Will Cause span log error
|
|
|
|
StartSpanFromContextWithSkip(context.Background(), 10000)
|
|
|
|
|
|
|
|
id, sampled, found := InfoFromSpan(nil)
|
|
|
|
assert.Equal(t, id, "")
|
|
|
|
assert.Equal(t, sampled, false)
|
|
|
|
assert.Equal(t, found, false)
|
|
|
|
|
|
|
|
id, sampled, found = InfoFromContext(nil)
|
|
|
|
assert.Equal(t, id, "")
|
|
|
|
assert.Equal(t, sampled, false)
|
|
|
|
assert.Equal(t, found, false)
|
|
|
|
}
|