-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
252 lines (235 loc) · 7.87 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"log"
"math"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/google/go-github/v69/github"
)
type Config struct {
Bucket string `json:"Bucket"`
InfluxDBHost string `json:"InfluxDBHost"`
InfluxDBApiToken string `json:"InfluxDBApiToken"`
Org string `json:"Org"`
GithubApiToken string `json:"GithubApiToken"`
}
type retryableTransport struct {
transport http.RoundTripper
TLSHandshakeTimeout time.Duration
ResponseHeaderTimeout time.Duration
}
const retryCount = 3
func shouldRetry(err error, resp *http.Response) bool {
if err != nil {
return true
}
if resp == nil {
return true
}
switch resp.StatusCode {
case http.StatusInternalServerError, http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout:
return true
default:
return false
}
}
func (t *retryableTransport) RoundTrip(req *http.Request) (*http.Response, error) {
var bodyBytes []byte
if req.Body != nil {
bodyBytes, _ = io.ReadAll(req.Body)
req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
}
resp, err := t.transport.RoundTrip(req)
retries := 0
for shouldRetry(err, resp) && retries < retryCount {
backoff := time.Duration(math.Pow(2, float64(retries))) * time.Second
time.Sleep(backoff)
if resp != nil && resp.Body != nil {
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}
if req.Body != nil {
req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
}
if resp != nil && resp.Status != "" {
log.Printf("Previous request failed with %s", resp.Status)
}
log.Printf("Retry %d of request to: %s", retries+1, req.URL)
resp, err = t.transport.RoundTrip(req)
retries++
}
return resp, err
}
func main() {
confFilePath := "github_exporter.json"
confData, err := os.Open(confFilePath)
if err != nil {
log.Fatalln("Error reading config file: ", err)
}
defer confData.Close()
var config Config
err = json.NewDecoder(confData).Decode(&config)
if err != nil {
log.Fatalln("Error reading configuration: ", err)
}
if config.Bucket == "" {
log.Fatalln("Bucket is required")
}
if config.InfluxDBHost == "" {
log.Fatalln("InfluxDBHost is required")
}
if config.InfluxDBApiToken == "" {
log.Fatalln("InfluxDBApiToken is required")
}
if config.Org == "" {
log.Fatalln("Org is required")
}
if config.GithubApiToken == "" {
log.Fatalln("GithubApiToken is required")
}
transport := &retryableTransport{
transport: &http.Transport{},
TLSHandshakeTimeout: 30 * time.Second,
ResponseHeaderTimeout: 30 * time.Second,
}
client := &http.Client{
Timeout: 30 * time.Second,
Transport: transport,
}
ghc := github.NewClient(client).WithAuthToken(config.GithubApiToken)
opts := &github.RepositoryListByAuthenticatedUserOptions{Type: "owner"}
repos, resp, err := ghc.Repositories.ListByAuthenticatedUser(context.Background(), opts)
if _, ok := err.(*github.RateLimitError); ok {
log.Fatalln("Hit rate limit")
}
if _, ok := err.(*github.AbuseRateLimitError); ok {
log.Fatalln("Hit secondary rate limit")
}
if err != nil {
log.Fatalln("Error getting list of repositories: ", err)
}
if resp.StatusCode != http.StatusOK {
log.Fatalln("Error getting list of repositories: ", resp.Status)
}
payload := bytes.Buffer{}
wg := &sync.WaitGroup{}
for _, repo := range repos {
if *repo.Archived || *repo.Fork || *repo.Private || *repo.Disabled {
continue
}
wg.Add(1)
go func(payload *bytes.Buffer) {
defer wg.Done()
clones, _, err := ghc.Repositories.ListTrafficClones(context.Background(), repo.GetOwner().GetLogin(), repo.GetName(), nil)
if err != nil {
log.Fatalln("Error getting clones traffic data: ", err)
}
for _, value := range clones.Clones {
influxLine := fmt.Sprintf("github_stats_clones,repo=%s count=%d,uniques=%d %v\n", repo.GetFullName(), *value.Count, *value.Uniques, value.Timestamp.Time.Unix())
payload.WriteString(influxLine)
}
}(&payload)
wg.Add(1)
go func(payload *bytes.Buffer) {
defer wg.Done()
paths, _, err := ghc.Repositories.ListTrafficPaths(context.Background(), repo.GetOwner().GetLogin(), repo.GetName())
if err != nil {
log.Fatalln("Error getting paths traffic data: ", err)
}
for _, value := range paths {
influxLine := fmt.Sprintf("github_stats_paths,repo=%s,path=%s count=%d,uniques=%d %v\n", repo.GetFullName(), *value.Path, *value.Count, *value.Uniques, time.Now().Unix())
payload.WriteString(influxLine)
}
}(&payload)
wg.Add(1)
go func(payload *bytes.Buffer) {
defer wg.Done()
refs, _, err := ghc.Repositories.ListTrafficReferrers(context.Background(), repo.GetOwner().GetLogin(), repo.GetName())
if err != nil {
log.Fatalln("Error getting referrers traffic data: ", err)
}
for _, value := range refs {
influxLine := fmt.Sprintf("github_stats_referrals,repo=%s,referrer=%s count=%d,uniques=%d %v\n", repo.GetFullName(), *value.Referrer, *value.Count, *value.Uniques, time.Now().Unix())
payload.WriteString(influxLine)
}
}(&payload)
wg.Add(1)
go func(payload *bytes.Buffer) {
defer wg.Done()
views, _, err := ghc.Repositories.ListTrafficViews(context.Background(), repo.GetOwner().GetLogin(), repo.GetName(), nil)
if err != nil {
log.Fatalln("Error getting views traffic data: ", err)
}
for _, value := range views.Views {
influxLine := fmt.Sprintf("github_stats_views,repo=%s count=%d,uniques=%d %v\n", repo.GetFullName(), *value.Count, *value.Uniques, value.Timestamp.Time.Unix())
payload.WriteString(influxLine)
}
}(&payload)
wg.Add(1)
go func(payload *bytes.Buffer) {
defer wg.Done()
starsLine := fmt.Sprintf("github_stats_stars,repo=%s count=%d %v\n", repo.GetFullName(), *repo.StargazersCount, time.Now().Unix())
payload.WriteString(starsLine)
forksLine := fmt.Sprintf("github_stats_forks,repo=%s count=%d %v\n", repo.GetFullName(), *repo.ForksCount, time.Now().Unix())
payload.WriteString(forksLine)
}(&payload)
wg.Add(1)
go func(payload *bytes.Buffer) {
defer wg.Done()
workflows, _, err := ghc.Actions.ListWorkflows(context.Background(), repo.GetOwner().GetLogin(), repo.GetName(), nil)
if err != nil {
log.Fatalln("Error getting workflows: ", err)
}
for _, workflow := range workflows.Workflows {
runs, _, err := ghc.Actions.ListWorkflowRunsByID(context.Background(), repo.GetOwner().GetLogin(), repo.GetName(), *workflow.ID, nil)
if err != nil {
log.Fatalln("Error getting workflow runs: ", err)
}
for _, run := range runs.WorkflowRuns {
duration := run.UpdatedAt.GetTime().Sub(*run.RunStartedAt.GetTime()).Seconds()
influxLine := fmt.Sprintf("github_stats_actions,repo=%s,workflow=%s duration=%.0f %v\n", repo.GetFullName(), strings.ReplaceAll(*workflow.Name, " ", "\\ "), duration, run.CreatedAt.GetTime().UnixMilli())
payload.WriteString(influxLine)
}
}
}(&payload)
}
wg.Wait()
if len(payload.Bytes()) == 0 {
log.Fatalln("No data to send")
}
var buf bytes.Buffer
w := gzip.NewWriter(&buf)
w.Write(payload.Bytes())
err = w.Close()
if err != nil {
log.Fatalln("Error compressing data: ", err)
}
url := fmt.Sprintf("https://%s/api/v2/write?precision=s&org=%s&bucket=%s", config.InfluxDBHost, config.Org, config.Bucket)
post, _ := http.NewRequest("POST", url, &buf)
post.Header.Set("Accept", "application/json")
post.Header.Set("Authorization", "Token "+config.InfluxDBApiToken)
post.Header.Set("Content-Encoding", "gzip")
post.Header.Set("Content-Type", "text/plain; charset=utf-8")
postResp, err := client.Do(post)
if err != nil {
log.Fatalln("Error sending data: ", err)
}
defer postResp.Body.Close()
statusOK := resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices
if !statusOK {
body, err := io.ReadAll(postResp.Body)
if err != nil {
log.Fatalln("Error reading data: ", err)
}
log.Fatalln("Error sending data: ", postResp.Status, string(body))
}
}