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

otel: add test for document equivalence between agent filestream input and filebeat receiver #6681

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
219af16
otel: add test for agent hybrid
mauri870 Dec 6, 2024
f2915df
Merge branch 'main' into hybrid-agent-test
mauri870 Jan 28, 2025
74e9b51
add missing fingerprint config options to index small files
mauri870 Jan 28, 2025
309640b
ensure new lines are written to the input file
mauri870 Jan 28, 2025
83e9896
disable compression level so we inspect the requests
mauri870 Jan 28, 2025
7051ed8
add queue timeout to normal beats and separate path.home for fbreceiver
mauri870 Jan 28, 2025
f86787c
add path.home for normal filebeat
mauri870 Jan 29, 2025
f130377
fix agent not starting
mauri870 Jan 31, 2025
6e7e882
print elastic-agent output if test fails
mauri870 Jan 31, 2025
db4caef
add note for metadata field
mauri870 Jan 31, 2025
3a9c9ac
fail if ignored field is not present in both maps
mauri870 Feb 5, 2025
4f8c12d
Merge branch 'main' into hybrid-agent-test
mauri870 Feb 5, 2025
3985e84
add host metadata processor, remove 'host.*' from ignored fields
mauri870 Feb 5, 2025
59253fa
Use global processor
mauri870 Feb 5, 2025
c0f6415
more strict rules for ignored fields to avoid false positives
mauri870 Feb 5, 2025
9359f0c
use add_fields to add missing fields, use a single index
mauri870 Feb 6, 2025
2d94e09
Merge branch 'main' into hybrid-agent-test
mauri870 Feb 6, 2025
1238a43
add add_cloud_metadata processor
mauri870 Feb 7, 2025
541729d
Merge branch 'main' into hybrid-agent-test
mauri870 Feb 7, 2025
763318b
avoid checking cmd.Wait error since it is bogus
mauri870 Feb 7, 2025
7de70ca
Merge branch 'main' into hybrid-agent-test
mauri870 Feb 10, 2025
ff2f810
Merge branch 'main' into hybrid-agent-test
mauri870 Feb 11, 2025
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 pkg/testing/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ func (f *Fixture) ExecStatus(ctx context.Context, opts ...process.CmdOption) (Ag
status := AgentStatusOutput{}
if uerr := json.Unmarshal(out, &status); uerr != nil {
return AgentStatusOutput{},
fmt.Errorf("could not unmarshal agent status output: %w", errors.Join(uerr, err))
fmt.Errorf("could not unmarshal agent status output: %w:\n%s", errors.Join(uerr, err), out)
} else if status.IsZero() {
return status, fmt.Errorf("agent status output is empty: %w", err)
}
Expand Down
270 changes: 270 additions & 0 deletions testing/integration/otel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package integration
import (
"bytes"
"context"
"encoding/base64"
"errors"
"fmt"
"net/url"
Expand All @@ -20,9 +21,11 @@ import (
"text/template"
"time"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/elastic-agent-libs/mapstr"
"github.com/elastic/elastic-agent-libs/testing/estools"
"github.com/elastic/elastic-agent/pkg/control/v2/client"
aTesting "github.com/elastic/elastic-agent/pkg/testing"
Expand Down Expand Up @@ -1099,3 +1102,270 @@ service:
fixtureWg.Wait()
require.True(t, err == nil || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded), "Retrieved unexpected error: %s", err.Error())
}

func TestHybridAgentE2E(t *testing.T) {
// This test is a hybrid agent test that ingests a single log with
// filebeat and fbreceiver. It then compares the final documents in
// Elasticsearch to ensure they have no meaningful differences.
info := define.Require(t, define.Requirements{
Group: Default,
Local: true,
OS: []define.OS{
{Type: define.Windows},
{Type: define.Linux},
{Type: define.Darwin},
},
Stack: &define.Stack{},
})
tmpDir := t.TempDir()
numEvents := 1
fbIndex := "logs-generic-default"
fbReceiverIndex := "logs-generic-default"

inputFile, err := os.CreateTemp(tmpDir, "input-*.log")
require.NoError(t, err, "failed to create input log file")
inputFilePath := inputFile.Name()
for i := 0; i < numEvents; i++ {
_, err = inputFile.Write([]byte(fmt.Sprintf("Line %d", i)))
require.NoErrorf(t, err, "failed to write line %d to temp file", i)
_, err = inputFile.Write([]byte("\n"))
require.NoErrorf(t, err, "failed to write newline to input file")
time.Sleep(100 * time.Millisecond)
}
err = inputFile.Close()
require.NoError(t, err, "failed to close data input file")

t.Cleanup(func() {
if t.Failed() {
contents, err := os.ReadFile(inputFilePath)
if err != nil {
t.Logf("no data file to import at %s", inputFilePath)
return
}
t.Logf("contents of input file: %s\n", string(contents))
}
})

type configOptions struct {
InputPath string
HomeDir string
ESEndpoint string
ESApiKey string
BeatsESApiKey string
FBReceiverIndex string
}
esEndpoint, err := getESHost()
require.NoError(t, err, "error getting elasticsearch endpoint")
esApiKey, err := createESApiKey(info.ESClient)
require.NoError(t, err, "error creating API key")
require.True(t, len(esApiKey.Encoded) > 1, "api key is invalid %q", esApiKey)

configTemplate := `agent.logging.level: info
agent.logging.to_stderr: true
inputs:
- id: filestream-filebeat
type: filestream
paths:
- {{.InputPath}}
prospector.scanner.fingerprint.enabled: false
file_identity.native: ~
use_output: default
queue.mem.flush.timeout: 0s
path.home: {{.HomeDir}}/filebeat
outputs:
default:
type: elasticsearch
hosts: [{{.ESEndpoint}}]
api_key: {{.BeatsESApiKey}}
compression_level: 0
receivers:
filebeatreceiver:
filebeat:
inputs:
- type: filestream
id: filestream-fbreceiver
enabled: true
paths:
- {{.InputPath}}
prospector.scanner.fingerprint.enabled: false
file_identity.native: ~
processors:
- add_host_metadata: ~
- add_fields:
fields:
dataset: generic
namespace: default
type: logs
target: data_stream
- add_fields:
fields:
dataset: generic
target: event
output:
otelconsumer:
logging:
level: info
selectors:
- '*'
path.home: {{.HomeDir}}/fbreceiver
queue.mem.flush.timeout: 0s
exporters:
debug:
use_internal_logger: false
verbosity: detailed
elasticsearch/log:
endpoints:
- {{.ESEndpoint}}
compression: none
api_key: {{.ESApiKey}}
logs_index: {{.FBReceiverIndex}}
batcher:
enabled: true
flush_timeout: 1s
mapping:
mode: bodymap
service:
pipelines:
logs:
receivers:
- filebeatreceiver
exporters:
- elasticsearch/log
- debug
`

beatsApiKey, err := base64.StdEncoding.DecodeString(esApiKey.Encoded)
require.NoError(t, err, "error decoding api key")

var configBuffer bytes.Buffer
require.NoError(t,
template.Must(template.New("config").Parse(configTemplate)).Execute(&configBuffer,
configOptions{
InputPath: inputFilePath,
HomeDir: tmpDir,
ESEndpoint: esEndpoint,
ESApiKey: esApiKey.Encoded,
BeatsESApiKey: string(beatsApiKey),
FBReceiverIndex: fbReceiverIndex,
}))
configContents := configBuffer.Bytes()
t.Cleanup(func() {
if t.Failed() {
t.Logf("Contents of agent config file:\n%s\n", string(configContents))
}
})

// Now we can actually create the fixture and run it
fixture, err := define.NewFixtureFromLocalBuild(t, define.Version())
require.NoError(t, err)

ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(5*time.Minute))
defer cancel()

err = fixture.Prepare(ctx)
require.NoError(t, err)
err = fixture.Configure(ctx, configContents)
require.NoError(t, err)

cmd, err := fixture.PrepareAgentCommand(ctx, nil)
require.NoError(t, err)
cmd.WaitDelay = 1 * time.Second

var output strings.Builder
cmd.Stderr = &output
cmd.Stdout = &output

err = cmd.Start()
require.NoError(t, err)

t.Cleanup(func() {
if t.Failed() {
t.Log("Elastic-Agent output:")
t.Log(output.String())
}
})

require.Eventually(t, func() bool {
err = fixture.IsHealthy(ctx)
if err != nil {
t.Logf("waiting for agent healthy: %s", err.Error())
return false
}
return true
}, 1*time.Minute, 1*time.Second)

var docs estools.Documents
actualHits := &struct {
Hits int
}{}
require.Eventually(t,
func() bool {
findCtx, findCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer findCancel()

docs, err = estools.GetLogsForIndexWithContext(findCtx, info.ESClient, ".ds-"+fbIndex+"*", map[string]interface{}{
"log.file.path": inputFilePath,
})
require.NoError(t, err)

actualHits.Hits = docs.Hits.Total.Value

return actualHits.Hits == numEvents*2 // filebeat + fbreceiver
},
1*time.Minute, 1*time.Second,
"Expected %d logs in elasticsearch, got: %v", numEvents, actualHits)

doc1 := docs.Hits.Hits[0].Source
doc2 := docs.Hits.Hits[1].Source
ignoredFields := []string{
// Expected to change between filebeat and fbreceiver
"@timestamp",
"agent.ephemeral_id",
"agent.id",

// Missing from fbreceiver doc
"elastic_agent.id",
"elastic_agent.snapshot",
"elastic_agent.version",

// TODO: fbreceiver adds metadata fields that are internal in filebeat.
// Remove this once https://github.com/elastic/beats/pull/42412
// is available in agent.
"@metadata.beat",
"@metadata.type",
"@metadata.version",
}

assertMapsEqual(t, doc1, doc2, ignoredFields, "expected documents to be equal")
cancel()
err = cmd.Wait()
require.True(t, err == nil || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) || strings.Contains(err.Error(), "signal: killed"), "Retrieved unexpected error: %s", err.Error())
}

func assertMapsEqual(t *testing.T, m1, m2 mapstr.M, ignoredFields []string, msg string) {
t.Helper()

flatM1 := m1.Flatten()
flatM2 := m2.Flatten()
for _, f := range ignoredFields {
hasKeyM1, _ := flatM1.HasKey(f)
hasKeyM2, _ := flatM2.HasKey(f)

if !hasKeyM1 && !hasKeyM2 {
assert.Failf(t, msg, "ignored field %q does not exist in either map, please remove it from the ignored fields", f)
}

// If the ignored field exists and is equal in both maps then it shouldn't be ignored
if hasKeyM1 && hasKeyM2 {
valM1, _ := flatM1.GetValue(f)
valM2, _ := flatM2.GetValue(f)
if valM1 == valM2 {
assert.Failf(t, msg, "ignored field %q is equal in both maps, please remove it from the ignored fields", f)
}
}

flatM1.Delete(f)
flatM2.Delete(f)
}
require.Equal(t, "", cmp.Diff(flatM1, flatM2), "expected maps to be equal")
}
Loading