Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Indices filter flag #764

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ elasticsearch_exporter --help
| es.shards | 1.0.3rc1 | If true, query stats for all indices in the cluster, including shard-level stats (implies `es.indices=true`). | false |
| es.snapshots | 1.0.4rc1 | If true, query stats for the cluster snapshots. | false |
| es.slm | | If true, query stats for SLM. | false |
| es.indices_selector | | If set, Only the selcted indices will be queried [settings, stats, mappings] based on the provided pattern. `Example: prefix_1_*,prefix_2_*` | _all |
| es.data_stream | | If true, query state for Data Steams. | false |
| es.timeout | 1.0.2 | Timeout for trying to get stats from Elasticsearch. (ex: 20s) | 5s |
| es.ca | 1.0.2 | Path to PEM file that contains trusted Certificate Authorities for the Elasticsearch connection. | |
Expand Down
22 changes: 14 additions & 8 deletions collector/indices.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Indices struct {
logger log.Logger
client *http.Client
url *url.URL
indicesSelector string
shards bool
aliases bool
clusterInfoCh chan *clusterinfo.Response
Expand All @@ -74,7 +75,7 @@ type Indices struct {
}

// NewIndices defines Indices Prometheus metrics
func NewIndices(logger log.Logger, client *http.Client, url *url.URL, shards bool, includeAliases bool) *Indices {
func NewIndices(logger log.Logger, client *http.Client, url *url.URL, shards bool, includeAliases bool, indicesSelector string) *Indices {

indexLabels := labels{
keys: func(...string) []string {
Expand Down Expand Up @@ -119,12 +120,13 @@ func NewIndices(logger log.Logger, client *http.Client, url *url.URL, shards boo
}

indices := &Indices{
logger: logger,
client: client,
url: url,
shards: shards,
aliases: includeAliases,
clusterInfoCh: make(chan *clusterinfo.Response),
logger: logger,
client: client,
url: url,
indicesSelector: indicesSelector,
shards: shards,
aliases: includeAliases,
clusterInfoCh: make(chan *clusterinfo.Response),
lastClusterInfo: &clusterinfo.Response{
ClusterName: "unknown_cluster",
},
Expand Down Expand Up @@ -1102,7 +1104,11 @@ func (i *Indices) fetchAndDecodeIndexStats() (indexStatsResponse, error) {
var isr indexStatsResponse

u := *i.url
u.Path = path.Join(u.Path, "/_all/_stats")
indicesStatsQueryPath := fmt.Sprintf("/%s/_stats", i.indicesSelector)
_ = level.Debug(i.logger).Log(
"msg", fmt.Sprintf("indices fetch query path: %s", indicesStatsQueryPath),
)
u.Path = path.Join(u.Path, indicesStatsQueryPath)
if i.shards {
u.RawQuery = "ignore_unavailable=true&level=shards"
} else {
Expand Down
19 changes: 11 additions & 8 deletions collector/indices_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ type indicesMappingsMetric struct {

// IndicesMappings information struct
type IndicesMappings struct {
logger log.Logger
client *http.Client
url *url.URL
logger log.Logger
client *http.Client
url *url.URL
indicesSelector string

up prometheus.Gauge
totalScrapes, jsonParseFailures prometheus.Counter
Expand All @@ -49,13 +50,14 @@ type IndicesMappings struct {
}

// NewIndicesMappings defines Indices IndexMappings Prometheus metrics
func NewIndicesMappings(logger log.Logger, client *http.Client, url *url.URL) *IndicesMappings {
func NewIndicesMappings(logger log.Logger, client *http.Client, url *url.URL, indicesSelector string) *IndicesMappings {
subsystem := "indices_mappings_stats"

return &IndicesMappings{
logger: logger,
client: client,
url: url,
logger: logger,
client: client,
url: url,
indicesSelector: indicesSelector,

up: prometheus.NewGauge(prometheus.GaugeOpts{
Name: prometheus.BuildFQName(namespace, subsystem, "up"),
Expand Down Expand Up @@ -157,7 +159,8 @@ func (im *IndicesMappings) getAndParseURL(u *url.URL) (*IndicesMappingsResponse,

func (im *IndicesMappings) fetchAndDecodeIndicesMappings() (*IndicesMappingsResponse, error) {
u := *im.url
u.Path = path.Join(u.Path, "/_all/_mappings")
indicesMappingsQueryPath := fmt.Sprintf("/%s/_mappings", im.indicesSelector)
u.Path = path.Join(u.Path, indicesMappingsQueryPath)
return im.getAndParseURL(&u)
}

Expand Down
4 changes: 2 additions & 2 deletions collector/indices_mappings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestMapping(t *testing.T) {
if err != nil {
t.Fatalf("Failed to parse URL: %s", err)
}
c := NewIndicesMappings(log.NewNopLogger(), http.DefaultClient, u)
c := NewIndicesMappings(log.NewNopLogger(), http.DefaultClient, u, "_all")
imr, err := c.fetchAndDecodeIndicesMappings()
if err != nil {
t.Fatalf("Failed to fetch or decode indices mappings: %s", err)
Expand Down Expand Up @@ -362,7 +362,7 @@ func TestIndexMappingFieldCount(t *testing.T) {
if err != nil {
t.Fatalf("Failed to parse URL: %s", err)
}
c := NewIndicesMappings(log.NewNopLogger(), http.DefaultClient, u)
c := NewIndicesMappings(log.NewNopLogger(), http.DefaultClient, u, "_all")
indicesMappingsResponse, err := c.fetchAndDecodeIndicesMappings()
if err != nil {
t.Fatalf("Failed to fetch or decode indices mappings: %s", err)
Expand Down
19 changes: 11 additions & 8 deletions collector/indices_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import (

// IndicesSettings information struct
type IndicesSettings struct {
logger log.Logger
client *http.Client
url *url.URL
logger log.Logger
client *http.Client
url *url.URL
indicesSelector string

up prometheus.Gauge
readOnlyIndices prometheus.Gauge
Expand All @@ -52,11 +53,12 @@ type indicesSettingsMetric struct {
}

// NewIndicesSettings defines Indices Settings Prometheus metrics
func NewIndicesSettings(logger log.Logger, client *http.Client, url *url.URL) *IndicesSettings {
func NewIndicesSettings(logger log.Logger, client *http.Client, url *url.URL, indicesSelector string) *IndicesSettings {
return &IndicesSettings{
logger: logger,
client: client,
url: url,
logger: logger,
client: client,
url: url,
indicesSelector: indicesSelector,

up: prometheus.NewGauge(prometheus.GaugeOpts{
Name: prometheus.BuildFQName(namespace, "indices_settings_stats", "up"),
Expand Down Expand Up @@ -154,7 +156,8 @@ func (cs *IndicesSettings) getAndParseURL(u *url.URL, data interface{}) error {
func (cs *IndicesSettings) fetchAndDecodeIndicesSettings() (IndicesSettingsResponse, error) {

u := *cs.url
u.Path = path.Join(u.Path, "/_all/_settings")
indicesSettingsQueryPath := fmt.Sprintf("/%s/_settings", cs.indicesSelector)
u.Path = path.Join(u.Path, indicesSettingsQueryPath)
var asr IndicesSettingsResponse
err := cs.getAndParseURL(&u, &asr)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion collector/indices_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestIndicesSettings(t *testing.T) {
if err != nil {
t.Fatalf("Failed to parse URL: %s", err)
}
c := NewIndicesSettings(log.NewNopLogger(), http.DefaultClient, u)
c := NewIndicesSettings(log.NewNopLogger(), http.DefaultClient, u, "_all")
nsr, err := c.fetchAndDecodeIndicesSettings()
if err != nil {
t.Fatalf("Failed to fetch or decode indices settings: %s", err)
Expand Down
4 changes: 2 additions & 2 deletions collector/indices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestIndices(t *testing.T) {
if err != nil {
t.Fatalf("Failed to parse URL: %s", err)
}
i := NewIndices(log.NewNopLogger(), http.DefaultClient, u, false, false)
i := NewIndices(log.NewNopLogger(), http.DefaultClient, u, false, false, "_all")
stats, err := i.fetchAndDecodeIndexStats()
if err != nil {
t.Fatalf("Failed to fetch or decode indices stats: %s", err)
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestAliases(t *testing.T) {
if err != nil {
t.Fatalf("Failed to parse URL: %s", err)
}
i := NewIndices(log.NewNopLogger(), http.DefaultClient, u, false, true)
i := NewIndices(log.NewNopLogger(), http.DefaultClient, u, false, true, "_all")
stats, err := i.fetchAndDecodeIndexStats()
if err != nil {
t.Fatalf("Failed to fetch or decode indices stats: %s", err)
Expand Down
9 changes: 6 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func main() {
esExportSLM = kingpin.Flag("es.slm",
"Export stats for SLM snapshots.").
Default("false").Bool()
esIndicesSelector = kingpin.Flag("es.indices_selector",
"Selcted indices by name for which metrics should be exposed, using prefix with wildcards and/or commas as multi-selection delimiter.").
chapost1 marked this conversation as resolved.
Show resolved Hide resolved
Default("_all").String()
esExportDataStream = kingpin.Flag("es.data_stream",
"Export stas for Data Streams.").
Default("false").Bool()
Expand Down Expand Up @@ -200,7 +203,7 @@ func main() {

if *esExportIndices || *esExportShards {
prometheus.MustRegister(collector.NewShards(logger, httpClient, esURL))
iC := collector.NewIndices(logger, httpClient, esURL, *esExportShards, *esExportIndexAliases)
iC := collector.NewIndices(logger, httpClient, esURL, *esExportShards, *esExportIndexAliases, *esIndicesSelector)
prometheus.MustRegister(iC)
if registerErr := clusterInfoRetriever.RegisterConsumer(iC); registerErr != nil {
level.Error(logger).Log("msg", "failed to register indices collector in cluster info")
Expand All @@ -217,11 +220,11 @@ func main() {
}

if *esExportIndicesSettings {
prometheus.MustRegister(collector.NewIndicesSettings(logger, httpClient, esURL))
prometheus.MustRegister(collector.NewIndicesSettings(logger, httpClient, esURL, *esIndicesSelector))
}

if *esExportIndicesMappings {
prometheus.MustRegister(collector.NewIndicesMappings(logger, httpClient, esURL))
prometheus.MustRegister(collector.NewIndicesMappings(logger, httpClient, esURL, *esIndicesSelector))
}

if *esExportILM {
Expand Down