/
main.go
39 lines (33 loc) · 1.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"log"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/ptone/promiot"
)
// to run, set the environment variables like:
// GCLOUD_PROJECT=ptone-serverless METRICS_SUBSCRIPTION=metric-pull go run main.go
func main() {
project := os.Getenv("GCLOUD_PROJECT")
if project == "" {
log.Fatal("GCLOUD_PROJECT env var must be set")
}
subscription := os.Getenv("METRICS_SUBSCRIPTION")
if subscription == "" {
log.Fatal("METRICS_SUBSCRIPTION env var must be set")
}
// A receiver receives encoded metrics on PubSub topic, and unpacks them into a for ready to be gathered on scrape
receiver := promiot.NewPromiotReceiver(project, subscription)
// The receiver is added to the array of default gatherers
prometheus.DefaultGatherer = prometheus.Gatherers{
prometheus.DefaultGatherer,
receiver,
}
// start pulling messages from the pubsub topic
go receiver.Receive()
// expose the collected metrics to be scraped by prometheus
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":8080", nil))
}