-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[filebeat] Elasticsearch state storage for httpjson and cel inputs #41446
Merged
Merged
Changes from all commits
Commits
Show all changes
77 commits
Select commit
Hold shift + click to select a range
55c72d3
[filebeat] Elasticsearch state storage for httpjson input
aleksmaus 1bf288d
Fixup tests
aleksmaus e003053
Linter
aleksmaus dfce978
Enabled elastisearch storage support for cel input and some cleanup
aleksmaus e2e25fa
Remove the "hack" with .Each implementation
aleksmaus 953355b
Merge branch 'main' into poc/es_state_store
aleksmaus c1fc2a8
Adjust for the latest main es client signature change
aleksmaus c9b0256
Make check happy
aleksmaus 21d451d
Fixed missing interface method on test mock store
aleksmaus ffb9364
Add error check in ES store Each
aleksmaus 10d212f
Parameterize the supported input types through environment variables
aleksmaus 24000d7
Delete the dev tests file
aleksmaus a0d019e
Address code review comments
aleksmaus 34003d4
Add refresh=wait_for for data consistency
aleksmaus 36e9983
Revert "Add refresh=wait_for for data consistency"
aleksmaus aee4112
Cleanup
aleksmaus 76b11a4
Added updated_at field
aleksmaus 1fe1c0e
Merge branch 'main' into poc/es_state_store
aleksmaus 3c5bab1
Merge branch 'main' into poc/es_state_store
aleksmaus 4ac1bd0
Eliminate AGENTLESS_ELASTICSEARCH_STATE_STORE_ENABLED setting. The El…
aleksmaus 535560e
Merge branch 'main' into poc/es_state_store
aleksmaus 4940097
Merge branch 'main' into poc/es_state_store
aleksmaus e73a1b7
Added logging per code review request
aleksmaus 6c39fd0
Some cleanup and notifier utz
aleksmaus 716758f
Add some integration testing coverage
aleksmaus 5ec7ed4
Merge branch 'main' into poc/es_state_store
aleksmaus 904d8e8
Merge branch 'main' into poc/es_state_store
aleksmaus 02d684a
Merge branch 'main' into poc/es_state_store
aleksmaus 5a9cdb5
Fix the Filebeat test harness for agentbeat tests
aleksmaus fd4cd00
Fixed the imports formatting for the notifier_test
aleksmaus 8dd3938
Merge branch 'main' into poc/es_state_store
aleksmaus c52bd65
Merge branch 'main' into poc/es_state_store
aleksmaus 8779697
Merge branch 'main' into poc/es_state_store
aleksmaus c195cae
Remove loading API key through envvar workaround
orestisfl dfafeff
remove os import
orestisfl e192747
nitpick
orestisfl 71fd5dc
Use simple Mutex
orestisfl d4bc727
Notifier: add docs
orestisfl 5569e7f
integration test: check elasticsearch is used
orestisfl a6fbdb3
Merge branch 'main' into poc/es_state_store
orestisfl aa96d56
revert loading env in NewFilebeat()
orestisfl 71901b0
Remove Env
orestisfl 970b878
typo
orestisfl 28a8332
HandlerFunc: check error
orestisfl 426c681
Fix log-check
orestisfl b15b0bb
Re-use existing debug log
orestisfl 3e2aed3
re-use function
orestisfl 89c6e9c
checkFilebeatLogs helper
orestisfl fa8742e
Make test check store initialization
orestisfl bdf0777
Debug-log inputID
orestisfl b71b11e
Introduce var inputUnit
orestisfl 3d11005
UUID input id
orestisfl 6b98fab
Check that zero and then one key is read
orestisfl af14d49
Check inputID is configured
orestisfl 4fc4566
output unit func
orestisfl 3e859b8
final atomic.Bool
orestisfl 3041e60
Merge remote-tracking branch 'origin/main' into poc/es_state_store
orestisfl 083fd86
comment
orestisfl 2e173a4
Change uuid library
orestisfl c9ebe0f
Merge branch 'main' into poc/es_state_store
orestisfl c69aefe
check number of http calls
orestisfl f49cbfd
typo
orestisfl 0cb23d7
integration test: Add more elaborate checks
orestisfl fe16a10
Fix delta
orestisfl 89ac0bf
Merge branch 'main' into poc/es_state_store
orestisfl 8770e00
Revert input-logfile changes
orestisfl cef8721
fix store_test.go
orestisfl a988659
reduce debug-log
orestisfl 51cb319
avoid return error
orestisfl 8d362da
cleanup
orestisfl 59c4d52
notifier: Call listener with last config
orestisfl 5b27a39
Simplify test
orestisfl 040f3c5
Merge branch 'main' into poc/es_state_store
orestisfl c490984
nitpick
orestisfl 1b7842e
sanity
orestisfl 9e4c86e
Add unit tests for feature flag
orestisfl c1f5971
godoc
orestisfl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,43 +18,77 @@ | |
package beater | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/elastic/beats/v7/filebeat/config" | ||
"github.com/elastic/beats/v7/filebeat/features" | ||
"github.com/elastic/beats/v7/libbeat/beat" | ||
"github.com/elastic/beats/v7/libbeat/statestore" | ||
"github.com/elastic/beats/v7/libbeat/statestore/backend" | ||
"github.com/elastic/beats/v7/libbeat/statestore/backend/es" | ||
"github.com/elastic/beats/v7/libbeat/statestore/backend/memlog" | ||
"github.com/elastic/elastic-agent-libs/logp" | ||
"github.com/elastic/elastic-agent-libs/paths" | ||
) | ||
|
||
type filebeatStore struct { | ||
registry *statestore.Registry | ||
esRegistry *statestore.Registry | ||
orestisfl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
storeName string | ||
cleanInterval time.Duration | ||
|
||
// Notifies the Elasticsearch store about configuration change | ||
// which is available only after the beat runtime manager connects to the Agent | ||
// and receives the output configuration | ||
notifier *es.Notifier | ||
} | ||
|
||
func openStateStore(info beat.Info, logger *logp.Logger, cfg config.Registry) (*filebeatStore, error) { | ||
memlog, err := memlog.New(logger, memlog.Settings{ | ||
func openStateStore(ctx context.Context, info beat.Info, logger *logp.Logger, cfg config.Registry) (*filebeatStore, error) { | ||
var ( | ||
reg backend.Registry | ||
err error | ||
|
||
esreg *es.Registry | ||
notifier *es.Notifier | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Optional) pointers to potentially |
||
) | ||
|
||
if features.IsElasticsearchStateStoreEnabled() { | ||
notifier = es.NewNotifier() | ||
esreg = es.New(ctx, logger, notifier) | ||
} | ||
|
||
reg, err = memlog.New(logger, memlog.Settings{ | ||
Root: paths.Resolve(paths.Data, cfg.Path), | ||
FileMode: cfg.Permissions, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &filebeatStore{ | ||
registry: statestore.NewRegistry(memlog), | ||
store := &filebeatStore{ | ||
registry: statestore.NewRegistry(reg), | ||
storeName: info.Beat, | ||
cleanInterval: cfg.CleanInterval, | ||
}, nil | ||
notifier: notifier, | ||
} | ||
|
||
if esreg != nil { | ||
store.esRegistry = statestore.NewRegistry(esreg) | ||
} | ||
|
||
return store, nil | ||
} | ||
|
||
func (s *filebeatStore) Close() { | ||
s.registry.Close() | ||
} | ||
|
||
func (s *filebeatStore) Access() (*statestore.Store, error) { | ||
// Access returns the storage registry depending on the type. Default is the file store. | ||
func (s *filebeatStore) Access(typ string) (*statestore.Store, error) { | ||
if features.IsElasticsearchStateStoreEnabledForInput(typ) && s.esRegistry != nil { | ||
return s.esRegistry.Get(s.storeName) | ||
} | ||
return s.registry.Get(s.storeName) | ||
} | ||
|
||
|
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,59 @@ | ||
// 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 features | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
// List of input types Elasticsearch state store is enabled for | ||
var esTypesEnabled map[string]struct{} | ||
|
||
var isESEnabled bool | ||
|
||
func init() { | ||
initFromEnv("AGENTLESS_ELASTICSEARCH_STATE_STORE_INPUT_TYPES") | ||
} | ||
|
||
func initFromEnv(envName string) { | ||
esTypesEnabled = make(map[string]struct{}) | ||
|
||
arr := strings.Split(os.Getenv(envName), ",") | ||
for _, e := range arr { | ||
k := strings.TrimSpace(e) | ||
if k != "" { | ||
esTypesEnabled[k] = struct{}{} | ||
} | ||
} | ||
isESEnabled = len(esTypesEnabled) > 0 | ||
} | ||
|
||
// IsElasticsearchStateStoreEnabled returns true if feature is enabled for agentless | ||
func IsElasticsearchStateStoreEnabled() bool { | ||
return isESEnabled | ||
} | ||
|
||
// IsElasticsearchStateStoreEnabledForInput returns true if the provided input type uses Elasticsearch for state storage if the Elasticsearch state store feature is enabled | ||
func IsElasticsearchStateStoreEnabledForInput(inputType string) bool { | ||
if IsElasticsearchStateStoreEnabled() { | ||
_, ok := esTypesEnabled[inputType] | ||
return ok | ||
} | ||
return false | ||
} |
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,86 @@ | ||
// 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 features | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_initFromEnv(t *testing.T) { | ||
const envName = "TEST_AGENTLESS_ENV" | ||
|
||
t.Run("Without setting env", func(t *testing.T) { | ||
// default init | ||
assert.False(t, IsElasticsearchStateStoreEnabled()) | ||
assert.Empty(t, esTypesEnabled) | ||
assert.False(t, IsElasticsearchStateStoreEnabledForInput("xxx")) | ||
|
||
// init from env | ||
initFromEnv(envName) | ||
assert.False(t, IsElasticsearchStateStoreEnabled()) | ||
assert.Empty(t, esTypesEnabled) | ||
assert.False(t, IsElasticsearchStateStoreEnabledForInput("xxx")) | ||
}) | ||
|
||
tests := []struct { | ||
name string | ||
value string | ||
wantEnabled bool | ||
wantContains []string | ||
}{ | ||
{ | ||
name: "Empty", | ||
value: "", | ||
wantEnabled: false, | ||
wantContains: nil, | ||
}, | ||
{ | ||
name: "Single value", | ||
value: "xxx", | ||
wantEnabled: true, | ||
wantContains: []string{"xxx"}, | ||
}, | ||
{ | ||
name: "Multiple values", | ||
value: "xxx,yyy", | ||
wantEnabled: true, | ||
wantContains: []string{"xxx", "yyy"}, | ||
}, | ||
{ | ||
name: "Multiple values with spaces", | ||
value: ",,, , xxx , yyy, ,,,,", | ||
wantEnabled: true, | ||
wantContains: []string{"xxx", "yyy"}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Setenv(envName, tt.value) | ||
initFromEnv(envName) | ||
|
||
assert.Equal(t, tt.wantEnabled, IsElasticsearchStateStoreEnabled()) | ||
for _, contain := range tt.wantContains { | ||
assert.Contains(t, esTypesEnabled, contain) | ||
assert.True(t, IsElasticsearchStateStoreEnabledForInput(contain)) | ||
} | ||
assert.Len(t, esTypesEnabled, len(tt.wantContains)) | ||
}) | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Godoc for this parameter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest something like