Skip to content

Commit 61e151c

Browse files
Merge pull request #12 from williamchanrico/refactor_collector
Refactor variable names and function extraction
2 parents 4363669 + c8f2f1c commit 61e151c

File tree

3 files changed

+62
-51
lines changed

3 files changed

+62
-51
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
NAME := planet-exporter
44
BIN_DIRECTORY := ./bin
55
REVISION := $(shell git rev-parse --short HEAD 2>/dev/null)
6-
VERSION := v0.1.1
6+
VERSION := v0.2.0-dev
77

88
ifndef REVISION
99
override REVISION = none

collector/task/darkstat/darkstat.go

+38-27
Original file line numberDiff line numberDiff line change
@@ -111,47 +111,64 @@ func Collect(ctx context.Context) error {
111111
ctxCollect, ctxCollectCancel := context.WithCancel(ctx)
112112
defer ctxCollectCancel()
113113

114-
inventoryHosts := inventory.Get()
115-
116-
localAddr, err := network.DefaultLocalAddr()
117-
if err != nil {
118-
return err
119-
}
120-
// To label source traffic that we need to build dependency graph
121-
localHostgroup := localAddr.String()
122-
localDomain := localAddr.String()
123-
localInventory, ok := inventoryHosts[localAddr.String()]
124-
if ok {
125-
localHostgroup = localInventory.Hostgroup
126-
localDomain = localInventory.Domain
127-
}
128-
log.Debugf("Local address don't exist in inventory: %v", localAddr.String())
129-
130114
// Scrape darkstat prometheus endpoint for host_bytes_total
131-
var darkstatHostBytesTotal *prom2json.Family
115+
var darkstatHostBytesTotalMetric *prom2json.Family
132116
darkstatScrape, err := singleton.prometheusClient.Scrape(ctxCollect, singleton.darkstatAddr)
133117
if err != nil {
134118
return err
135119
}
136120
for _, v := range darkstatScrape {
137121
if v.Name == "host_bytes_total" {
138-
darkstatHostBytesTotal = v
122+
darkstatHostBytesTotalMetric = v
139123
break
140124
}
141125
}
142-
if darkstatHostBytesTotal == nil {
126+
if darkstatHostBytesTotalMetric == nil {
143127
return fmt.Errorf("Metric host_bytes_total doesn't exist")
144128
}
145129

146130
// Extract relevant data out of host_bytes_total
131+
hosts, err := toHostMetrics(darkstatHostBytesTotalMetric)
132+
if err != nil {
133+
return err
134+
}
135+
136+
singleton.mu.Lock()
137+
singleton.hosts = hosts
138+
singleton.mu.Unlock()
139+
140+
log.Debugf("taskdarkstat.Collect retrieved %v downstreams metrics", len(hosts))
141+
log.Debugf("taskdarkstat.Collect process took %v", time.Since(startTime))
142+
return nil
143+
}
144+
145+
// toHostMetrics converts darkstatHostBytesTotal metrics into planet explorer prometheus metrics.
146+
func toHostMetrics(darkstatHostBytesTotal *prom2json.Family) ([]Metric, error) {
147147
var hosts []Metric
148+
149+
inventoryHosts := inventory.Get()
150+
151+
localAddr, err := network.DefaultLocalAddr()
152+
if err != nil {
153+
return nil, err
154+
}
155+
// To label source traffic that we need to build dependency graph
156+
localHostgroup := localAddr.String()
157+
localDomain := localAddr.String()
158+
localInventory, ok := inventoryHosts[localAddr.String()]
159+
if ok {
160+
localHostgroup = localInventory.Hostgroup
161+
localDomain = localInventory.Domain
162+
}
163+
log.Debugf("Local address don't exist in inventory: %v", localAddr.String())
164+
148165
for _, m := range darkstatHostBytesTotal.Metrics {
149166
metric := m.(prom2json.Metric)
150167

151168
ip := net.ParseIP(metric.Labels["ip"])
152169

153170
// Skip its own IP as we don't need it
154-
if ip.Equal(localAddr) {
171+
if ip.Equal(nil) || ip.Equal(localAddr) {
155172
continue
156173
}
157174

@@ -183,11 +200,5 @@ func Collect(ctx context.Context) error {
183200
})
184201
}
185202

186-
singleton.mu.Lock()
187-
singleton.hosts = hosts
188-
singleton.mu.Unlock()
189-
190-
log.Debugf("taskdarkstat.Collect retrieved %v downstreams metrics", len(hosts))
191-
log.Debugf("taskdarkstat.Collect process took %v", time.Since(startTime))
192-
return nil
203+
return hosts, nil
193204
}

collector/task/ebpf/tcptop.go

+23-23
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ var (
4949
)
5050

5151
const (
52-
send_bytes = "ebpf_exporter_ipv4_send_bytes"
53-
recv_bytes = "ebpf_exporter_ipv4_recv_bytes"
54-
ingress = "ingress"
55-
egress = "egress"
52+
sendBytes = "ebpf_exporter_ipv4_send_bytes"
53+
recvBytes = "ebpf_exporter_ipv4_recv_bytes"
54+
ingress = "ingress"
55+
egress = "egress"
5656
)
5757

5858
func init() {
@@ -125,33 +125,33 @@ func Collect(ctx context.Context) error {
125125
if err != nil {
126126
return err
127127
}
128-
var send_bytes_metric *prom2json.Family
129-
var recv_bytes_metric *prom2json.Family
128+
var sendBytesMetric *prom2json.Family
129+
var recvBytesMetric *prom2json.Family
130130
for _, v := range ebpfScrape {
131-
if v.Name == send_bytes {
132-
send_bytes_metric = v
131+
if v.Name == sendBytes {
132+
sendBytesMetric = v
133133
}
134-
if v.Name == recv_bytes {
135-
recv_bytes_metric = v
134+
if v.Name == recvBytes {
135+
recvBytesMetric = v
136136
}
137-
if send_bytes_metric != nil && recv_bytes_metric != nil {
137+
if sendBytesMetric != nil && recvBytesMetric != nil {
138138
break
139139
}
140140
}
141-
if send_bytes_metric == nil {
142-
return fmt.Errorf("Metric %v doesn't exist", send_bytes)
141+
if sendBytesMetric == nil {
142+
return fmt.Errorf("Metric %v doesn't exist", sendBytes)
143143
}
144-
if recv_bytes_metric == nil {
145-
return fmt.Errorf("Metric %v doesn't exist", recv_bytes)
144+
if recvBytesMetric == nil {
145+
return fmt.Errorf("Metric %v doesn't exist", recvBytes)
146146
}
147147

148-
sendHostBytes, err := toHostMetrics(send_bytes_metric, egress)
148+
sendHostBytes, err := toHostMetrics(sendBytesMetric, egress)
149149
if err != nil {
150-
log.Errorf("Conversion to host metric failed for %v, err: %v", send_bytes, err)
150+
log.Errorf("Conversion to host metric failed for %v, err: %v", sendBytes, err)
151151
}
152-
recvHostBytes, err := toHostMetrics(recv_bytes_metric, ingress)
152+
recvHostBytes, err := toHostMetrics(recvBytesMetric, ingress)
153153
if err != nil {
154-
log.Errorf("Conversion to host metric failed for %v, err: %v", recv_bytes, err)
154+
log.Errorf("Conversion to host metric failed for %v, err: %v", recvBytes, err)
155155
}
156156

157157
singleton.mu.Lock()
@@ -164,7 +164,7 @@ func Collect(ctx context.Context) error {
164164
}
165165

166166
// toHostMetrics converts ebpf metrics into planet explorer prometheus metrics.
167-
func toHostMetrics(bytes_metric *prom2json.Family, direction string) ([]Metric, error) {
167+
func toHostMetrics(bytesMetric *prom2json.Family, direction string) ([]Metric, error) {
168168
var hosts []Metric
169169
inventoryHosts := inventory.Get()
170170

@@ -183,11 +183,11 @@ func toHostMetrics(bytes_metric *prom2json.Family, direction string) ([]Metric,
183183
}
184184
log.Debugf("Local address doesn't exist in the inventory: %v", localAddr.String())
185185

186-
for _, m := range bytes_metric.Metrics {
186+
for _, m := range bytesMetric.Metrics {
187187
metric := m.(prom2json.Metric)
188-
destIp := net.ParseIP(metric.Labels["daddr"])
188+
destIP := net.ParseIP(metric.Labels["daddr"])
189189

190-
if destIp.Equal(localAddr) || destIp.Equal(nil) {
190+
if destIP.Equal(nil) || destIP.Equal(localAddr) {
191191
continue
192192
}
193193

0 commit comments

Comments
 (0)