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 slowlog metricset to redis #7745

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ https://github.com/elastic/beats/compare/v6.2.3...master[Check the HEAD diff]
- Release prometheus collector metricset as GA. {pull}7660[7660]
- Add Elasticsearch `cluster_stats` metricset. {pull}7638[7638]
- Added `basepath` setting for HTTP-based metricsets {pull}7700[7700]
- Add `slowlog` metricset to Redis {pull}7745[7745]

*Packetbeat*

Expand Down
88 changes: 88 additions & 0 deletions metricbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -15238,6 +15238,94 @@ type: long



--

[float]
== slowlog fields

An entry in the Redis slow queries log.



*`redis.slowlog.id`*::
+
--
type: long

A unique progressive identifier for every slow log entry.


--

*`redis.slowlog.command`*::
+
--
type: keyword

The command


--

*`redis.slowlog.key`*::
+
--
type: keyword

--

*`redis.slowlog.args`*::
+
--
type: array

The array composing the arguments of the command.


--

*`redis.slowlog.duration.us`*::
+
--
type: long

format: duration

The amount of time needed for its execution, in microseconds.


--

*`redis.slowlog.timestamp`*::
+
--
type: date

format: epoch_millis

The unix timestamp at which the logged command was processed.


--

*`redis.slowlog.client.name`*::
+
--
type: keyword

Client IP address and port (4.0 only).


--

*`redis.slowlog.client.address`*::
+
--
type: keyword

Client name if set via the `CLIENT SETNAME` command (4.0 only).


--

[[exported-fields-system]]
Expand Down
4 changes: 4 additions & 0 deletions metricbeat/docs/modules/redis.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ The following metricsets are available:

* <<metricbeat-metricset-redis-keyspace,keyspace>>

* <<metricbeat-metricset-redis-slowlog,slowlog>>

include::redis/info.asciidoc[]

include::redis/keyspace.asciidoc[]

include::redis/slowlog.asciidoc[]

23 changes: 23 additions & 0 deletions metricbeat/docs/modules/redis/slowlog.asciidoc
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-slowlog]]
=== Redis slowlog metricset

experimental[]

include::../../../module/redis/slowlog/_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/slowlog/_meta/data.json[]
----
3 changes: 2 additions & 1 deletion metricbeat/docs/modules_list.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ This file is generated! See scripts/docs_collector.py
|<<metricbeat-metricset-rabbitmq-node,node>> beta[]
|<<metricbeat-metricset-rabbitmq-queue,queue>> beta[]
|<<metricbeat-module-redis,Redis>> |image:./images/icon-yes.png[Prebuilt dashboards are available] |
.2+| .2+| |<<metricbeat-metricset-redis-info,info>>
.3+| .3+| |<<metricbeat-metricset-redis-info,info>>
|<<metricbeat-metricset-redis-keyspace,keyspace>>
|<<metricbeat-metricset-redis-slowlog,slowlog>> experimental[]
|<<metricbeat-module-system,System>> |image:./images/icon-yes.png[Prebuilt dashboards are available] |
.13+| .13+| |<<metricbeat-metricset-system-core,core>>
|<<metricbeat-metricset-system-cpu,cpu>>
Expand Down
1 change: 1 addition & 0 deletions metricbeat/include/list.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion metricbeat/module/redis/fields.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions metricbeat/module/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,30 @@ func FetchRedisInfo(stat string, c rd.Conn) (map[string]string, error) {
return ParseRedisInfo(out), nil
}

// FetchSlowLog returns slow operations on redis
func FetchSlowLog(c rd.Conn) ([]interface{}, error) {
defer c.Close()

logs, err := rd.Values(c.Do("SLOWLOG", "get"))
if err != nil {
logp.Err("Error retrieving slowlog: %v", err)
return nil, err
}

return logs, nil
}

// FetchSlowLogLength returns count of slow operations
func FetchSlowLogLength(c rd.Conn) (int64, error) {
count, err := rd.Int64(c.Do("SLOWLOG", "len"))
if err != nil {
logp.Err("Error retrieving slowlog len: %v", err)
return 0, err
}

return count, nil
}

// CreatePool creates a redis connection pool
func CreatePool(
host, password, network string,
Expand Down
19 changes: 19 additions & 0 deletions metricbeat/module/redis/slowlog/_meta/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"@timestamp":"2016-05-23T08:05:34.853Z",
"beat":{
"hostname":"beathost",
"name":"beathost"
},
"metricset":{
"host":"localhost",
"module":"redis",
"name":"slowlog",
"rtt":44269
},
"redis":{
"slowlog":{
"example": "slowlog"
}
},
"type":"metricsets"
}
2 changes: 2 additions & 0 deletions metricbeat/module/redis/slowlog/_meta/docs.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The Redis `slowlog` metricset collects slowlog from Redis by running the
http://redis.io/commands/slowlog[`SLOWLOG`] command and parsing the returned result.
39 changes: 39 additions & 0 deletions metricbeat/module/redis/slowlog/_meta/fields.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
- name: slowlog
type: group
description: >
An entry in the Redis slow queries log.
fields:
- name: id
type: long
description: >
A unique progressive identifier for every slow log entry.
- name: command
type: keyword
description: >
The command
- name: key
type: keyword
descirption: >
The key affected by the command
- name: args
type: array
description: >
The array composing the arguments of the command.
- name: duration.us
type: long
format: duration
description: >
The amount of time needed for its execution, in microseconds.
- name: timestamp
type: date
format: epoch_millis
description: >
The unix timestamp at which the logged command was processed.
- name: client.name
type: keyword
description: >
Client IP address and port (4.0 only).
- name: client.address
type: keyword
description: >
Client name if set via the `CLIENT SETNAME` command (4.0 only).
88 changes: 88 additions & 0 deletions metricbeat/module/redis/slowlog/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// 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 slowlog

import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
rd "github.com/garyburd/redigo/redis"
)

// log contains all data related to one slowlog entry
type log struct {
id int64
timestamp int64
duration int
cmd string
key string
args []string
clientAddress string
clientName string
}

// Map data to MapStr
func eventMapping(slowlogs []interface{}) []common.MapStr {
var events []common.MapStr
for _, item := range slowlogs {
entry, err := rd.Values(item, nil)
if err != nil {
logp.Err("Error loading slowlog values: %s", err)
continue
}

var log log
var args []string
rd.Scan(entry, &log.id, &log.timestamp, &log.duration, &args, &log.clientAddress, &log.clientName)

// This splits up the args into cmd, key, args.
argsLen := len(args)
if argsLen > 0 {
log.cmd = args[0]
}
if argsLen > 1 {
log.key = args[1]
}

// This could contain confidential data, processors should be used to drop it if needed
if argsLen > 2 {
log.args = args[2:]
}

event := common.MapStr{
"id": log.id,
"command": log.cmd,
"key": log.key,
"duration": common.MapStr{
"us": log.duration,
},
"timestamp": log.timestamp,
"client": common.MapStr{
"name": log.clientName,
"address": log.clientAddress,
},
}

if log.args != nil {
event["args"] = log.args
}

events = append(events, event)
}

return events
}
Loading