Skip to content

Commit

Permalink
chore: put back deploy metrics because deploy-canary
Browse files Browse the repository at this point in the history
  • Loading branch information
rbjornstad committed Jul 10, 2024
1 parent e203f60 commit e9450a1
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 10 deletions.
38 changes: 35 additions & 3 deletions cmd/testapp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ import (
"io"
"net/http"
"os"
"strconv"
"time"

"github.com/nais/testapp/internal/metrics"
"github.com/prometheus/common/version"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
)

var (
bindAddr string
pingResponse string
connectURL string
bindAddr string
pingResponse string
connectURL string
deployStartTimestamp int64
)

func init() {
Expand All @@ -25,10 +28,25 @@ func init() {
flag.StringVar(&bindAddr, "bind-address", ":8080", "ip:port where http requests are served")
flag.StringVar(&pingResponse, "ping-response", "pong\n", "what to respond when pinged")
flag.StringVar(&connectURL, "connect-url", "https://google.com", "URL to connect to with /connect")
flag.Int64Var(&deployStartTimestamp, "deploy-start-time", getEnvInt("DEPLOY_START", time.Now().UnixNano()), "unix timestamp with nanoseconds, specifies when NAIS deploy of testapp started")
flag.Parse()
}

func main() {
metrics.StartTimestamp.SetToCurrentTime()
metrics.DeployTimestamp.Set(float64(deployStartTimestamp) / 10e8)

metrics.LeadTime.Set(timeSinceDeploy())
metrics.TimeSinceDeploy.Set(timeSinceDeploy())
tick := time.NewTicker(time.Second)
go func() {
for range tick.C {
metrics.TimeSinceDeploy.Set(timeSinceDeploy())
}
}()

http.Handle("/metrics", metrics.Handler())

http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
param := r.URL.Query().Get("delay")
if d, err := time.ParseDuration(param); err == nil {
Expand Down Expand Up @@ -92,3 +110,17 @@ func main() {
log.Fatal(err)
}
}

func getEnvInt(key string, fallback int64) int64 {
if value, ok := os.LookupEnv(key); ok {
i, _ := strconv.ParseInt(value, 10, 64)
return i
}

return fallback
}

func timeSinceDeploy() float64 {
deployStartTime := time.Unix(0, deployStartTimestamp)
return time.Since(deployStartTime).Seconds()
}
18 changes: 13 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
module github.com/nais/testapp

go 1.22

require (
github.com/prometheus/client_golang v1.19.1
github.com/prometheus/common v0.55.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/pflag v1.0.5
golang.org/x/sys v0.22.0 // indirect
)

require github.com/prometheus/common v0.55.0

require golang.org/x/sys v0.21.0 // indirect

go 1.19
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
22 changes: 20 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE=
github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc=
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
51 changes: 51 additions & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package metrics

import (
"net/http"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

const (
namespace = "nais"
subsystem = "testapp"
)

func gauge(name, help string) prometheus.Gauge {
return prometheus.NewGauge(prometheus.GaugeOpts{
Name: name,
Help: help,
Namespace: namespace,
Subsystem: subsystem,
})
}

func hist(name, help string, buckets []float64) prometheus.Histogram {
return prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
Help: help,
Buckets: buckets,
})
}

var (
LeadTime = gauge("lead_time", "Seconds used in deployment pipeline, from making the request until the application is available")
TimeSinceDeploy = gauge("time_since_deploy", "Seconds since the latest deploy of this application")

DeployTimestamp = gauge("deploy_timestamp", "Timestamp when the deploy of this application was triggered in the pipeline")
StartTimestamp = gauge("start_timestamp", "Start time of the application")
)

func init() {
prometheus.MustRegister(LeadTime)
prometheus.MustRegister(TimeSinceDeploy)
prometheus.MustRegister(DeployTimestamp)
prometheus.MustRegister(StartTimestamp)
}

func Handler() http.Handler {
return promhttp.Handler()
}

0 comments on commit e9450a1

Please sign in to comment.