-
-
Save moznion/20ad8797af22df6b1365c2fd6f5a8a74 to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"encoding/json" | |
"log" | |
"net/http" | |
"github.com/prometheus/client_golang/prometheus" | |
) | |
const namespace = "h2o" | |
type exporter struct { | |
scrapeFailures prometheus.Counter | |
uptime prometheus.Gauge | |
connections prometheus.Gauge | |
maxConnections prometheus.Gauge | |
listeners prometheus.Gauge | |
workerThreads prometheus.Gauge | |
} | |
type h2oServerStatus struct { | |
ServerVersion string `json:"server-version"` | |
OpensslVersion string `json:"openssl-version"` | |
CurrentTime string `json:"current-time"` | |
RestartTime string `json:"restart-time"` | |
Uptime int `json:"uptime"` | |
Generation interface{} `json:"generation"` | |
Connections int `json:"connections"` | |
MaxConnections int `json:"max-connections"` | |
Listeners int `json:"listeners"` | |
WorkerThreads int `json:"worker-threads"` | |
} | |
func newExporter() *exporter { | |
return &exporter{ | |
uptime: prometheus.NewGauge(prometheus.GaugeOpts{ | |
Namespace: namespace, | |
Name: "uptime", | |
Help: "uptime", | |
}), | |
connections: prometheus.NewGauge(prometheus.GaugeOpts{ | |
Namespace: namespace, | |
Name: "connections", | |
Help: "connections", | |
}), | |
maxConnections: prometheus.NewGauge(prometheus.GaugeOpts{ | |
Namespace: namespace, | |
Name: "max_connections", | |
Help: "max_connections", | |
}), | |
listeners: prometheus.NewGauge(prometheus.GaugeOpts{ | |
Namespace: namespace, | |
Name: "listeners", | |
Help: "listeners", | |
}), | |
workerThreads: prometheus.NewGauge(prometheus.GaugeOpts{ | |
Namespace: namespace, | |
Name: "worker_threads", | |
Help: "worker_threads", | |
}), | |
scrapeFailures: prometheus.NewCounter(prometheus.CounterOpts{ | |
Namespace: namespace, | |
Name: "exporter_scrape_failures_total", | |
Help: "Number of errors while scraping.", | |
}), | |
} | |
} | |
func (e *exporter) Describe(ch chan<- *prometheus.Desc) { | |
e.scrapeFailures.Describe(ch) | |
e.uptime.Describe(ch) | |
e.connections.Describe(ch) | |
e.maxConnections.Describe(ch) | |
e.listeners.Describe(ch) | |
e.workerThreads.Describe(ch) | |
} | |
func (e *exporter) Collect(ch chan<- prometheus.Metric) { | |
resp, _ := http.Get("http://127.0.0.1:7777/server-status/json") | |
defer resp.Body.Close() | |
var status h2oServerStatus | |
dec := json.NewDecoder(resp.Body) | |
dec.Decode(&status) | |
e.uptime.Set(float64(status.Uptime)) | |
e.connections.Set(float64(status.Connections)) | |
e.maxConnections.Set(float64(status.MaxConnections)) | |
e.listeners.Set(float64(status.Listeners)) | |
e.workerThreads.Set(float64(status.WorkerThreads)) | |
e.uptime.Collect(ch) | |
e.connections.Collect(ch) | |
e.maxConnections.Collect(ch) | |
e.listeners.Collect(ch) | |
e.workerThreads.Collect(ch) | |
} | |
func main() { | |
exporter := newExporter() | |
prometheus.MustRegister(exporter) | |
http.Handle("/metrics", prometheus.Handler()) | |
port := ":9999" | |
log.Print("Listening 127.0.0.1", port) | |
log.Fatal(http.ListenAndServe(port, nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
これはgithub.comの方に公開する予定はありませんか?