Skip to content

Commit

Permalink
Merge pull request #950 from fujiwara/fix-es
Browse files Browse the repository at this point in the history
Fix Elasticsearch plugin.
  • Loading branch information
yseto committed Nov 9, 2022
2 parents 9dbeaf5 + fbaf8c9 commit 66cf1f8
Show file tree
Hide file tree
Showing 3 changed files with 988 additions and 202 deletions.
72 changes: 53 additions & 19 deletions mackerel-plugin-elasticsearch/lib/elasticsearch.go
@@ -1,6 +1,7 @@
package mpelasticsearch

import (
"crypto/tls"
"encoding/json"
"errors"
"flag"
Expand All @@ -25,42 +26,45 @@ var metricPlace = map[string][]string{
"total_refresh": {"indices", "refresh", "total"},
"total_flush": {"indices", "flush", "total"},
"total_warmer": {"indices", "warmer", "total"},
"total_percolate": {"indices", "percolate", "total"},
"total_suggest": {"indices", "suggest", "total"},
"total_percolate": {"indices", "percolate", "total"}, // MISSINGv7 = no value after v7.0 (at least)
"total_suggest": {"indices", "suggest", "total"}, // MISSINGv7
"docs_count": {"indices", "docs", "count"},
"docs_deleted": {"indices", "docs", "deleted"},
"fielddata_size": {"indices", "fielddata", "memory_size_in_bytes"},
"filter_cache_size": {"indices", "filter_cache", "memory_size_in_bytes"},
"filter_cache_size": {"indices", "filter_cache", "memory_size_in_bytes"}, // MISSINGv7
"segments_size": {"indices", "segments", "memory_in_bytes"},
"segments_index_writer_size": {"indices", "segments", "index_writer_memory_in_bytes"},
"segments_version_map_size": {"indices", "segments", "version_map_memory_in_bytes"},
"segments_fixed_bit_set_size": {"indices", "segments", "fixed_bit_set_memory_in_bytes"},
"evictions_fielddata": {"indices", "fielddata", "evictions"},
"evictions_filter_cache": {"indices", "filter_cache", "evictions"},
"evictions_filter_cache": {"indices", "filter_cache", "evictions"}, // MISSINGv7
"heap_used": {"jvm", "mem", "heap_used_in_bytes"},
"heap_max": {"jvm", "mem", "heap_max_in_bytes"},
"threads_generic": {"thread_pool", "generic", "threads"},
"threads_index": {"thread_pool", "index", "threads"},
"threads_snapshot_data": {"thread_pool", "snapshot_data", "threads"},
"threads_index": {"thread_pool", "index", "threads"}, // MISSINGv7
"threads_snapshot_data": {"thread_pool", "snapshot_data", "threads"}, // MISSINGv7
"threads_get": {"thread_pool", "get", "threads"},
"threads_bench": {"thread_pool", "bench", "threads"},
"threads_bench": {"thread_pool", "bench", "threads"}, // MISSINGv7
"threads_snapshot": {"thread_pool", "snapshot", "threads"},
"threads_merge": {"thread_pool", "merge", "threads"},
"threads_suggest": {"thread_pool", "suggest", "threads"},
"threads_bulk": {"thread_pool", "bulk", "threads"},
"threads_optimize": {"thread_pool", "optimize", "threads"},
"threads_merge": {"thread_pool", "merge", "threads"}, // MISSINGv7
"threads_suggest": {"thread_pool", "suggest", "threads"}, // MISSINGv7
"threads_bulk": {"thread_pool", "bulk", "threads"}, // MISSINGv7
"threads_optimize": {"thread_pool", "optimize", "threads"}, // MISSINGv7
"threads_warmer": {"thread_pool", "warmer", "threads"},
"threads_flush": {"thread_pool", "flush", "threads"},
"threads_search": {"thread_pool", "search", "threads"},
"threads_percolate": {"thread_pool", "percolate", "threads"},
"threads_percolate": {"thread_pool", "percolate", "threads"}, // MISSINGv7
"threads_refresh": {"thread_pool", "refresh", "threads"},
"threads_management": {"thread_pool", "management", "threads"},
"threads_fetch_shard_started": {"thread_pool", "fetch_shard_started", "threads"},
"threads_fetch_shard_store": {"thread_pool", "fetch_shard_store", "threads"},
"threads_listener": {"thread_pool", "listener", "threads"},
"threads_listener": {"thread_pool", "listener", "threads"}, // MISSINGv8
"count_rx": {"transport", "rx_count"},
"count_tx": {"transport", "tx_count"},
"open_file_descriptors": {"process", "open_file_descriptors"},
"compilations": {"script", "compilations"},
"cache_evictions": {"script", "cache_evictions"},
"compilation_limit_triggered": {"script", "compilation_limit_triggered"},
}

func getFloatValue(s map[string]interface{}, keys []string) (float64, error) {
Expand Down Expand Up @@ -89,9 +93,13 @@ func getFloatValue(s map[string]interface{}, keys []string) (float64, error) {

// ElasticsearchPlugin mackerel plugin for Elasticsearch
type ElasticsearchPlugin struct {
URI string
Prefix string
LabelPrefix string
URI string
Prefix string
LabelPrefix string
Insecure bool
User string
Password string
SuppressMissingError bool
}

// FetchMetrics interface for mackerelplugin
Expand All @@ -101,8 +109,15 @@ func (p ElasticsearchPlugin) FetchMetrics() (map[string]float64, error) {
return nil, err
}
req.Header.Set("User-Agent", "mackerel-plugin-elasticsearch")

resp, err := http.DefaultClient.Do(req)
if p.User != "" && p.Password != "" {
req.SetBasicAuth(p.User, p.Password)
}
client := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: p.Insecure},
},
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -130,7 +145,9 @@ func (p ElasticsearchPlugin) FetchMetrics() (map[string]float64, error) {
for k, v := range metricPlace {
val, err := getFloatValue(node, v)
if err != nil {
logger.Errorf("Failed to find '%s': %s", k, err)
if !p.SuppressMissingError {
logger.Errorf("Failed to find '%s': %s", k, err)
}
continue
}

Expand Down Expand Up @@ -243,6 +260,15 @@ func (p ElasticsearchPlugin) GraphDefinition() map[string]mp.Graphs {
{Name: "open_file_descriptors", Label: "Open File Descriptors"},
},
},
p.Prefix + ".script": {
Label: (p.LabelPrefix + " Script"),
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "compilations", Label: "Compilations", Diff: true},
{Name: "cache_evictions", Label: "Cache Evictions", Diff: true},
{Name: "compilation_limit_triggered", Label: "Compilation Limit Triggered", Diff: true},
},
},
}

