-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_stats_exporter.go
281 lines (239 loc) · 7.44 KB
/
stream_stats_exporter.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package main
import (
"fmt"
vlc "github.com/adrg/libvlc-go/v3"
"github.com/tatsushid/go-fastping"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
_ "net/http/pprof"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
)
const (
namespace = "monitoring"
)
var (
listenAddress = kingpin.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":8080").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
//streamingTime = kingpin.Flag("analysis.streaming-time", "Time of the connection to the stream.").Default("5").Duration()
// Metrics about the Stream Stats Exporter itself.
ssDuration = prometheus.NewSummary(prometheus.SummaryOpts{Name: prometheus.BuildFQName(namespace, "exporter", "duration_seconds"), Help: "Duration of collections by the Stream Stats Exporter."})
ssErrors = prometheus.NewCounter(prometheus.CounterOpts{Name: prometheus.BuildFQName(namespace, "exporter", "errors_total"), Help: "Errors raised by the Stream Stats Exporter."})
)
// Exporter collects network stats from the given address and exports them using
// the prometheus metrics package.
type Exporter struct {
target string
streamingTime int
mutex sync.RWMutex
success *prometheus.Desc
bitrate *prometheus.Desc
latency *prometheus.Desc
}
// NewExporter returns an initialized Exporter.
func NewExporter(target string, streamingTime int) *Exporter {
return &Exporter{
target: target,
streamingTime: streamingTime,
success: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "success"), "Was the last measurement for the probe successful.", nil, nil),
bitrate: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "bitrate"), "Bitrate of the stream in kbit/s.", nil, nil),
latency: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "latency"), "Latency of the target in ms.", nil, nil),
}
}
// Describe describes all the metrics exported by the Stream Stats Exporter. It
// implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- e.success
ch <- e.bitrate
ch <- e.latency
}
// Collect measures network stats and delivers them as Prometheus
// metrics. It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock() // To protect metrics from concurrent collects.
defer e.mutex.Unlock()
bitrate, latency, err := runAnalysis(e.target, e.streamingTime)
if err != nil {
ch <- prometheus.MustNewConstMetric(e.success, prometheus.GaugeValue, 0)
ssErrors.Inc()
log.Errorf("Failed to run network analysis: %s", err)
return
}
ch <- prometheus.MustNewConstMetric(e.success, prometheus.GaugeValue, 1)
ch <- prometheus.MustNewConstMetric(e.bitrate, prometheus.GaugeValue, bitrate)
ch <- prometheus.MustNewConstMetric(e.latency, prometheus.GaugeValue, latency)
}
func handler(w http.ResponseWriter, r *http.Request) {
target := r.URL.Query().Get("target")
if target == "" {
http.Error(w, "'target' parameter must be specified", http.StatusBadRequest)
ssErrors.Inc()
return
}
var streamingTime int
var period = r.URL.Query().Get("streamingTime")
if period != "" {
var err error
streamingTime, err = strconv.Atoi(period)
if err != nil {
http.Error(w, fmt.Sprintf("'streamingTime' parameter must be an integer: %s", err), http.StatusBadRequest)
ssErrors.Inc()
return
}
}
if streamingTime == 0 {
streamingTime = 5
}
start := time.Now()
registry := prometheus.NewRegistry()
exporter := NewExporter(target, streamingTime)
registry.MustRegister(exporter)
// Delegate http serving to Prometheus client library, which will call collector.Collect.
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
duration := time.Since(start).Seconds()
ssDuration.Observe(duration)
}
func runAnalysis(streamUrl string, streamingTime int) (float64, float64, error) {
var bitrate float64
var latency float64
var err error
var wg sync.WaitGroup
// Measure bitrate in the background
wg.Add(1)
go func() {
bitrate, err = getBitrate(streamUrl, streamingTime, &wg)
}()
// Measure latency in the background
wg.Add(1)
go func() {
latency, err = getLatency(streamUrl, &wg)
}()
// Wait for the background tasks to complete
wg.Wait()
if err != nil {
return 0, 0, err
} else {
return bitrate, latency, nil
}
}
// Connects to the stream and report its average bitrate.
func getBitrate(videoUrl string, streamingTime int, wg *sync.WaitGroup) (float64, error) {
defer wg.Done()
if err := vlc.Init("--no-video", "--quiet"); err != nil {
return 0, err
}
defer vlc.Release()
// Create a new player.
player, err := vlc.NewPlayer()
if err != nil {
return 0, err
}
defer func() {
player.Stop()
}()
media, err := player.LoadMediaFromURL(videoUrl)
if err != nil {
return 0, err
}
defer media.Release()
// Start playing the media.
err = player.Play()
if err != nil {
return 0, err
}
// Start collecting the metrics.
var counter = 0
var bitRates float64 = 0
for !player.IsPlaying() { // wait for the player to start
}
for player.IsPlaying() { // collect bitrate
stats, err := media.Stats()
if err != nil {
return 0, nil
}
inputBitrate := stats.DemuxBitRate * 8
if inputBitrate != 0 {
bitRates += inputBitrate
counter++
}
if counter == streamingTime {
player.Stop()
media.Release()
}
time.Sleep(1 * time.Second)
}
return bitRates / float64(counter), nil
}
// Parses domain name from the url.
func parseDomainFromUrl(urlAddress string) (string, error) {
u, err := url.Parse(urlAddress)
if err != nil {
return "", err
}
return strings.Split(u.Host, ":")[0], nil
}
// Measures latency for the url.
func getLatency(url string, wg *sync.WaitGroup) (float64, error) {
defer wg.Done()
var latency float64
domain, err := parseDomainFromUrl(url)
if err != nil {
return 0, err
}
p := fastping.NewPinger()
ra, err := net.ResolveIPAddr("ip4:icmp", domain)
if err != nil {
return 0, err
}
p.AddIPAddr(ra)
p.OnRecv = func(addr *net.IPAddr, rtt time.Duration) {
latency = float64(rtt.Milliseconds())
}
err = p.Run()
if err != nil {
return 0, err
}
return latency, nil
}
func main() {
log.AddFlags(kingpin.CommandLine)
kingpin.Version(version.Print("stream_stats_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
log.Info("Starting Stream Stats Exporter", version.Info())
log.Info("Build context", version.BuildContext())
prometheus.MustRegister(version.NewCollector("stream_stats_exporter"))
prometheus.MustRegister(ssDuration)
prometheus.MustRegister(ssErrors)
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/probe", handler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
_, err := w.Write([]byte(`<html>
<head><title>Stream Stats Exporter</title></head>
<body>
<h1>Stream Stats Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
<p><a href="/probe">Probe</a></p>
</html>`))
if err != nil {
log.Warnf("Failed to write to HTTP client: %s", err)
}
})
srv := &http.Server{
Addr: *listenAddress,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
}
log.Infof("Listening on %s", srv.Addr)
log.Fatal(srv.ListenAndServe())
}