forked from digitalocean/openvswitch_exporter
/
datapath.go
158 lines (138 loc) · 4.03 KB
/
datapath.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
// Copyright 2018 DigitalOcean.
//
// 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 ovsexporter
import (
"fmt"
"github.com/digitalocean/go-openvswitch/ovsnl"
"github.com/prometheus/client_golang/prometheus"
)
var _ prometheus.Collector = &datapathCollector{}
// A datapathCollector is a prometheus.Collector for Open vSwitch datapaths.
type datapathCollector struct {
StatsHitsTotal *prometheus.Desc
StatsMissesTotal *prometheus.Desc
StatsLostTotal *prometheus.Desc
StatsFlows *prometheus.Desc
MegaflowStatsMaskHitsTotal *prometheus.Desc
MegaflowStatsMasks *prometheus.Desc
listDatapaths func() ([]ovsnl.Datapath, error)
}
// newDatapathCollector creates a prometheus.Collector for Open vSwitch
// datapaths. The input function can be swapped for testing.
func newDatapathCollector(fn func() ([]ovsnl.Datapath, error)) prometheus.Collector {
const (
subsystem = "datapath"
)
var (
labels = []string{"datapath"}
)
return &datapathCollector{
StatsHitsTotal: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "stats_hits_total"),
"Number of flow table matches.",
labels, nil,
),
StatsMissesTotal: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "stats_misses_total"),
"Number of flow table misses.",
labels, nil,
),
StatsLostTotal: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "stats_lost_total"),
"Number of flow table misses not sent to userspace.",
labels, nil,
),
StatsFlows: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "stats_flows"),
"Number of flows present.",
labels, nil,
),
MegaflowStatsMaskHitsTotal: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "megaflow_stats_mask_hits_total"),
"Number of megaflow masks used for flow lookups.",
labels, nil,
),
MegaflowStatsMasks: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "megaflow_stats_masks"),
"Number of megaflow masks present.",
labels, nil,
),
listDatapaths: fn,
}
}
// Describe implements prometheus.Collector.
func (c *datapathCollector) Describe(ch chan<- *prometheus.Desc) {
ds := []*prometheus.Desc{
c.StatsHitsTotal,
c.StatsMissesTotal,
c.StatsLostTotal,
c.StatsFlows,
c.MegaflowStatsMaskHitsTotal,
c.MegaflowStatsMasks,
}
for _, d := range ds {
ch <- d
}
}
// Collect implements prometheus.Collector.
func (c *datapathCollector) Collect(ch chan<- prometheus.Metric) {
dps, err := c.listDatapaths()
if err != nil {
ch <- prometheus.NewInvalidMetric(c.StatsHitsTotal, fmt.Errorf("error listing datapaths: %v", err))
return
}
for _, d := range dps {
// Expose per-datapath metrics using statistics structures.
tuples := []struct {
d *prometheus.Desc
t prometheus.ValueType
v uint64
}{
{
d: c.StatsHitsTotal,
t: prometheus.CounterValue,
v: d.Stats.Hit,
},
{
d: c.StatsMissesTotal,
t: prometheus.CounterValue,
v: d.Stats.Missed,
},
{
d: c.StatsLostTotal,
t: prometheus.CounterValue,
v: d.Stats.Lost,
},
{
d: c.StatsFlows,
t: prometheus.GaugeValue,
v: d.Stats.Flows,
},
{
d: c.MegaflowStatsMaskHitsTotal,
t: prometheus.CounterValue,
v: d.MegaflowStats.MaskHits,
},
{
d: c.MegaflowStatsMasks,
t: prometheus.GaugeValue,
v: uint64(d.MegaflowStats.Masks),
},
}
for _, t := range tuples {
// Label each metric with the datapath's name.
ch <- prometheus.MustNewConstMetric(t.d, t.t, float64(t.v), d.Name)
}
}
}