diff --git a/example/router.go b/example/router.go index 204b3f6e286552e09ccc01fcc47efbd8960b5a7f..03a4ca4c49f85b4645997ff73b015fcad38bfd47 100644 --- a/example/router.go +++ b/example/router.go @@ -3,16 +3,30 @@ package main import ( "fmt" "io" - "log" "net/http" "strconv" "time" "gitlab.com/gitlab-org/labkit/correlation" + "gitlab.com/gitlab-org/labkit/errortracking" + logkit "gitlab.com/gitlab-org/labkit/log" "gitlab.com/gitlab-org/labkit/tracing" tracingcorrelation "gitlab.com/gitlab-org/labkit/tracing/correlation" ) +func handle(pattern string, handler http.Handler) { + // Note: handlers applied in reverse order + handler = tracingcorrelation.BaggageHandler(handler) + // Use this route identifier with the tracing middleware + handler = tracing.Handler(handler, tracing.WithRouteIdentifier(pattern)) + handler = errortracking.NewHandler(handler) + handler = logkit.AccessLogger(handler) + handler = correlation.InjectCorrelationID(handler, correlation.WithPropagation()) + + // Listen and propagate traces + http.Handle(pattern, handler) +} + func main() { tracing.Initialize(tracing.WithServiceName("router")) @@ -27,59 +41,55 @@ func main() { } // Listen and propagate traces - http.Handle("/query", - // Add the tracing middleware in - correlation.InjectCorrelationID( - tracing.Handler( - tracingcorrelation.BaggageHandler( - - http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - query := r.URL.Query() - ttlString := query.Get("ttl") - var ttl int - var err error - - if ttlString == "" { - ttl = 1 - } else { - ttl, err = strconv.Atoi(ttlString) - if err != nil { - ttl = 1 - } - } - - ttl-- - if ttl < 0 { - fmt.Fprintf(w, "Hello") - return - } - - nextURL := fmt.Sprintf("http://localhost:8080/query?ttl=%d", ttl) - req, err := http.NewRequest(http.MethodGet, nextURL, nil) - if err != nil { - w.WriteHeader(500) - return - } - - req = req.WithContext(r.Context()) - - resp, err := client.Do(req) - if err != nil { - w.WriteHeader(500) - return - } - defer resp.Body.Close() - - _, err = io.Copy(w, resp.Body) - if err != nil { - w.WriteHeader(500) - return - } - })), - // Use this route identifier with the tracing middleware - tracing.WithRouteIdentifier("/query"), - ), - correlation.WithPropagation())) - - log.Fatal(http.ListenAndServe(":8080", nil)) + handle("/query", + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + query := r.URL.Query() + ttlString := query.Get("ttl") + var ttl int + var err error + + if ttlString == "" { + ttl = 1 + } else { + ttl, err = strconv.Atoi(ttlString) + if err != nil { + ttl = 1 + } + } + + ttl-- + if ttl < 0 { + fmt.Fprintf(w, "Hello") + return + } + + nextURL := fmt.Sprintf("http://localhost:8080/query?ttl=%d", ttl) + req, err := http.NewRequest(http.MethodGet, nextURL, nil) + if err != nil { + w.WriteHeader(500) + return + } + + req = req.WithContext(r.Context()) + + resp, err := client.Do(req) + if err != nil { + w.WriteHeader(500) + return + } + defer resp.Body.Close() + + _, err = io.Copy(w, resp.Body) + if err != nil { + w.WriteHeader(500) + return + } + })) + + // Example of testing what happens during a panic + handle("/panic", http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { + panic("on request") + })) + + logkit.WithError(http.ListenAndServe(":8080", nil)).Fatal("failed to start") } diff --git a/example/run-datadog-static b/example/run-datadog-static index 8cacb2e068963d9118dc047411cbca69def5c00c..ba031a0cd3959e27bc780bb1b201aa990c5d0910 100755 --- a/example/run-datadog-static +++ b/example/run-datadog-static @@ -3,7 +3,7 @@ set -xeuo pipefail IFS=$'\n\t' -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "${SCRIPT_DIR}" docker run -d --name dd-agent \ @@ -17,18 +17,14 @@ docker run -d --name dd-agent \ -p 8126:8126 \ datadog/agent:latest -function finish { +function finish() { docker rm -f dd-agent || true } trap finish EXIT -export GO111MODULE=off - go build \ -tags "tracer_static tracer_static_datadog" \ router.go GITLAB_TRACING=opentracing://datadog \ - ./router - - + ./router diff --git a/example/run-jaeger-static b/example/run-jaeger-static index 495448582e02c6d5a2539a9f51392b129dcd1fb4..368806e5ff3e820b78e5d38683d7f964982b24aa 100755 --- a/example/run-jaeger-static +++ b/example/run-jaeger-static @@ -3,7 +3,7 @@ set -xeuo pipefail IFS=$'\n\t' -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "${SCRIPT_DIR}" docker run -d --name labkit_jaeger \ @@ -17,17 +17,14 @@ docker run -d --name labkit_jaeger \ -p 9411:9411 \ jaegertracing/all-in-one:latest -function finish { +function finish() { docker rm -f labkit_jaeger || true } trap finish EXIT -export GO111MODULE=off - go build \ -tags "tracer_static tracer_static_jaeger" \ router.go GITLAB_TRACING="opentracing://jaeger?sampler=const&sampler_param=1" \ - ./router - + ./router diff --git a/example/run-no-tracing b/example/run-no-tracing index 103053f544816bb521ec4436034fbdb2ce48bd33..4d4848f632c09d8889ca57e23f31371b9e42173e 100755 --- a/example/run-no-tracing +++ b/example/run-no-tracing @@ -3,12 +3,9 @@ set -xeuo pipefail IFS=$'\n\t' -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "${SCRIPT_DIR}" -export GO111MODULE=off - go build router.go ./router -