-
Notifications
You must be signed in to change notification settings - Fork 5k
[beatreceivers] Introduce otel mode for metricbeat #45145
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
fd4c9e4
[beatreceivers] Introduce otel mode for metricbeat
khushijain21 e33fe59
fix test
khushijain21 ae1e993
nit
khushijain21 0a592f4
remove processor
khushijain21 920921e
make check
khushijain21 42619f6
Update x-pack/libbeat/common/otelbeat/otel.go
khushijain21 2d3bebb
Update x-pack/libbeat/common/otelbeat/otel.go
khushijain21 2f9e1fc
address review comments
khushijain21 a83a364
fix ci
khushijain21 f5130a6
Merge branch 'main' into mbreceiver
khushijain21 2ea9de4
add test
khushijain21 4567a89
add e2e test
khushijain21 4124415
Merge branch 'main' into mbreceiver
mauri870 94f4760
fix ci related issues and add monitoring
khushijain21 b03639b
Merge branch 'main' into mbreceiver
khushijain21 1c21f54
add proxy dep
khushijain21 8fc946b
fix mage
khushijain21 4905496
Merge branch 'main' into mbreceiver
khushijain21 885c39b
address comments
khushijain21 d04f196
Merge branch 'main' into mbreceiver
khushijain21 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 hidden or 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 hidden or 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,68 @@ | ||
| // 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 providers | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "go.opentelemetry.io/collector/confmap" | ||
|
|
||
| "github.com/elastic/beats/v7/libbeat/cfgfile" | ||
| ) | ||
|
|
||
| var schemeMap = map[string]string{ | ||
| "fb": "filebeatreceiver", | ||
| "mb": "metricbeatreceiver", | ||
| } | ||
|
|
||
| func LoadConfig(uri string, schemeName string) (*confmap.Retrieved, error) { | ||
|
khushijain21 marked this conversation as resolved.
|
||
| if !strings.HasPrefix(uri, schemeName+":") { | ||
| return nil, fmt.Errorf("%q uri is not supported by %q provider", uri, schemeName) | ||
| } | ||
|
|
||
| // Load beat config file | ||
| cfg, err := cfgfile.Load(filepath.Clean(uri[len(schemeName)+1:]), nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var receiverMap map[string]any | ||
| err = cfg.Unpack(&receiverMap) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| receiverName := schemeMap[schemeName] | ||
| // beat specific configuration is defined here | ||
| cfgMap := map[string]any{ | ||
| "receivers": map[string]any{ | ||
| receiverName: receiverMap, | ||
| }, | ||
| "service": map[string]any{ | ||
| "pipelines": map[string]any{ | ||
| "logs": map[string]any{ | ||
| "receivers": []string{receiverName}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| return confmap.NewRetrieved(cfgMap) | ||
| } | ||
This file contains hidden or 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 hidden or 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,52 @@ | ||
| // 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 mbprovider | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "go.opentelemetry.io/collector/confmap" | ||
|
|
||
| "github.com/elastic/beats/v7/libbeat/otelbeat/providers" | ||
| ) | ||
|
|
||
| const schemeName = "mb" | ||
|
|
||
| type provider struct{} | ||
|
|
||
| // The Provider provides configuration, and allows to watch/monitor for changes. | ||
|
khushijain21 marked this conversation as resolved.
Outdated
|
||
| func NewFactory() confmap.ProviderFactory { | ||
| return confmap.NewProviderFactory(newProvider) | ||
| } | ||
|
|
||
| func newProvider(confmap.ProviderSettings) confmap.Provider { | ||
| return &provider{} | ||
| } | ||
|
|
||
| // Retrieve retrieves the beat configuration file and constructs otel config | ||
| func (fmp *provider) Retrieve(_ context.Context, uri string, _ confmap.WatcherFunc) (*confmap.Retrieved, error) { | ||
| return providers.LoadConfig(uri, schemeName) | ||
| } | ||
|
khushijain21 marked this conversation as resolved.
|
||
|
|
||
| func (*provider) Scheme() string { | ||
| return schemeName | ||
| } | ||
|
|
||
| func (*provider) Shutdown(context.Context) error { | ||
| return nil | ||
| } | ||
|
khushijain21 marked this conversation as resolved.
Outdated
|
||
127 changes: 127 additions & 0 deletions
127
libbeat/otelbeat/providers/mbprovider/mbprovider_test.go
This file contains hidden or 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,127 @@ | ||
| // 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 mbprovider | ||
|
|
||
| import ( | ||
| "context" | ||
| _ "embed" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.opentelemetry.io/collector/confmap" | ||
|
|
||
| "gopkg.in/yaml.v2" | ||
| ) | ||
|
|
||
| var beatsConfig = ` | ||
| metricbeat.modules: | ||
| - module: system | ||
| metricsets: | ||
| - cpu # CPU usage | ||
| - load # CPU load averages | ||
| enabled: true | ||
| period: 10s | ||
| processes: ['.*'] | ||
|
|
||
|
|
||
| output: | ||
| elasticsearch: | ||
| hosts: ["https://localhost:9200"] | ||
| username: elastic | ||
| password: changeme | ||
| index: form-otel-exporter | ||
| ssl.enabled: false | ||
| ` | ||
|
|
||
| var expectedOutput = ` | ||
| receivers: | ||
| metricbeatreceiver: | ||
| metricbeat: | ||
| modules: | ||
| - module: system | ||
| enabled: true | ||
| metricsets: | ||
| - cpu | ||
| - load | ||
| processes: ['.*'] | ||
| period: 10s | ||
| path: | ||
| config: . | ||
| data: ./data | ||
| home: . | ||
| logs: ./logs | ||
| output: | ||
| elasticsearch: | ||
| hosts: ["https://localhost:9200"] | ||
| username: elastic | ||
| password: changeme | ||
| index: form-otel-exporter | ||
| ssl: | ||
| enabled: false | ||
|
|
||
| service: | ||
| pipelines: | ||
| logs: | ||
| receivers: | ||
| - "metricbeatreceiver" | ||
| ` | ||
|
|
||
| func TestMetricbeatProvider(t *testing.T) { | ||
| p := provider{} | ||
|
|
||
| t.Run("test metricbeat provider", func(t *testing.T) { | ||
|
|
||
| tempFile, err := os.CreateTemp("", "metricbeat.yml") | ||
| require.NoError(t, err, "error creating temp file") | ||
| defer os.Remove(tempFile.Name()) // Clean up the file after we're done | ||
| defer tempFile.Close() | ||
|
khushijain21 marked this conversation as resolved.
Outdated
|
||
|
|
||
| content := []byte(beatsConfig) | ||
| _, err = tempFile.Write(content) | ||
| require.NoError(t, err, "error creating temp file") | ||
|
khushijain21 marked this conversation as resolved.
Outdated
|
||
|
|
||
| // prefix file path with fb: | ||
| ret, err := p.Retrieve(context.Background(), "mb:"+tempFile.Name(), nil) | ||
| require.NoError(t, err) | ||
|
|
||
| retValue, err := ret.AsRaw() | ||
| require.NoError(t, err) | ||
| expOutput := newFromYamlString(t, expectedOutput) | ||
|
|
||
| // convert it into a common type | ||
| want, err := yaml.Marshal(expOutput.ToStringMap()) | ||
| require.NoError(t, err) | ||
| got, err := yaml.Marshal(retValue) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, string(want), string(got)) | ||
| assert.NoError(t, p.Shutdown(context.Background())) | ||
| }) | ||
|
|
||
| } | ||
|
|
||
| func newFromYamlString(t *testing.T, input string) *confmap.Conf { | ||
| t.Helper() | ||
| var rawConf map[string]any | ||
| err := yaml.Unmarshal([]byte(input), &rawConf) | ||
| require.NoError(t, err) | ||
|
|
||
| return confmap.NewFromStringMap(rawConf) | ||
| } | ||
This file contains hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.