twitter-prometheus/main.go

74 lines
1.9 KiB
Go
Raw Normal View History

2020-01-02 18:05:42 +00:00
package main
import (
"context"
"encoding/json"
2020-08-02 16:16:40 +01:00
"flag"
2020-01-02 18:05:42 +00:00
"fmt"
"net/http"
"net/url"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
func probeTwitter(ctx context.Context, target string, registry *prometheus.Registry) (success bool) {
followersGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "twitter_followers",
Help: "The number of followers of the twitter account.",
},
[]string{"screen_name"})
registry.MustRegister(followersGauge)
res, err := http.Get(fmt.Sprintf("https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=%s", url.QueryEscape(target)))
if err != nil {
log.Errorf("fetching url: %s", err)
return false
}
data := []struct {
FollowersCount int `json:"followers_count"`
}{}
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
log.Errorf("decoding json: %s", err)
return false
}
followersGauge.With(prometheus.Labels{"screen_name": target}).Set(float64(data[0].FollowersCount))
return true
}
func twitterHandler(w http.ResponseWriter, r *http.Request) {
username := r.URL.Query().Get("username")
if username == "" {
http.Error(w, "username parameter is missing", http.StatusBadRequest)
return
}
reg := prometheus.NewRegistry()
ctx, cancel := context.WithTimeout(r.Context(), time.Duration(5*time.Second))
defer cancel()
if success := probeTwitter(ctx, username, reg); !success {
log.Error("Probe failed!")
}
h := promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
func main() {
2020-08-02 16:16:40 +01:00
var listenAddr string
flag.StringVar(&listenAddr, "listen", "0.0.0.0:9700", "Address to listen on")
flag.Parse()
2020-01-02 18:05:42 +00:00
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/probe", func(w http.ResponseWriter, r *http.Request) {
twitterHandler(w, r)
})
2020-08-02 16:16:40 +01:00
log.Infof("Listening on %s", listenAddr)
http.ListenAndServe(listenAddr, nil)
2020-01-02 18:05:42 +00:00
}