forked from mattbostock/webdriver_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
probe.go
126 lines (115 loc) · 4.86 KB
/
probe.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
// Copyright 2016 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"net/http"
"github.com/prometheus/common/log"
"github.com/sclevine/agouti"
)
const msInSecond = 1000
// from https://www.w3.org/TR/navigation-timing/#sec-navigation-timing-interface
type navigationTimings struct {
ConnectEnd float64
ConnectStart float64
DomComplete float64
DomContentLoadedEventEnd float64
DomContentLoadedEventStart float64
DomInteractive float64
DomLoading float64
DomainLookupEnd float64
DomainLookupStart float64
FetchStart float64
LoadEventEnd float64
LoadEventStart float64
NavigationStart float64
RedirectEnd float64
RedirectStart float64
RequestStart float64
ResponseEnd float64
ResponseStart float64
SecureConnectionStart float64
UnloadEventEnd float64
UnloadEventStart float64
}
func probe(target string, w http.ResponseWriter) bool {
// new page to ensure a clean session (no caching)
options := []agouti.Option{
agouti.Desired(agouti.NewCapabilities().Browser("chrome").With("javascriptEnabled")),
agouti.Timeout(5),
}
page, err := driver.NewPage(options...)
if err != nil {
log.Error(err)
return false
}
defer page.Destroy()
err = page.Navigate(target)
if err != nil {
log.Error(err)
return false
}
url, err := page.URL()
if err != nil {
log.Error(err)
return false
}
if url != target {
log.Errorf("got unexpected URL, ensure target accounts for any redirects; got %q, expected %q", url, target)
return false
}
var timings *navigationTimings
err = page.RunScript("return window.performance.timing;", nil, &timings)
if err != nil {
log.Error(err)
return false
}
fmt.Fprintf(w, "navigation_timing_connect_end_seconds %f\n", timings.ConnectEnd/msInSecond)
fmt.Fprintf(w, "navigation_timing_connect_start_seconds %f\n", timings.ConnectStart/msInSecond)
fmt.Fprintf(w, "navigation_timing_dom_complete_seconds %f\n", timings.DomComplete/msInSecond)
fmt.Fprintf(w, "navigation_timing_dom_content_loaded_event_end_seconds %f\n", timings.DomContentLoadedEventEnd/msInSecond)
fmt.Fprintf(w, "navigation_timing_dom_content_loaded_event_start_seconds %f\n", timings.DomContentLoadedEventStart/msInSecond)
fmt.Fprintf(w, "navigation_timing_dom_interactive_seconds %f\n", timings.DomInteractive/msInSecond)
fmt.Fprintf(w, "navigation_timing_dom_loading_seconds %f\n", timings.DomLoading/msInSecond)
fmt.Fprintf(w, "navigation_timing_domain_lookup_end_seconds %f\n", timings.DomainLookupEnd/msInSecond)
fmt.Fprintf(w, "navigation_timing_domain_lookup_start_seconds %f\n", timings.DomainLookupStart/msInSecond)
fmt.Fprintf(w, "navigation_timing_fetch_start_seconds %f\n", timings.FetchStart/msInSecond)
fmt.Fprintf(w, "navigation_timing_load_event_end_seconds %f\n", timings.LoadEventEnd/msInSecond)
fmt.Fprintf(w, "navigation_timing_load_event_start_seconds %f\n", timings.LoadEventStart/msInSecond)
fmt.Fprintf(w, "navigation_timing_redirect_end_seconds %f\n", timings.RedirectEnd/msInSecond)
fmt.Fprintf(w, "navigation_timing_redirect_start_seconds %f\n", timings.RedirectStart/msInSecond)
fmt.Fprintf(w, "navigation_timing_request_start_seconds %f\n", timings.RequestStart/msInSecond)
fmt.Fprintf(w, "navigation_timing_response_end_seconds %f\n", timings.ResponseEnd/msInSecond)
fmt.Fprintf(w, "navigation_timing_response_start_seconds %f\n", timings.ResponseStart/msInSecond)
fmt.Fprintf(w, "navigation_timing_secure_connection_start_seconds %f\n", timings.SecureConnectionStart/msInSecond)
fmt.Fprintf(w, "navigation_timing_start_seconds %f\n", timings.NavigationStart/msInSecond)
fmt.Fprintf(w, "navigation_timing_unload_event_end_seconds %f\n", timings.UnloadEventEnd/msInSecond)
fmt.Fprintf(w, "navigation_timing_unload_event_start_seconds %f\n", timings.UnloadEventStart/msInSecond)
logs, err := page.ReadAllLogs("browser")
if err != nil {
log.Error(err)
return false
}
var warningCount, severeCount int
for _, log := range logs {
if log.Level == "WARNING" {
warningCount++
}
if log.Level == "SEVERE" {
severeCount++
}
}
fmt.Fprintf(w, "browser_log_warning_count %d\n", warningCount)
fmt.Fprintf(w, "browser_log_severe_count %d\n", severeCount)
return true
}