Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

My Favorite Talks at GopherCon 2019

Yuki Ito
August 20, 2019

My Favorite Talks at GopherCon 2019

Yuki Ito

August 20, 2019
Tweet

More Decks by Yuki Ito

Other Decks in Technology

Transcript

  1. Table of Contents !2 Observability in Go Socket to Me:

    Where do Sockets Live in Go? Day 0 (Pre-Conference Workshop): Day 2:
  2. Table of Contents !3 Observability in Go Socket to Me:

    Where do Sockets Live in Go? Day 0 (Pre-Conference Workshop): Day 2:
  3. Logs !7 log package func handler(w http.ResponseWriter, r *http.Request) {

    status := http.StatusOK // net/http returns 200 by default defer func(t time.Time) { log.Printf( "%s %q => %d (%2.3fs)\n", r.Method, r.URL.String(), status, time.Since(t).Seconds(), ) }(time.Now()) // … }
  4. Logs !8 log package func main() { log.SetFlags(log.Flags() | log.Lshortfile)

    log.Println("Listening at: http://localhost:" + port) http.ListenAndServe(":"+port, nil) // ... } 2019/08/20 19:00:00 server.go:54: Listening at: http://localhost:8080
  5. Logs !9 Structured Logs github.com/sirupsen/logrus 2019/08/20 19:00:00 server.go:54: Listening at:

    http://localhost:8080 { “level”:"info", "msg":"Listening at: http://localhost:8080”, “time":"2019-07-17T21:26:08-07:00" }
  6. !10 Log Trace Metric How should we log? If the

    only observability tool we have is logs, then we should log everything.
  7. !11 Log Trace Metric How should we log? If we

    have metrics and traces, then reduce logging to important events meant largely for human consumption.
  8. Metrics !13 expvar package import ( "expvar" // ... )

    func main() { // ... // Expose the port value ep := expvar.NewString("Port") ep.Set(port) http.ListenAndServe(":"+port, nil) //... }
  9. Metrics !14 expvar package package expvar // ... func init()

    { http.HandleFunc("/debug/vars", expvarHandler) Publish("cmdline", Func(cmdline)) Publish("memstats", Func(memstats)) }
  10. Metrics !16 expvar makes us be able to expose metrics

    We can recognize only `current status` of system. Hoever…
  11. Metrics !18 import ( // ... "github.com/prometheus/client_golang/prometheus/promhttp" ) func main()

    { // ... http.Handle("/metrics", promhttp.Handler()) // … http.ListenAndServe(":"+port, nil) // ... }
  12. Traces !24 import ( // ... "contrib.go.opencensus.io/exporter/jaeger" "go.opencensus.io/trace" ) func

    main() { je, err := jaeger.NewExporter(jaeger.Options{ CollectorEndpoint: "http://localhost:14268/api/traces", Process: jaeger.Process{ ServiceName: "servicea", Tags: []jaeger.Tag{ jaeger.StringTag("server", "1"), jaeger.StringTag("port", port), jaeger.BoolTag("demo", true), }, }, }) trace.RegisterExporter(je) }
  13. Traces !25 func queryServiceBHandler(c *http.Client, url string) http.HandlerFunc { return

    func(w http.ResponseWriter, r *http.Request) { ctx, span := trace.StartSpan( r.Context(), "queryServiceBHandler", ) defer span.End() // ... } }
  14. Table of Contents !29 Observability in Go Socket to Me:

    Where do Sockets Live in Go? Day 0 (Pre-Conference Workshop): Day 2:
  15. !32 func (srv *Server) Serve(l net.Listener) error { // ...

    for { rw, e := l.Accept() // ... c := srv.newConn(rw) go c.serve(ctx) } } Socket to me net/http/server.go
  16. !37 func Listen(network, address string) (Listener, error) { var lc

    ListenConfig return lc.Listen(context.Background(), network, address) } net.Listen() Socket Options in Go
  17. !40 func listenControler(network, address string, c syscall.RawConn) error { return

    c.Control(func(fd uintptr) { syscall.SetsockoptInt( int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1, ) }) } Socket Options in Go
  18. !41 func listenControler(network, address string, c syscall.RawConn) error { return

    c.Control(func(fd uintptr) { syscall.SetsockoptInt( int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1, ) }) } Socket Options in Go
  19. Table of Contents !42 Observability in Go Socket to Me:

    Where do Sockets Live in Go? Day 0 (Pre-Conference Workshop): Day 2: