github.com/gogf/gf/v2@v2.7.4/net/ghttp/ghttp_z_unit_feature_otel_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package ghttp_test
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/gogf/gf/v2/frame/g"
    15  	"github.com/gogf/gf/v2/net/ghttp"
    16  	"github.com/gogf/gf/v2/net/gtrace"
    17  	"github.com/gogf/gf/v2/test/gtest"
    18  	"github.com/gogf/gf/v2/util/guid"
    19  )
    20  
    21  func Test_OTEL_TraceID(t *testing.T) {
    22  	var (
    23  		traceId string
    24  	)
    25  	s := g.Server(guid.S())
    26  	s.BindHandler("/", func(r *ghttp.Request) {
    27  		traceId = gtrace.GetTraceID(r.Context())
    28  		r.Response.Write(r.GetUrl())
    29  	})
    30  	s.SetDumpRouterMap(false)
    31  	s.Start()
    32  	defer s.Shutdown()
    33  
    34  	time.Sleep(100 * time.Millisecond)
    35  	gtest.C(t, func(t *gtest.T) {
    36  		prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
    37  		client := g.Client()
    38  		client.SetBrowserMode(true)
    39  		client.SetPrefix(prefix)
    40  		res, err := client.Get(ctx, "/")
    41  		t.AssertNil(err)
    42  		defer res.Close()
    43  
    44  		t.Assert(res.Header.Get("Trace-Id"), traceId)
    45  	})
    46  }