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

Fixing pod name indexer to use both namespace, pod name to frame index key #4775

Merged
merged 2 commits into from
Jul 31, 2017
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: 2 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ https://github.com/elastic/beats/compare/v6.0.0-beta1...master[Check the HEAD di

*Affecting all Beats*

- Fix pod name indexer to use both namespace, pod name to frame index key {pull}4775[4775]

*Filebeat*

*Heartbeat*
Expand Down
56 changes: 51 additions & 5 deletions libbeat/processors/add_kubernetes_metadata/indexing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ import (
"sync"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/fmtstr"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs/codec"
"github.com/elastic/beats/libbeat/outputs/codec/format"
"github.com/elastic/beats/libbeat/publisher/beat"
)

//Names of indexers and matchers that have been defined.
const (
PodNameIndexerName = "pod_name"
FieldMatcherName = "fields"
ContainerIndexerName = "container"
ContainerIndexerName = "container"
PodNameIndexerName = "pod_name"
FieldMatcherName = "fields"
FieldFormatMatcherName = "field_format"
)

// Indexing is the singleton Register instance where all Indexers and Matchers
Expand Down Expand Up @@ -244,14 +250,14 @@ func (p *PodNameIndexer) GetMetadata(pod *Pod) []MetadataIndex {
data := p.genMeta.GenerateMetaData(pod)
return []MetadataIndex{
{
Index: pod.Metadata.Name,
Index: fmt.Sprintf("%s/%s", pod.Metadata.Namespace, pod.Metadata.Name),
Data: data,
},
}
}

func (p *PodNameIndexer) GetIndexes(pod *Pod) []string {
return []string{pod.Metadata.Name}
return []string{fmt.Sprintf("%s/%s", pod.Metadata.Namespace, pod.Metadata.Name)}
}

// ContainerIndexer indexes pods based on all their containers IDs
Expand Down Expand Up @@ -329,3 +335,43 @@ func (f *FieldMatcher) MetadataIndex(event common.MapStr) string {

return ""
}

type FieldFormatMatcher struct {
Codec codec.Codec
}

func NewFieldFormatMatcher(cfg common.Config) (Matcher, error) {
config := struct {
Format string `config:"format"`
}{}

err := cfg.Unpack(&config)
if err != nil {
return nil, fmt.Errorf("fail to unpack the `format` configuration of `field_format` matcher: %s", err)
}

if config.Format == "" {
return nil, fmt.Errorf("`format` of `field_format` matcher can't be empty")
}

return &FieldFormatMatcher{
Codec: format.New(fmtstr.MustCompileEvent(config.Format)),
}, nil

}

func (f *FieldFormatMatcher) MetadataIndex(event common.MapStr) string {
bytes, err := f.Codec.Encode("", &beat.Event{
Fields: event,
})

if err != nil {
logp.Debug("kubernetes", "Unable to apply field format pattern on event")
}

if len(bytes) == 0 {
return ""
}

return string(bytes)
}
51 changes: 49 additions & 2 deletions libbeat/processors/add_kubernetes_metadata/indexing_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package add_kubernetes_metadata

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -31,7 +32,7 @@ func TestPodIndexer(t *testing.T) {

indexers := podIndexer.GetMetadata(&pod)
assert.Equal(t, len(indexers), 1)
assert.Equal(t, indexers[0].Index, podName)
assert.Equal(t, indexers[0].Index, fmt.Sprintf("%s/%s", ns, podName))

expected := common.MapStr{
"pod": common.MapStr{
Expand All @@ -47,7 +48,7 @@ func TestPodIndexer(t *testing.T) {

indices := podIndexer.GetIndexes(&pod)
assert.Equal(t, len(indices), 1)
assert.Equal(t, indices[0], podName)
assert.Equal(t, indices[0], fmt.Sprintf("%s/%s", ns, podName))
}

func TestContainerIndexer(t *testing.T) {
Expand Down Expand Up @@ -255,3 +256,49 @@ func TestFilteredGenMetaExclusion(t *testing.T) {
ok, _ = labelMap.HasKey("x")
assert.Equal(t, ok, false)
}

func TestFieldFormatMatcher(t *testing.T) {
testCfg := map[string]interface{}{}
fieldCfg, err := common.NewConfigFrom(testCfg)

assert.Nil(t, err)
matcher, err := NewFieldFormatMatcher(*fieldCfg)
assert.NotNil(t, err)

testCfg["format"] = `%{[namespace]}/%{[pod]}`
fieldCfg, _ = common.NewConfigFrom(testCfg)

matcher, err = NewFieldFormatMatcher(*fieldCfg)
assert.NotNil(t, matcher)
assert.Nil(t, err)

event := common.MapStr{
"namespace": "foo",
"pod": "bar",
}

out := matcher.MetadataIndex(event)
assert.Equal(t, "foo/bar", out)

event = common.MapStr{
"foo": "bar",
}
out = matcher.MetadataIndex(event)
assert.Empty(t, out)

testCfg["format"] = `%{[dimensions.namespace]}/%{[dimensions.pod]}`
fieldCfg, _ = common.NewConfigFrom(testCfg)
matcher, err = NewFieldFormatMatcher(*fieldCfg)
assert.NotNil(t, matcher)
assert.Nil(t, err)

event = common.MapStr{
"dimensions": common.MapStr{
"pod": "bar",
"namespace": "foo",
},
}

out = matcher.MetadataIndex(event)
assert.Equal(t, "foo/bar", out)
}
8 changes: 4 additions & 4 deletions metricbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -2790,28 +2790,28 @@ Bytes in non-idle span.


[[exported-fields-graphite]]
== graphite Fields
== graphite fields

[]experimental
graphite Module



[float]
== graphite Fields
== graphite fields




[float]
== server Fields
== server fields

server



[float]
=== graphite.server.example
=== `graphite.server.example`

type: keyword

Expand Down