Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add application monitoring #8

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.13 as build
FROM golang:1.14 as build
WORKDIR /go/src/app
COPY . .
RUN make
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ all: build

.PHONY: build
build:
CGO_ENABLED=0 go build
CGO_ENABLED=0 GO111MODULE=on go build

.PHONY: image
image:
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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```

2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/argoproj/rollouts-demo

go 1.12

require github.com/prometheus/client_golang v0.9.2
61 changes: 59 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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{
Expand All @@ -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
Expand All @@ -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,
Expand Down