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

Improves Docker Labels handling and output #3024

Merged
merged 3 commits into from
Nov 18, 2016
Merged
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
2 changes: 1 addition & 1 deletion metricbeat/module/docker/container/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func eventMapping(cont *dc.APIContainers) common.MapStr {
"status": cont.Status,
}

labels := docker.BuildLabelArray(cont.Labels)
labels := docker.DeDotLabels(cont.Labels)
if len(labels) > 0 {
event["labels"] = labels
}
Expand Down
2 changes: 1 addition & 1 deletion metricbeat/module/docker/cpu/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestCPUService_GetCpuStats(t *testing.T) {
"id": containerID,
"name": "name1",
"socket": docker.GetSocket(),
"labels": docker.BuildLabelArray(labels),
"labels": docker.DeDotLabels(labels),
},
},
"usage": common.MapStr{
Expand Down
30 changes: 11 additions & 19 deletions metricbeat/module/docker/helper.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package docker

import (
"sort"
"strings"

"github.com/elastic/beats/libbeat/common"
Expand All @@ -12,7 +11,7 @@ import (
type Container struct {
Id string
Name string
Labels []common.MapStr
Labels common.MapStr
Socket *string
}

Expand All @@ -35,7 +34,7 @@ func NewContainer(container *docker.APIContainers) *Container {
return &Container{
Id: container.ID,
Name: ExtractContainerName(container.Names),
Labels: BuildLabelArray(container.Labels),
Labels: DeDotLabels(container.Labels),
}
}

Expand All @@ -52,24 +51,17 @@ func ExtractContainerName(names []string) string {
return strings.Trim(output, "/")
}

func BuildLabelArray(labels map[string]string) []common.MapStr {
// DeDotLabels returns a new common.MapStr containing a copy of the labels
// where the dots in each label name have been changed to an underscore.
func DeDotLabels(labels map[string]string) common.MapStr {

outputLabels := make([]common.MapStr, len(labels))
i := 0
var keys []string
for key := range labels {
keys = append(keys, key)
}
sort.Strings(keys)
for _, k := range keys {
// Replace all . in the labels by _
// TODO: WHY?
outputLabels := common.MapStr{}

for k, v := range labels {
// This is necessary, so ES does not interpret '.' fields as new nested JSONs, and also makes this compatible with ES 2.4
label := strings.Replace(k, ".", "_", -1)
outputLabels[i] = common.MapStr{
"key": label,
"value": labels[k],
}
i++
outputLabels.Put(label, v)
}

return outputLabels
}
2 changes: 1 addition & 1 deletion metricbeat/module/docker/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestMemoryService_GetMemoryStats(t *testing.T) {
"id": containerID,
"name": "name1",
"socket": docker.GetSocket(),
"labels": docker.BuildLabelArray(labels),
"labels": docker.DeDotLabels(labels),
},
},
"fail": common.MapStr{
Expand Down