Skip to content

Commit c60a124

Browse files
Fix generation of data stream namespace used in Beat configuration (#47140) (#47144)
* Fix comparison * Add unit test * Adding CHANGELOG entry * Fix unit test (cherry picked from commit 7c3d7c1) Co-authored-by: Shaunak Kashyap <[email protected]>
1 parent cd39f66 commit c60a124

File tree

3 files changed

+193
-2
lines changed

3 files changed

+193
-2
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: bug-fix
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Allows users to customize their data stream namespace to "generic".
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
19+
#description:
20+
21+
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
22+
component: all
23+
24+
# PR URL; optional; the PR number that added the changeset.
25+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
26+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
27+
# Please provide it if you are adding a fragment for a different PR.
28+
#pr: https://github.com/owner/repo/1234
29+
30+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
31+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
32+
#issue: https://github.com/owner/repo/1234

x-pack/libbeat/management/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ func metadataFromDatastreamValues(defaultDataStreamType string, expected *proto.
342342
if newNamespace := streamExpected.GetDataStream().GetNamespace(); newNamespace != "" {
343343
setNamespace = newNamespace
344344
}
345-
if newNamespace := expected.GetDataStream().GetNamespace(); newNamespace != "" && newNamespace != DefaultDatasetName {
345+
if newNamespace := expected.GetDataStream().GetNamespace(); newNamespace != "" && newNamespace != DefaultNamespaceName {
346346
setNamespace = newNamespace
347347
}
348348

x-pack/libbeat/management/generate_test.go

Lines changed: 160 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func TestOutputIndex(t *testing.T) {
178178
}
179179
inStream := map[string]interface{}{}
180180
outStream := injectIndexStream(dataStreamType, unit, stream, inStream)
181-
require.Equal(t, "synthetics-icmp-default", outStream["index"])
181+
require.Equal(t, "synthetics-icmp-example", outStream["index"])
182182

183183
//test Defaults
184184
emptyStream := &proto.Stream{DataStream: &proto.DataStream{}}
@@ -222,6 +222,165 @@ func findFieldsInProcessors(t *testing.T, configFields map[string]interface{}, c
222222
}
223223
}
224224

225+
func TestMetadataFromDatastreamValues(t *testing.T) {
226+
cases := map[string]struct {
227+
defaultDataStreamType string
228+
expected *proto.UnitExpectedConfig
229+
streamExpected *proto.Stream
230+
231+
expectedStreamType string
232+
expectedDataset string
233+
expectedNamespace string
234+
}{
235+
// defaults test case
236+
"default_type": {
237+
defaultDataStreamType: "logs",
238+
239+
expectedStreamType: "logs",
240+
expectedDataset: DefaultDatasetName,
241+
expectedNamespace: DefaultNamespaceName,
242+
},
243+
244+
// type test cases
245+
"type_from_expected": {
246+
defaultDataStreamType: "logs",
247+
expected: &proto.UnitExpectedConfig{
248+
DataStream: &proto.DataStream{
249+
Type: "expected-metrics",
250+
},
251+
},
252+
253+
expectedStreamType: "expected-metrics",
254+
expectedDataset: DefaultDatasetName,
255+
expectedNamespace: DefaultNamespaceName,
256+
},
257+
"type_from_stream": {
258+
defaultDataStreamType: "logs",
259+
streamExpected: &proto.Stream{
260+
DataStream: &proto.DataStream{
261+
Type: "stream-metrics",
262+
},
263+
},
264+
265+
expectedStreamType: "stream-metrics",
266+
expectedDataset: DefaultDatasetName,
267+
expectedNamespace: DefaultNamespaceName,
268+
},
269+
"type_from_both": {
270+
defaultDataStreamType: "logs",
271+
expected: &proto.UnitExpectedConfig{
272+
DataStream: &proto.DataStream{
273+
Type: "expected-metrics",
274+
},
275+
},
276+
streamExpected: &proto.Stream{
277+
DataStream: &proto.DataStream{
278+
Type: "stream-metrics",
279+
},
280+
},
281+
282+
expectedStreamType: "expected-metrics",
283+
expectedDataset: DefaultDatasetName,
284+
expectedNamespace: DefaultNamespaceName,
285+
},
286+
287+
// dataset test cases
288+
"dataset_from_expected": {
289+
defaultDataStreamType: "logs",
290+
expected: &proto.UnitExpectedConfig{
291+
DataStream: &proto.DataStream{
292+
Dataset: "expected-dataset",
293+
},
294+
},
295+
296+
expectedStreamType: "logs",
297+
expectedDataset: "expected-dataset",
298+
expectedNamespace: DefaultNamespaceName,
299+
},
300+
"dataset_from_stream": {
301+
defaultDataStreamType: "logs",
302+
streamExpected: &proto.Stream{
303+
DataStream: &proto.DataStream{
304+
Dataset: "stream-dataset",
305+
},
306+
},
307+
308+
expectedStreamType: "logs",
309+
expectedDataset: "stream-dataset",
310+
expectedNamespace: DefaultNamespaceName,
311+
},
312+
"dataset_from_both": {
313+
defaultDataStreamType: "logs",
314+
expected: &proto.UnitExpectedConfig{
315+
DataStream: &proto.DataStream{
316+
Dataset: "expected-dataset",
317+
},
318+
},
319+
streamExpected: &proto.Stream{
320+
DataStream: &proto.DataStream{
321+
Dataset: "stream-dataset",
322+
},
323+
},
324+
325+
expectedStreamType: "logs",
326+
expectedDataset: "expected-dataset",
327+
expectedNamespace: DefaultNamespaceName,
328+
},
329+
330+
// namespace test cases
331+
"namespace_from_expected": {
332+
defaultDataStreamType: "logs",
333+
expected: &proto.UnitExpectedConfig{
334+
DataStream: &proto.DataStream{
335+
Namespace: "expected-namespace",
336+
},
337+
},
338+
339+
expectedStreamType: "logs",
340+
expectedDataset: DefaultDatasetName,
341+
expectedNamespace: "expected-namespace",
342+
},
343+
"namespace_from_stream": {
344+
defaultDataStreamType: "logs",
345+
streamExpected: &proto.Stream{
346+
DataStream: &proto.DataStream{
347+
Namespace: "stream-namespace",
348+
},
349+
},
350+
351+
expectedStreamType: "logs",
352+
expectedDataset: DefaultDatasetName,
353+
expectedNamespace: "stream-namespace",
354+
},
355+
"namespace_from_both": {
356+
defaultDataStreamType: "logs",
357+
expected: &proto.UnitExpectedConfig{
358+
DataStream: &proto.DataStream{
359+
Namespace: "expected-namespace",
360+
},
361+
},
362+
streamExpected: &proto.Stream{
363+
DataStream: &proto.DataStream{
364+
Namespace: "stream-namespace",
365+
},
366+
},
367+
368+
expectedStreamType: "logs",
369+
expectedDataset: DefaultDatasetName,
370+
expectedNamespace: "expected-namespace",
371+
},
372+
}
373+
374+
for name, tc := range cases {
375+
t.Run(name, func(t *testing.T) {
376+
streamType, dataset, namespace := metadataFromDatastreamValues(tc.defaultDataStreamType, tc.expected, tc.streamExpected)
377+
require.Equal(t, tc.expectedStreamType, streamType)
378+
require.Equal(t, tc.expectedDataset, dataset)
379+
require.Equal(t, tc.expectedNamespace, namespace)
380+
})
381+
}
382+
}
383+
225384
func buildConfigMap(t *testing.T, unitRaw *proto.UnitExpectedConfig, agentInfo *client.AgentInfo) mapstr.M {
226385
reloadCfg, err := generateBeatConfig(unitRaw, agentInfo)
227386
require.NoError(t, err, "error in generateBeatConfig")

0 commit comments

Comments
 (0)