Skip to content

Commit f02a049

Browse files
Fix generation of data stream namespace used in Beat configuration (#47140) (#47145)
* 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 436ff57 commit f02a049

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
@@ -348,7 +348,7 @@ func metadataFromDatastreamValues(defaultDataStreamType string, expected *proto.
348348
if newNamespace := streamExpected.GetDataStream().GetNamespace(); newNamespace != "" {
349349
setNamespace = newNamespace
350350
}
351-
if newNamespace := expected.GetDataStream().GetNamespace(); newNamespace != "" && newNamespace != DefaultDatasetName {
351+
if newNamespace := expected.GetDataStream().GetNamespace(); newNamespace != "" && newNamespace != DefaultNamespaceName {
352352
setNamespace = newNamespace
353353
}
354354

x-pack/libbeat/management/generate_test.go

Lines changed: 160 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func TestOutputIndex(t *testing.T) {
238238
}
239239
inStream := map[string]any{}
240240
outStream := injectIndexStream(dataStreamType, unit, stream, inStream)
241-
require.Equal(t, "synthetics-icmp-default", outStream["index"])
241+
require.Equal(t, "synthetics-icmp-example", outStream["index"])
242242

243243
//test Defaults
244244
emptyStream := &proto.Stream{DataStream: &proto.DataStream{}}
@@ -288,6 +288,165 @@ func findFieldsInProcessors(t *testing.T, configFields map[string]any, cfgMap ma
288288
}
289289
}
290290

291+
func TestMetadataFromDatastreamValues(t *testing.T) {
292+
cases := map[string]struct {
293+
defaultDataStreamType string
294+
expected *proto.UnitExpectedConfig
295+
streamExpected *proto.Stream
296+
297+
expectedStreamType string
298+
expectedDataset string
299+
expectedNamespace string
300+
}{
301+
// defaults test case
302+
"default_type": {
303+
defaultDataStreamType: "logs",
304+
305+
expectedStreamType: "logs",
306+
expectedDataset: DefaultDatasetName,
307+
expectedNamespace: DefaultNamespaceName,
308+
},
309+
310+
// type test cases
311+
"type_from_expected": {
312+
defaultDataStreamType: "logs",
313+
expected: &proto.UnitExpectedConfig{
314+
DataStream: &proto.DataStream{
315+
Type: "expected-metrics",
316+
},
317+
},
318+
319+
expectedStreamType: "expected-metrics",
320+
expectedDataset: DefaultDatasetName,
321+
expectedNamespace: DefaultNamespaceName,
322+
},
323+
"type_from_stream": {
324+
defaultDataStreamType: "logs",
325+
streamExpected: &proto.Stream{
326+
DataStream: &proto.DataStream{
327+
Type: "stream-metrics",
328+
},
329+
},
330+
331+
expectedStreamType: "stream-metrics",
332+
expectedDataset: DefaultDatasetName,
333+
expectedNamespace: DefaultNamespaceName,
334+
},
335+
"type_from_both": {
336+
defaultDataStreamType: "logs",
337+
expected: &proto.UnitExpectedConfig{
338+
DataStream: &proto.DataStream{
339+
Type: "expected-metrics",
340+
},
341+
},
342+
streamExpected: &proto.Stream{
343+
DataStream: &proto.DataStream{
344+
Type: "stream-metrics",
345+
},
346+
},
347+
348+
expectedStreamType: "expected-metrics",
349+
expectedDataset: DefaultDatasetName,
350+
expectedNamespace: DefaultNamespaceName,
351+
},
352+
353+
// dataset test cases
354+
"dataset_from_expected": {
355+
defaultDataStreamType: "logs",
356+
expected: &proto.UnitExpectedConfig{
357+
DataStream: &proto.DataStream{
358+
Dataset: "expected-dataset",
359+
},
360+
},
361+
362+
expectedStreamType: "logs",
363+
expectedDataset: "expected-dataset",
364+
expectedNamespace: DefaultNamespaceName,
365+
},
366+
"dataset_from_stream": {
367+
defaultDataStreamType: "logs",
368+
streamExpected: &proto.Stream{
369+
DataStream: &proto.DataStream{
370+
Dataset: "stream-dataset",
371+
},
372+
},
373+
374+
expectedStreamType: "logs",
375+
expectedDataset: "stream-dataset",
376+
expectedNamespace: DefaultNamespaceName,
377+
},
378+
"dataset_from_both": {
379+
defaultDataStreamType: "logs",
380+
expected: &proto.UnitExpectedConfig{
381+
DataStream: &proto.DataStream{
382+
Dataset: "expected-dataset",
383+
},
384+
},
385+
streamExpected: &proto.Stream{
386+
DataStream: &proto.DataStream{
387+
Dataset: "stream-dataset",
388+
},
389+
},
390+
391+
expectedStreamType: "logs",
392+
expectedDataset: "expected-dataset",
393+
expectedNamespace: DefaultNamespaceName,
394+
},
395+
396+
// namespace test cases
397+
"namespace_from_expected": {
398+
defaultDataStreamType: "logs",
399+
expected: &proto.UnitExpectedConfig{
400+
DataStream: &proto.DataStream{
401+
Namespace: "expected-namespace",
402+
},
403+
},
404+
405+
expectedStreamType: "logs",
406+
expectedDataset: DefaultDatasetName,
407+
expectedNamespace: "expected-namespace",
408+
},
409+
"namespace_from_stream": {
410+
defaultDataStreamType: "logs",
411+
streamExpected: &proto.Stream{
412+
DataStream: &proto.DataStream{
413+
Namespace: "stream-namespace",
414+
},
415+
},
416+
417+
expectedStreamType: "logs",
418+
expectedDataset: DefaultDatasetName,
419+
expectedNamespace: "stream-namespace",
420+
},
421+
"namespace_from_both": {
422+
defaultDataStreamType: "logs",
423+
expected: &proto.UnitExpectedConfig{
424+
DataStream: &proto.DataStream{
425+
Namespace: "expected-namespace",
426+
},
427+
},
428+
streamExpected: &proto.Stream{
429+
DataStream: &proto.DataStream{
430+
Namespace: "stream-namespace",
431+
},
432+
},
433+
434+
expectedStreamType: "logs",
435+
expectedDataset: DefaultDatasetName,
436+
expectedNamespace: "expected-namespace",
437+
},
438+
}
439+
440+
for name, tc := range cases {
441+
t.Run(name, func(t *testing.T) {
442+
streamType, dataset, namespace := metadataFromDatastreamValues(tc.defaultDataStreamType, tc.expected, tc.streamExpected)
443+
require.Equal(t, tc.expectedStreamType, streamType)
444+
require.Equal(t, tc.expectedDataset, dataset)
445+
require.Equal(t, tc.expectedNamespace, namespace)
446+
})
447+
}
448+
}
449+
291450
func buildConfigMap(t *testing.T, unitRaw *proto.UnitExpectedConfig, agentInfo *client.AgentInfo) mapstr.M {
292451
reloadCfg, err := generateBeatConfig(unitRaw, agentInfo)
293452
require.NoError(t, err, "error in generateBeatConfig")

0 commit comments

Comments
 (0)