feat: add trace in httpc (#2011)

This commit is contained in:
chen quan
2022-06-17 02:01:14 -05:00
committed by GitHub
parent 0147d7a9d1
commit 6518eb10b3
2 changed files with 66 additions and 3 deletions

View File

@@ -4,15 +4,25 @@ import (
"context"
"net/http"
"net/http/httptest"
"net/http/httptrace"
"testing"
"github.com/stretchr/testify/assert"
ztrace "github.com/zeromicro/go-zero/core/trace"
"github.com/zeromicro/go-zero/rest/httpx"
"github.com/zeromicro/go-zero/rest/internal/header"
"github.com/zeromicro/go-zero/rest/router"
"go.opentelemetry.io/otel/trace"
)
func TestDoRequest(t *testing.T) {
ztrace.StartAgent(ztrace.Config{
Name: "go-zero-test",
Endpoint: "http://localhost:14268/api/traces",
Batcher: "jaeger",
Sampler: 1.0,
})
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))
defer svr.Close()
@@ -21,6 +31,8 @@ func TestDoRequest(t *testing.T) {
resp, err := DoRequest(req)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
spanContext := trace.SpanContextFromContext(resp.Request.Context())
assert.True(t, spanContext.IsValid())
}
func TestDoRequest_NotFound(t *testing.T) {
@@ -187,3 +199,17 @@ func TestDo_Json(t *testing.T) {
_, err = Do(context.Background(), http.MethodPost, svr.URL+"/nodes/:key", data)
assert.NotNil(t, err)
}
func TestDo_WithClientHttpTrace(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
defer svr.Close()
_, err := Do(httptrace.WithClientTrace(context.Background(),
&httptrace.ClientTrace{
DNSStart: func(info httptrace.DNSStartInfo) {
assert.Equal(t, "localhost", info.Host)
},
}), http.MethodGet, svr.URL, nil)
assert.Nil(t, err)
}