Skip to content

Commit

Permalink
Improve Prometheus labeling
Browse files Browse the repository at this point in the history
  • Loading branch information
corny committed Jan 6, 2019
1 parent d64a6c6 commit 37cf45b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestHTTP(t *testing.T) {
if response != nil {
assert.Equal(200, response.StatusCode)
bytes, _ := ioutil.ReadAll(response.Body)
assert.Contains(string(bytes), "sshproxy_connections_established 1")
assert.Contains(string(bytes), `sshproxy_connections_total{state="established"} 1`)
}
}

Expand Down
21 changes: 9 additions & 12 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,21 @@ type prometheusExporter struct {
var (
metrics = prometheusExporter{}

sshConnEstablishedDesc = prometheus.NewDesc("sshproxy_connections_established", "Established SSH connections", nil, nil)
sshConnFailedDesc = prometheus.NewDesc("sshproxy_connections_failed", "Failed SSH connections", nil, nil)
sshForwardEstablishedDesc = prometheus.NewDesc("sshproxy_forwardings_established", "Established SSH forwardings", nil, nil)
sshForwardFailedDesc = prometheus.NewDesc("sshproxy_forwardings_failed", "Failed SSH forwardings", nil, nil)
variableLabels = []string{"state"}
sshConnectionsDesc = prometheus.NewDesc("sshproxy_connections_total", "SSH connections", variableLabels, nil)
sshForwardingsDesc = prometheus.NewDesc("sshproxy_forwardings_total", "TCP forwardings", variableLabels, nil)
)

// Describe implements (part of the) prometheus.Collector interface.
func (e *prometheusExporter) Describe(c chan<- *prometheus.Desc) {
c <- sshConnEstablishedDesc
c <- sshConnFailedDesc
c <- sshForwardEstablishedDesc
c <- sshForwardFailedDesc
c <- sshConnectionsDesc
c <- sshForwardingsDesc
}

// Collect implements (part of the) prometheus.Collector interface.
func (e prometheusExporter) Collect(c chan<- prometheus.Metric) {
c <- prometheus.MustNewConstMetric(sshConnEstablishedDesc, prometheus.CounterValue, float64(e.connections.established))
c <- prometheus.MustNewConstMetric(sshConnFailedDesc, prometheus.CounterValue, float64(e.connections.failed))
c <- prometheus.MustNewConstMetric(sshForwardEstablishedDesc, prometheus.CounterValue, float64(e.forwardings.established))
c <- prometheus.MustNewConstMetric(sshForwardFailedDesc, prometheus.CounterValue, float64(e.forwardings.failed))
c <- prometheus.MustNewConstMetric(sshConnectionsDesc, prometheus.CounterValue, float64(e.connections.established), "established")
c <- prometheus.MustNewConstMetric(sshConnectionsDesc, prometheus.CounterValue, float64(e.connections.failed), "failed")
c <- prometheus.MustNewConstMetric(sshForwardingsDesc, prometheus.CounterValue, float64(e.forwardings.established), "established")
c <- prometheus.MustNewConstMetric(sshForwardingsDesc, prometheus.CounterValue, float64(e.forwardings.failed), "failed")
}

0 comments on commit 37cf45b

Please sign in to comment.