return graphdef
Expand All @@ -256,6 +282,10 @@ func Do() {
optPrefix := flag.String("metric-key-prefix", "elasticsearch", "Metric key prefix")
optLabelPrefix := flag.String("metric-label-prefix", "", "Metric Label prefix")
optTempfile := flag.String("tempfile", "", "Temp file name")
optInsecure := flag.Bool("insecure", false, "Skip TLS certificate verification")
optUser := flag.String("user", "", "Basic auth user")
optPassword := flag.String("password", "", "Basic auth password")
optSuppressMissingError := flag.Bool("suppress-missing-error", false, "Suppress ERROR for missing values")
flag.Parse()

var elasticsearch ElasticsearchPlugin
Expand All @@ -266,6 +296,10 @@ func Do() {
} else {
elasticsearch.LabelPrefix = *optLabelPrefix
}
elasticsearch.Insecure = *optInsecure
elasticsearch.User = *optUser
elasticsearch.Password = *optPassword
elasticsearch.SuppressMissingError = *optSuppressMissingError

helper := mp.NewMackerelPlugin(elasticsearch)
if *optTempfile != "" {
Expand Down
14 changes: 8 additions & 6 deletions mackerel-plugin-elasticsearch/lib/elasticsearch_test.go
Expand Up @@ -31,6 +31,7 @@ func TestGraphDefinition(t *testing.T) {
assert.EqualValues(t, "threads_fetch_shard_started", graphdef["elasticsearch.thread_pool.threads"].Metrics[16].Name)
assert.EqualValues(t, "threads_fetch_shard_store", graphdef["elasticsearch.thread_pool.threads"].Metrics[17].Name)
assert.EqualValues(t, "threads_listener", graphdef["elasticsearch.thread_pool.threads"].Metrics[18].Name)
assert.EqualValues(t, "compilation_limit_triggered", graphdef["elasticsearch.script"].Metrics[2].Name)
}

func TestFetchMetrics(t *testing.T) {
Expand All @@ -44,10 +45,11 @@ func TestFetchMetrics(t *testing.T) {
t.Fatal(err)
}

assert.EqualValues(t, 6991, stat["http_opened"])
assert.EqualValues(t, 1, stat["threads_generic"])
assert.EqualValues(t, 0, stat["threads_merge"])
assert.EqualValues(t, 2, stat["threads_fetch_shard_started"])
assert.EqualValues(t, 3, stat["threads_fetch_shard_store"])
assert.EqualValues(t, 1, stat["threads_listener"])
assert.EqualValues(t, 37, stat["http_opened"])
assert.EqualValues(t, 8, stat["threads_generic"])
assert.EqualValues(t, 13, stat["threads_search"])
assert.EqualValues(t, 0, stat["threads_fetch_shard_started"])
assert.EqualValues(t, 0, stat["threads_fetch_shard_store"])
assert.EqualValues(t, 331, stat["open_file_descriptors"])
assert.EqualValues(t, 1, stat["compilations"])
}

0 comments on commit 66cf1f8

Please sign in to comment.