Skip to content

Commit 32bba44

Browse files
committed
1 parent 95d7b47 commit 32bba44

File tree

3 files changed

+1173
-0
lines changed

3 files changed

+1173
-0
lines changed

examples/cmd/webapp-influxdb/main.go

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"time"
8+
9+
"sourcegraph.com/sourcegraph/appdash"
10+
"sourcegraph.com/sourcegraph/appdash/httptrace"
11+
"sourcegraph.com/sourcegraph/appdash/traceapp"
12+
13+
"github.com/codegangsta/negroni"
14+
"github.com/gorilla/context"
15+
"github.com/gorilla/mux"
16+
17+
influxDBServer "github.com/influxdata/influxdb/cmd/influxd/run"
18+
"github.com/influxdata/influxdb/toml"
19+
)
20+
21+
const CtxSpanID = 0
22+
23+
var collector appdash.Collector
24+
25+
func main() {
26+
conf, err := influxDBServer.NewDemoConfig()
27+
if err != nil {
28+
log.Fatalf("failed to create influxdb config, error: %v", err)
29+
}
30+
31+
// Enables InfluxDB server authentication.
32+
conf.HTTPD.AuthEnabled = true
33+
34+
// Enables retention policies which will be executed within an interval of 30 minutes.
35+
conf.Retention.Enabled = true
36+
conf.Retention.CheckInterval = toml.Duration(30 * time.Minute)
37+
38+
// Disables sending anonymous data to m.influxdb.com
39+
// See: https://docs.influxdata.com/influxdb/v0.10/administration/config/#reporting-disabled-false
40+
conf.ReportingDisabled = true
41+
42+
// InfluxDB server auth credentials. If user does not exist yet it will
43+
// be created as admin user.
44+
user := appdash.InfluxDBAdminUser{Username: "demo", Password: "demo"}
45+
46+
// Retention policy named "one_day_only" with a duration of "1d" - meaning db data older than "1d" will be deleted
47+
// with an interval checking set by `conf.Retention.CheckInterval`.
48+
// Minimum duration time is 1 hour ("1h") - See: github.com/influxdata/influxdb/issues/5198
49+
defaultRP := appdash.InfluxDBRetentionPolicy{Name: "one_day_only", Duration: "1d"}
50+
51+
store, err := appdash.NewInfluxDBStore(appdash.InfluxDBStoreConfig{
52+
AdminUser: user,
53+
BuildInfo: &influxDBServer.BuildInfo{},
54+
DefaultRP: defaultRP,
55+
Server: conf,
56+
})
57+
if err != nil {
58+
log.Fatalf("failed to create influxdb store, error: %v", err)
59+
}
60+
defer func() {
61+
if err := store.Close(); err != nil {
62+
log.Fatal(err)
63+
}
64+
}()
65+
tapp := traceapp.New(nil)
66+
tapp.Store = store
67+
tapp.Queryer = store
68+
log.Println("Appdash web UI running on HTTP :8700")
69+
go func() {
70+
log.Fatal(http.ListenAndServe(":8700", tapp))
71+
}()
72+
collector = appdash.NewLocalCollector(store)
73+
tracemw := httptrace.Middleware(collector, &httptrace.MiddlewareConfig{
74+
RouteName: func(r *http.Request) string { return r.URL.Path },
75+
SetContextSpan: func(r *http.Request, spanID appdash.SpanID) {
76+
context.Set(r, CtxSpanID, spanID)
77+
},
78+
})
79+
router := mux.NewRouter()
80+
router.HandleFunc("/", Home)
81+
router.HandleFunc("/endpoint", Endpoint)
82+
n := negroni.Classic()
83+
n.Use(negroni.HandlerFunc(tracemw))
84+
n.UseHandler(router)
85+
n.Run(":8699")
86+
}
87+
88+
func Home(w http.ResponseWriter, r *http.Request) {
89+
span := context.Get(r, CtxSpanID).(appdash.SpanID)
90+
httpClient := &http.Client{
91+
Transport: &httptrace.Transport{
92+
Recorder: appdash.NewRecorder(span, collector),
93+
SetName: true,
94+
},
95+
}
96+
for i := 0; i < 3; i++ {
97+
resp, err := httpClient.Get("http://localhost:8699/endpoint")
98+
if err != nil {
99+
log.Println("/endpoint:", err)
100+
continue
101+
}
102+
resp.Body.Close()
103+
}
104+
fmt.Fprintf(w, `<p>Three API requests have been made!</p>`)
105+
fmt.Fprintf(w, `<p><a href="http://localhost:8700/traces/%s" target="_">View the trace (ID:%s)</a></p>`, span.Trace, span.Trace)
106+
}
107+
108+
func Endpoint(w http.ResponseWriter, r *http.Request) {
109+
time.Sleep(200 * time.Millisecond)
110+
fmt.Fprintf(w, "Slept for 200ms!")
111+
}

0 commit comments

Comments
 (0)