Skip to content

Commit

Permalink
Merge pull request #38 from mbobakov/master
Browse files Browse the repository at this point in the history
Support for filter zones
  • Loading branch information
sysulq authored Sep 28, 2017
2 parents ad91859 + e40ee9e commit adf2d03
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 15 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Simple server that scrapes Nginx [vts](https://github.com/vozlt/nginx-module-vts
* [Metrics](#metrics)
* [Server main](#server-main)
* [Server zones](#server-zones)
* [Filter zones](#filter-zones)
* [Upstreams](#upstreams)

## Dependency
Expand Down Expand Up @@ -121,6 +122,30 @@ nginx_server_bytes{direction="in",host="test.domain.com"} 21
nginx_server_cache{host="test.domain.com",status="bypass"} 2
```

### Filter zones

**Metrics details**

Nginx data | Name | Exposed informations
------------------ | ------------------------------- | ------------------------
**Requests** | `{NAMESPACE}_filter_requests` | code [2xx, 3xx, 4xx, 5xx and total], filter, filter name
**Bytes** | `{NAMESPACE}_filter_bytes` | direction [in, out], filter, filter name
**Response time** | `{NAMESPACE}_filter_response` | filter, filter name

**Metrics output example**

``` txt
# Filter Requests
nginx_upstream_requests{code="1xx", filter="country", filterName="BY"} 0
# Filter Bytes
nginx_upstream_bytes{direction="in", filter="country", filterName="BY"} 0
# Filter Response time
nginx_upstream_response{filter="country", filterName="BY"} 99
```


### Upstreams

**Metrics details**
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.6
0.7
49 changes: 35 additions & 14 deletions nginx_vts_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ type NginxVts struct {
Handled int `json:"handled"`
Requests int `json:"requests"`
} `json:"connections"`
ServerZones map[string]Server `json:"serverZones"`
UpstreamZones map[string][]Upstream `json:"upstreamZones"`
CacheZones map[string]Cache `json:"cacheZones"`
ServerZones map[string]Server `json:"serverZones"`
UpstreamZones map[string][]Upstream `json:"upstreamZones"`
FilterZones map[string]map[string]Upstream `json:"filterZones"`
CacheZones map[string]Cache `json:"cacheZones"`
}

type Server struct {
Expand Down Expand Up @@ -138,7 +139,7 @@ type Cache struct {
type Exporter struct {
URI string

serverMetrics, upstreamMetrics, cacheMetrics map[string]*prometheus.Desc
serverMetrics, upstreamMetrics, filterMetrics, cacheMetrics map[string]*prometheus.Desc
}

func newServerMetric(metricName string, docString string, labels []string) *prometheus.Desc {
Expand All @@ -155,6 +156,13 @@ func newUpstreamMetric(metricName string, docString string, labels []string) *pr
)
}

func newFilterMetric(metricName string, docString string, labels []string) *prometheus.Desc {
return prometheus.NewDesc(
prometheus.BuildFQName(*metricsNamespace, "filter", metricName),
docString, labels, nil,
)
}

func newCacheMetric(metricName string, docString string, labels []string) *prometheus.Desc {
return prometheus.NewDesc(
prometheus.BuildFQName(*metricsNamespace, "cache", metricName),
Expand All @@ -177,6 +185,11 @@ func NewExporter(uri string) *Exporter {
"bytes": newUpstreamMetric("bytes", "request/response bytes", []string{"upstream", "direction"}),
"response": newUpstreamMetric("response", "request response time", []string{"upstream", "backend"}),
},
filterMetrics: map[string]*prometheus.Desc{
"requests": newFilterMetric("requests", "requests counter", []string{"filter", "filterName", "code"}),
"bytes": newFilterMetric("bytes", "request/response bytes", []string{"filter", "filterName", "direction"}),
"response": newFilterMetric("response", "request response time", []string{"filter", "filterName"}),
},
cacheMetrics: map[string]*prometheus.Desc{
"requests": newCacheMetric("requests", "cache requests counter", []string{"zone", "status"}),
"bytes": newCacheMetric("bytes", "cache request/response bytes", []string{"zone", "direction"}),
Expand All @@ -191,6 +204,9 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
for _, m := range e.upstreamMetrics {
ch <- m
}
for _, m := range e.filterMetrics {
ch <- m
}
for _, m := range e.cacheMetrics {
ch <- m
}
Expand Down Expand Up @@ -265,16 +281,21 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {

ch <- prometheus.MustNewConstMetric(e.upstreamMetrics["response"], prometheus.GaugeValue, float64(s.ResponseMsec), name, s.Server)
}

ch <- prometheus.MustNewConstMetric(e.upstreamMetrics["requests"], prometheus.CounterValue, total, name, "total")
ch <- prometheus.MustNewConstMetric(e.upstreamMetrics["requests"], prometheus.CounterValue, one, name, "1xx")
ch <- prometheus.MustNewConstMetric(e.upstreamMetrics["requests"], prometheus.CounterValue, two, name, "2xx")
ch <- prometheus.MustNewConstMetric(e.upstreamMetrics["requests"], prometheus.CounterValue, three, name, "3xx")
ch <- prometheus.MustNewConstMetric(e.upstreamMetrics["requests"], prometheus.CounterValue, four, name, "4xx")
ch <- prometheus.MustNewConstMetric(e.upstreamMetrics["requests"], prometheus.CounterValue, five, name, "5xx")

ch <- prometheus.MustNewConstMetric(e.upstreamMetrics["bytes"], prometheus.CounterValue, inbytes, name, "in")
ch <- prometheus.MustNewConstMetric(e.upstreamMetrics["bytes"], prometheus.CounterValue, outbytes, name, "out")
}
// FilterZones
for filter, values := range nginxVtx.FilterZones {
for name, stat := range values {
ch <- prometheus.MustNewConstMetric(e.filterMetrics["response"], prometheus.GaugeValue, float64(stat.ResponseMsec), filter, name)
ch <- prometheus.MustNewConstMetric(e.filterMetrics["requests"], prometheus.CounterValue, float64(stat.RequestCounter), filter, name, "total")
ch <- prometheus.MustNewConstMetric(e.filterMetrics["requests"], prometheus.CounterValue, float64(stat.Responses.OneXx), filter, name, "1xx")
ch <- prometheus.MustNewConstMetric(e.filterMetrics["requests"], prometheus.CounterValue, float64(stat.Responses.TwoXx), filter, name, "2xx")
ch <- prometheus.MustNewConstMetric(e.filterMetrics["requests"], prometheus.CounterValue, float64(stat.Responses.ThreeXx), filter, name, "3xx")
ch <- prometheus.MustNewConstMetric(e.filterMetrics["requests"], prometheus.CounterValue, float64(stat.Responses.FourXx), filter, name, "4xx")
ch <- prometheus.MustNewConstMetric(e.filterMetrics["requests"], prometheus.CounterValue, float64(stat.Responses.FiveXx), filter, name, "5xx")

ch <- prometheus.MustNewConstMetric(e.filterMetrics["bytes"], prometheus.CounterValue, float64(stat.InBytes), filter, name, "in")
ch <- prometheus.MustNewConstMetric(e.filterMetrics["bytes"], prometheus.CounterValue, float64(stat.OutBytes), filter, name, "out")
}
}

// CacheZones
Expand Down

0 comments on commit adf2d03

Please sign in to comment.