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/README.md b/README.md index ba7e5a080..e0e0d9591 100644 --- a/README.md +++ b/README.md @@ -61,3 +61,10 @@ To release new images: ```bash make release IMAGE_NAMESPACE=argoproj DOCKER_PUSH=true ``` + +## Application Metrics + +To get application metrics: + +```curl http://localhost:8080/metrics``` + 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..95eaa3dd2 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,9 +52,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", + }) ) func main() { + prometheus.MustRegister(counter) + prometheus.MustRegister(gauge) + prometheus.MustRegister(histogram) + prometheus.MustRegister(summary) + recordMetrics() + var ( listenAddr string terminationDelay int @@ -55,8 +107,13 @@ 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("/", prometheus.InstrumentHandler( + "/", http.FileServer(http.Dir("./")), + )) + router.HandleFunc("/color", prometheus.InstrumentHandlerFunc( + "/color", getColor, + )) server := &http.Server{ Addr: listenAddr,