Skip to content

Commit

Permalink
Add an example of how to use Walk to add the Middleware to every hand…
Browse files Browse the repository at this point in the history
…ler (#5)

Signed-off-by: Louis-Etienne Dorval <[email protected]>
  • Loading branch information
ledor473 authored and yurishkuro committed Jan 10, 2019
1 parent 3ee496a commit ced6667
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 23 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# go-gorilla
OpenTracing instrumentation for Gorilla framework (github.com/gorilla)

## Usage

See usage examples in [gorilla/example_test.go](gorilla/example_test.go).

87 changes: 64 additions & 23 deletions gorilla/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,86 @@ package gorilla_test

import (
"fmt"
"github.com/opentracing/opentracing-go"
"io"
"log"
"net/http"
"time"

"github.com/gorilla/mux"
"github.com/opentracing-contrib/go-gorilla/gorilla"
//"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/uber/jaeger-client-go/config"
)

func ExampleTracingMiddleware() {
// http listen port
httpAddress := ":8800"
// http listen port
const httpAddress = ":8800"

func ExampleGorilla_TracingSingleRoute() {
tracer, _, err := getTracer()

if err != nil {
log.Fatal("cannot initialize Jaeger Tracer", err)
}

//jaeger agent port
jaegerHostPort := ":6831"
myHandler := func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
idstr := vars["productId"]
//do something
data := "Hello, we get " + idstr
fmt.Fprintf(w, data)
}

r := mux.NewRouter()

pattern := "/v1/products/{productId}"

middleware := gorilla.Middleware(
tracer,
http.HandlerFunc(myHandler),
)

r.Handle(pattern, middleware)
log.Fatal(http.ListenAndServe(httpAddress, r))
}

func ExampleGorilla_TracingAllRoutes() {
tracer, _, err := getTracer()

if err != nil {
log.Fatal("cannot initialize Jaeger Tracer", err)
}

okHandler := func(w http.ResponseWriter, r *http.Request) {
// do something
data := "Hello"
fmt.Fprintf(w, data)
}

r := mux.NewRouter()
// Create multiples routes
r.HandleFunc("/v1/products", okHandler)
r.HandleFunc("/v1/products/{productId}", okHandler)
r.HandleFunc("/v2/products", okHandler)
r.HandleFunc("/v2/products/{productId}", okHandler)
r.HandleFunc("/v3/products", okHandler)
r.HandleFunc("/v3/products/{productId}", okHandler)
r.HandleFunc("/v4/products", okHandler)
r.HandleFunc("/v4/products/{productId}", okHandler)

// Add tracing to all routes
_ = r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
route.Handler(
gorilla.Middleware(tracer, route.GetHandler()))
return nil
})

log.Fatal(http.ListenAndServe(httpAddress, r))
}

func getTracer() (opentracing.Tracer, io.Closer, error) {
//jaeger agent port
jaegerHostPort := ":6831"

cfg := config.Configuration{
Sampler: &config.SamplerConfig{
Type: "const",
Expand All @@ -36,23 +93,7 @@ func ExampleTracingMiddleware() {
LocalAgentHostPort: jaegerHostPort,
},
}
tracer, _, err := cfg.New(
return cfg.New(
"ExampleTracingMiddleware", //service name
)

if err != nil {
log.Fatal("cannot initialize Jaeger Tracer", err)
}

r := mux.NewRouter()

pattern := "/v1/products/{productId}"

middleware := gorilla.Middleware(
tracer,
http.HandlerFunc(myHandler),
)

r.Handle(pattern, middleware)
log.Fatal(http.ListenAndServe(httpAddress, r))
}

0 comments on commit ced6667

Please sign in to comment.