This repository has been archived by the owner on Aug 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
108 lines (97 loc) · 2.67 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"encoding/json"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
type APIResponse struct {
Data Data `json:"data"`
}
type Data struct {
Id string `json:"id"`
Name string `json:"name"`
Username string `json:"username"`
PublicMetrics PublicMetrics `json:"public_metrics"`
}
type PublicMetrics struct {
FollowersCount int `json:"followers_count"`
FollowingCount int `json:"following_count"`
TweetCount int `json:"tweet_count"`
ListedCount int `json:"listed_count"`
}
var (
twitterFollowers = promauto.NewGauge(prometheus.GaugeOpts{
Name: "twitter_followers",
Help: "Total number of twitter followers",
})
twitterFollowing = promauto.NewGauge(prometheus.GaugeOpts{
Name: "twitter_following",
Help: "Total number of accounts followed",
})
tweetCount = promauto.NewGauge(prometheus.GaugeOpts{
Name: "tweet_count",
Help: "Total number of tweets",
})
listedCount = promauto.NewGauge(prometheus.GaugeOpts{
Name: "listed_count",
Help: "Number of list the user belongs to",
})
)
func recordMetrics() {
go func() {
for {
err, body := getTwitterData()
if err != nil {
panic(err)
}
metrics := getPublicMetrics(body)
twitterFollowers.Set(float64(metrics.FollowersCount))
twitterFollowing.Set(float64(metrics.FollowingCount))
tweetCount.Set(float64(metrics.TweetCount))
listedCount.Set(float64(metrics.ListedCount))
fmt.Println(metrics.FollowingCount)
time.Sleep(10 * time.Second)
}
}()
}
func main() {
recordMetrics()
http.Handle("/metrics", promhttp.Handler())
_ = http.ListenAndServe(":8080", nil)
}
func getTwitterData() (error, []byte) {
var TwitterToken = os.Getenv("TWITTER_TOKEN")
var TwitterHandle = os.Getenv("TWITTER_HANDLE")
if TwitterToken == "" || TwitterHandle == "" {
log.Println("Please define TWITTER_TOKEN and the TWITTER_HANDLE environment variable")
os.Exit(100)
}
client := &http.Client{}
url := "https://api.twitter.com/2/users/by/username/" + TwitterHandle + "?user.fields=public_metrics"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err)
}
req.Header.Set("Authorization", "Bearer "+TwitterToken)
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
return err, body
}
func getPublicMetrics(apiResponse []byte) PublicMetrics {
var response APIResponse
if err := json.Unmarshal(apiResponse, &response); err != nil {
log.Println(err)
}
return response.Data.PublicMetrics
}