-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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 redis key metricset #9657
Add redis key metricset #9657
Changes from 1 commit
39aebc0
ce32554
9cedbe1
9dcf378
32881d4
9621ba2
1a78456
700b610
aa1e849
331b847
b38a1ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//// | ||
This file is generated! See scripts/docs_collector.py | ||
//// | ||
|
||
[[metricbeat-metricset-redis-key]] | ||
=== Redis key metricset | ||
|
||
beta[] | ||
|
||
include::../../../module/redis/key/_meta/docs.asciidoc[] | ||
|
||
|
||
==== Fields | ||
|
||
For a description of each field in the metricset, see the | ||
<<exported-fields-redis,exported fields>> section. | ||
|
||
Here is an example document generated by this metricset: | ||
|
||
[source,json] | ||
---- | ||
include::../../../module/redis/key/_meta/data.json[] | ||
---- |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"@timestamp": "2017-10-12T08:05:34.853Z", | ||
"agent": { | ||
"hostname": "host.example.com", | ||
"name": "host.example.com" | ||
}, | ||
"event": { | ||
"dataset": "redis.key", | ||
"duration": 115000, | ||
"module": "redis" | ||
}, | ||
"metricset": { | ||
"name": "key" | ||
}, | ||
"redis": { | ||
"key": { | ||
"expire": { | ||
"ttl": 360 | ||
}, | ||
"length": 3, | ||
"name": "foo", | ||
"type": "string", | ||
"value": "bar" | ||
}, | ||
"keyspace": { | ||
"id": "db0" | ||
} | ||
}, | ||
"service": { | ||
"address": "192.168.16.2:6379", | ||
"type": "redis" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
The Redis `key` metricset collects information about Redis keys. | ||
|
||
For each key matching one of the configured patterns, an event is sent to | ||
elasticsearch with information about this key, what includes the type, its | ||
jsoriano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
length when available, the value when it is a string, and its ttl. | ||
|
||
Patterns are configured as a list containing these fields: | ||
* `pattern` (required): pattern for key names, as accepted by the Redis | ||
`KEYS` or `SCAN` commands. | ||
* `max` (optional): safeguard when using patterns with wildcards to avoid | ||
collecting too many keys (Default: 0, no limit) | ||
* `keyspace` (optional): Identifier of the database to use to look for the keys | ||
(Default: 0) | ||
|
||
For example the following configuration will collect information about all keys | ||
whose name starts with `pipeline-*`, with a limit of 20 keys. | ||
|
||
[source,yaml] | ||
------------------------------------------------------------------------------ | ||
- module: redis | ||
metricsets: ['key'] | ||
key.patterns: | ||
- name: 'pipeline-*' | ||
max: 20 | ||
jsoriano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
------------------------------------------------------------------------------ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
- name: key | ||
type: group | ||
description: > | ||
`key` contains information about keys. | ||
release: beta | ||
fields: | ||
- name: name | ||
type: keyword | ||
description: > | ||
Key name. | ||
|
||
- name: type | ||
type: keyword | ||
description: > | ||
Key type as shown by `TYPE` command. | ||
|
||
- name: length | ||
type: long | ||
description: > | ||
Length of the key (Number of elements for lists, length for strings, cardinality for sets). | ||
|
||
- name: value | ||
type: text | ||
jsoriano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
description: > | ||
Value of the key for keys of type `string`. | ||
|
||
- name: expire.ttl | ||
type: long | ||
description: > | ||
Seconds to expire. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package key | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/metricbeat/mb" | ||
) | ||
|
||
func eventMapping(r mb.ReporterV2, keyspace uint, info map[string]interface{}) { | ||
r.Event(mb.Event{ | ||
MetricSetFields: info, | ||
ModuleFields: common.MapStr{ | ||
"keyspace": common.MapStr{ | ||
"id": fmt.Sprintf("db%d", keyspace), | ||
jsoriano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package key | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/elastic/beats/libbeat/logp" | ||
"github.com/elastic/beats/metricbeat/mb" | ||
"github.com/elastic/beats/metricbeat/mb/parse" | ||
"github.com/elastic/beats/metricbeat/module/redis" | ||
|
||
rd "github.com/garyburd/redigo/redis" | ||
) | ||
|
||
var ( | ||
debugf = logp.MakeDebug("redis-key") | ||
) | ||
|
||
func init() { | ||
mb.Registry.MustAddMetricSet("redis", "key", New, | ||
mb.WithHostParser(parse.PassThruHostParser), | ||
) | ||
} | ||
|
||
// MetricSet for fetching Redis server information and statistics. | ||
type MetricSet struct { | ||
mb.BaseMetricSet | ||
pool *rd.Pool | ||
patterns []KeyPattern | ||
} | ||
|
||
// KeyPattern contains the information required to query keys | ||
type KeyPattern struct { | ||
jsoriano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Keyspace uint `config:"keyspace"` | ||
Pattern string `config:"pattern" validate:"required"` | ||
Max uint `config:"max"` | ||
} | ||
|
||
// New creates new instance of MetricSet | ||
func New(base mb.BaseMetricSet) (mb.MetricSet, error) { | ||
// Unpack additional configuration options. | ||
config := struct { | ||
IdleTimeout time.Duration `config:"idle_timeout"` | ||
Network string `config:"network"` | ||
MaxConn int `config:"maxconn" validate:"min=1"` | ||
Password string `config:"password"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Config and pool initialization can be probably reused in other metricsets, to be fixed in a follow up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue created for follow ups #9678 |
||
Patterns []KeyPattern `config:"key.patterns" validate:"nonzero,required"` | ||
}{ | ||
Network: "tcp", | ||
MaxConn: 10, | ||
Password: "", | ||
} | ||
err := base.Module().UnpackConfig(&config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &MetricSet{ | ||
BaseMetricSet: base, | ||
pool: redis.CreatePool(base.Host(), config.Password, config.Network, | ||
config.MaxConn, config.IdleTimeout, base.Module().Config().Timeout), | ||
patterns: config.Patterns, | ||
}, nil | ||
} | ||
|
||
// Fetch fetches information from Redis keys | ||
func (m *MetricSet) Fetch(r mb.ReporterV2) { | ||
conn := m.pool.Get() | ||
for _, p := range m.patterns { | ||
if err := redis.Select(conn, p.Keyspace); err != nil { | ||
logp.Err("Failed to select keyspace %d: %s", p.Keyspace, err) | ||
continue | ||
} | ||
|
||
keys, err := redis.FetchKeys(conn, p.Pattern, p.Max) | ||
if err != nil { | ||
logp.Err("Failed to fetch list of keys in keyspace %d with pattern '%s': %s", p.Keyspace, p.Pattern, err) | ||
continue | ||
} | ||
if p.Max > 0 && len(keys) > int(p.Max) { | ||
debugf("Collecting stats for %d keys, but there are more available for pattern '%s' in keyspace %d", p.Max) | ||
keys = keys[:p.Max] | ||
} | ||
|
||
for _, key := range keys { | ||
keyInfo, err := redis.FetchKeyInfo(conn, key) | ||
if err != nil { | ||
logp.Err("Failed to fetch key info for key %s in keyspace %d", key, p.Keyspace) | ||
continue | ||
} | ||
eventMapping(r, p.Keyspace, keyInfo) | ||
} | ||
} | ||
} | ||
|
||
// Close connections | ||
func (m *MetricSet) Close() error { | ||
return m.pool.Close() | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this needs to be added in other metricsets too, to be added in the follow-up PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we go GA with this directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, let's go for GA, I forgot to add the beta warning in the metricset in any case 😄