This repository has been archived by the owner on Sep 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstatus.go
149 lines (127 loc) · 2.81 KB
/
status.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
package tgstatus
import (
"context"
"strconv"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"github.com/gotd/td/telegram"
"github.com/gotd/td/tg"
)
type Status struct {
appID int
appHash string
log *zap.Logger
mux sync.Mutex
checks []*Check
seen *prometheus.GaugeVec
}
func (s *Status) Describe(descs chan<- *prometheus.Desc) {
s.seen.Describe(descs)
}
func (s *Status) Collect(metrics chan<- prometheus.Metric) {
now := time.Now()
s.mux.Lock()
for _, c := range s.checks {
s.setMetric(now, c)
}
s.mux.Unlock()
s.seen.Collect(metrics)
}
func (s *Status) Report() []Report {
var reports []Report
s.mux.Lock()
for _, c := range s.checks {
reports = append(reports, c.Report())
}
s.mux.Unlock()
return reports
}
func (s *Status) config(ctx context.Context) (*tg.Config, error) {
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
client := telegram.NewClient(s.appID, s.appHash, telegram.Options{
Logger: s.log.Named("client.initial"),
})
cfg := make(chan *tg.Config, 1)
s.log.Debug("Getting config")
if err := client.Run(ctx, func(ctx context.Context) error {
s.log.Debug("Started client")
gotCfg, err := tg.NewClient(client).HelpGetConfig(ctx)
if err != nil {
s.log.Debug("Got error on HelpGetConfig", zap.Error(err))
return err
}
s.log.Debug("Got config")
cfg <- gotCfg
return nil
}); err != nil {
close(cfg)
continue
}
s.log.Debug("Waiting for config or context done")
select {
case <-ctx.Done():
return nil, ctx.Err()
case gotConfig := <-cfg:
return gotConfig, nil
}
}
}
func (s *Status) setMetric(now time.Time, c *Check) {
labels := prometheus.Labels{
"dc": strconv.Itoa(c.id),
}
s.seen.With(labels).Set(now.Sub(c.Report().Seen).Seconds())
}
func (s *Status) Run(ctx context.Context) error {
cfg, err := s.config(ctx)
if err != nil {
return err
}
g, gCtx := errgroup.WithContext(ctx)
now := time.Now()
s.mux.Lock()
for _, dc := range cfg.DCOptions {
if dc.Ipv6 || dc.TCPObfuscatedOnly || dc.Static || dc.MediaOnly {
continue
}
check := &Check{
appID: s.appID,
appHash: s.appHash,
rate: time.Second * 10,
id: dc.ID,
option: dc,
ip: dc.IPAddress,
port: dc.Port,
seen: now,
log: s.log.With(
zap.Int("dc", dc.ID),
),
}
s.setMetric(now, check)
s.checks = append(s.checks, check)
g.Go(func() error {
return check.Loop(gCtx)
})
}
s.mux.Unlock()
return g.Wait()
}
func New(appID int, appHash string, log *zap.Logger) *Status {
return &Status{
appID: appID,
appHash: appHash,
log: log,
seen: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "telegram_dc_seen",
Help: "Seconds from last contact with server",
}, []string{"dc"}),
}
}