-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract metrics stuff into own package
- Loading branch information
1 parent
34735db
commit 9e797f5
Showing
5 changed files
with
199 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package metrics | ||
|
||
import "github.com/prometheus/client_golang/prometheus" | ||
|
||
// Collection is a struct containing pointers to all metrics that should be | ||
// exposed to Prometheus | ||
type Collection struct { | ||
CountTotal *prometheus.CounterVec | ||
ResponseBytesTotal *prometheus.CounterVec | ||
RequestBytesTotal *prometheus.CounterVec | ||
UpstreamSeconds *prometheus.SummaryVec | ||
UpstreamSecondsHist *prometheus.HistogramVec | ||
UpstreamConnectSeconds *prometheus.SummaryVec | ||
UpstreamConnectSecondsHist *prometheus.HistogramVec | ||
ResponseSeconds *prometheus.SummaryVec | ||
ResponseSecondsHist *prometheus.HistogramVec | ||
ParseErrorsTotal prometheus.Counter | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package metrics | ||
|
||
import ( | ||
"github.com/martin-helmich/prometheus-nginxlog-exporter/config" | ||
"github.com/martin-helmich/prometheus-nginxlog-exporter/relabeling" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
func inLabels(label string, labels []string) bool { | ||
for _, l := range labels { | ||
if label == l { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// Init initializes a metrics struct | ||
func (m *Collection) Init(cfg *config.NamespaceConfig) { | ||
cfg.MustCompile() | ||
|
||
labels := cfg.OrderedLabelNames | ||
counterLabels := labels | ||
|
||
for i := range cfg.RelabelConfigs { | ||
if !cfg.RelabelConfigs[i].OnlyCounter { | ||
labels = append(labels, cfg.RelabelConfigs[i].TargetLabel) | ||
} | ||
counterLabels = append(counterLabels, cfg.RelabelConfigs[i].TargetLabel) | ||
} | ||
|
||
for _, r := range relabeling.DefaultRelabelings { | ||
if !inLabels(r.TargetLabel, labels) { | ||
labels = append(labels, r.TargetLabel) | ||
} | ||
if !inLabels(r.TargetLabel, counterLabels) { | ||
counterLabels = append(counterLabels, r.TargetLabel) | ||
} | ||
} | ||
|
||
m.CountTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
Namespace: cfg.NamespacePrefix, | ||
ConstLabels: cfg.NamespaceLabels, | ||
Name: "http_response_count_total", | ||
Help: "Amount of processed HTTP requests", | ||
}, counterLabels) | ||
|
||
m.ResponseBytesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
Namespace: cfg.NamespacePrefix, | ||
ConstLabels: cfg.NamespaceLabels, | ||
Name: "http_response_size_bytes", | ||
Help: "Total amount of transferred bytes", | ||
}, labels) | ||
|
||
m.RequestBytesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
Namespace: cfg.NamespacePrefix, | ||
ConstLabels: cfg.NamespaceLabels, | ||
Name: "http_request_size_bytes", | ||
Help: "Total amount of received bytes", | ||
}, labels) | ||
|
||
m.UpstreamSeconds = prometheus.NewSummaryVec(prometheus.SummaryOpts{ | ||
Namespace: cfg.NamespacePrefix, | ||
ConstLabels: cfg.NamespaceLabels, | ||
Name: "http_upstream_time_seconds", | ||
Help: "Time needed by upstream servers to handle requests", | ||
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, | ||
}, labels) | ||
|
||
m.UpstreamSecondsHist = prometheus.NewHistogramVec(prometheus.HistogramOpts{ | ||
Namespace: cfg.NamespacePrefix, | ||
ConstLabels: cfg.NamespaceLabels, | ||
Name: "http_upstream_time_seconds_hist", | ||
Help: "Time needed by upstream servers to handle requests", | ||
Buckets: cfg.HistogramBuckets, | ||
}, labels) | ||
|
||
m.UpstreamConnectSeconds = prometheus.NewSummaryVec(prometheus.SummaryOpts{ | ||
Namespace: cfg.NamespacePrefix, | ||
ConstLabels: cfg.NamespaceLabels, | ||
Name: "http_upstream_connect_time_seconds", | ||
Help: "Time needed to connect to upstream servers", | ||
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, | ||
}, labels) | ||
|
||
m.UpstreamConnectSecondsHist = prometheus.NewHistogramVec(prometheus.HistogramOpts{ | ||
Namespace: cfg.NamespacePrefix, | ||
ConstLabels: cfg.NamespaceLabels, | ||
Name: "http_upstream_connect_time_seconds_hist", | ||
Help: "Time needed to connect to upstream servers", | ||
Buckets: cfg.HistogramBuckets, | ||
}, labels) | ||
|
||
m.ResponseSeconds = prometheus.NewSummaryVec(prometheus.SummaryOpts{ | ||
Namespace: cfg.NamespacePrefix, | ||
ConstLabels: cfg.NamespaceLabels, | ||
Name: "http_response_time_seconds", | ||
Help: "Time needed by NGINX to handle requests", | ||
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, | ||
}, labels) | ||
|
||
m.ResponseSecondsHist = prometheus.NewHistogramVec(prometheus.HistogramOpts{ | ||
Namespace: cfg.NamespacePrefix, | ||
ConstLabels: cfg.NamespaceLabels, | ||
Name: "http_response_time_seconds_hist", | ||
Help: "Time needed by NGINX to handle requests", | ||
Buckets: cfg.HistogramBuckets, | ||
}, labels) | ||
|
||
m.ParseErrorsTotal = prometheus.NewCounter(prometheus.CounterOpts{ | ||
Namespace: cfg.NamespacePrefix, | ||
ConstLabels: cfg.NamespaceLabels, | ||
Name: "parse_errors_total", | ||
Help: "Total number of log file lines that could not be parsed", | ||
}) | ||
} |
Oops, something went wrong.