github.com/zooyer/miskit@v1.0.71/trace/trace_test.go (about)

     1  package trace
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"log"
     7  	"net/http"
     8  	"testing"
     9  )
    10  
    11  func TestNew(t *testing.T) {
    12  	var trace = New(nil, "test")
    13  	trace.Lang = "zh-CN"
    14  	trace.Tag = "压测"
    15  	trace.Content = json.RawMessage(`{"key":"value"}`)
    16  
    17  	// HTTP请求传递
    18  	req, err := http.NewRequest("GET", "http://www.baidu.com", nil)
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	trace.SetHeader(req.Header)
    23  
    24  	// baidu
    25  	trace = New(req, "baidu")
    26  	log.Println(trace.String())
    27  
    28  	// context上下文传递
    29  	var ctx = context.Background()
    30  	ctx = Set(ctx, trace)
    31  	trace = Get(ctx)
    32  	log.Println(trace.String())
    33  
    34  	// 下游请求
    35  	trace = trace.GenChild()
    36  	trace.SetHeader(req.Header)
    37  	trace = New(req, "child")
    38  	log.Println(trace.String())
    39  }