-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
[extension/subprocess] Create subprocess extension (#6467) #6512
Closed
NassimBtk
wants to merge
2
commits into
open-telemetry:main
from
sentrysoftware:feature/issue-6467-create-subprocess-extension
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include ../../Makefile.Common |
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,37 @@ | ||
# Subprocess Extension | ||
|
||
The `subprocess` extension facilitates running a subprocess of the collector. | ||
|
||
You can provide the configuration to the subprocess via the command line arguments. | ||
|
||
You can also specify the working directory of the command. | ||
|
||
## Configuration | ||
|
||
The executable path is required: | ||
|
||
- `executable_path`: The path of the command to run. If `executable_path` is relative, then it is evaluated relative to the working directory. | ||
|
||
The following settings can be optionally configured: | ||
|
||
- `args`: The command line arguments to use. | ||
- `working_directory`: Defines the working directory of the command. If it is not configured, the `subprocess` extension runs the command in the calling process's current directory. | ||
|
||
## Example Config | ||
|
||
```yaml | ||
extensions: | ||
health_check: | ||
sub_process: | ||
executable_path: bin/cmd | ||
args: | ||
- --port | ||
- 5989 | ||
working_directory: /usr/local/ | ||
|
||
receivers: | ||
# {...} | ||
service: | ||
|
||
extensions: [sub_process] | ||
``` |
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,33 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed 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 subprocess | ||
|
||
import ( | ||
"go.opentelemetry.io/collector/config" | ||
) | ||
|
||
// Config has the configuration for the extension. | ||
type Config struct { | ||
config.ExtensionSettings `mapstructure:",squash"` | ||
|
||
// The path to the executable. | ||
ExecutablePath string `mapstructure:"executable_path"` | ||
|
||
// Command line arguments. | ||
Args []string `mapstructure:"args"` | ||
|
||
// Working directory. | ||
WorkingDirectory string `mapstructure:"working_directory"` | ||
} |
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,55 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed 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 subprocess | ||
|
||
import ( | ||
"path" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/component/componenttest" | ||
"go.opentelemetry.io/collector/config" | ||
"go.opentelemetry.io/collector/config/configtest" | ||
) | ||
|
||
func TestLoadConfig(t *testing.T) { | ||
t.Parallel() | ||
factories, err := componenttest.NopFactories() | ||
require.NoError(t, err) | ||
|
||
factory := NewFactory() | ||
factories.Extensions[typeStr] = factory | ||
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories) | ||
|
||
require.Nil(t, err) | ||
require.NotNil(t, cfg) | ||
|
||
ext0 := cfg.Extensions[config.NewComponentID(typeStr)] | ||
assert.Equal(t, factory.CreateDefaultConfig(), ext0) | ||
|
||
ext1 := cfg.Extensions[config.NewComponentIDWithName(typeStr, "1")] | ||
assert.Equal(t, | ||
&Config{ | ||
ExtensionSettings: config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "1")), | ||
ExecutablePath: "bin/cmd", | ||
Args: []string{"arg1", "arg2"}, | ||
WorkingDirectory: "/usr/local", | ||
}, | ||
ext1) | ||
|
||
assert.Equal(t, 1, len(cfg.Service.Extensions)) | ||
assert.Equal(t, config.NewComponentIDWithName(typeStr, "1"), cfg.Service.Extensions[0]) | ||
} |
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,47 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed 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 subprocess | ||
|
||
import ( | ||
"context" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/config" | ||
"go.opentelemetry.io/collector/extension/extensionhelper" | ||
) | ||
|
||
const ( | ||
// The value of extension "type" in configuration. | ||
typeStr = "sub_process" | ||
) | ||
|
||
// NewFactory creates a factory for SubProcess extension. | ||
func NewFactory() component.ExtensionFactory { | ||
return extensionhelper.NewFactory( | ||
typeStr, | ||
createDefaultConfig, | ||
createExtension) | ||
} | ||
|
||
func createDefaultConfig() config.Extension { | ||
return &Config{ | ||
ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), | ||
} | ||
} | ||
|
||
func createExtension(_ context.Context, params component.ExtensionCreateSettings, cfg config.Extension) (component.Extension, error) { | ||
config := cfg.(*Config) | ||
return newProcessManager(config, params.Logger), nil | ||
} |
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,45 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed 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 subprocess | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/component/componenttest" | ||
"go.opentelemetry.io/collector/config" | ||
"go.opentelemetry.io/collector/config/configtest" | ||
) | ||
|
||
func TestFactory_CreateDefaultConfig(t *testing.T) { | ||
cfg := createDefaultConfig() | ||
assert.Equal(t, &Config{ | ||
ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)), | ||
}, cfg) | ||
|
||
assert.NoError(t, configtest.CheckConfigStruct(cfg)) | ||
ext, err := createExtension(context.Background(), componenttest.NewNopExtensionCreateSettings(), cfg) | ||
require.NoError(t, err) | ||
require.NotNil(t, ext) | ||
} | ||
|
||
func TestFactory_CreateExtension(t *testing.T) { | ||
cfg := createDefaultConfig().(*Config) | ||
ext, err := createExtension(context.Background(), componenttest.NewNopExtensionCreateSettings(), cfg) | ||
require.NoError(t, err) | ||
require.NotNil(t, ext) | ||
} |
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,36 @@ | ||
module github.com/open-telemetry/opentelemetry-collector-contrib/extension/subprocess | ||
|
||
go 1.17 | ||
|
||
require ( | ||
github.com/stretchr/testify v1.7.0 | ||
go.opentelemetry.io/collector v0.40.0 | ||
go.uber.org/zap v1.19.1 | ||
|
||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/benbjohnson/clock v1.2.0 // indirect | ||
github.com/fsnotify/fsnotify v1.4.9 // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/knadh/koanf v1.3.2 // indirect | ||
github.com/kr/pretty v0.3.0 // indirect | ||
github.com/magiconair/properties v1.8.5 // indirect | ||
github.com/mitchellh/copystructure v1.2.0 // indirect | ||
github.com/mitchellh/mapstructure v1.4.2 // indirect | ||
github.com/mitchellh/reflectwalk v1.0.2 // indirect | ||
github.com/pelletier/go-toml v1.9.3 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/spf13/cast v1.4.1 // indirect | ||
go.opentelemetry.io/collector/model v0.40.0 // indirect | ||
go.opentelemetry.io/otel v1.2.0 // indirect | ||
go.opentelemetry.io/otel/metric v0.25.0 // indirect | ||
go.opentelemetry.io/otel/trace v1.2.0 // indirect | ||
go.uber.org/atomic v1.9.0 // indirect | ||
go.uber.org/multierr v1.7.0 // indirect | ||
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect | ||
) |
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.
Could I ask you to add in
t.Parallel()
, just to help speed up the number of tests that can be executed concurrently