From 3d777ffddb9da219ace541ce5872c63ecc8f3bdf Mon Sep 17 00:00:00 2001 From: Vihas Makwana Date: Mon, 9 Jun 2025 18:02:26 +0530 Subject: [PATCH 1/8] make elasticsearchreceiver compatible with 9.x --- receiver/elasticsearchreceiver/client.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/receiver/elasticsearchreceiver/client.go b/receiver/elasticsearchreceiver/client.go index 90b072ec64f31..f6f8c961fe84a 100644 --- a/receiver/elasticsearchreceiver/client.go +++ b/receiver/elasticsearchreceiver/client.go @@ -230,9 +230,9 @@ func (c defaultElasticsearchClient) doRequest(ctx context.Context, path string) req.Header.Add("Authorization", c.authHeader) } - // See https://www.elastic.co/guide/en/elasticsearch/reference/8.0/api-conventions.html#api-compatibility - // the compatible-with=7 should signal to newer version of Elasticsearch to use the v7.x API format - req.Header.Add("Accept", "application/vnd.elasticsearch+json; compatible-with=7") + // See https://www.elastic.co/docs/reference/elasticsearch/rest-apis/api-conventions#api-compatibility + // the compatible-with=8 should signal to newer version of Elasticsearch to use the v8.x API format + req.Header.Add("Accept", "application/vnd.elasticsearch+json; compatible-with=8") resp, err := c.client.Do(req) if err != nil { From ea330f2aa1220d046b562675129d57ae45623a6e Mon Sep 17 00:00:00 2001 From: Vihas Makwana Date: Mon, 9 Jun 2025 18:03:55 +0530 Subject: [PATCH 2/8] changelog --- .chloggen/elasticsearchreceiver-9.yaml | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .chloggen/elasticsearchreceiver-9.yaml diff --git a/.chloggen/elasticsearchreceiver-9.yaml b/.chloggen/elasticsearchreceiver-9.yaml new file mode 100644 index 0000000000000..2b67fcd474042 --- /dev/null +++ b/.chloggen/elasticsearchreceiver-9.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: elasticsearchreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Add support for ES version 9 + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [39929] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] From dbb26d659e6bf88086aef61b4f05a6edf7d39b6c Mon Sep 17 00:00:00 2001 From: Vihas Makwana Date: Wed, 18 Jun 2025 19:19:56 +0530 Subject: [PATCH 3/8] initial commit --- extension/storage/filestorage/config.go | 2 ++ extension/storage/filestorage/extension.go | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/extension/storage/filestorage/config.go b/extension/storage/filestorage/config.go index ef842a93b6163..c988a0eb957ab 100644 --- a/extension/storage/filestorage/config.go +++ b/extension/storage/filestorage/config.go @@ -31,6 +31,8 @@ type Config struct { CreateDirectory bool `mapstructure:"create_directory,omitempty"` DirectoryPermissions string `mapstructure:"directory_permissions,omitempty"` directoryPermissionsParsed int64 `mapstructure:"-,omitempty"` + + Recreate bool `mapstructure:"recreate,omitempty"` } // CompactionConfig defines configuration for optional file storage compaction. diff --git a/extension/storage/filestorage/extension.go b/extension/storage/filestorage/extension.go index ff8ec568de83e..88bf3b8c7a407 100644 --- a/extension/storage/filestorage/extension.go +++ b/extension/storage/filestorage/extension.go @@ -71,6 +71,11 @@ func (lfs *localFileStorage) GetClient(_ context.Context, kind component.Kind, e rawName = sanitize(rawName) absoluteName := filepath.Join(lfs.cfg.Directory, rawName) + if lfs.cfg.Recreate { + if err := os.Remove(absoluteName); err != nil && !errors.Is(err, os.ErrNotExist) { + return nil, fmt.Errorf("error removing the database. Please remove %s manually: %v", absoluteName, err) + } + } client, err := newClient(lfs.logger, absoluteName, lfs.cfg.Timeout, lfs.cfg.Compaction, !lfs.cfg.FSync) if err != nil { return nil, err From 7fbd45dedcb9b01c2caccf0f3ca91edea9d714df Mon Sep 17 00:00:00 2001 From: Vihas Makwana Date: Mon, 30 Jun 2025 14:51:09 +0530 Subject: [PATCH 4/8] changelog --- ...lasticsearchreceiver-9.yaml => filestorage-panic.yaml} | 8 ++++---- receiver/elasticsearchreceiver/client.go | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) rename .chloggen/{elasticsearchreceiver-9.yaml => filestorage-panic.yaml} (88%) diff --git a/.chloggen/elasticsearchreceiver-9.yaml b/.chloggen/filestorage-panic.yaml similarity index 88% rename from .chloggen/elasticsearchreceiver-9.yaml rename to .chloggen/filestorage-panic.yaml index 2b67fcd474042..5575216bf5d32 100644 --- a/.chloggen/elasticsearchreceiver-9.yaml +++ b/.chloggen/filestorage-panic.yaml @@ -1,16 +1,16 @@ # Use this changelog template to create an entry for release notes. # One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' -change_type: enhancement +change_type: bug_fix # The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) -component: elasticsearchreceiver +component: filestorageextension # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). -note: Add support for ES version 9 +note: Add an option to recreate databse if the database file is corrupted. # Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. -issues: [39929] +issues: [35899] # (Optional) One or more lines of additional information to render under the primary note. # These lines will be padded with 2 spaces and then inserted directly into the document. diff --git a/receiver/elasticsearchreceiver/client.go b/receiver/elasticsearchreceiver/client.go index f6f8c961fe84a..90b072ec64f31 100644 --- a/receiver/elasticsearchreceiver/client.go +++ b/receiver/elasticsearchreceiver/client.go @@ -230,9 +230,9 @@ func (c defaultElasticsearchClient) doRequest(ctx context.Context, path string) req.Header.Add("Authorization", c.authHeader) } - // See https://www.elastic.co/docs/reference/elasticsearch/rest-apis/api-conventions#api-compatibility - // the compatible-with=8 should signal to newer version of Elasticsearch to use the v8.x API format - req.Header.Add("Accept", "application/vnd.elasticsearch+json; compatible-with=8") + // See https://www.elastic.co/guide/en/elasticsearch/reference/8.0/api-conventions.html#api-compatibility + // the compatible-with=7 should signal to newer version of Elasticsearch to use the v7.x API format + req.Header.Add("Accept", "application/vnd.elasticsearch+json; compatible-with=7") resp, err := c.client.Do(req) if err != nil { From ed9fe8576f245818a9a9d514b7d01d906576495d Mon Sep 17 00:00:00 2001 From: Vihas Makwana Date: Tue, 1 Jul 2025 15:56:49 +0530 Subject: [PATCH 5/8] docs and rename --- extension/storage/filestorage/README.md | 4 ++++ extension/storage/filestorage/extension.go | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/extension/storage/filestorage/README.md b/extension/storage/filestorage/README.md index cea3a8c97bbc8..90d7619b7cef5 100644 --- a/extension/storage/filestorage/README.md +++ b/extension/storage/filestorage/README.md @@ -31,6 +31,10 @@ The default timeout is `1s`. By default, the directories will be created with `0750 (rwxr-x---)` permissions, minus the process umask. Use `directory_permissions` to customize directory creation permissions, minus the process umask. +`recreate` when set, will remane the existing data storage to `{filename}.backup` and a new data file will be created from scratch. This option is useful if underlying database is corrupted and as a result, it can halt the entire collector process due to a panic. See (#36840)[https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/36840] for more details. + +> [!Note] +> Enabling `recreate` will regenerate the database files, which may lead to data duplication. ## Compaction `compaction` defines how and when files should be compacted. There are two modes of compaction available (both of which can be set concurrently): diff --git a/extension/storage/filestorage/extension.go b/extension/storage/filestorage/extension.go index 88bf3b8c7a407..f5956c1594128 100644 --- a/extension/storage/filestorage/extension.go +++ b/extension/storage/filestorage/extension.go @@ -72,8 +72,8 @@ func (lfs *localFileStorage) GetClient(_ context.Context, kind component.Kind, e rawName = sanitize(rawName) absoluteName := filepath.Join(lfs.cfg.Directory, rawName) if lfs.cfg.Recreate { - if err := os.Remove(absoluteName); err != nil && !errors.Is(err, os.ErrNotExist) { - return nil, fmt.Errorf("error removing the database. Please remove %s manually: %v", absoluteName, err) + if err := os.Rename(absoluteName, filepath.Join(absoluteName, ".backup")); err != nil { + return nil, fmt.Errorf("error renaming the database. Please remove %s manually: %v", absoluteName, err) } } client, err := newClient(lfs.logger, absoluteName, lfs.cfg.Timeout, lfs.cfg.Compaction, !lfs.cfg.FSync) From ee330e50901c05ab31801df72dfc95c249b4efed Mon Sep 17 00:00:00 2001 From: Vihas Makwana Date: Wed, 2 Jul 2025 18:49:52 +0530 Subject: [PATCH 6/8] test case --- extension/storage/filestorage/extension.go | 4 +- .../storage/filestorage/extension_test.go | 78 +++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/extension/storage/filestorage/extension.go b/extension/storage/filestorage/extension.go index f5956c1594128..a2be454b49a06 100644 --- a/extension/storage/filestorage/extension.go +++ b/extension/storage/filestorage/extension.go @@ -72,8 +72,8 @@ func (lfs *localFileStorage) GetClient(_ context.Context, kind component.Kind, e rawName = sanitize(rawName) absoluteName := filepath.Join(lfs.cfg.Directory, rawName) if lfs.cfg.Recreate { - if err := os.Rename(absoluteName, filepath.Join(absoluteName, ".backup")); err != nil { - return nil, fmt.Errorf("error renaming the database. Please remove %s manually: %v", absoluteName, err) + if err := os.Rename(absoluteName, absoluteName+".backup"); err != nil { + return nil, fmt.Errorf("error renaming the database. Please remove %s manually: %w", absoluteName, err) } } client, err := newClient(lfs.logger, absoluteName, lfs.cfg.Timeout, lfs.cfg.Compaction, !lfs.cfg.FSync) diff --git a/extension/storage/filestorage/extension_test.go b/extension/storage/filestorage/extension_test.go index cc936b6b1e247..fd7b840ff11dd 100644 --- a/extension/storage/filestorage/extension_test.go +++ b/extension/storage/filestorage/extension_test.go @@ -612,3 +612,81 @@ func TestDirectoryCreation(t *testing.T) { }) } } + +func TestRecreate(t *testing.T) { + ctx := context.Background() + temp := t.TempDir() + f := NewFactory() + + config := f.CreateDefaultConfig().(*Config) + config.Directory = temp + + // step 1: create an extension with default config and write some data + { + ext, err := f.Create(ctx, extensiontest.NewNopSettings(f.Type()), config) + require.NoError(t, err) + require.NotNil(t, ext) + + se, ok := ext.(storage.Extension) + require.True(t, ok) + + client, err := se.GetClient(ctx, component.KindReceiver, component.MustNewID("filelog"), "") + require.NoError(t, err) + require.NotNil(t, client) + + // write the data and make sure it is set in the subsequent get. + require.NoError(t, client.Set(ctx, "key", []byte("val"))) + val, err := client.Get(ctx, "key") + require.Equal(t, val, []byte("val")) + require.NoError(t, err) + + // close the extension + require.NoError(t, client.Close(ctx)) + require.NoError(t, ext.Shutdown(ctx)) + } + + // step 2: re-create the extension to make sure that the data is therw + { + ext, err := f.Create(ctx, extensiontest.NewNopSettings(f.Type()), config) + require.NoError(t, err) + require.NotNil(t, ext) + se, ok := ext.(storage.Extension) + require.True(t, ok) + + client, err := se.GetClient(ctx, component.KindReceiver, component.MustNewID("filelog"), "") + require.NoError(t, err) + require.NotNil(t, client) + + // make sure that the data exists from the previous pass. + val, err := client.Get(ctx, "key") + require.Equal(t, val, []byte("val")) + require.NoError(t, err) + + // close the extension + require.NoError(t, client.Close(ctx)) + require.NoError(t, ext.Shutdown(ctx)) + } + + // step 3: re-create the extension, but with Recreate=true and make sure that the data is not preset + { + config.Recreate = true + ext, err := f.Create(ctx, extensiontest.NewNopSettings(f.Type()), config) + require.NoError(t, err) + require.NotNil(t, ext) + se, ok := ext.(storage.Extension) + require.True(t, ok) + + client, err := se.GetClient(ctx, component.KindReceiver, component.MustNewID("filelog"), "") + require.NoError(t, err) + require.NotNil(t, client) + + // The data shouldn't exist. + val, err := client.Get(ctx, "key") + require.Nil(t, val) + require.NoError(t, err) + + // close the extension + require.NoError(t, client.Close(ctx)) + require.NoError(t, ext.Shutdown(ctx)) + } +} From 3ea02e51fb93a9750bbc307cc8d020711629d8aa Mon Sep 17 00:00:00 2001 From: Vihas Makwana <121151420+VihasMakwana@users.noreply.github.com> Date: Tue, 15 Jul 2025 12:28:21 +0530 Subject: [PATCH 7/8] Update extension/storage/filestorage/README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mikołaj Świątek --- extension/storage/filestorage/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension/storage/filestorage/README.md b/extension/storage/filestorage/README.md index 90d7619b7cef5..740b575fb2969 100644 --- a/extension/storage/filestorage/README.md +++ b/extension/storage/filestorage/README.md @@ -31,7 +31,7 @@ The default timeout is `1s`. By default, the directories will be created with `0750 (rwxr-x---)` permissions, minus the process umask. Use `directory_permissions` to customize directory creation permissions, minus the process umask. -`recreate` when set, will remane the existing data storage to `{filename}.backup` and a new data file will be created from scratch. This option is useful if underlying database is corrupted and as a result, it can halt the entire collector process due to a panic. See (#36840)[https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/36840] for more details. +`recreate` when set, will rename the existing data storage to `{filename}.backup` and a new data file will be created from scratch. This option is useful if underlying database is corrupted and as a result, it can halt the entire collector process due to a panic. See (#36840)[https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/36840] for more details. > [!Note] > Enabling `recreate` will regenerate the database files, which may lead to data duplication. From 3439f57261ba78c3a5095cbcde188a24ab963808 Mon Sep 17 00:00:00 2001 From: Vihas Makwana Date: Tue, 15 Jul 2025 12:35:23 +0530 Subject: [PATCH 8/8] docs --- extension/storage/filestorage/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension/storage/filestorage/README.md b/extension/storage/filestorage/README.md index 740b575fb2969..eab8faa805d5c 100644 --- a/extension/storage/filestorage/README.md +++ b/extension/storage/filestorage/README.md @@ -34,7 +34,7 @@ Use `directory_permissions` to customize directory creation permissions, minus t `recreate` when set, will rename the existing data storage to `{filename}.backup` and a new data file will be created from scratch. This option is useful if underlying database is corrupted and as a result, it can halt the entire collector process due to a panic. See (#36840)[https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/36840] for more details. > [!Note] -> Enabling `recreate` will regenerate the database files, which may lead to data duplication. +> Enabling `recreate` will regenerate the database files, which may lead to data duplication or data loss. ## Compaction `compaction` defines how and when files should be compacted. There are two modes of compaction available (both of which can be set concurrently):