├── .gitignore ├── README.md ├── postprocessor.go ├── preprocessor.go ├── Gopkg.toml ├── misc.go ├── repository.go ├── metrics.go ├── breakfasts.json ├── tracing.go ├── api.go ├── logging.go ├── Gopkg.lock ├── main.go └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Breakfast Solutions 2 | 3 | A dumb example service for a workshop I did on observability. 4 | 5 | [Here is a video](https://vimeo.com/267641392) of the workshop. 6 | -------------------------------------------------------------------------------- /postprocessor.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "math/rand" 6 | "time" 7 | ) 8 | 9 | type postprocessor func(ctx context.Context, username string, success bool) context.Context 10 | 11 | func basicPostprocess(ctx context.Context, username string, success bool) context.Context { 12 | d := 1 13 | if !success { 14 | d *= 50 15 | } 16 | time.Sleep(time.Duration(d+rand.Intn(d)) * time.Millisecond) 17 | return ctx 18 | } 19 | -------------------------------------------------------------------------------- /preprocessor.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "strings" 6 | "time" 7 | ) 8 | 9 | type preprocessor func(ctx context.Context, originIP string) context.Context 10 | 11 | func geoPreprocess(ctx context.Context, region string) context.Context { 12 | var delay time.Duration 13 | switch strings.ToLower(region) { 14 | case "au": 15 | delay = 100 * time.Millisecond 16 | default: 17 | delay = 1 * time.Millisecond 18 | } 19 | time.Sleep(delay) 20 | return ctx 21 | } 22 | -------------------------------------------------------------------------------- /Gopkg.toml: -------------------------------------------------------------------------------- 1 | 2 | [[constraint]] 3 | name = "github.com/go-kit/kit" 4 | version = "0.7.0" 5 | 6 | [[constraint]] 7 | name = "github.com/gorilla/mux" 8 | version = "1.6.1" 9 | 10 | [[constraint]] 11 | name = "github.com/oklog/run" 12 | version = "1.0.0" 13 | 14 | [[constraint]] 15 | name = "github.com/opentracing/opentracing-go" 16 | version = "1.0.2" 17 | 18 | [[constraint]] 19 | branch = "master" 20 | name = "github.com/prometheus/client_golang" 21 | 22 | [[constraint]] 23 | name = "github.com/uber/jaeger-client-go" 24 | version = "2.12.0" 25 | 26 | [[constraint]] 27 | name = "github.com/uber/jaeger-lib" 28 | version = "1.4.0" 29 | 30 | [prune] 31 | go-tests = true 32 | unused-packages = true 33 | -------------------------------------------------------------------------------- /misc.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "strings" 7 | 8 | "github.com/go-kit/kit/log" 9 | "github.com/go-kit/kit/log/level" 10 | ) 11 | 12 | func hstsAPIMiddleware(next http.Handler) http.Handler { 13 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 14 | w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains") 15 | next.ServeHTTP(w, r) 16 | }) 17 | } 18 | 19 | type interceptingWriter struct { 20 | count int 21 | code int 22 | http.ResponseWriter 23 | } 24 | 25 | func (iw *interceptingWriter) WriteHeader(code int) { 26 | iw.code = code 27 | iw.ResponseWriter.WriteHeader(code) 28 | } 29 | 30 | func (iw *interceptingWriter) Write(p []byte) (int, error) { 31 | iw.count += len(p) 32 | return iw.ResponseWriter.Write(p) 33 | } 34 | 35 | func normalize(path string) string { 36 | switch { 37 | case path == "" || path == "/": 38 | return "/" 39 | default: 40 | return "/" + strings.FieldsFunc(path, func(r rune) bool { return r == '/' })[0] 41 | } 42 | } 43 | 44 | type logAdapter struct{ log.Logger } 45 | 46 | func (a logAdapter) Error(msg string) { 47 | level.Error(a.Logger).Log("component", "Jaeger", "msg", msg) 48 | } 49 | 50 | func (a logAdapter) Infof(msg string, args ...interface{}) { 51 | level.Info(a.Logger).Log("component", "Jaeger", "msg", fmt.Sprintf(msg, args...)) 52 | } 53 | -------------------------------------------------------------------------------- /repository.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "encoding/json" 6 | "errors" 7 | "fmt" 8 | "io/ioutil" 9 | "math/rand" 10 | "strings" 11 | "time" 12 | ) 13 | 14 | type repository interface { 15 | getBreakfast(ctx context.Context, username string, breakfastID uint64) (breakfast, error) 16 | getRandomBreakfast(ctx context.Context, username string) (breakfast, error) 17 | } 18 | 19 | type breakfast struct { 20 | ID uint64 `json:"id"` 21 | Name string `json:"name"` 22 | Image string `json:"image"` 23 | Description string `json:"description"` 24 | } 25 | 26 | type breakfasts []breakfast 27 | 28 | func newRepository(filename string) (a breakfasts, err error) { 29 | buf, err := ioutil.ReadFile(filename) 30 | if err != nil { 31 | return nil, err 32 | } 33 | return a, json.Unmarshal(buf, &a) 34 | } 35 | 36 | func mustNewRepository(filename string) breakfasts { 37 | a, err := newRepository(filename) 38 | if err != nil { 39 | panic(err) 40 | } 41 | return a 42 | } 43 | 44 | func (a breakfasts) getBreakfast(_ context.Context, username string, breakfastID uint64) (breakfast, error) { 45 | fakeDatabaseOperation(username) 46 | for _, b := range a { 47 | if b.ID == breakfastID { 48 | return b, nil 49 | } 50 | } 51 | return breakfast{}, fmt.Errorf("no breakfast with ID %d", breakfastID) 52 | } 53 | 54 | func (a breakfasts) getRandomBreakfast(_ context.Context, username string) (breakfast, error) { 55 | fakeDatabaseOperation(username) 56 | if len(a) <= 0 { 57 | return breakfast{}, errors.New("no breakfasts available") 58 | } 59 | return a[rand.Intn(len(a))], nil 60 | } 61 | 62 | func fakeDatabaseOperation(username string) { 63 | var shardDelay time.Duration 64 | { 65 | var min, max int 66 | switch strings.ToLower(username)[0] { 67 | case 'a', 'b', 'c', 'd', 'e': 68 | min, max = 15, 35 69 | case 'f', 'g', 'h', 'i', 'j': 70 | min, max = 20, 40 71 | case 'k', 'l', 'm', 'n', 'o': 72 | min, max = 150, 300 73 | case 'p', 'q', 'r', 's', 't': 74 | min, max = 60, 80 75 | case 'u', 'v', 'w', 'x', 'y': 76 | min, max = 10, 30 77 | default: 78 | min, max = 10, 20 79 | } 80 | shardDelay = time.Duration(rand.Intn(max-min)+min) * time.Millisecond 81 | } 82 | var clockDelay time.Duration 83 | { 84 | if time.Now().Format("04") == "00" { 85 | clockDelay = 300 * time.Millisecond 86 | } 87 | } 88 | time.Sleep(shardDelay + clockDelay) 89 | } 90 | -------------------------------------------------------------------------------- /metrics.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "net/http" 7 | "time" 8 | 9 | "github.com/prometheus/client_golang/prometheus" 10 | ) 11 | 12 | func metricsAPIMiddleware(next http.Handler, duration *prometheus.HistogramVec) http.Handler { 13 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 14 | var ( 15 | iw = &interceptingWriter{0, http.StatusOK, w} 16 | ctx = context.WithValue(r.Context(), contextHistogramKey{}, duration) 17 | ) 18 | defer func(begin time.Time) { 19 | duration.WithLabelValues( 20 | "API", normalize(r.URL.Path), fmt.Sprint(iw.code == 200), 21 | ).Observe(time.Since(begin).Seconds()) 22 | }(time.Now()) 23 | next.ServeHTTP(iw, r.WithContext(ctx)) 24 | }) 25 | } 26 | 27 | func metricsPreprocessMiddleware(next preprocessor) preprocessor { 28 | return func(ctx context.Context, region string) context.Context { 29 | defer func(begin time.Time) { 30 | getContextHistogram(ctx).WithLabelValues( 31 | "preprocessor", "preprocess", "true", 32 | ).Observe(time.Since(begin).Seconds()) 33 | }(time.Now()) 34 | return next(ctx, region) 35 | } 36 | } 37 | 38 | type metricsRepoMiddleware struct { 39 | next repository 40 | } 41 | 42 | func (m metricsRepoMiddleware) getBreakfast(ctx context.Context, username string, breakfastID uint64) (b breakfast, err error) { 43 | defer func(begin time.Time) { 44 | getContextHistogram(ctx).WithLabelValues( 45 | "DB", "getBreakfast", fmt.Sprint(err == nil), 46 | ).Observe(time.Since(begin).Seconds()) 47 | }(time.Now()) 48 | return m.next.getBreakfast(ctx, username, breakfastID) 49 | } 50 | 51 | func (m metricsRepoMiddleware) getRandomBreakfast(ctx context.Context, username string) (b breakfast, err error) { 52 | defer func(begin time.Time) { 53 | getContextHistogram(ctx).WithLabelValues( 54 | "DB", "getRandomBreakfast", fmt.Sprint(err == nil), 55 | ).Observe(time.Since(begin).Seconds()) 56 | }(time.Now()) 57 | return m.next.getRandomBreakfast(ctx, username) 58 | } 59 | 60 | func metricsPostprocessMiddleware(next postprocessor) postprocessor { 61 | return func(ctx context.Context, username string, success bool) context.Context { 62 | defer func(begin time.Time) { 63 | getContextHistogram(ctx).WithLabelValues( 64 | "postprocessor", "postprocess", fmt.Sprint(success), 65 | ).Observe(time.Since(begin).Seconds()) 66 | }(time.Now()) 67 | return next(ctx, username, success) 68 | } 69 | } 70 | 71 | // 72 | // 73 | // 74 | 75 | type contextHistogramKey struct{} 76 | 77 | func getContextHistogram(ctx context.Context) *prometheus.HistogramVec { 78 | histogram, ok := ctx.Value(contextHistogramKey{}).(*prometheus.HistogramVec) 79 | if !ok { 80 | panic("no context histogram") 81 | } 82 | return histogram 83 | } 84 | -------------------------------------------------------------------------------- /breakfasts.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": 5424, 4 | "name": "French toast", 5 | "description": "And eggs and sausages! This is looking pretty good.", 6 | "image": "/images/11855393546_7eaa945ba7_o.jpg" 7 | }, 8 | { 9 | "id": 8048, 10 | "name": "Juice and coffee and some kind of cheesy thing", 11 | "description": "Look how nicely it's all arranged. Practically Instagram-worthy.", 12 | "image": "/images/14419029768_abd13147bc_o.jpg" 13 | }, 14 | { 15 | "id": 7568, 16 | "name": "Cereal and coffee", 17 | "description": "On second thought, are you sure that's not cola?", 18 | "image": "/images/14629779054_b7596fe590_o.jpg" 19 | }, 20 | { 21 | "id": 9415, 22 | "name": "American Breakfast", 23 | "description": "I can feel my arteries clogging from over here.", 24 | "image": "/images/38749642194_1de4dbca83_o.jpg" 25 | }, 26 | { 27 | "id": 8283, 28 | "name": "Strawberries and pancakes", 29 | "description": "Go ahead, you deserve it. Maybe. Probably.", 30 | "image": "/images/5011557502_b0fccc3f25_o.jpg" 31 | }, 32 | { 33 | "id": 6612, 34 | "name": "Bread, bread, and OJ", 35 | "description": "What is this, France?", 36 | "image": "/images/5868700397_86079c3e81_o.jpg" 37 | }, 38 | { 39 | "id": 7094, 40 | "name": "Ham and an egg and stuff", 41 | "description": "There's some cheese, too, and a little plastic pitcher. I guess it's alright.", 42 | "image": "/images/6476002779_bcd8cd3e3e_o.jpg" 43 | }, 44 | { 45 | "id": 4416, 46 | "name": "Beans!!!", 47 | "description": "Beans and eggs, beans and eggs, and some blueberries too?", 48 | "image": "/images/7478700064_62511fbcac_o.jpg" 49 | }, 50 | { 51 | "id": 9110, 52 | "name": "Eggs and peppers", 53 | "description": "It looks like someone put some fancy paprikas on the eggs, too.", 54 | "image": "/images/8100785650_a1cfc0fcf2_o.jpg" 55 | }, 56 | { 57 | "id": 4876, 58 | "name": "Fruit and bread", 59 | "description": "There's some cucumber slices, and half of an egg.", 60 | "image": "/images/8189091693_d824d68a12_o.jpg" 61 | }, 62 | { 63 | "id": 4071, 64 | "name": "Bagels and some yogurt", 65 | "description": "Continental breakfast at a bad hotel.", 66 | "image": "/images/826820181_36f4f46a0b_o.jpg" 67 | }, 68 | { 69 | "id": 6473, 70 | "name": "Eggs on rice", 71 | "description": "I think there's some katsu sauce on it, too? Yum.", 72 | "image": "/images/8627912241_9ed2d84477_o.jpg" 73 | } 74 | ] -------------------------------------------------------------------------------- /tracing.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "net/http" 6 | "time" 7 | 8 | opentracing "github.com/opentracing/opentracing-go" 9 | ) 10 | 11 | func tracingAPIMiddleware(next http.Handler) http.Handler { 12 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 13 | iw := &interceptingWriter{0, http.StatusOK, w} 14 | span, ctx := opentracing.StartSpanFromContext(r.Context(), "api_request") 15 | defer span.Finish() 16 | defer func(begin time.Time) { 17 | span.LogKV( 18 | "remote_addr", r.RemoteAddr, 19 | "method", r.Method, 20 | "url", r.URL, 21 | "content_length", r.ContentLength, 22 | "status_code", iw.code, 23 | "status_text", http.StatusText(iw.code), 24 | "response_size", iw.count, 25 | "took", time.Since(begin).String(), 26 | "sec", time.Since(begin).Seconds(), 27 | ) 28 | }(time.Now()) 29 | next.ServeHTTP(iw, r.WithContext(ctx)) 30 | }) 31 | } 32 | 33 | func tracingPreprocessMiddleware(next preprocessor) preprocessor { 34 | return func(ctx context.Context, region string) context.Context { 35 | span, ctx := opentracing.StartSpanFromContext(ctx, "preprocess") 36 | defer span.Finish() 37 | defer func(begin time.Time) { 38 | span.LogKV( 39 | "region", region, 40 | "took", time.Since(begin).String(), 41 | "sec", time.Since(begin).Seconds(), 42 | ) 43 | }(time.Now()) 44 | return next(ctx, region) 45 | } 46 | } 47 | 48 | type tracingRepoMiddleware struct { 49 | next repository 50 | } 51 | 52 | func (m tracingRepoMiddleware) getBreakfast(ctx context.Context, username string, breakfastID uint64) (b breakfast, err error) { 53 | span, ctx := opentracing.StartSpanFromContext(ctx, "db_request") 54 | defer span.Finish() 55 | defer func(begin time.Time) { 56 | span.LogKV( 57 | "method", "getBreakfast", 58 | "username", username, 59 | "breakfast_id", breakfastID, 60 | "took", time.Since(begin).String(), 61 | "sec", time.Since(begin).Seconds(), 62 | "success", err == nil, 63 | "returned_breakfast_id", b.ID, 64 | "err", err, 65 | ) 66 | }(time.Now()) 67 | return m.next.getBreakfast(ctx, username, breakfastID) 68 | } 69 | 70 | func (m tracingRepoMiddleware) getRandomBreakfast(ctx context.Context, username string) (b breakfast, err error) { 71 | span, ctx := opentracing.StartSpanFromContext(ctx, "db_request") 72 | defer span.Finish() 73 | defer func(begin time.Time) { 74 | span.LogKV( 75 | "method", "getRandomBreakfast", 76 | "username", username, 77 | "took", time.Since(begin).String(), 78 | "sec", time.Since(begin).Seconds(), 79 | "success", err == nil, 80 | "returned_breakfast_id", b.ID, 81 | "err", err, 82 | ) 83 | }(time.Now()) 84 | return m.next.getRandomBreakfast(ctx, username) 85 | } 86 | 87 | func tracingPostprocessMiddleware(next postprocessor) postprocessor { 88 | return func(ctx context.Context, username string, success bool) context.Context { 89 | span, ctx := opentracing.StartSpanFromContext(ctx, "postprocess") 90 | defer span.Finish() 91 | defer func(begin time.Time) { 92 | span.LogKV( 93 | "username", username, 94 | "success", success, 95 | "took", time.Since(begin).String(), 96 | "sec", time.Since(begin).Seconds(), 97 | ) 98 | }(time.Now()) 99 | return next(ctx, username, success) 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /api.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "net/http" 7 | "strconv" 8 | 9 | "github.com/gorilla/mux" 10 | ) 11 | 12 | type api struct { 13 | pre preprocessor 14 | repo repository 15 | post postprocessor 16 | *mux.Router 17 | } 18 | 19 | func newAPI(pre preprocessor, repo repository, post postprocessor, imagedir string) *api { 20 | a := &api{ 21 | pre: pre, 22 | repo: repo, 23 | post: post, 24 | } 25 | r := mux.NewRouter() 26 | { 27 | r.StrictSlash(true) 28 | r.Methods("GET").Path("/").HandlerFunc(a.handleRoot) 29 | r.Methods("GET").Path("/breakfasts/{id:[0-9]+}").HandlerFunc(a.handleGetBreakfast) 30 | r.Methods("GET").PathPrefix("/images").Handler(http.StripPrefix("/images", http.FileServer(http.Dir(imagedir)))) 31 | r.Methods("GET").Path("/admin").HandlerFunc(a.handleAdmin) 32 | } 33 | a.Router = r 34 | return a 35 | } 36 | 37 | func (a *api) handleRoot(w http.ResponseWriter, r *http.Request) { 38 | var ( 39 | username = getUsername(r) 40 | region = getRegion(r) 41 | ) 42 | 43 | a.pre(r.Context(), region) 44 | 45 | b, err := a.repo.getRandomBreakfast(r.Context(), username) 46 | 47 | a.post(r.Context(), username, err == nil) 48 | 49 | if err != nil { 50 | http.Error(w, err.Error(), http.StatusServiceUnavailable) 51 | return 52 | } 53 | 54 | w.Header().Set("Cache-Control", "private") // don't cache, it's random! 55 | w.Header().Set("Content-Type", "text/html; charset=utf-8") 56 | writeHTML(w, b) 57 | } 58 | 59 | func (a *api) handleGetBreakfast(w http.ResponseWriter, r *http.Request) { 60 | var ( 61 | username = getUsername(r) 62 | region = getRegion(r) 63 | id, _ = strconv.ParseUint(mux.Vars(r)["id"], 10, 64) 64 | ) 65 | 66 | a.pre(r.Context(), region) 67 | 68 | b, err := a.repo.getBreakfast(r.Context(), username, id) 69 | 70 | a.post(r.Context(), username, err == nil) 71 | 72 | if err != nil { 73 | http.Error(w, err.Error(), http.StatusNotFound) 74 | return 75 | } 76 | 77 | w.Header().Set("Content-Type", "text/html; charset=utf-8") 78 | writeHTML(w, b) 79 | } 80 | 81 | func (a *api) handleAdmin(w http.ResponseWriter, r *http.Request) { 82 | code, _ := strconv.Atoi(r.URL.Query().Get("code")) 83 | if code == 0 { 84 | code = http.StatusUnauthorized 85 | } 86 | http.Error(w, fmt.Sprintf("admin returning %d", code), code) 87 | } 88 | 89 | func getUsername(r *http.Request) string { 90 | username := r.URL.Query().Get("username") 91 | if username == "" { 92 | username = "" 93 | } 94 | return username 95 | } 96 | 97 | func getRegion(r *http.Request) string { 98 | region := r.URL.Query().Get("region") 99 | if region == "" { 100 | region = "??" 101 | } 102 | return region 103 | } 104 | 105 | func writeHTML(w io.Writer, b breakfast) { 106 | fmt.Fprintf(w, "Breakfast Solutions\n") 107 | fmt.Fprintf(w, "\n") 108 | fmt.Fprintf(w, "

Breakfast Solutions

\n") 109 | fmt.Fprintf(w, `

%s

`+"\n", b.Name) 110 | fmt.Fprintf(w, "
\n") 111 | fmt.Fprintf(w, ``+"\n", b.Image) 112 | fmt.Fprintf(w, "
\n") 113 | fmt.Fprintf(w, "
\n") 114 | fmt.Fprintf(w, "%s\n", b.Description) 115 | fmt.Fprintf(w, "
\n") 116 | fmt.Fprintf(w, `Permalink`+"\n", b.ID) 117 | fmt.Fprintf(w, "\n") 118 | } 119 | -------------------------------------------------------------------------------- /logging.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "net/http" 7 | "time" 8 | 9 | "github.com/go-kit/kit/log" 10 | ) 11 | 12 | func loggingAPIMiddleware(next http.Handler, logger log.Logger) http.Handler { 13 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 14 | var ( 15 | iw = &interceptingWriter{0, http.StatusOK, w} 16 | cl = &contextLogger{} 17 | ctx = r.Context() 18 | ) 19 | { 20 | ctx = context.WithValue(ctx, contextLoggerKey{}, cl) 21 | } 22 | cl.add( 23 | "http_req_remoteaddr", r.RemoteAddr, 24 | "http_req_method", r.Method, 25 | "http_req_url", r.URL.String(), 26 | "http_req_contentlength", r.ContentLength, 27 | ) 28 | begin := time.Now() 29 | next.ServeHTTP(iw, r.WithContext(ctx)) 30 | cl.add( 31 | "http_resp_statuscode", iw.code, 32 | "http_resp_statustext", http.StatusText(iw.code), 33 | "http_resp_size", iw.count, 34 | "http_resp_took", time.Since(begin).String(), 35 | "http_resp_sec", time.Since(begin).Seconds(), 36 | ) 37 | logger.Log(cl.Keyvals...) 38 | }) 39 | } 40 | 41 | func loggingPreprocessMiddleware(next preprocessor) preprocessor { 42 | return func(ctx context.Context, region string) context.Context { 43 | defer func(begin time.Time) { 44 | getContextLogger(ctx).add( 45 | "preprocess_region", region, 46 | "preprocess_took", time.Since(begin).String(), 47 | "preprocess_sec", time.Since(begin).Seconds(), 48 | ) 49 | }(time.Now()) 50 | return next(ctx, region) 51 | } 52 | } 53 | 54 | type loggingRepoMiddleware struct { 55 | next repository 56 | } 57 | 58 | func (m loggingRepoMiddleware) getBreakfast(ctx context.Context, username string, breakfastID uint64) (b breakfast, err error) { 59 | defer func(begin time.Time) { 60 | getContextLogger(ctx).add( 61 | "db_method", "getBreakfast", 62 | "db_username", username, 63 | "db_breakfast_id", breakfastID, 64 | "db_took", time.Since(begin).String(), 65 | "db_sec", time.Since(begin).Seconds(), 66 | "db_success", err == nil, 67 | "db_returned_breakfast_id", b.ID, 68 | "db_err", err, 69 | ) 70 | }(time.Now()) 71 | return m.next.getBreakfast(ctx, username, breakfastID) 72 | } 73 | 74 | func (m loggingRepoMiddleware) getRandomBreakfast(ctx context.Context, username string) (b breakfast, err error) { 75 | defer func(begin time.Time) { 76 | getContextLogger(ctx).add( 77 | "db_method", "getRandomBreakfast", 78 | "db_username", username, 79 | "db_took", time.Since(begin).String(), 80 | "db_sec", time.Since(begin).Seconds(), 81 | "db_success", err == nil, 82 | "db_returned_breakfast_id", b.ID, 83 | "db_err", err, 84 | ) 85 | }(time.Now()) 86 | return m.next.getRandomBreakfast(ctx, username) 87 | } 88 | 89 | func loggingPostprocessMiddleware(next postprocessor) postprocessor { 90 | return func(ctx context.Context, username string, success bool) context.Context { 91 | defer func(begin time.Time) { 92 | getContextLogger(ctx).add( 93 | "postprocess_username", username, 94 | "postprocess_success", fmt.Sprint(success), 95 | "postprocess_took", time.Since(begin).String(), 96 | "postprocess_sec", time.Since(begin).Seconds(), 97 | ) 98 | }(time.Now()) 99 | return next(ctx, username, success) 100 | } 101 | } 102 | 103 | // 104 | // 105 | // 106 | 107 | type contextLoggerKey struct{} 108 | 109 | type contextLogger struct{ Keyvals []interface{} } 110 | 111 | func (l *contextLogger) add(keyvals ...interface{}) { l.Keyvals = append(l.Keyvals, keyvals...) } 112 | 113 | func getContextLogger(ctx context.Context) *contextLogger { 114 | logger, ok := ctx.Value(contextLoggerKey{}).(*contextLogger) 115 | if !ok { 116 | panic("no context logger") 117 | } 118 | return logger 119 | } 120 | -------------------------------------------------------------------------------- /Gopkg.lock: -------------------------------------------------------------------------------- 1 | # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. 2 | 3 | 4 | [[projects]] 5 | name = "github.com/apache/thrift" 6 | packages = ["lib/go/thrift"] 7 | revision = "b2a4d4ae21c789b689dd162deb819665567f481c" 8 | version = "0.10.0" 9 | 10 | [[projects]] 11 | branch = "master" 12 | name = "github.com/beorn7/perks" 13 | packages = ["quantile"] 14 | revision = "3a771d992973f24aa725d07868b467d1ddfceafb" 15 | 16 | [[projects]] 17 | branch = "master" 18 | name = "github.com/codahale/hdrhistogram" 19 | packages = ["."] 20 | revision = "3a0bb77429bd3a61596f5e8a3172445844342120" 21 | 22 | [[projects]] 23 | name = "github.com/go-kit/kit" 24 | packages = [ 25 | "log", 26 | "log/level" 27 | ] 28 | revision = "ca4112baa34cb55091301bdc13b1420a122b1b9e" 29 | version = "v0.7.0" 30 | 31 | [[projects]] 32 | name = "github.com/go-logfmt/logfmt" 33 | packages = ["."] 34 | revision = "390ab7935ee28ec6b286364bba9b4dd6410cb3d5" 35 | version = "v0.3.0" 36 | 37 | [[projects]] 38 | name = "github.com/go-stack/stack" 39 | packages = ["."] 40 | revision = "259ab82a6cad3992b4e21ff5cac294ccb06474bc" 41 | version = "v1.7.0" 42 | 43 | [[projects]] 44 | name = "github.com/golang/protobuf" 45 | packages = ["proto"] 46 | revision = "925541529c1fa6821df4e44ce2723319eb2be768" 47 | version = "v1.0.0" 48 | 49 | [[projects]] 50 | name = "github.com/gorilla/context" 51 | packages = ["."] 52 | revision = "1ea25387ff6f684839d82767c1733ff4d4d15d0a" 53 | version = "v1.1" 54 | 55 | [[projects]] 56 | name = "github.com/gorilla/mux" 57 | packages = ["."] 58 | revision = "53c1911da2b537f792e7cafcb446b05ffe33b996" 59 | version = "v1.6.1" 60 | 61 | [[projects]] 62 | branch = "master" 63 | name = "github.com/kr/logfmt" 64 | packages = ["."] 65 | revision = "b84e30acd515aadc4b783ad4ff83aff3299bdfe0" 66 | 67 | [[projects]] 68 | name = "github.com/matttproud/golang_protobuf_extensions" 69 | packages = ["pbutil"] 70 | revision = "3247c84500bff8d9fb6d579d800f20b3e091582c" 71 | version = "v1.0.0" 72 | 73 | [[projects]] 74 | name = "github.com/oklog/run" 75 | packages = ["."] 76 | revision = "4dadeb3030eda0273a12382bb2348ffc7c9d1a39" 77 | version = "v1.0.0" 78 | 79 | [[projects]] 80 | name = "github.com/opentracing/opentracing-go" 81 | packages = [ 82 | ".", 83 | "ext", 84 | "log" 85 | ] 86 | revision = "1949ddbfd147afd4d964a9f00b24eb291e0e7c38" 87 | version = "v1.0.2" 88 | 89 | [[projects]] 90 | branch = "master" 91 | name = "github.com/prometheus/client_golang" 92 | packages = [ 93 | "prometheus", 94 | "prometheus/promauto", 95 | "prometheus/promhttp" 96 | ] 97 | revision = "2f0e84125ddfaac80c0fc5f9917b668760664359" 98 | 99 | [[projects]] 100 | branch = "master" 101 | name = "github.com/prometheus/client_model" 102 | packages = ["go"] 103 | revision = "99fa1f4be8e564e8a6b613da7fa6f46c9edafc6c" 104 | 105 | [[projects]] 106 | branch = "master" 107 | name = "github.com/prometheus/common" 108 | packages = [ 109 | "expfmt", 110 | "internal/bitbucket.org/ww/goautoneg", 111 | "model" 112 | ] 113 | revision = "38c53a9f4bfcd932d1b00bfc65e256a7fba6b37a" 114 | 115 | [[projects]] 116 | branch = "master" 117 | name = "github.com/prometheus/procfs" 118 | packages = [ 119 | ".", 120 | "internal/util", 121 | "nfs", 122 | "xfs" 123 | ] 124 | revision = "8b1c2da0d56deffdbb9e48d4414b4e674bd8083e" 125 | 126 | [[projects]] 127 | name = "github.com/uber/jaeger-client-go" 128 | packages = [ 129 | ".", 130 | "config", 131 | "internal/baggage", 132 | "internal/baggage/remote", 133 | "internal/spanlog", 134 | "internal/throttler", 135 | "log", 136 | "rpcmetrics", 137 | "thrift-gen/agent", 138 | "thrift-gen/baggage", 139 | "thrift-gen/jaeger", 140 | "thrift-gen/sampling", 141 | "thrift-gen/zipkincore", 142 | "utils" 143 | ] 144 | revision = "c107110d057826281414cb964f167bce5be17588" 145 | version = "v2.12.0" 146 | 147 | [[projects]] 148 | name = "github.com/uber/jaeger-lib" 149 | packages = ["metrics"] 150 | revision = "4267858c0679cd4e47cefed8d7f70fd386cfb567" 151 | version = "v1.4.0" 152 | 153 | [[projects]] 154 | branch = "master" 155 | name = "golang.org/x/net" 156 | packages = ["context"] 157 | revision = "61147c48b25b599e5b561d2e9c4f3e1ef489ca41" 158 | 159 | [solve-meta] 160 | analyzer-name = "dep" 161 | analyzer-version = 1 162 | inputs-digest = "6fa1b715920f54c2648b45d9c3c8c675d5893d51e52fbec407c73e997373f8f1" 163 | solver-name = "gps-cdcl" 164 | solver-version = 1 165 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "flag" 6 | "fmt" 7 | "net" 8 | "net/http" 9 | "os" 10 | "os/signal" 11 | "syscall" 12 | "time" 13 | 14 | "github.com/uber/jaeger-client-go" 15 | 16 | "github.com/go-kit/kit/log" 17 | "github.com/go-kit/kit/log/level" 18 | "github.com/oklog/run" 19 | "github.com/prometheus/client_golang/prometheus" 20 | "github.com/prometheus/client_golang/prometheus/promauto" 21 | "github.com/prometheus/client_golang/prometheus/promhttp" 22 | jaegerconfig "github.com/uber/jaeger-client-go/config" 23 | jaegermetrics "github.com/uber/jaeger-lib/metrics" 24 | ) 25 | 26 | func main() { 27 | var ( 28 | apiAddr = flag.String("api", ":443", "API listen address") 29 | promAddr = flag.String("prometheus", ":8081", "Prometheus listen address") 30 | jaegerAddr = flag.String("jaeger", "", "Jaeger host:port") 31 | oklogAddr = flag.String("oklog", "", "OK Log host:port") 32 | cert = flag.String("cert", "certs/server.crt", "TLS certificate") 33 | key = flag.String("key", "certs/server.key", "TLS key") 34 | db = flag.String("db", "breakfasts.json", "database file") 35 | images = flag.String("images", "images/", "image dir") 36 | debug = flag.Bool("debug", false, "print debug info") 37 | ) 38 | flag.Parse() 39 | 40 | var console log.Logger 41 | { 42 | console = log.NewLogfmtLogger(os.Stderr) 43 | loglevel := level.AllowInfo() 44 | if *debug { 45 | loglevel = level.AllowDebug() 46 | } 47 | console = level.NewFilter(console, loglevel) 48 | } 49 | 50 | var structured log.Logger 51 | { 52 | if *oklogAddr != "" { 53 | conn, err := net.DialTimeout("tcp", *oklogAddr, time.Second) 54 | if err != nil { 55 | level.Error(console).Log("err", err) 56 | os.Exit(1) 57 | } 58 | defer conn.Close() 59 | structured = log.NewJSONLogger(conn) 60 | level.Info(console).Log("logging", "enabled", "oklog", *oklogAddr) 61 | } else { 62 | structured = log.NewNopLogger() 63 | level.Info(console).Log("logging", "disabled") 64 | } 65 | } 66 | 67 | var ( 68 | duration = promauto.NewHistogramVec(prometheus.HistogramOpts{ 69 | Namespace: "breakfast_solutions", 70 | Subsystem: "service", 71 | Name: "request_duration_seconds", 72 | Help: "Duration of each phase of a request in seconds.", 73 | Buckets: prometheus.DefBuckets, 74 | }, []string{"component", "operation", "success"}) 75 | ) 76 | 77 | { 78 | if *jaegerAddr != "" { 79 | transport, err := jaeger.NewUDPTransport(*jaegerAddr, 0) 80 | if err != nil { 81 | level.Error(console).Log("err", err) 82 | os.Exit(1) 83 | } 84 | cfg := jaegerconfig.Configuration{ 85 | Sampler: &jaegerconfig.SamplerConfig{ 86 | Type: jaeger.SamplerTypeConst, 87 | Param: 1.0, 88 | }, 89 | } 90 | closer, err := cfg.InitGlobalTracer( 91 | "breakfast_solutions", 92 | jaegerconfig.Logger(logAdapter{console}), 93 | jaegerconfig.Metrics(jaegermetrics.NullFactory), 94 | jaegerconfig.Reporter(jaeger.NewRemoteReporter(transport)), 95 | ) 96 | if err != nil { 97 | level.Error(console).Log("err", err) 98 | os.Exit(1) 99 | } 100 | defer closer.Close() 101 | level.Info(console).Log("tracing", "enabled", "jaeger", *jaegerAddr) 102 | } else { 103 | level.Info(console).Log("tracing", "disabled") 104 | } 105 | } 106 | 107 | var pre preprocessor 108 | { 109 | pre = geoPreprocess 110 | pre = loggingPreprocessMiddleware(pre) 111 | pre = metricsPreprocessMiddleware(pre) 112 | pre = tracingPreprocessMiddleware(pre) 113 | } 114 | 115 | var repo repository 116 | { 117 | repo = mustNewRepository(*db) 118 | repo = loggingRepoMiddleware{repo} 119 | repo = metricsRepoMiddleware{repo} 120 | repo = tracingRepoMiddleware{repo} 121 | } 122 | 123 | var post postprocessor 124 | { 125 | post = basicPostprocess 126 | post = loggingPostprocessMiddleware(post) 127 | post = metricsPostprocessMiddleware(post) 128 | post = tracingPostprocessMiddleware(post) 129 | } 130 | 131 | var api http.Handler 132 | { 133 | api = newAPI(pre, repo, post, *images) 134 | api = hstsAPIMiddleware(api) 135 | api = loggingAPIMiddleware(api, structured) 136 | api = metricsAPIMiddleware(api, duration) 137 | api = tracingAPIMiddleware(api) 138 | } 139 | 140 | var g run.Group 141 | { 142 | server := &http.Server{Addr: *apiAddr, Handler: api} 143 | g.Add(func() error { 144 | level.Info(console).Log("api_addr", *apiAddr) 145 | return server.ListenAndServeTLS(*cert, *key) 146 | }, func(error) { 147 | ctx, cancel := context.WithTimeout(context.Background(), time.Second) 148 | defer cancel() 149 | server.Shutdown(ctx) 150 | }) 151 | } 152 | { 153 | mux := http.NewServeMux() 154 | mux.Handle("/metrics", promhttp.Handler()) 155 | server := &http.Server{Addr: *promAddr, Handler: mux} 156 | g.Add(func() error { 157 | level.Info(console).Log("prometheus_addr", *promAddr) 158 | return server.ListenAndServe() 159 | }, func(error) { 160 | ctx, cancel := context.WithTimeout(context.Background(), time.Second) 161 | defer cancel() 162 | server.Shutdown(ctx) 163 | }) 164 | } 165 | { 166 | ctx, cancel := context.WithCancel(context.Background()) 167 | g.Add(func() error { 168 | c := make(chan os.Signal, 1) 169 | signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) 170 | select { 171 | case sig := <-c: 172 | return fmt.Errorf("received signal %s", sig) 173 | case <-ctx.Done(): 174 | return ctx.Err() 175 | } 176 | }, func(error) { 177 | cancel() 178 | }) 179 | } 180 | level.Info(console).Log("exit", g.Run()) 181 | } 182 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------