From 9c26599ebfd711d0a328d0369bb806a02e7b4cbb Mon Sep 17 00:00:00 2001 From: Ariel Simhon Date: Wed, 22 Jul 2020 15:00:52 +0300 Subject: [PATCH 01/11] add monitoring --- Dockerfile | 2 +- Makefile | 2 +- go.mod | 2 ++ main.go | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++--- release.sh | 2 ++ 5 files changed, 80 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 518bc7d9c..0d4015149 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.13 as build +FROM golang:1.14 as build WORKDIR /go/src/app COPY . . RUN make diff --git a/Makefile b/Makefile index b2abc1f64..6fceb62ea 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ all: build .PHONY: build build: - CGO_ENABLED=0 go build + CGO_ENABLED=0 GO111MODULE=on go build .PHONY: image image: diff --git a/go.mod b/go.mod index 7ed11cb86..e66b716e4 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/argoproj/rollouts-demo go 1.12 + +require github.com/prometheus/client_golang v0.9.2 diff --git a/main.go b/main.go index 31a85926b..7a0444469 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,10 @@ import ( "strconv" "syscall" "time" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/prometheus/client_golang/prometheus/promhttp" ) const ( @@ -27,6 +31,15 @@ const ( defaultTerminationDelay = 10 ) +func recordMetrics() { + go func() { + for { + opsProcessed.Inc() + time.Sleep(2 * time.Second) + } + }() +} + var ( color = os.Getenv("COLOR") colors = []string{ @@ -39,15 +52,56 @@ var ( } envErrorRate = os.Getenv("ERROR_RATE") envLatency = os.Getenv("LATENCY") + + opsProcessed = promauto.NewCounter(prometheus.CounterOpts{ + Name: "myapp_processed_ops_total", + Help: "The total number of processed events", + }) + + counter = prometheus.NewCounter( + prometheus.CounterOpts{ + Namespace: "golang", + Name: "my_counter", + Help: "This is my counter", + }) + + gauge = prometheus.NewGauge( + prometheus.GaugeOpts{ + Namespace: "golang", + Name: "my_gauge", + Help: "This is my gauge", + }) + + histogram = prometheus.NewHistogram( + prometheus.HistogramOpts{ + Namespace: "golang", + Name: "my_histogram", + Help: "This is my histogram", + }) + + summary = prometheus.NewSummary( + prometheus.SummaryOpts{ + Namespace: "golang", + Name: "my_summary", + Help: "This is my summary", + }) + ) func main() { + + prometheus.MustRegister(counter) + prometheus.MustRegister(gauge) + prometheus.MustRegister(histogram) + prometheus.MustRegister(summary) + + recordMetrics() var ( listenAddr string terminationDelay int numCPUBurn string ) - flag.StringVar(&listenAddr, "listen-addr", ":8080", "server listen address") + flag.StringVar(&listenAddr, "listen-addr", ":8443", "server listen address") flag.IntVar(&terminationDelay, "termination-delay", defaultTerminationDelay, "termination delay in seconds") flag.StringVar(&numCPUBurn, "cpu-burn", "", "burn specified number of cpus (number or 'all')") flag.Parse() @@ -55,8 +109,17 @@ func main() { rand.Seed(time.Now().UnixNano()) router := http.NewServeMux() - router.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("./")))) - router.HandleFunc("/color", getColor) + router.Handle("/metrics", promhttp.Handler()) + + //router.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("./")))) + //router.HandleFunc("/color", getColor) + + router.Handle("/", prometheus.InstrumentHandler( + "fileserver", http.FileServer(http.Dir("./")), + )) + router.HandleFunc("/color", prometheus.InstrumentHandlerFunc( + "/color", getColor, + )) server := &http.Server{ Addr: listenAddr, @@ -68,6 +131,14 @@ func main() { signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) go func() { + for { + counter.Add(rand.Float64() * 5) + gauge.Add(rand.Float64()*15 - 5) + histogram.Observe(rand.Float64() * 10) + summary.Observe(rand.Float64() * 10) + + time.Sleep(time.Second) + } sig := <-quit server.SetKeepAlivesEnabled(false) log.Printf("Signal %v caught. Shutting down in %vs", sig, terminationDelay) diff --git a/release.sh b/release.sh index 4b383343a..df2a06b6a 100755 --- a/release.sh +++ b/release.sh @@ -1,5 +1,7 @@ #!/bin/bash +IMAGE_NAMESPACE=docker.artifactory.a.intuit.com/sandbox/sandbox/asimhon-jul-02/service +DOCKER_PUSH=true set -x -e strings=( From 80b999f931fb13797b7fff4b52fc9afe6a631ede Mon Sep 17 00:00:00 2001 From: Ariel Simhon Date: Wed, 22 Jul 2020 15:01:28 +0300 Subject: [PATCH 02/11] fix handler name --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 7a0444469..be0f01c50 100644 --- a/main.go +++ b/main.go @@ -115,7 +115,7 @@ func main() { //router.HandleFunc("/color", getColor) router.Handle("/", prometheus.InstrumentHandler( - "fileserver", http.FileServer(http.Dir("./")), + "/", http.FileServer(http.Dir("./")), )) router.HandleFunc("/color", prometheus.InstrumentHandlerFunc( "/color", getColor, From bcf46d57b1f4b4e3e94aa58a0177f398ee697926 Mon Sep 17 00:00:00 2001 From: Ariel Simhon Date: Wed, 22 Jul 2020 15:03:26 +0300 Subject: [PATCH 03/11] fix ident --- main.go | 111 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 55 insertions(+), 56 deletions(-) diff --git a/main.go b/main.go index be0f01c50..aa655f417 100644 --- a/main.go +++ b/main.go @@ -16,9 +16,9 @@ import ( "syscall" "time" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promauto" - "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/prometheus/client_golang/prometheus/promhttp" ) const ( @@ -32,12 +32,12 @@ const ( ) func recordMetrics() { - go func() { - for { - opsProcessed.Inc() - time.Sleep(2 * time.Second) - } - }() + go func() { + for { + opsProcessed.Inc() + time.Sleep(2 * time.Second) + } + }() } var ( @@ -53,49 +53,48 @@ var ( envErrorRate = os.Getenv("ERROR_RATE") envLatency = os.Getenv("LATENCY") - opsProcessed = promauto.NewCounter(prometheus.CounterOpts{ - Name: "myapp_processed_ops_total", - Help: "The total number of processed events", - }) - - counter = prometheus.NewCounter( - prometheus.CounterOpts{ - Namespace: "golang", - Name: "my_counter", - Help: "This is my counter", - }) - - gauge = prometheus.NewGauge( - prometheus.GaugeOpts{ - Namespace: "golang", - Name: "my_gauge", - Help: "This is my gauge", - }) - - histogram = prometheus.NewHistogram( - prometheus.HistogramOpts{ - Namespace: "golang", - Name: "my_histogram", - Help: "This is my histogram", - }) - - summary = prometheus.NewSummary( - prometheus.SummaryOpts{ - Namespace: "golang", - Name: "my_summary", - Help: "This is my summary", - }) - + opsProcessed = promauto.NewCounter(prometheus.CounterOpts{ + Name: "myapp_processed_ops_total", + Help: "The total number of processed events", + }) + + counter = prometheus.NewCounter( + prometheus.CounterOpts{ + Namespace: "golang", + Name: "my_counter", + Help: "This is my counter", + }) + + gauge = prometheus.NewGauge( + prometheus.GaugeOpts{ + Namespace: "golang", + Name: "my_gauge", + Help: "This is my gauge", + }) + + histogram = prometheus.NewHistogram( + prometheus.HistogramOpts{ + Namespace: "golang", + Name: "my_histogram", + Help: "This is my histogram", + }) + + summary = prometheus.NewSummary( + prometheus.SummaryOpts{ + Namespace: "golang", + Name: "my_summary", + Help: "This is my summary", + }) ) func main() { - prometheus.MustRegister(counter) - prometheus.MustRegister(gauge) - prometheus.MustRegister(histogram) - prometheus.MustRegister(summary) + prometheus.MustRegister(counter) + prometheus.MustRegister(gauge) + prometheus.MustRegister(histogram) + prometheus.MustRegister(summary) - recordMetrics() + recordMetrics() var ( listenAddr string terminationDelay int @@ -109,7 +108,7 @@ func main() { rand.Seed(time.Now().UnixNano()) router := http.NewServeMux() - router.Handle("/metrics", promhttp.Handler()) + router.Handle("/metrics", promhttp.Handler()) //router.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("./")))) //router.HandleFunc("/color", getColor) @@ -131,14 +130,14 @@ func main() { signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) go func() { - for { - counter.Add(rand.Float64() * 5) - gauge.Add(rand.Float64()*15 - 5) - histogram.Observe(rand.Float64() * 10) - summary.Observe(rand.Float64() * 10) - - time.Sleep(time.Second) - } + for { + counter.Add(rand.Float64() * 5) + gauge.Add(rand.Float64()*15 - 5) + histogram.Observe(rand.Float64() * 10) + summary.Observe(rand.Float64() * 10) + + time.Sleep(time.Second) + } sig := <-quit server.SetKeepAlivesEnabled(false) log.Printf("Signal %v caught. Shutting down in %vs", sig, terminationDelay) From 3b0cb6b487b154247e978ec543f1a0a2af5bf523 Mon Sep 17 00:00:00 2001 From: Ariel Simhon Date: Wed, 22 Jul 2020 17:12:04 +0300 Subject: [PATCH 04/11] update port --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index aa655f417..d7bf96986 100644 --- a/main.go +++ b/main.go @@ -100,7 +100,7 @@ func main() { terminationDelay int numCPUBurn string ) - flag.StringVar(&listenAddr, "listen-addr", ":8443", "server listen address") + flag.StringVar(&listenAddr, "listen-addr", ":8080", "server listen address") flag.IntVar(&terminationDelay, "termination-delay", defaultTerminationDelay, "termination delay in seconds") flag.StringVar(&numCPUBurn, "cpu-burn", "", "burn specified number of cpus (number or 'all')") flag.Parse() From 6387f3afeb2e701d8ea7b4b49a74213aafbdfcf1 Mon Sep 17 00:00:00 2001 From: Ariel Simhon Date: Wed, 22 Jul 2020 17:12:26 +0300 Subject: [PATCH 05/11] cleanup --- main.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/main.go b/main.go index d7bf96986..3309357ef 100644 --- a/main.go +++ b/main.go @@ -110,9 +110,6 @@ func main() { router := http.NewServeMux() router.Handle("/metrics", promhttp.Handler()) - //router.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("./")))) - //router.HandleFunc("/color", getColor) - router.Handle("/", prometheus.InstrumentHandler( "/", http.FileServer(http.Dir("./")), )) From 4c4e9432f870c4ee0a1f1dbfc8cd0ee3d89e7452 Mon Sep 17 00:00:00 2001 From: Ariel Simhon Date: Wed, 22 Jul 2020 17:13:03 +0300 Subject: [PATCH 06/11] cleanup releases --- release.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/release.sh b/release.sh index df2a06b6a..4b383343a 100755 --- a/release.sh +++ b/release.sh @@ -1,7 +1,5 @@ #!/bin/bash -IMAGE_NAMESPACE=docker.artifactory.a.intuit.com/sandbox/sandbox/asimhon-jul-02/service -DOCKER_PUSH=true set -x -e strings=( From 0cd92e90bc5ed8993a6353517ad965c805dad679 Mon Sep 17 00:00:00 2001 From: Ariel Simhon Date: Wed, 22 Jul 2020 17:21:12 +0300 Subject: [PATCH 07/11] add metrics in readme --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index ba7e5a080..4a72059bc 100644 --- a/README.md +++ b/README.md @@ -61,3 +61,8 @@ To release new images: ```bash make release IMAGE_NAMESPACE=argoproj DOCKER_PUSH=true ``` + +## Application Metrics + +To get application metrics: +```curl http://localhost:8080/metrics``` From 7a0d86e41245dd98c52266c84d345f376abe326c Mon Sep 17 00:00:00 2001 From: Ariel Simhon Date: Wed, 22 Jul 2020 17:26:04 +0300 Subject: [PATCH 08/11] fix SIGTERM issue --- main.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/main.go b/main.go index 3309357ef..43ad49c2a 100644 --- a/main.go +++ b/main.go @@ -127,14 +127,6 @@ func main() { signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) go func() { - for { - counter.Add(rand.Float64() * 5) - gauge.Add(rand.Float64()*15 - 5) - histogram.Observe(rand.Float64() * 10) - summary.Observe(rand.Float64() * 10) - - time.Sleep(time.Second) - } sig := <-quit server.SetKeepAlivesEnabled(false) log.Printf("Signal %v caught. Shutting down in %vs", sig, terminationDelay) From 60df07c0822f9b56e3a9f12a0bfc20adc78fb610 Mon Sep 17 00:00:00 2001 From: Ariel Simhon Date: Wed, 22 Jul 2020 17:26:53 +0300 Subject: [PATCH 09/11] update readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4a72059bc..04a350506 100644 --- a/README.md +++ b/README.md @@ -65,4 +65,5 @@ make release IMAGE_NAMESPACE=argoproj DOCKER_PUSH=true ## Application Metrics To get application metrics: + ```curl http://localhost:8080/metrics``` From e94a76a35962a370033903fcd844a779e61f70cd Mon Sep 17 00:00:00 2001 From: Ariel Simhon Date: Thu, 23 Jul 2020 09:23:38 +0300 Subject: [PATCH 10/11] white space --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 04a350506..e0e0d9591 100644 --- a/README.md +++ b/README.md @@ -67,3 +67,4 @@ make release IMAGE_NAMESPACE=argoproj DOCKER_PUSH=true To get application metrics: ```curl http://localhost:8080/metrics``` + From 1453070301ef167e33745b0f2ecfa3144b686b52 Mon Sep 17 00:00:00 2001 From: Ariel Simhon Date: Thu, 23 Jul 2020 09:27:26 +0300 Subject: [PATCH 11/11] cleanup --- main.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/main.go b/main.go index 43ad49c2a..95eaa3dd2 100644 --- a/main.go +++ b/main.go @@ -88,13 +88,12 @@ var ( ) func main() { - prometheus.MustRegister(counter) prometheus.MustRegister(gauge) prometheus.MustRegister(histogram) prometheus.MustRegister(summary) - recordMetrics() + var ( listenAddr string terminationDelay int @@ -109,7 +108,6 @@ func main() { router := http.NewServeMux() router.Handle("/metrics", promhttp.Handler()) - router.Handle("/", prometheus.InstrumentHandler( "/", http.FileServer(http.Dir("./")), ))