forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetcher.go
105 lines (87 loc) · 3.12 KB
/
fetcher.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
// Copyright 2019 Anapaya Systems
//
// 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 metrics
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/scionproto/scion/go/lib/prom"
)
const revSrcPathReply = "path_reply"
// RequestLabels contains the labels for the request metrics.
type RequestLabels struct {
Result string
}
// Labels returns the labels.
func (l RequestLabels) Labels() []string {
return []string{prom.LabelResult}
}
// Values returns the values.
func (l RequestLabels) Values() []string {
return []string{l.Result}
}
// WithResult returns a copy of l with the result changed.
func (l RequestLabels) WithResult(result string) RequestLabels {
l.Result = result
return l
}
// RevocationLabels are the labels for revocation metrics.
type RevocationLabels struct {
Result string
Src string
}
// Labels returns the labels.
func (l RevocationLabels) Labels() []string {
return []string{prom.LabelResult, prom.LabelSrc}
}
// Values returns the values.
func (l RevocationLabels) Values() []string {
return []string{l.Result, l.Src}
}
// WithResult returns a copy of l with the result changed.
func (l RevocationLabels) WithResult(result string) RevocationLabels {
l.Result = result
return l
}
// Fetcher exposes all metrics for the fetcher.
type Fetcher interface {
SegRequests(labels RequestLabels) prometheus.Counter
RevocationsReceived(labels RevocationLabels) prometheus.Counter
UpdateRevocation(stored int, dbErrs int, verifyErrs int)
}
type fetcher struct {
segRequest *prometheus.CounterVec
revocations *prometheus.CounterVec
}
// NewFetcher creates fetcher metrics struct.
func NewFetcher(namespace string) Fetcher {
sub := "fetcher"
return fetcher{
segRequest: prom.NewCounterVecWithLabels(namespace, sub, "seg_requests_total",
"The number of segment request sent.", RequestLabels{Result: OkSuccess}),
revocations: prom.NewCounterVecWithLabels(namespace, "", "received_revocations_total",
"The amount of revocations received.",
RevocationLabels{Result: OkSuccess, Src: revSrcPathReply}),
}
}
func (f fetcher) SegRequests(l RequestLabels) prometheus.Counter {
return f.segRequest.WithLabelValues(l.Values()...)
}
func (f fetcher) RevocationsReceived(l RevocationLabels) prometheus.Counter {
l.Src = revSrcPathReply
return f.revocations.WithLabelValues(l.Values()...)
}
func (f fetcher) UpdateRevocation(stored int, dbErrs int, verifyErrs int) {
f.RevocationsReceived(RevocationLabels{Result: OkSuccess}).Add(float64(stored))
f.RevocationsReceived(RevocationLabels{Result: ErrDB}).Add(float64(dbErrs))
f.RevocationsReceived(RevocationLabels{Result: ErrVerify}).Add(float64(verifyErrs))
}