Created
August 8, 2023 05:11
-
-
Save Jeongseup/20651c558d5a331cd844ace51be42da6 to your computer and use it in GitHub Desktop.
prometheus async custom exporter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"time" | |
"github.com/prometheus/client_golang/prometheus" | |
"github.com/prometheus/client_golang/prometheus/promauto" | |
"github.com/prometheus/client_golang/prometheus/promhttp" | |
) | |
var ( | |
reg = prometheus.NewRegistry() | |
factory = promauto.With(reg) | |
requests = factory.NewGauge( | |
prometheus.GaugeOpts{ | |
Name: "hello_worlds_total", | |
Help: "Hello Worlds requested.", | |
}) | |
requestHealth = factory.NewGauge( | |
prometheus.GaugeOpts{ | |
Name: "hello_worlds_health", | |
Help: "Hello Worlds health checker", | |
}) | |
opsProcessed = factory.NewCounter(prometheus.CounterOpts{ | |
Name: "myapp_processed_ops_total", | |
Help: "The total number of processed events", | |
}) | |
) | |
func increaseHelloWorld() { | |
fmt.Println("call") | |
for { | |
fmt.Println("for loop") | |
timestamp := time.Now().Unix() | |
if timestamp%2 == 0 { | |
fmt.Println("It is an Even Number") | |
// data update in only timestamp is even | |
requests.Add(2.0) | |
requestHealth.Set(1) | |
} else { | |
fmt.Println("It is an Odd Number") | |
// data fail checker | |
requestHealth.Set(0) | |
} | |
// data ops checker | |
opsProcessed.Inc() | |
time.Sleep(3 * time.Second) | |
} | |
} | |
func handlerFunction(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("Hello World")) | |
} | |
func defaultFunction(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte(` | |
<html> | |
<head> | |
<title> | |
My Custom Node Exporter | |
</title> | |
</head> | |
<body> | |
<h1> | |
My Custom Node Exporter | |
</h1> | |
<p> | |
<a href="/metrics">Metrics</a> | |
<a href="/increase">Increase Start</a> | |
</p> | |
</body> | |
</html> | |
`)) | |
} | |
func main() { | |
handler := promhttp.HandlerFor(reg, promhttp.HandlerOpts{}) | |
http.HandleFunc("/increase", handlerFunction) | |
http.HandleFunc("/", defaultFunction) | |
http.Handle("/metrics", handler) | |
go increaseHelloWorld() | |
log.Fatal(http.ListenAndServe(":8000", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment