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

[extension/subprocess] Create subprocess extension (#6467) #6512

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions extension/subprocess/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
37 changes: 37 additions & 0 deletions extension/subprocess/README.md
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]
```
33 changes: 33 additions & 0 deletions extension/subprocess/config.go
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"`
}
55 changes: 55 additions & 0 deletions extension/subprocess/config_test.go
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) {
Copy link
Contributor

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

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])
}
47 changes: 47 additions & 0 deletions extension/subprocess/factory.go
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
}
45 changes: 45 additions & 0 deletions extension/subprocess/factory_test.go
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)
}
36 changes: 36 additions & 0 deletions extension/subprocess/go.mod
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
)
Loading