package main
import (
"flag"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var addr = flag.String("address", ":9000", "The address to listen on for HTTP requests.")
var rpcDurations = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Name: "rpc_durations_seconds",
Help: "RPC latency distributions.",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
},
[]string{"service"},
)
var up = prometheus.NewDesc(
"ldap_up",
"watch ladap info",
[]string{"name", "class"},
nil,
)
var rpc = prometheus.NewDesc(
"rpc_request_total",
"rpc request total",
[]string{"rpc_func", "rpc_source"},
nil,
)
var version = prometheus.MustNewConstMetric(
prometheus.NewDesc("ldap_exporter_version", "ldap exporter version", []string{"type", "build"}, nil),
prometheus.GaugeValue,
1.0,
"cust", "2020-09-08",
)
// MyLDAPCollector myLDAPCollector
type MyLDAPCollector struct {
up *prometheus.Desc
rpc *prometheus.Desc
version *prometheus.Desc
}
// Describe describe
func (c MyLDAPCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.up
ch <- c.rpc
ch <- c.version
}
// Collect collect
func (c MyLDAPCollector) Collect(ch chan<- prometheus.Metric) {
upMetrics := prometheus.MustNewConstMetric(up, prometheus.GaugeValue, 1, "dalong", "demo")
rpcRequest := prometheus.MustNewConstMetric(rpc, prometheus.CounterValue, 1000, "login", "a")
ch <- version
ch <- upMetrics
ch <- rpcRequest
}
func init() {
myLDAPCollector := MyLDAPCollector{
up: up,
rpc: rpc,
version: version.Desc(),
}
// Add MyCus Collector
prometheus.MustRegister(myLDAPCollector)
// Add rpc summary collector
prometheus.MustRegister(rpcDurations)
// Add Go module build info.
// prometheus.MustRegister(prometheus.NewBuildInfoCollector())
}
func main() {
flag.Parse()
e := gin.Default()
e.GET("/api", func(c *gin.Context) {
rpcDurations.WithLabelValues("mydemo").Observe(22)
c.String(200, "dalongdemo")
})
e.GET("/", func(c *gin.Context) {
indexpage := `<html>
<body>
<h1>ldap exporter</h1>
<p><a href="/metrics">metrics</a></p>
</body>
</html>`
c.Writer.Write([]byte(indexpage))
})
e.GET("/metrics", func(ctx *gin.Context) {
// promhttp.Handler()
// gin Handler do not support promhttp.Handler
promhttp.Handler().ServeHTTP(ctx.Writer, ctx.Request)
// promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{})).ServeHTTP(ctx.Writer, ctx.Request)
})
e.Run(*addr)
}