-
-
Notifications
You must be signed in to change notification settings - Fork 285
/
metrics.go
107 lines (91 loc) Β· 3.25 KB
/
metrics.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
package resolver
import (
"context"
"sync/atomic"
"time"
"github.com/safing/portbase/log"
"github.com/safing/portbase/modules"
"github.com/safing/portbase/notifications"
)
var (
slowQueriesSensorCnt atomic.Int64
slowQueriesSensorSum atomic.Int64
)
// reportRequestDuration reports successful query request duration.
func reportRequestDuration(started time.Time, resolver *Resolver) {
// TODO: Record prometheus metrics for all resolvers separately.
// Add query times from system and configured resolvers to slow queries sensor.
switch resolver.Info.Source {
case ServerSourceConfigured, ServerSourceOperatingSystem:
slowQueriesSensorCnt.Add(1)
slowQueriesSensorSum.Add(int64(time.Since(started)))
default:
}
}
// getSlowQueriesSensorValue returns the current avg query time recorded by the
// slow queries sensor.
func getSlowQueriesSensorValue() (avgQueryTime time.Duration) {
// Get values and check them.
sum := slowQueriesSensorSum.Load()
cnt := slowQueriesSensorCnt.Load()
if cnt < 1 {
cnt = 1
}
return time.Duration(sum / cnt)
}
// resetSlowQueriesSensorValue reset the slow queries sensor values.
func resetSlowQueriesSensorValue() {
slowQueriesSensorCnt.Store(0)
slowQueriesSensorSum.Store(0)
}
var suggestUsingStaleCacheNotification *notifications.Notification
func suggestUsingStaleCacheTask(ctx context.Context, t *modules.Task) error {
switch {
case useStaleCache() || useStaleCacheConfigOption.IsSetByUser():
// If setting is already active, disable task repeating.
t.Repeat(0)
// Delete local reference, if used.
if suggestUsingStaleCacheNotification != nil {
suggestUsingStaleCacheNotification.Delete()
suggestUsingStaleCacheNotification = nil
}
case suggestUsingStaleCacheNotification != nil:
// Check if notification is already active.
suggestUsingStaleCacheNotification.Lock()
defer suggestUsingStaleCacheNotification.Unlock()
if suggestUsingStaleCacheNotification.Meta().IsDeleted() {
// Reset local reference if notification was deleted.
suggestUsingStaleCacheNotification = nil
}
case getSlowQueriesSensorValue() > 100*time.Millisecond:
log.Warningf(
"resolver: suggesting user to use stale dns cache with avg query time of %s for config and system resolvers",
getSlowQueriesSensorValue().Round(time.Millisecond),
)
// Notify user.
suggestUsingStaleCacheNotification = ¬ifications.Notification{
EventID: "resolver:suggest-using-stale-cache",
Type: notifications.Info,
Title: "Speed Up Website Loading",
Message: "Portmaster has detected that websites may load slower because DNS queries are currently slower than expected. You may want to switch your DNS provider or enable using expired DNS cache entries for better performance.",
ShowOnSystem: getSlowQueriesSensorValue() > 500*time.Millisecond,
Expires: time.Now().Add(10 * time.Minute).Unix(),
AvailableActions: []*notifications.Action{
{
Text: "Open Setting",
Type: notifications.ActionTypeOpenSetting,
Payload: ¬ifications.ActionTypeOpenSettingPayload{
Key: CfgOptionUseStaleCacheKey,
},
},
{
ID: "ack",
Text: "Got it!",
},
},
}
notifications.Notify(suggestUsingStaleCacheNotification)
}
resetSlowQueriesSensorValue()
return nil
}