forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure metadata updates don't replace existing pod metrics (elastic#7573
) * Ensure metadata updates don't replace existing pod metrics Before things change, `add_kubernetes_metadata` would replace everything under `kubernetes.pod` when enriching events. This is not what you want, especially if you are using it in conjunction with the Kubernetes module, where you could smash pod metrics when enriching. * Update CHANGELOG.asciidoc (cherry picked from commit 3297924)
Showing
3 changed files
with
102 additions
and
10 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
88 changes: 88 additions & 0 deletions
88
libbeat/processors/add_kubernetes_metadata/kubernetes_test.go
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,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 add_kubernetes_metadata | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/beats/libbeat/beat" | ||
"github.com/elastic/beats/libbeat/common" | ||
) | ||
|
||
// Test metadata updates don't replace existing pod metrics | ||
func TestAnnotatorDeepUpdate(t *testing.T) { | ||
cfg := common.MustNewConfigFrom(map[string]interface{}{ | ||
"lookup_fields": []string{"kubernetes.pod.name"}, | ||
}) | ||
matcher, err := NewFieldMatcher(*cfg) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
processor := kubernetesAnnotator{ | ||
cache: newCache(10 * time.Second), | ||
matchers: &Matchers{ | ||
matchers: []Matcher{matcher}, | ||
}, | ||
} | ||
|
||
processor.cache.set("foo", common.MapStr{ | ||
"pod": common.MapStr{ | ||
"labels": common.MapStr{ | ||
"dont": "replace", | ||
"original": "fields", | ||
}, | ||
}, | ||
}) | ||
|
||
event, err := processor.Run(&beat.Event{ | ||
Fields: common.MapStr{ | ||
"kubernetes": common.MapStr{ | ||
"pod": common.MapStr{ | ||
"name": "foo", | ||
"id": "pod_id", | ||
"metrics": common.MapStr{ | ||
"a": 1, | ||
"b": 2, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, common.MapStr{ | ||
"kubernetes": common.MapStr{ | ||
"pod": common.MapStr{ | ||
"name": "foo", | ||
"id": "pod_id", | ||
"metrics": common.MapStr{ | ||
"a": 1, | ||
"b": 2, | ||
}, | ||
"labels": common.MapStr{ | ||
"dont": "replace", | ||
"original": "fields", | ||
}, | ||
}, | ||
}, | ||
}, event.Fields) | ||
} |