示例#1
0
文件: main.go 项目: bg451/appdash
func main() {
	// Create a recent in-memory store, evicting data after 20s.
	//
	// The store defines where information about traces (i.e. spans and
	// annotations) will be stored during the lifetime of the application. This
	// application uses a MemoryStore store wrapped by a RecentStore with an
	// eviction time of 20s (i.e. all data after 20s is deleted from memory).
	memStore := appdash.NewMemoryStore()
	store := &appdash.RecentStore{
		MinEvictAge: 20 * time.Second,
		DeleteStore: memStore,
	}

	// Start the Appdash web UI on port 8700.
	//
	// This is the actual Appdash web UI -- usable as a Go package itself, We
	// embed it directly into our application such that visiting the web server
	// on HTTP port 8700 will bring us to the web UI, displaying information
	// about this specific web-server (another alternative would be to connect
	// to a centralized Appdash collection server).
	url, err := url.Parse("http://localhost:8700")
	if err != nil {
		log.Fatal(err)
	}
	tapp, err := traceapp.New(nil, url)
	if err != nil {
		log.Fatal(err)
	}
	tapp.Store = store
	tapp.Queryer = memStore
	log.Println("Appdash web UI running on HTTP :8700")
	go func() {
		log.Fatal(http.ListenAndServe(":8700", tapp))
	}()

	// We will use a local collector (as we are running the Appdash web UI
	// embedded within our app).
	//
	// A collector is responsible for collecting the information about traces
	// (i.e. spans and annotations) and placing them into a store. In this app
	// we use a local collector (we could also use a remote collector, sending
	// the information to a remote Appdash collection server).
	collector := appdash.NewLocalCollector(store)

	// Here we use the local collector to create a new opentracing.Tracer
	tracer := appdashtracer.NewTracer(collector)
	opentracing.InitGlobalTracer(tracer)

	// Setup our router (for information, see the gorilla/mux docs):
	router := mux.NewRouter()
	router.HandleFunc("/", Home)
	router.HandleFunc("/endpoint", Endpoint)

	// Setup Negroni for our app (for information, see the negroni docs):
	n := negroni.Classic()
	n.UseHandler(router)
	n.Run(":8699")
}
示例#2
0
func TestTracer(t *testing.T) {
	log.SetFlags(0)

	var tracer ot.Tracer
	tracer = &Tracer{}
	ot.InitGlobalTracer(tracer)

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		serverSpan, err := tracer.Join(
			"serverSpan",
			ot.TextMap,
			ot.HTTPHeaderTextMapCarrier(r.Header))
		if err != nil {
			panic(err)
		}
		time.Sleep(time.Second * 1)
		serverSpan.Finish()
		fmt.Fprintln(w, "Hello, client")
	}))
	defer ts.Close()

	span, nctx := ot.StartSpanFromContext(context.TODO(), "main_test")
	defer span.Finish()

	foo(nctx, "bar", 0)

	httpClient := &http.Client{}
	httpReq, _ := http.NewRequest("GET", ts.URL, nil)

	// Transmit the span's TraceContext as HTTP headers on our
	// outbound request.
	tracer.Inject(
		span,
		ot.TextMap,
		ot.HTTPHeaderTextMapCarrier(httpReq.Header))

	if _, err := httpClient.Do(httpReq); err != nil {
		panic(err)
	}
}