Skip to content

Commit b65b0f7

Browse files
committedMay 22, 2017
Refactor metrics registration
Iterate over all defined metrics and register them based on type instead of explictly register every one. This will help us to avoid code duplication.
1 parent 63d90d3 commit b65b0f7

File tree

2 files changed

+23
-51
lines changed

2 files changed

+23
-51
lines changed
 

‎doc/metrics.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@
2121
* `http_probe_code` - Gauge. HTTP status code, 0 if no HTTP response.
2222
* `http_probe_total_time_ms` - Gauge. Total duration of http transaction.
2323
* `http_probe_content_transfer_time_ms` - Gauge. The duration of content
24-
transfer time from the first reponse byte till the end (in ms).
24+
transfer from the first response byte till the end (in ms).
2525
* `http_probe_tcp_connection_time_ms` - Gauge. TCP establishing time
2626
(in ms).
2727
* `http_probe_dns_lookup_time_ms` - Gauge. DNS lookup time (in ms).
2828
* `http_probe_connect_time_ms` - Gauge. Connection time in ms.
2929
* `http_probe_server_processing_time_ms` - Gauge. Server processing time
3030
(in ms).
3131

32-
3332
## Prometheus configuration example
3433

3534
### Scrape config

‎pkg/utils/metrics.go

+22-49
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package utils
1616

1717
import (
1818
"fmt"
19+
"reflect"
1920
"strings"
2021

2122
"github.com/golang/glog"
@@ -33,6 +34,7 @@ func NewAgentMetrics(ai *AgentInfo) AgentMetrics {
3334
}
3435
name := ai.NodeName
3536

37+
// Basic Counter metrics
3638
am.ErrorCount = prometheus.NewCounter(prometheus.CounterOpts{
3739
Namespace: "ncagent",
3840
Name: "error_count_total",
@@ -46,15 +48,7 @@ func NewAgentMetrics(ai *AgentInfo) AgentMetrics {
4648
Help: "Total number of reports (keepalive messages) from the agent.",
4749
})
4850

49-
if counter, ok := tryRegisterCounter(am.ErrorCount); !ok {
50-
// use existing counter
51-
am.ErrorCount = counter
52-
}
53-
if counter, ok := tryRegisterCounter(am.ReportCount); !ok {
54-
// use existing counter
55-
am.ReportCount = counter
56-
}
57-
51+
// GaugeVec metrics for HTTP probes
5852
am.ProbeConnectionResult = prometheus.NewGaugeVec(
5953
prometheus.GaugeOpts{
6054
Namespace: "ncagent",
@@ -75,7 +69,7 @@ func NewAgentMetrics(ai *AgentInfo) AgentMetrics {
7569
prometheus.GaugeOpts{
7670
Namespace: "ncagent",
7771
Name: "http_probe_total_time_ms",
78-
Help: "The duration of total http request.",
72+
Help: "The total duration of http request.",
7973
},
8074
[]string{"agent", "url"},
8175
)
@@ -84,8 +78,8 @@ func NewAgentMetrics(ai *AgentInfo) AgentMetrics {
8478
Namespace: "ncagent",
8579
Name: "http_probe_content_transfer_time_ms",
8680
Help: fmt.Sprint(
87-
"The duration of content transfer time, from the first ",
88-
"reponse byte till the end (in ms).",
81+
"The duration of content transfer, from the first ",
82+
"response byte till the end (in ms).",
8983
),
9084
},
9185
[]string{"agent", "url"},
@@ -123,41 +117,20 @@ func NewAgentMetrics(ai *AgentInfo) AgentMetrics {
123117
[]string{"agent", "url"},
124118
)
125119

126-
if gauge, ok := tryRegisterGaugeVec(am.ProbeConnectionResult); !ok {
127-
// use existing gauge
128-
am.ProbeConnectionResult = gauge
129-
}
130-
if gauge, ok := tryRegisterGaugeVec(am.ProbeHTTPCode); !ok {
131-
// use existing gauge
132-
am.ProbeHTTPCode = gauge
133-
}
134-
if gauge, ok := tryRegisterGaugeVec(am.ProbeTotal); !ok {
135-
// use existing gauge
136-
am.ProbeTotal = gauge
137-
}
138-
if gauge, ok := tryRegisterGaugeVec(am.ProbeContentTransfer); !ok {
139-
// use existing gauge
140-
am.ProbeContentTransfer = gauge
141-
}
142-
if gauge, ok := tryRegisterGaugeVec(am.ProbeTCPConnection); !ok {
143-
// use existing gauge
144-
am.ProbeTCPConnection = gauge
145-
}
146-
if gauge, ok := tryRegisterGaugeVec(am.ProbeTCPConnection); !ok {
147-
// use existing gauge
148-
am.ProbeTCPConnection = gauge
149-
}
150-
if gauge, ok := tryRegisterGaugeVec(am.ProbeDNSLookup); !ok {
151-
// use existing gauge
152-
am.ProbeDNSLookup = gauge
153-
}
154-
if gauge, ok := tryRegisterGaugeVec(am.ProbeConnect); !ok {
155-
// use existing gauge
156-
am.ProbeConnect = gauge
157-
}
158-
if gauge, ok := tryRegisterGaugeVec(am.ProbeServerProcessing); !ok {
159-
// use existing gauge
160-
am.ProbeServerProcessing = gauge
120+
// Let's register all the metrics now
121+
params := reflect.ValueOf(&am).Elem()
122+
for i := 0; i < params.NumField(); i++ {
123+
if e, ok := params.Field(i).Interface().(*prometheus.GaugeVec); ok {
124+
if exists, ok := tryRegisterGaugeVec(e); !ok {
125+
params.Field(i).Set(reflect.ValueOf(exists))
126+
}
127+
} else if e, ok := params.Field(i).Interface().(prometheus.Counter); ok {
128+
if exists, ok := tryRegisterCounter(e); !ok {
129+
params.Field(i).Set(reflect.ValueOf(exists))
130+
}
131+
} else {
132+
glog.V(5).Infof("Skipping %v since it's not prometheus metric.", params.Type().Field(i).Name)
133+
}
161134
}
162135

163136
return am
@@ -194,7 +167,7 @@ func tryRegisterGaugeVec(m *prometheus.GaugeVec) (*prometheus.GaugeVec, bool) {
194167
return m, true
195168
}
196169

197-
// UpdateAgentBaseMetrics function updates basic metrcis with reports and
170+
// UpdateAgentBaseMetrics function updates basic metrics with reports and
198171
// error counters
199172
func UpdateAgentBaseMetrics(am AgentMetrics, report, error bool) {
200173
if report {
@@ -207,7 +180,7 @@ func UpdateAgentBaseMetrics(am AgentMetrics, report, error bool) {
207180
}
208181
}
209182

210-
// UpdateAgentProbeMetrics functions updates HTTP probe metrics.
183+
// UpdateAgentProbeMetrics function updates HTTP probe metrics.
211184
func UpdateAgentProbeMetrics(ai AgentInfo, am AgentMetrics) {
212185

213186
suffix := "private_network"

0 commit comments

Comments
 (0)
Please sign in to comment.