├── .gitignore ├── lesson-002-prometheus-metrics ├── images │ ├── prom-graph.png │ └── prom-table.png ├── go.mod ├── main.go ├── app │ ├── metrics.go │ └── server.go ├── config │ └── prometheus.yml ├── README.md └── go.sum ├── README.md ├── lesson-001-web-server-cli ├── main.go ├── app │ └── server.go └── README.md ├── lesson-000-web-server ├── main.go └── README.md └── lesson-003-web-server-graceful-shutdown ├── main.go └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.swo 3 | -------------------------------------------------------------------------------- /lesson-002-prometheus-metrics/images/prom-graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/contributing-to-kubernetes/go-examples/HEAD/lesson-002-prometheus-metrics/images/prom-graph.png -------------------------------------------------------------------------------- /lesson-002-prometheus-metrics/images/prom-table.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/contributing-to-kubernetes/go-examples/HEAD/lesson-002-prometheus-metrics/images/prom-table.png -------------------------------------------------------------------------------- /lesson-002-prometheus-metrics/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/contributing-to-kubernetes/go-examples/lesson-002-prometheus-metrics 2 | 3 | go 1.13 4 | 5 | require ( 6 | github.com/prometheus/client_golang v0.9.3 7 | github.com/spf13/cobra v1.0.0 8 | ) 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Go Examples 2 | 3 | A variety of example Go programs to get you comfortable writing Go and contributing to Kubernetes. 4 | 5 | ## Table of Contents 6 | 7 | * [Lesson 0: Building a Web Server](./lesson-000-web-server) 8 | * [Lesson 1: Converting a Web Server Into a CLI](./lesson-001-web-server-cli) 9 | * [Lesson 2: Instrumenting Your Application - Prometheus Metrics](./lesson-002-prometheus-metrics) 10 | * [Lesson 3: Web server graceful shutdown](./lesson-003-web-server-graceful-shutdown) -------------------------------------------------------------------------------- /lesson-001-web-server-cli/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "os" 6 | 7 | "github.com/contributing-to-kubernetes/go-examples/lesson-001-web-server-cli/app" 8 | ) 9 | 10 | func main() { 11 | // This is the entrypoint into our app. 12 | // Here we import a cobra command and execute it. The cobra command will 13 | // execute some action. 14 | command := app.NewServerCommand() 15 | if err := command.Execute(); err != nil { 16 | log.Println(err) 17 | os.Exit(1) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /lesson-002-prometheus-metrics/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "os" 6 | 7 | "github.com/contributing-to-kubernetes/go-examples/lesson-002-prometheus-metrics/app" 8 | ) 9 | 10 | func main() { 11 | // This is the entrypoint into our app. 12 | // Here we import a cobra command and execute it. The cobra command will 13 | // execute some action. 14 | command := app.NewServerCommand() 15 | if err := command.Execute(); err != nil { 16 | log.Println(err) 17 | os.Exit(1) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /lesson-002-prometheus-metrics/app/metrics.go: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import ( 4 | "github.com/prometheus/client_golang/prometheus" 5 | ) 6 | 7 | var ( 8 | responsesTotal = prometheus.NewCounterVec( 9 | prometheus.CounterOpts{ 10 | Namespace: "http", 11 | Name: "responses_total", 12 | Help: "Total http responses", 13 | }, 14 | []string{ 15 | // HTTP status code. 16 | "code", 17 | }, 18 | ) 19 | ) 20 | 21 | // RegisterMetrics registers all metrics for the server. 22 | func RegisterMetrics() { 23 | prometheus.MustRegister(responsesTotal) 24 | } 25 | -------------------------------------------------------------------------------- /lesson-002-prometheus-metrics/config/prometheus.yml: -------------------------------------------------------------------------------- 1 | # See 2 | # https://github.com/prometheus/prometheus/blob/master/docs/getting_started.md. 3 | global: 4 | scrape_interval: 15s # By default, scrape targets every 15 seconds. 5 | 6 | # A scrape configuration with two endpoint to scrape: 7 | # - Prometheus itself in localhost:9090 8 | # - Our server in localhost:8080 9 | scrape_configs: 10 | # The job name is added as a label `job=` to any timeseries scraped from this config. 11 | - job_name: "prometheus" 12 | # Override the global default and scrape targets from this job every 5 seconds. 13 | scrape_interval: 5s 14 | static_configs: 15 | - targets: ["localhost:9090"] 16 | - job_name: "api-server" 17 | scrape_interval: 5s 18 | static_configs: 19 | - targets: ["localhost:8080"] 20 | -------------------------------------------------------------------------------- /lesson-000-web-server/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "os" 7 | ) 8 | 9 | // listenAddr is the address where our web server will be listening for 10 | // requests. 11 | const listenAddr = "0.0.0.0:8080" 12 | 13 | // homeHandler takes a response writer to build a response for the given 14 | // request. 15 | // This http handler will greet you with the hostname of the machine where this 16 | // app is running. 17 | func homeHandler(w http.ResponseWriter, r *http.Request) { 18 | // We begin by looking up the hostname. 19 | host, err := os.Hostname() 20 | if err != nil { 21 | // If we see an error then we return an http code 500 and we tell the 22 | // client what the error was. 23 | w.WriteHeader(http.StatusInternalServerError) 24 | errMsg := fmt.Sprintf("we saw an error: %v\n", err) 25 | fmt.Fprintf(w, errMsg) 26 | return 27 | } 28 | 29 | // Build a string with the hostname. 30 | greeting := fmt.Sprintf("Greeting from %s!\n", host) 31 | fmt.Fprintf(w, greeting) 32 | } 33 | 34 | func main() { 35 | // Create a request multiplexer. This will match an incoming request to a 36 | // route. 37 | mux := http.NewServeMux() 38 | 39 | // Register homeHandler with the router "/". This means that a request to 40 | // http://0.0.0.0:8080/ will be handled by the 'homeHandler' function. 41 | mux.HandleFunc("/", homeHandler) 42 | 43 | // Start the webserver by registering our multiplexer and listening to 44 | // 'listenAddr'. 45 | fmt.Printf("starting web server at %s\n", listenAddr) 46 | http.ListenAndServe(listenAddr, mux) 47 | } 48 | -------------------------------------------------------------------------------- /lesson-001-web-server-cli/app/server.go: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "net/http" 7 | "os" 8 | 9 | "github.com/spf13/cobra" 10 | ) 11 | 12 | // listenAddr is the listening address for the web server we will run. 13 | var listenAddr string 14 | 15 | // NewServerCommand creates a cobra command. A command represents an action, 16 | // the action in our case is to run our web server. 17 | func NewServerCommand() *cobra.Command { 18 | // Before going forward, please take a look at this page: 19 | // https://godoc.org/github.com/spf13/cobra#Command, here you will find a 20 | // reference to how a cobra command is structured. 21 | cmd := &cobra.Command{ 22 | Use: "server", 23 | Short: "web server", 24 | Long: `The server is an example application to mimick the organization of the 25 | Kubernetes API server.`, 26 | 27 | // Print usage when the command errors. 28 | SilenceUsage: false, 29 | Version: "v1.0.0", 30 | 31 | // This is the function that will be executed when we actually run our CLI. 32 | // This function will return an error. 33 | RunE: func(cmd *cobra.Command, args []string) error { 34 | log.Printf("version: %+v", cmd.Version) 35 | log.Printf("args: %#v", args) 36 | return Run(listenAddr) 37 | }, 38 | } 39 | 40 | // Define flags. 41 | cmd.PersistentFlags().StringVarP(&listenAddr, "addr", "a", "0.0.0.0:8080", "server's address") 42 | 43 | return cmd 44 | } 45 | 46 | // homeHandler processes an incoming HTTP request by creating a response to 47 | // greet the HTTP client with the host name of the computer running this app. 48 | func homeHandler(w http.ResponseWriter, r *http.Request) { 49 | host, err := os.Hostname() 50 | if err != nil { 51 | w.WriteHeader(http.StatusInternalServerError) 52 | errMsg := fmt.Sprintf("we saw an error: %v\n", err) 53 | fmt.Fprintf(w, errMsg) 54 | return 55 | } 56 | 57 | greeting := fmt.Sprintf("Greeting from %s!\n", host) 58 | fmt.Fprintf(w, greeting) 59 | } 60 | 61 | // Run registers http handlers to endpoints and starts the web server. 62 | func Run(addr string) error { 63 | mux := http.NewServeMux() 64 | mux.HandleFunc("/", homeHandler) 65 | 66 | log.Printf("starting server at %s\n", addr) 67 | return http.ListenAndServe(addr, mux) 68 | } 69 | -------------------------------------------------------------------------------- /lesson-003-web-server-graceful-shutdown/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "log" 7 | "net/http" 8 | "os" 9 | "os/signal" 10 | "syscall" 11 | "time" 12 | ) 13 | 14 | // listenAddr is the address where our web server will be listening for 15 | // requests. 16 | const listenAddr = "0.0.0.0:8080" 17 | 18 | // homeHandler takes a response writer to build a response for the given 19 | // request. 20 | // This http handler will greet you with the hostname of the machine where this 21 | // app is running. 22 | func homeHandler(w http.ResponseWriter, r *http.Request) { 23 | // We begin by looking up the hostname. 24 | host, err := os.Hostname() 25 | if err != nil { 26 | // If we see an error then we return an http code 500 and we tell the 27 | // client what the error was. 28 | w.WriteHeader(http.StatusInternalServerError) 29 | errMsg := fmt.Sprintf("we saw an error: %v\n", err) 30 | fmt.Fprintf(w, errMsg) 31 | return 32 | } 33 | 34 | // Build a string with the hostname. 35 | greeting := fmt.Sprintf("Greeting from %s!\n", host) 36 | fmt.Fprintf(w, greeting) 37 | time.Sleep(5 * time.Second) 38 | } 39 | 40 | func main() { 41 | done := make(chan bool, 1) 42 | sigint := make(chan os.Signal, 1) 43 | 44 | // Create a request multiplexer. This will match an incoming request to a 45 | // route. 46 | mux := http.NewServeMux() 47 | server := http.Server{Addr: listenAddr, Handler: mux} 48 | 49 | // Register homeHandler with the router "/". This means that a request to 50 | // http://0.0.0.0:8080/ will be handled by the 'homeHandler' function. 51 | mux.HandleFunc("/", homeHandler) 52 | 53 | signal.Notify(sigint, 54 | os.Interrupt, // Sent from the terminal, e.g., Ctrl+C. 55 | syscall.SIGTERM, // Sent to signal pod to shutdown or something. 56 | ) 57 | 58 | go func() { 59 | <-sigint 60 | log.Printf("Shutting down server") 61 | 62 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 63 | defer cancel() 64 | 65 | server.SetKeepAlivesEnabled(false) 66 | if err := server.Shutdown(ctx); err != nil { 67 | log.Fatalf("Unable to do graceful shutdown: %v", err) 68 | } 69 | 70 | close(done) 71 | }() 72 | 73 | if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { 74 | log.Fatal(err) 75 | } 76 | 77 | <-done 78 | log.Printf("Gracefully stopped") 79 | } 80 | -------------------------------------------------------------------------------- /lesson-002-prometheus-metrics/app/server.go: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "net/http" 7 | "os" 8 | 9 | "github.com/spf13/cobra" 10 | 11 | "github.com/prometheus/client_golang/prometheus" 12 | "github.com/prometheus/client_golang/prometheus/promhttp" 13 | ) 14 | 15 | // listenAddr is the listening address for the web server we will run. 16 | var listenAddr string 17 | 18 | // NewServerCommand creates a cobra command. A command represents an action, 19 | // the action in our case is to run out web server. 20 | func NewServerCommand() *cobra.Command { 21 | // Before going forward, please take a look at this page: 22 | // https://godoc.org/github.com/spf13/cobra#Command, here you will find a 23 | // reference to how a cobra command is structured. 24 | cmd := &cobra.Command{ 25 | Use: "server", 26 | Short: "web server", 27 | Long: `The server is an example application to mimick the organization of the 28 | Kubernetes API server.`, 29 | 30 | // Print usage when the command errors. 31 | SilenceUsage: false, 32 | Version: "v1.0.0", 33 | 34 | // This is the function that will be executed when we actually run our CLI. 35 | // This function will return an error. 36 | RunE: func(cmd *cobra.Command, args []string) error { 37 | log.Printf("version: %+v", cmd.Version) 38 | log.Printf("args: %#v", args) 39 | return Run(listenAddr) 40 | }, 41 | } 42 | 43 | // Define flags. 44 | cmd.PersistentFlags().StringVarP(&listenAddr, "addr", "a", "0.0.0.0:8080", "server's address") 45 | 46 | // Register metrics. 47 | RegisterMetrics() 48 | 49 | return cmd 50 | } 51 | 52 | // homeHandler processes an incoming HTTP request by creating a response to 53 | // greet the HTTP client with the host name of the computer running this app. 54 | func homeHandler(w http.ResponseWriter, r *http.Request) { 55 | host, err := os.Hostname() 56 | if err != nil { 57 | w.WriteHeader(http.StatusInternalServerError) 58 | errMsg := fmt.Sprintf("we saw an error: %v\n", err) 59 | fmt.Fprintf(w, errMsg) 60 | responsesTotal.With(prometheus.Labels{"code": "500"}).Inc() 61 | return 62 | } 63 | 64 | greeting := fmt.Sprintf("Greeting from %s!\n", host) 65 | fmt.Fprintf(w, greeting) 66 | responsesTotal.With(prometheus.Labels{"code": "200"}).Inc() 67 | } 68 | 69 | // Run registers http handlers to endpoints and starts the web server. 70 | func Run(addr string) error { 71 | mux := http.NewServeMux() 72 | mux.HandleFunc("/", homeHandler) 73 | mux.Handle("/metrics", promhttp.Handler()) 74 | 75 | log.Printf("starting server at %s\n", addr) 76 | return http.ListenAndServe(addr, mux) 77 | } 78 | -------------------------------------------------------------------------------- /lesson-000-web-server/README.md: -------------------------------------------------------------------------------- 1 | # Lesson 0: Building a Web Server 2 | 3 | First step in our jourmey: we will build a web server. 4 | Why? 5 | Because it will teach us how to use a lot of basic building block for more complicated programs later on. 6 | 7 | But before we dive into code, we will present some concepts we will be working on in this lesson. 8 | 9 | ## Knowledge bits 10 | 11 | - In Go, we organize our code into modules. Go modules are in turn made up of packages. 12 | 13 | In our case we will only define the `main` package. This specific package name is used by the Go compiler to know that the package should compile as an executable program. 14 | 15 | The `main` **function** in the **package** `main` will be the entry point of our executable. 16 | 17 | More info on this topic can be found in this in https://blog.golang.org/using-go-modules. 18 | 19 | - Functions in Go can return more than one value. It is a common pattern in Go to always return an error on operations that may fail. One should then check if the error variable is `nil`, which means there was no error, or to handle the error if not `nil`. 20 | 21 | For example: 22 | 23 | ``` 24 | host, err := os.Hostname() 25 | if err != nil { 26 | fmt.Sprintf("we saw an error: %v\n", err) 27 | } 28 | 29 | fmt.Println("everything is awesome") 30 | ``` 31 | 32 | - pointers: no, don't worry about the naming (you can not do pointer arithmetic as in C or C++). Pointers in Go are used to pass variables by reference to functions. This means, we will be using the exact same object that was passed. 33 | 34 | - when using `Sprintf`, `Fprintf` or `Printf` (or any printing function that ends with an `f`) we can use several **printing verbs** to present the data. (official docs in https://golang.org/pkg/fmt/) In this lesson we will use: 35 | - `%v`: we will output the value in a default format 36 | - `%s`: the output would be the `string` format 37 | 38 | ## Lesson content 39 | 40 | Our example program lives here in [`main.go`](./main.go). 41 | 42 | The `go.mod` file indicates that in this directory we have a Go module. 43 | We got that one from doing a `go mod init`, nothing fancy. 44 | 45 | To better understand the mechanics of our example please go though the 46 | documentation at https://godoc.org/net/http. 47 | 48 | ## Testing It 49 | 50 | To run the app you can compile 51 | ``` 52 | $ go build -o app 53 | ``` 54 | and build it 55 | ``` 56 | $ ./app 57 | starting web server at 0.0.0.0:8080 58 | ``` 59 | 60 | If you read through the code, you'll notice that we registered an HTTP handler 61 | (a function that processes incoming requests) for the route `/`. 62 | You can see this by sending a request to it 63 | ``` 64 | $ curl http://localhost:8080/ 65 | Greeting from alejandrox1-machine1! 66 | ``` 67 | 68 | Incidentally, since we only have one route registered, and that is the root, 69 | all requests will be handled by it 70 | ``` 71 | $ curl http://localhost:8080/hello/ 72 | Greeting from alejandrox1-machine1! 73 | ``` 74 | 75 | And another one 76 | ``` 77 | $ curl http://localhost:8080/hello/again/ 78 | Greeting from alejandrox1-machine1! 79 | ``` 80 | 81 | ### Next steps (optional) 82 | 83 | Couple interesting things we can do: 84 | - add more routes to the existing server 85 | - be able to read URL params (e.g http://localhost:8080/hello?user=alejandrox&lesson=0) and output 86 | ``` 87 | Greeting from alejandrox1-machine1! 88 | Params: 89 | user: alejandrox 90 | lesson: 0 91 | ``` 92 | -------------------------------------------------------------------------------- /lesson-001-web-server-cli/README.md: -------------------------------------------------------------------------------- 1 | # Lesson 1: Customizing a Web Server 2 | 3 | In [lesson 0](../lesson-000-web-server/) we built a simple web server. 4 | In this lesson, we will build on top of this web server. 5 | We will add the ability for the user to run the web server in the command-line 6 | and to configure it through flags and arguments. 7 | 8 | This kind of component is something you will see in multiple projects in the Kubernetes community. 9 | One great example is the 10 | [Kubernetes API server](https://github.com/kubernetes/kubernetes/tree/master/cmd/kube-apiserver). 11 | 12 | So let's get to it. 13 | 14 | ## Knowledge bits 15 | 16 | - we can reference packages that belongs to the standard library like "os" directly by adding a line in the import section. 17 | 18 | And we can also reference external packages like `github.com/contributing-to-kubernetes/go-examples/lesson-001-web-server-cli/app` directly 19 | 20 | - in this lesson we will be using a variant of the `nil` pattern explained in lesson-00: 21 | 22 | ``` 23 | command := app.NewServerCommand() 24 | if err := command.Execute(); err != nil { 25 | log.Println(err) 26 | os.Exit(1) 27 | } 28 | ``` 29 | 30 | Here we use the definition of `err` and the condition to change our program workflow in the same line. 31 | 32 | - after checking the actual code from this lesson, you might be wondering "where are those `-h` and `-v` flags come from?" 33 | 34 | Don't panic, those are flags added by default by the `cobra` library as explained in the [overview](https://github.com/spf13/cobra#overview) section of the documentation and and in the [version flag section](https://github.com/spf13/cobra#version-flag) 35 | 36 | ## Getting Started 37 | In order to build our web server as a CLI we will use 38 | https://github.com/spf13/cobra. 39 | 40 | The main addition to our web server will be the definition of a cobra command to define flags and receive arguments. 41 | 42 | ## Testing It 43 | 44 | Since we have a CLI, we now have useful descriptions and help messages 45 | ``` 46 | $ go build -o server && ./server --help 47 | The server is an example application to mimick the organization of the 48 | Kubernetes API server. 49 | 50 | Usage: 51 | server [flags] 52 | 53 | Flags: 54 | -a, --addr string server's address (default "0.0.0.0:8080") 55 | -h, --help help for server 56 | -v, --version version for server 57 | ``` 58 | 59 | You can see we have some flags. 60 | Let's see what version our app is 61 | ``` 62 | $ go build -o server && ./server -v 63 | server version v1.0.0 64 | ``` 65 | 66 | And to run it 67 | ``` 68 | $ go build -o server && ./server 69 | 2020/05/17 14:42:57 version: v1.0.0 70 | 2020/05/17 14:42:57 args: []string{} 71 | 2020/05/17 14:42:57 starting server at 0.0.0.0:8080 72 | ``` 73 | 74 | And finally, if you want to specify a different listening address for the server 75 | ``` 76 | $ go build -o server && ./server -a 0.0.0.0:9090 77 | 2020/05/17 14:43:40 version: v1.0.0 78 | 2020/05/17 14:43:40 args: []string{} 79 | 2020/05/17 14:43:40 starting server at 0.0.0.0:9090 80 | ``` 81 | 82 | You should be able to test it by going over to http://localhost:9090/ 83 | 84 | ## Next steps (optional) 85 | 86 | - we can use [viper](https://github.com/spf13/viper) library to make use of another common pattern which is reading configuration from files. 87 | 88 | For example having a file with the server address: 89 | 90 | ``` 91 | hostname: "0.0.0.0:8123" 92 | ``` 93 | 94 | - accept more flags to the CLI. Use and see how to deal with required flags, optional ones. 95 | 96 | - we can think about how to make our program more resilient and for example assume defaults if a required flag is missing,.. -------------------------------------------------------------------------------- /lesson-003-web-server-graceful-shutdown/README.md: -------------------------------------------------------------------------------- 1 | # Lesson 3: Web server graceful shutdown 2 | 3 | Taking as a foundation the webserver created in [lesson 000](../lesson-000-web-server/), we will be adding `graceful shutdown` functionality to it. 4 | 5 | In order to achieve this, we will be using `channels` and `goroutines` 6 | 7 | ## Knowledge bits 8 | 9 | 10 | ### Goroutines 11 | 12 | A `goroutine` is a lightweight thread of execution managed by the Go runtime; it is the way we have to run a piece of code concurrently with the original calling code, as we can see explained in the [oficial documentation.](https://tour.golang.org/concurrency/1) 13 | 14 | A small example could be: 15 | 16 | ``` 17 | func say(s string) { 18 | for i := 0; i < 5; i++ { 19 | time.Sleep(100 * time.Millisecond) 20 | fmt.Println(s) 21 | } 22 | } 23 | 24 | func main() { 25 | go say("k8s") 26 | say("contribute") 27 | } 28 | ``` 29 | 30 | Calling `go say()` is where we start a new `goroutine`, calling our already defined `say` function, while the `main` function runs in its own `goroutine` (called the _main goroutine_) 31 | 32 | 33 | ### Channels 34 | 35 | Channels are a concurrency synchronization technique we can use in Go. 36 | 37 | We can create a channel using the keyword `chan`, and we can transport data of only one data type. 38 | 39 | From the [official documention](https://tour.golang.org/concurrency/2) we can see how data can be sent and received from the channel. 40 | 41 | ``` 42 | ch := make(chan int) 43 | 44 | ch <- v // Send v to channel ch. 45 | v := <-ch // Receive from ch, and 46 | // assign value to v. 47 | ``` 48 | 49 | **_Unbuffered_ vs _buffered_ channels** 50 | 51 | Channels are created **blocking by default** (or _unbuffered_) 52 | 53 | If we try to send a resource to an unbuffered channel, the channel will lock (preventing us from sending anything else into the channel) until someone else reads from it. 54 | This essentially means that an unbuffered channel is restricted to never having more than 1 element inside of it. 55 | And viceversa. 56 | 57 | On the other hand, buffered channels have capacity and they are able to keep a number of resources. 58 | The only times buffered channels will lock goroutines are when a sender tries to send a resource and the channel is full or when a goroutine tries to get a resource and the channel is empty. 59 | 60 | We can use this in our benefit and block our server from being closed until all pending requests have been served when we receive a specific signal. 61 | 62 | 63 | ### Signals 64 | 65 | Signals are software interrupts sent to a program to indicate that an event has happened. 66 | 67 | In this example we will take care of two specific signals: `os.Interrupt` and `syscall.SIGTERM`: 68 | 69 | - `os.Interrupt` this is tipically the signal sent when we type Control-C, which normally causes the program to exit. 70 | - `syscall.SIGTERM` is usually sent when you want to give the process an opportunity to clean up before termination. 71 | 72 | 73 | ## Lesson content 74 | 75 | After getting a quick intro to `channels` and `goroutines`, we will dive into our lesson. 76 | 77 | As we initially stated, the current implementation is based on our server from [lesson 000](../lesson-000-web-server/). 78 | 79 | 80 | To achieve _graceful shutdown_ we added the following changes: 81 | 82 | - created a `sigint` channel; we will use it to notify our goroutine we have received a signal: `os.Interrupt` or `syscall.SIGTERM` in this case 83 | - with `server.SetKeepAlivesEnabled(false)` we set the server to not keep alive any connection (which in fact is the desired effect of having a gracefull shutdown behavior) to allow our server to finish processing any requests already received before the app received a termination signal. 84 | The first step is to disable "keep alive" TCP connections [https://godoc.org/net/http#Server.SetKeepAlivesEnabled](https://godoc.org/net/http#Server.SetKeepAlivesEnabled) before proceeding with the graceful shutdown of the server [https://godoc.org/net/http#Server.Shutdown](https://godoc.org/net/http#Server.Shutdown) 85 | - create the `done` channel; this one will be used to let the main goroutine we have finished the graceful shutdown. 86 | Adding this in the `func main()` allows us to run code only after the app is shut down - if needed 87 | 88 | 89 | ## Testing It 90 | 91 | In this case we would need to run our server in one terminal and have an additional one, which we will use to `curl` our server. 92 | 93 | Terminal 1 will have our server running after compiling our source code. 94 | 95 | In Terminal 2 we will execute the following line: 96 | 97 | ``` 98 | for (( ; ; )); do curl http://localhost:8080;done 99 | ``` 100 | 101 | This is an infinite **for** loop in `bash`, which will be requesting our server using `curl`. Everytime it hits our server, it will print our `Greeting from _x y z_` message. 102 | 103 | To see our example working as we expect (having a graceful shutdown) we would need to hit `ctrl + c` in Terminal 1 where our server is running. 104 | 105 | _Hint: we would also need to `ctrl + c` our **for loop** in Terminal 2 to prevent our script to continue sending requests to our stopped server_ 106 | 107 | We could also open a new terminal and first find the PID of our process e.g. `pgrep -i main`. This command will output the our main process ID. We can then call `kill -SIGTERM _processid_` to see how our server behaves when it receives the other signal we defined in our server. 108 | -------------------------------------------------------------------------------- /lesson-002-prometheus-metrics/README.md: -------------------------------------------------------------------------------- 1 | # Lesson 2: Instrumenting Your Application - Prometheus Metrics 2 | 3 | Continuin from [lesson 1](../lesson-001-web-server-cli/), we will now add 4 | metrics to our server. 5 | 6 | 7 | ## Table of Contents 8 | 9 | * [Getting Started](#getting-started) 10 | * [Adding Basic Metrics](#adding-basic-metrics) 11 | * [Creating Your Custom Metrics](#creating-your-custom-metrics) 12 | * [Visualizing Our Metrics](#visualizing-our-metrics) 13 | * [References](#references) 14 | 15 | ## Getting Started 16 | 17 | Instrumenting your application is one of those things that will allow you to 18 | trully be able to runyour code in production. 19 | Metrics are meant to help you understand, in a non-intrusive way, what is going 20 | on in your code at all times. 21 | Without knowing data, development is just bad guessing. 22 | With good data (with metrics), development can still be guessing but it will at 23 | least be a bit more informed. 24 | 25 | For our little experiment here, we will work on surfacing Prometheus-type 26 | metrics since Prometheus is one of the good monitoring solutions that are out 27 | there and probably the most common one. 28 | 29 | Before we actually start with any code or technical things it is useful to put 30 | this readme into perspective. 31 | The work here is intended to give you a hands-on experience with metrics in a 32 | non-trivial but relatively simple environment. 33 | This is meant to spike your interest and by no means is this readme the whole 34 | story. 35 | Thus we will recommend some reading and mention some keywords to help guide you 36 | in your search for more information. 37 | 38 | **Note:** We highly recommend that you read this twice. Start by skimming 39 | through it, and playing with the code. Once you have tred things out yourself, 40 | read it again and make sure you understand the details :smile:. 41 | 42 | ## Adding Basic Metrics 43 | 44 | You are gonna love this! 45 | Thanks to the awesome Go client library, 46 | https://github.com/prometheus/client_golang/, we can actually get a lot of very 47 | useful metrics with just 2 lines of code. 48 | 49 | In [app/server.go](./app/server.go) add the following: 50 | ```go 51 | import ( 52 | ... 53 | "github.com/prometheus/client_golang/prometheus/promhttp" 54 | ) 55 | 56 | // Run registers http handlers to endpoints and starts the web server. 57 | func Run(addr string) error { 58 | ... 59 | mux.Handle("/metrics", promhttp.Handler()) 60 | ... 61 | } 62 | ``` 63 | 64 | and :tada: you now now have metrics! 65 | And we should go check them out. 66 | 67 | Run the server 68 | ``` 69 | $ go build -o server&& ./server 70 | 2020/07/11 17:58:34 version: v1.0.0 71 | 2020/07/11 17:58:34 args: []string{} 72 | 2020/07/11 17:58:34 starting server at 0.0.0.0:8080 73 | ``` 74 | 75 | and on your browser go to http://0.0.0.0:8080/metrics. 76 | 77 | You should now be looking at some very useful metrics (and examples of what 78 | good metrics look like) 79 | ``` 80 | # HELP go_goroutines Number of goroutines that currently exist. 81 | # TYPE go_goroutines gauge 82 | go_goroutines 8 83 | ... 84 | # HELP go_threads Number of OS threads created. 85 | # TYPE go_threads gauge 86 | go_threads 10 87 | # HELP process_cpu_seconds_total Total user and system CPU time spent in seconds. 88 | # TYPE process_cpu_seconds_total counter 89 | process_cpu_seconds_total 0 90 | ... 91 | # HELP process_open_fds Number of open file descriptors. 92 | # TYPE process_open_fds gauge 93 | process_open_fds 8 94 | ... 95 | ``` 96 | 97 | ## Creating Your Custom Metrics 98 | 99 | Next step, to add metrics to our server. 100 | We will add the metric `http_responses_total`. 101 | This one will give us an idea of how to go about adding any kind of metric one 102 | may possible need. 103 | 104 | The number of responses is a metric that increases - it doesn't make sense to 105 | decrease the number of total http responses (you can't take a response back). 106 | As such, a `counter` would be metric type to use. 107 | 108 | Furthermore, in order to allow the possibility for keeping track of what kind 109 | of response it was (e.g., 200, 400, 500) we will add a "dimension" for the 110 | status code. 111 | This dimension is implemented via a label (keep this idea in mind, later on we 112 | will visualize these dimensions). 113 | 114 | The simplest definition of a counter could looke like 115 | ```go 116 | responsesTotal = prometheus.NewCounter( 117 | prometheus.CounterOpts{ 118 | Namespace: "http", 119 | Name: "responses_total", 120 | Help: "Total http responses", 121 | }) 122 | ``` 123 | 124 | Such a counter has a `Inc()` and an `Add(float64)` methods, see the godoc page 125 | https://godoc.org/github.com/prometheus/client_golang/prometheus#Counter, 126 | Which means we could use our counter above as 127 | 128 | ```go 129 | func homeHandler(w http.ResponseWriter, r *http.Request) { 130 | ... 131 | // Increase our responses total counter by 1. 132 | responsesTotal.Inc() 133 | } 134 | ``` 135 | 136 | But because we want labels, we need to use a `CounterVec` instead of a plain 137 | counter. 138 | Or as explained in the GoDoc page 139 | 140 | > In addition to the fundamental metric types Gauge, Counter, Summary, and 141 | > Histogram, a very important part of the Prometheus data model is the 142 | > partitioning of samples along dimensions called labels, which results in 143 | > metric vectors. The fundamental types are GaugeVec, CounterVec, SummaryVec, 144 | > and HistogramVec. 145 | 146 | A "metric vector" being a collection of all instances of the metric with 147 | different label values (pretty similar to how a vecotr in mathematics can 148 | define position in multidimensional spaces, if yah think about it). 149 | 150 | Our counter will now look like 151 | ```go 152 | responsesTotal = prometheus.NewCounterVec( 153 | prometheus.CounterOpts{ 154 | Namespace: "http", 155 | Name: "responses_total", 156 | Help: "Total http responses", 157 | }, 158 | []string{ 159 | // HTTP status code. 160 | "code", 161 | }, 162 | ) 163 | ``` 164 | 165 | you can see it in [app/metrics.go](./app/metrics.go). 166 | You will also notice we define a `RegisterMetrics()` functions which is 167 | ESSENTIAL! 168 | If we don't register our metrics then they will not show. 169 | 170 | We register our `responsesTotal` metric in the function `NewServerCommand()` 171 | over in [app/server.go](./app/server.go) 172 | ```go 173 | func NewServerCommand() *cobra.Command { 174 | ... 175 | // Register metrics. 176 | RegisterMetrics() 177 | ... 178 | } 179 | ``` 180 | 181 | And to use our counter (that can have a value set for the http response status 182 | code) we can do something like 183 | ```go 184 | responsesTotal.With(prometheus.Labels{"code": "200"}).Inc() 185 | ``` 186 | 187 | Which will increase by 1 the total number of http responses (with status code 188 | 200). 189 | Similarly, in the case of a 500 status code 190 | ```go 191 | responsesTotal.With(prometheus.Labels{"code": "500"}).Inc() 192 | ``` 193 | 194 | 195 | The code in this directory already has this all setup for you. 196 | If you go ahead and run it, you will see our metric showing up in 197 | http://0.0.0.0:8080/metrics. 198 | 199 | If you query out home endpoint you will see the counter increasing. 200 | For example, after a couple `curl http://0.0.0.0/` we had 201 | ``` 202 | # HELP http_responses_total Total http responses 203 | # TYPE http_responses_total counter 204 | http_responses_total{code="200"} 5 205 | ``` 206 | 207 | Now that you have this basic example you can build anything. 208 | All other metric types are used in a similar manner. 209 | If you want to see more examples, always check out the godoc page for the Go 210 | prometheus client https://godoc.org/github.com/prometheus/client_golang/prometheus. 211 | 212 | ## Visualizing Our Metrics 213 | 214 | Since we have our Prometheus-style metrics, let's actually see them! 215 | So time to run Prometheus :rocket:. 216 | 217 | We will use the latest Prometheus release (at the time of this writing). 218 | You can see all releases in https://github.com/prometheus/prometheus/releases. 219 | 220 | Because we want to be fancy, we will run Prometheus in a container, 221 | https://github.com/prometheus/prometheus#docker-images. 222 | ``` 223 | $ docker run --rm -it --network host -v ${PWD}/config/prometheus.yml:/etc/prometheus/prometheus.yml quay.io/prometheus/prometheus:v2.19.2 224 | level=info ts=2020-07-11T23:39:58.520Z caller=main.go:302 msg="No time or size retention was set so using the default time retention" duration=15d 225 | level=info ts=2020-07-11T23:39:58.520Z caller=main.go:337 msg="Starting Prometheus" version="(version=2.19.2, branch=HEAD, revision=c448ada63d83002e9c1d2c9f84e09f55a61f0ff7)" 226 | level=info ts=2020-07-11T23:39:58.520Z caller=main.go:338 build_context="(go=go1.14.4, user=root@dd72efe1549d, date=20200626-09:02:20)" 227 | level=info ts=2020-07-11T23:39:58.520Z caller=main.go:339 host_details="(Linux 5.3.18-050318-generic #201912181133 SMP Wed Dec 18 16:36:09 UTC 2019 x86_64 alejandrox1-N501VW (none))" 228 | level=info ts=2020-07-11T23:39:58.520Z caller=main.go:340 fd_limits="(soft=1048576, hard=1048576)" 229 | level=info ts=2020-07-11T23:39:58.520Z caller=main.go:341 vm_limits="(soft=unlimited, hard=unlimited)" 230 | level=info ts=2020-07-11T23:39:58.521Z caller=main.go:678 msg="Starting TSDB ..." 231 | level=info ts=2020-07-11T23:39:58.521Z caller=web.go:524 component=web msg="Start listening for connections" address=0.0.0.0:9090 232 | level=info ts=2020-07-11T23:39:58.525Z caller=head.go:645 component=tsdb msg="Replaying WAL and on-disk memory mappable chunks if any, this may take a while" 233 | level=info ts=2020-07-11T23:39:58.525Z caller=head.go:706 component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0 234 | level=info ts=2020-07-11T23:39:58.525Z caller=head.go:709 component=tsdb msg="WAL replay completed" duration=338.231µs 235 | level=info ts=2020-07-11T23:39:58.526Z caller=main.go:694 fs_type=EXT4_SUPER_MAGIC 236 | level=info ts=2020-07-11T23:39:58.526Z caller=main.go:695 msg="TSDB started" 237 | level=info ts=2020-07-11T23:39:58.526Z caller=main.go:799 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml 238 | level=info ts=2020-07-11T23:39:58.527Z caller=main.go:827 msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml 239 | level=info ts=2020-07-11T23:39:58.527Z caller=main.go:646 msg="Server is ready to receive web requests." 240 | ``` 241 | 242 | Couple things to notice: 243 | * We are running Prometheus using the host Docker network (`--network host`). 244 | This means that Prometheus will be available to us in http:/localhost:9090 245 | and that Prometheus will be able to scrape metrics from our server (which is 246 | using port 8080). If we were running Prometheus with something like `-p 247 | 9090:9090` then we would be able to see Prometheus in http://localhost:9090 248 | but Prometheus would not be able to scrape metrics from our app. 249 | * We are mounting [config/prometheus.yml](./config/prometheus.yml) as a volume 250 | into the container. We are putting this file in 251 | `/etc/prometheus/prometheus.yml` inside of the contiainer, which is the 252 | default location for Prometheus to look for its configuration. For details 253 | about the Promtheus' configuration see 254 | https://github.com/prometheus/prometheus/blob/master/docs/getting_started.md. 255 | 256 | If you now go to http://localhost:9090 and execute the Prometheus query 257 | ``` 258 | http_responses_total 259 | ``` 260 | 261 | You will see the following 262 | ![](./images/prom-table.png) 263 | 264 | You will see our metric, with its current value of 6 and its labels (the 265 | `job_name` is specified by the Prometheus configuration file 266 | [config/prometheus.yml](./config/prometheus.yml)). 267 | 268 | A cool thing to do would be to click on "Graph", under our query and see the 269 | value of our metric over time. 270 | After doing a couple more `curl http://localhost:8080/` we had the following 271 | 272 | ![](./images/prom-graph.png) 273 | 274 | One more note on our Prometheus query. 275 | Right now, we have been visualizing the total number of http responses by using 276 | ``` 277 | http_responses_total 278 | ``` 279 | 280 | as the query. 281 | However, assuming we had a more complex server and we had some requests that 282 | ended up in more than 200 status code, we could further focus on them by using 283 | queries such as 284 | ``` 285 | http_responses_total{code="200"} 286 | ``` 287 | 288 | to see all responses that had a 200 status code. 289 | If we had multiple instances of our server, we could also visualize the 290 | responses per instance by using 291 | ``` 292 | http_responses_total{instance="localhost:8080"} 293 | ``` 294 | 295 | And if we wanted to see all the responses with a specific status code (i.e., 296 | 200) from a specific host 297 | ``` 298 | http_responses_total{code="200", instance="localhost:8080"} 299 | ``` 300 | 301 | ## References 302 | 303 | Some good reading material that will give you more context: 304 | * [Prometheus Documentation: Best Practices - INSTRUMENTATION](https://prometheus.io/docs/practices/instrumentation/) 305 | * [Prometheus metrics / OpenMetrics code instrumentation](https://sysdig.com/blog/prometheus-metrics/) 306 | * [Prometheus Blog Series (Part 4): Instrumenting code in Go and Java](https://blog.pvincent.io/2017/12/prometheus-blog-series-part-4-instrumenting-code-in-go-and-java/) 307 | -------------------------------------------------------------------------------- /lesson-002-prometheus-metrics/go.sum: -------------------------------------------------------------------------------- 1 | cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 2 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 3 | github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= 4 | github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= 5 | github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= 6 | github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= 7 | github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= 8 | github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0= 9 | github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= 10 | github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= 11 | github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= 12 | github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= 13 | github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= 14 | github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= 15 | github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= 16 | github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= 17 | github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= 18 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 19 | github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= 20 | github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= 21 | github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= 22 | github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= 23 | github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= 24 | github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= 25 | github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= 26 | github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= 27 | github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= 28 | github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= 29 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= 30 | github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= 31 | github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= 32 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 33 | github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= 34 | github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 35 | github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= 36 | github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= 37 | github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= 38 | github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= 39 | github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= 40 | github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= 41 | github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= 42 | github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= 43 | github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= 44 | github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= 45 | github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= 46 | github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= 47 | github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= 48 | github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= 49 | github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= 50 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 51 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 52 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 53 | github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= 54 | github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= 55 | github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= 56 | github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= 57 | github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= 58 | github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= 59 | github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= 60 | github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= 61 | github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 62 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 63 | github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= 64 | github.com/prometheus/client_golang v0.9.3 h1:9iH4JKXLzFbOAdtqv/a+j8aewx2Y8lAjAydhbaScPF8= 65 | github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= 66 | github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= 67 | github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx/RA8rT8tKFRuGUZhuA90OyIBpPCXkcbwU8DE= 68 | github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= 69 | github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= 70 | github.com/prometheus/common v0.4.0 h1:7etb9YClo3a6HjLzfl6rIQaU+FDfi0VSX39io3aQ+DM= 71 | github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= 72 | github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= 73 | github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084 h1:sofwID9zm4tzrgykg80hfFph1mryUeLRsUfoocVVmRY= 74 | github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= 75 | github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= 76 | github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= 77 | github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 78 | github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= 79 | github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= 80 | github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= 81 | github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= 82 | github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= 83 | github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= 84 | github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8= 85 | github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= 86 | github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= 87 | github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= 88 | github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= 89 | github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE= 90 | github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 91 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 92 | github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= 93 | github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= 94 | github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= 95 | github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= 96 | go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= 97 | go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= 98 | go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= 99 | go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= 100 | golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 101 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 102 | golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= 103 | golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= 104 | golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 105 | golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 106 | golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 107 | golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 108 | golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= 109 | golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= 110 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 111 | golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 112 | golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 113 | golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 114 | golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 115 | golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 116 | golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 117 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 118 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 119 | golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= 120 | golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 121 | golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 122 | golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= 123 | google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= 124 | google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= 125 | google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= 126 | google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= 127 | gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= 128 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 129 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 130 | gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= 131 | gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= 132 | gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 133 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 134 | honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 135 | --------------------------------------------------------------------------------