From 24a1e044fb1be02b482a69f51660c8a4e759a9f7 Mon Sep 17 00:00:00 2001 From: Ian Off Date: Thu, 12 Dec 2024 15:35:54 -0600 Subject: [PATCH] Skeleton collector with Prometheus endpoint 5/6 --- cmd/collector/collector_test.go | 21 ++++++++ cmd/collector/go.mod | 16 ++++++ cmd/collector/go.sum | 20 ++++++++ cmd/collector/main.go | 29 +++++++++++ docs/devlog.md | 91 +++++++++++++++++++++++++++++++++ 5 files changed, 177 insertions(+) create mode 100644 cmd/collector/collector_test.go create mode 100644 cmd/collector/go.mod create mode 100644 cmd/collector/go.sum create mode 100644 cmd/collector/main.go create mode 100644 docs/devlog.md diff --git a/cmd/collector/collector_test.go b/cmd/collector/collector_test.go new file mode 100644 index 0000000..3450e42 --- /dev/null +++ b/cmd/collector/collector_test.go @@ -0,0 +1,21 @@ +package collector + +import ( + "fmt" + "strings" + "testing" + "time" + + "github.com/prometheus/client_golang/prometheus/testutil" + "github.com/stretchr/testify/require" +) + +// TODO +func TestMetricsUp() { + // testutil.ScrapeAndCompare() + // "http://localhost:2112/metrics" + // # HELP perfpod_memory_collector_up_metric Test metric to confirm skeleton application functionality. + // # TYPE perfpod_memory_collector_up_metric gauge + // perfpod_memory_collector_up_metric 1 +} + diff --git a/cmd/collector/go.mod b/cmd/collector/go.mod new file mode 100644 index 0000000..5384a4d --- /dev/null +++ b/cmd/collector/go.mod @@ -0,0 +1,16 @@ +module memory-collector + +go 1.22.9 + +require ( + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/klauspost/compress v1.17.9 // indirect + github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/prometheus/client_golang v1.20.5 // indirect + github.com/prometheus/client_model v0.6.1 // indirect + github.com/prometheus/common v0.55.0 // indirect + github.com/prometheus/procfs v0.15.1 // indirect + golang.org/x/sys v0.22.0 // indirect + google.golang.org/protobuf v1.34.2 // indirect +) diff --git a/cmd/collector/go.sum b/cmd/collector/go.sum new file mode 100644 index 0000000..49cf790 --- /dev/null +++ b/cmd/collector/go.sum @@ -0,0 +1,20 @@ +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/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= +github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= +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/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y= +github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE= +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= +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= diff --git a/cmd/collector/main.go b/cmd/collector/main.go new file mode 100644 index 0000000..2772ae9 --- /dev/null +++ b/cmd/collector/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "net/http" + + "github.com/prometheus/client_golang/prometheus" + // "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/prometheus/client_golang/prometheus/promhttp" +) + +func recordMetrics() { + upMetric := prometheus.NewGauge(prometheus.GaugeOpts{ + Namespace: "perfpod", + Subsystem: "memory_collector", + Name: "up_metric", + Help: "Test metric to confirm skeleton application functionality.", + }) + prometheus.MustRegister(upMetric) + + upMetric.Set(1) +} + +func main() { + recordMetrics() + + http.Handle("/metrics", promhttp.Handler()) + http.ListenAndServe(":2112", nil) +} + diff --git a/docs/devlog.md b/docs/devlog.md new file mode 100644 index 0000000..dcb4348 --- /dev/null +++ b/docs/devlog.md @@ -0,0 +1,91 @@ +Documentation of development steps, environment, and dependencies + +Contributors: atimeofday; +Goals: Create skeleton collector with Prometheus endpoint; +Issues: https://github.com/perfpod/memory-collector/issues/19 + +Initial environment and tools: +``` +# Shell: Bash +distrobox create --image fedora:40 --name memory-collector +distrobox enter memory-collector +sudo dnf install git go + +# cd to preferred project directory +# Clone (fork of) project +git clone https://github.com/perfpod/memory-collector +cd memory-collector +``` + +Issue 19 objective 1: Create a `main.go` file in `cmd/collector` +``` +mkdir -p cmd/collector +cd cmd/collector +touch main.go +``` + +- Prometheus client_golang reference guide: https://prometheus.io/docs/guides/go-application/ +- Go package installation reference: https://go.dev/doc/go-get-install-deprecation +- Go Module reference: https://go.dev/ref/mod#go-mod-init +- `go get` and `go install` require a Go Module and/or @version tag as of Go 1.17 in August 2021 +- Prometheus go_client installation instructions appear to be outdated and missing a piece +- Submitted issue to Prometheus documentation repository: https://github.com/prometheus/docs/issues/2556#issue-2736636166 +- Proceeded with Prometheus client_golang guide +``` +cd cmd/collector +go mod init memory-collector +go get github.com/prometheus/client_golang/prometheus +go get github.com/prometheus/client_golang/prometheus/promauto +go get github.com/prometheus/client_golang/prometheus/promhttp +``` + +Issue 19 objective 2: Expose an endpoint on a known fixed port +``` +# Wrote and tested example Go exposition application from Prometheus guide +go run main.go & +curl http://localhost:2112/metrics +``` + +Issue 19 objective 3: Expose the `up` metric with value 1 +``` +# Created, registered, and set an 'up' metric in func main() +upMetric := prometheus.NewGauge(prometheus.GaugeOpts{ + Namespace: "perfpod", + Subsystem: "memory_collector", + Name: "up_metric", + Help: "Test metric to confirm skeleton application functionality.", +}) +prometheus.MustRegister(upMetric) + +upMetric.Set(1) +``` + +Issue 19 objective 4: Manually verify: query the endpoint using `curl` or `wget` +``` +curl -s http://localhost:2112/metrics | grep up_metric +``` +Output: +``` +# HELP perfpod_memory_collector_up_metric Test metric to confirm skeleton application functionality. +# TYPE perfpod_memory_collector_up_metric gauge +perfpod_memory_collector_up_metric 1 +``` + +Issue 19 objective 5: Move the code into a function (not `main()`) +``` +# Moved Up metric into "func recordMetrics()" and added function call in main() + +func main() { + recordMetrics() + + http.Handle("/metrics", promhttp.Handler()) + http.ListenAndServe(":2112", nil) +} + +# Repeated manual verification endpoint query +``` + +Issue 19 objective 6: Add an integration test that verifies the metrics are up, using client_golang's testutil +- TO DO +- May require assistance +