From d7ad6c44bda05c6c262a87b0a0592ca6b61c7e68 Mon Sep 17 00:00:00 2001 From: ston1th Date: Sun, 4 Aug 2019 21:51:11 +0200 Subject: [PATCH] added request tracing --- testapp/api/main.go | 35 +++++++++++++++++++++++++++++++---- testapp/db/main.go | 33 ++++++++++++++++++++++++++++++--- testapp/srv/main.go | 35 +++++++++++++++++++++++++++++++---- 3 files changed, 92 insertions(+), 11 deletions(-) diff --git a/testapp/api/main.go b/testapp/api/main.go index 58f2f38..b95c887 100644 --- a/testapp/api/main.go +++ b/testapp/api/main.go @@ -12,8 +12,35 @@ import ( var version string -func get(c *http.Client, url string) (body string, err error) { - resp, err := c.Get(url) +var tracer = []string{ + "x-request-id", + "x-b3-traceid", + "x-b3-spanid", + "x-b3-parentspanid", + "x-b3-sampled", + "x-b3-flags", + "x-ot-span-context", +} + +func trace(rd, rs *http.Request) { + if rd == nil || rs == nil { + return + } + for _, t := range tracer { + v := rs.Header.Get(t) + if v != "" { + rd.Header.Set(t, v) + } + } +} + +func get(c *http.Client, req *http.Request, url string) (body string, err error) { + r, err := http.NewRequest("GET", url, nil) + if err != nil { + return + } + trace(r, req) + resp, err := c.Do(r) if err != nil { return } @@ -39,11 +66,11 @@ func main() { Timeout: 5 * time.Second, }).DialContext, }} - health, err := get(c, "http://testapp-db:8080/healthz") + health, err := get(c, r, "http://testapp-db:8080/healthz") if err != nil { health = err.Error() } - ext, err := get(c, "http://httpbin.org/robots.txt") + ext, err := get(c, nil, "http://httpbin.org/robots.txt") if err != nil { ext = err.Error() } diff --git a/testapp/db/main.go b/testapp/db/main.go index d6c8c56..b35759c 100644 --- a/testapp/db/main.go +++ b/testapp/db/main.go @@ -12,8 +12,35 @@ import ( var version string -func get(c *http.Client, url string) (body string, err error) { - resp, err := c.Get(url) +var tracer = []string{ + "x-request-id", + "x-b3-traceid", + "x-b3-spanid", + "x-b3-parentspanid", + "x-b3-sampled", + "x-b3-flags", + "x-ot-span-context", +} + +func trace(rd, rs *http.Request) { + if rd == nil || rs == nil { + return + } + for _, t := range tracer { + v := rs.Header.Get(t) + if v != "" { + rd.Header.Set(t, v) + } + } +} + +func get(c *http.Client, req *http.Request, url string) (body string, err error) { + r, err := http.NewRequest("GET", url, nil) + if err != nil { + return + } + trace(r, req) + resp, err := c.Do(r) if err != nil { return } @@ -39,7 +66,7 @@ func main() { Timeout: 5 * time.Second, }).DialContext, }} - body, err := get(c, "http://testapp-api:8080/healthz") + body, err := get(c, r, "http://testapp-api:8080/healthz") if err != nil { body = err.Error() } diff --git a/testapp/srv/main.go b/testapp/srv/main.go index 1b3d940..b2587c4 100644 --- a/testapp/srv/main.go +++ b/testapp/srv/main.go @@ -14,8 +14,35 @@ import ( var version string -func get(c *http.Client, url string) (body string, err error) { - resp, err := c.Get(url) +var tracer = []string{ + "x-request-id", + "x-b3-traceid", + "x-b3-spanid", + "x-b3-parentspanid", + "x-b3-sampled", + "x-b3-flags", + "x-ot-span-context", +} + +func trace(rd, rs *http.Request) { + if rd == nil || rs == nil { + return + } + for _, t := range tracer { + v := rs.Header.Get(t) + if v != "" { + rd.Header.Set(t, v) + } + } +} + +func get(c *http.Client, req *http.Request, url string) (body string, err error) { + r, err := http.NewRequest("GET", url, nil) + if err != nil { + return + } + trace(r, req) + resp, err := c.Do(r) if err != nil { return } @@ -49,12 +76,12 @@ func main() { }).DialContext, }} dbUrl := "http://testapp-db:8080" - dbBody, err := get(c, dbUrl) + dbBody, err := get(c, r, dbUrl) if err != nil { dbBody = err.Error() } apiUrl := "http://testapp-api:8080" - apiBody, err := get(c, apiUrl) + apiBody, err := get(c, r, apiUrl) if err != nil { apiBody = err.Error() }