mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
opentracing -> opencensus (#802)
* update vendor directory, add go.opencensus.io * update imports * oops * s/opentracing/opencensus/ & remove prometheus / zipkin stuff & remove old stats * the dep train rides again * fix gin build * deps from last guy * start in on the agent metrics * she builds * remove tags for now, cardinality error is fussing. subscribe instead of register * update to patched version of opencensus to proceed for now TODO switch to a release * meh fix imports * println debug the bad boys * lace it with the tags * update deps again * fix all inconsistent cardinality errors * add our own logger * fix init * fix oom measure * remove bugged removal code * fix s3 measures * fix prom handler nil
This commit is contained in:
109
vendor/github.com/openzipkin/zipkin-go/example_httpserver_test.go
generated
vendored
Normal file
109
vendor/github.com/openzipkin/zipkin-go/example_httpserver_test.go
generated
vendored
Normal file
@@ -0,0 +1,109 @@
|
||||
package zipkin_test
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
zipkin "github.com/openzipkin/zipkin-go"
|
||||
zipkinhttp "github.com/openzipkin/zipkin-go/middleware/http"
|
||||
logreporter "github.com/openzipkin/zipkin-go/reporter/log"
|
||||
)
|
||||
|
||||
func Example() {
|
||||
// set up a span reporter
|
||||
reporter := logreporter.NewReporter(log.New(os.Stderr, "", log.LstdFlags))
|
||||
defer reporter.Close()
|
||||
|
||||
// create our local service endpoint
|
||||
endpoint, err := zipkin.NewEndpoint("myService", "localhost:0")
|
||||
if err != nil {
|
||||
log.Fatalf("unable to create local endpoint: %+v\n", err)
|
||||
}
|
||||
|
||||
// initialize our tracer
|
||||
tracer, err := zipkin.NewTracer(reporter, zipkin.WithLocalEndpoint(endpoint))
|
||||
if err != nil {
|
||||
log.Fatalf("unable to create tracer: %+v\n", err)
|
||||
}
|
||||
|
||||
// create global zipkin http server middleware
|
||||
serverMiddleware := zipkinhttp.NewServerMiddleware(
|
||||
tracer, zipkinhttp.TagResponseSize(true),
|
||||
)
|
||||
|
||||
// create global zipkin traced http client
|
||||
client, err := zipkinhttp.NewClient(tracer, zipkinhttp.ClientTrace(true))
|
||||
if err != nil {
|
||||
log.Fatalf("unable to create client: %+v\n", err)
|
||||
}
|
||||
|
||||
// initialize router
|
||||
router := mux.NewRouter()
|
||||
|
||||
// start web service with zipkin http server middleware
|
||||
ts := httptest.NewServer(serverMiddleware(router))
|
||||
defer ts.Close()
|
||||
|
||||
// set-up handlers
|
||||
router.Methods("GET").Path("/some_function").HandlerFunc(someFunc(client, ts.URL))
|
||||
router.Methods("POST").Path("/other_function").HandlerFunc(otherFunc(client))
|
||||
|
||||
// initiate a call to some_func
|
||||
req, err := http.NewRequest("GET", ts.URL+"/some_function", nil)
|
||||
if err != nil {
|
||||
log.Fatalf("unable to create http request: %+v\n", err)
|
||||
}
|
||||
|
||||
res, err := client.DoWithAppSpan(req, "some_function")
|
||||
if err != nil {
|
||||
log.Fatalf("unable to do http request: %+v\n", err)
|
||||
}
|
||||
res.Body.Close()
|
||||
|
||||
// Output:
|
||||
}
|
||||
|
||||
func someFunc(client *zipkinhttp.Client, url string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("some_function called with method: %s\n", r.Method)
|
||||
|
||||
// retrieve span from context (created by server middleware)
|
||||
span := zipkin.SpanFromContext(r.Context())
|
||||
span.Tag("custom_key", "some value")
|
||||
|
||||
// doing some expensive calculations....
|
||||
time.Sleep(25 * time.Millisecond)
|
||||
span.Annotate(time.Now(), "expensive_calc_done")
|
||||
|
||||
newRequest, err := http.NewRequest("POST", url+"/other_function", nil)
|
||||
if err != nil {
|
||||
log.Printf("unable to create client: %+v\n", err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := zipkin.NewContext(newRequest.Context(), span)
|
||||
|
||||
newRequest = newRequest.WithContext(ctx)
|
||||
|
||||
res, err := client.DoWithAppSpan(newRequest, "other_function")
|
||||
if err != nil {
|
||||
log.Printf("call to other_function returned error: %+v\n", err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
res.Body.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func otherFunc(client *zipkinhttp.Client) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("other_function called with method: %s\n", r.Method)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user