-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Metricbeat : new metricset image for docker module (#3467)
- Loading branch information
1 parent
3e77723
commit 15db49f
Showing
14 changed files
with
363 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//// | ||
This file is generated! See scripts/docs_collector.py | ||
//// | ||
|
||
[[metricbeat-metricset-docker-image]] | ||
include::../../../module/docker/image/_meta/docs.asciidoc[] | ||
|
||
|
||
==== Fields | ||
|
||
For a description of each field in the metricset, see the | ||
<<exported-fields-docker,exported fields>> section. | ||
|
||
Here is an example document generated by this metricset: | ||
|
||
[source,json] | ||
---- | ||
include::../../../module/docker/image/_meta/data.json[] | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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":"docker", | ||
"name":"image", | ||
"rtt":44269 | ||
}, | ||
"docker":{ | ||
"image":{ | ||
"example": "image" | ||
} | ||
}, | ||
"type":"metricsets" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
=== docker image MetricSet | ||
|
||
This is the image metricset of the module docker. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
- name: image | ||
type: group | ||
description: > | ||
Docker image metrics. | ||
fields: | ||
- name: id | ||
type: group | ||
description: > | ||
The image layers identifier. | ||
fields: | ||
- name: current | ||
type: keyword | ||
description: > | ||
Unique image identifier given upon its creation. | ||
- name: parent | ||
type: keyword | ||
description: > | ||
Identifier of the image, if it exists, from which the current image directly descends. | ||
- name: created | ||
type: date | ||
description: > | ||
Date and time when the image was created. | ||
- name: size | ||
type: group | ||
description: > | ||
Image size layers. | ||
fields: | ||
- name: virtual | ||
type: long | ||
description: > | ||
Size of the image. | ||
- name: regular | ||
type: long | ||
description: > | ||
Total size of the all cached images associated to the current image. | ||
# TODO : How to describe tags & labels list ? | ||
# - name: tags | ||
# type: list ? | ||
# description: > | ||
# Descriptive or given name(s) to the image. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package image | ||
|
||
import ( | ||
"github.com/elastic/beats/libbeat/common" | ||
|
||
"github.com/elastic/beats/metricbeat/module/docker" | ||
dc "github.com/fsouza/go-dockerclient" | ||
"time" | ||
) | ||
|
||
func eventsMapping(imagesList []dc.APIImages) []common.MapStr { | ||
events := []common.MapStr{} | ||
for _, image := range imagesList { | ||
events = append(events, eventMapping(&image)) | ||
} | ||
return events | ||
} | ||
|
||
func eventMapping(image *dc.APIImages) common.MapStr { | ||
event := common.MapStr{ | ||
"id": common.MapStr{ | ||
"current": image.ID, | ||
"parent": image.ParentID, | ||
}, | ||
"created": common.Time(time.Unix(image.Created, 0)), | ||
"size": common.MapStr{ | ||
"regular": image.Size, | ||
"virtual": image.VirtualSize, | ||
}, | ||
"tags": image.RepoTags, | ||
} | ||
labels := docker.DeDotLabels(image.Labels) | ||
if len(labels) > 0 { | ||
event["labels"] = labels | ||
} | ||
return event | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package image | ||
|
||
import ( | ||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/logp" | ||
"github.com/elastic/beats/metricbeat/mb" | ||
"github.com/elastic/beats/metricbeat/module/docker" | ||
|
||
dc "github.com/fsouza/go-dockerclient" | ||
) | ||
|
||
// init registers the MetricSet with the central registry. | ||
// The New method will be called after the setup of the module and before starting to fetch data | ||
func init() { | ||
if err := mb.Registry.AddMetricSet("docker", "image", New); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// MetricSet type defines all fields of the MetricSet | ||
// As a minimum it must inherit the mb.BaseMetricSet fields, but can be extended with | ||
// additional entries. These variables can be used to persist data or configuration between | ||
// multiple fetch calls. | ||
type MetricSet struct { | ||
mb.BaseMetricSet | ||
dockerClient *dc.Client | ||
} | ||
|
||
// New create a new instance of the MetricSet | ||
// Part of new is also setting up the configuration by processing additional | ||
// configuration entries if needed. | ||
func New(base mb.BaseMetricSet) (mb.MetricSet, error) { | ||
|
||
logp.Warn("EXPERIMENTAL: The docker info metricset is experimental") | ||
|
||
config := docker.Config{} | ||
if err := base.Module().UnpackConfig(&config); err != nil { | ||
return nil, err | ||
} | ||
|
||
client, err := docker.NewDockerClient(base.HostData().URI, config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &MetricSet{ | ||
BaseMetricSet: base, | ||
dockerClient: client, | ||
}, nil | ||
} | ||
|
||
// Fetch methods implements the data gathering and data conversion to the right format | ||
// It returns the event which is then forward to the output. In case of an error, a | ||
// descriptive error must be returned. | ||
func (m *MetricSet) Fetch() ([]common.MapStr, error) { | ||
|
||
images, err := m.dockerClient.ListImages(dc.ListImagesOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return eventsMapping(images), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package image | ||
|
||
import ( | ||
"testing" | ||
|
||
mbtest "github.com/elastic/beats/metricbeat/mb/testing" | ||
) | ||
|
||
func TestData(t *testing.T) { | ||
f := mbtest.NewEventsFetcher(t, getConfig()) | ||
err := mbtest.WriteEvents(f, t) | ||
if err != nil { | ||
t.Fatal("write", err) | ||
} | ||
} | ||
|
||
func getConfig() map[string]interface{} { | ||
return map[string]interface{}{ | ||
"module": "docker", | ||
"metricsets": []string{"image"}, | ||
"hosts": []string{"unix:///var/run/docker.sock"}, | ||
} | ||
} |
Oops, something went wrong.