forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "components" Command (open-telemetry#6322)
Enhancement: Added build command to output components in collector distribution in YAML format Signed-off-by: Maureen <[email protected]> Co-authored-by: Juraci Paixão Kröhling <[email protected]> Co-authored-by: Alex Boten <[email protected]>
- Loading branch information
1 parent
3e45b6e
commit 0a986af
Showing
5 changed files
with
179 additions
and
1 deletion.
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,16 @@ | ||
# 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. otlpreceiver) | ||
component: service | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Added components sub command which outputs components in collector distribution. | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [4671] | ||
|
||
# (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: |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// 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 service // import "go.opentelemetry.io/collector/service" | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
) | ||
|
||
type componentsOutput struct { | ||
BuildInfo component.BuildInfo | ||
Receivers []component.Type | ||
Processors []component.Type | ||
Exporters []component.Type | ||
Extensions []component.Type | ||
} | ||
|
||
// newBuildSubCommand constructs a new cobra.Command sub command using the given CollectorSettings. | ||
func newBuildSubCommand(set CollectorSettings) *cobra.Command { | ||
buildCmd := &cobra.Command{ | ||
Use: "components", | ||
Short: "Outputs available components in this collector distribution", | ||
Args: cobra.ExactArgs(0), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
|
||
components := componentsOutput{} | ||
for ext := range set.Factories.Extensions { | ||
components.Extensions = append(components.Extensions, ext) | ||
} | ||
for prs := range set.Factories.Processors { | ||
components.Processors = append(components.Processors, prs) | ||
} | ||
for rcv := range set.Factories.Receivers { | ||
components.Receivers = append(components.Receivers, rcv) | ||
} | ||
for exp := range set.Factories.Exporters { | ||
components.Exporters = append(components.Exporters, exp) | ||
} | ||
components.BuildInfo = set.BuildInfo | ||
yamlData, err := yaml.Marshal(components) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprint(cmd.OutOrStdout(), string(yamlData)) | ||
return nil | ||
}, | ||
} | ||
return buildCmd | ||
} |
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,66 @@ | ||
// 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 service | ||
|
||
import ( | ||
"bytes" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"gopkg.in/yaml.v3" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/component/componenttest" | ||
"go.opentelemetry.io/collector/featuregate" | ||
) | ||
|
||
func TestNewBuildSubCommand(t *testing.T) { | ||
factories, err := componenttest.NopFactories() | ||
require.NoError(t, err) | ||
|
||
cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-nop.yaml")})) | ||
require.NoError(t, err) | ||
|
||
set := CollectorSettings{ | ||
BuildInfo: component.NewDefaultBuildInfo(), | ||
Factories: factories, | ||
ConfigProvider: cfgProvider, | ||
telemetry: newColTelemetry(featuregate.NewRegistry()), | ||
} | ||
cmd := NewCommand(set) | ||
cmd.SetArgs([]string{"components"}) | ||
|
||
ExpectedYamlStruct := componentsOutput{ | ||
BuildInfo: component.NewDefaultBuildInfo(), | ||
Receivers: []component.Type{"nop"}, | ||
Processors: []component.Type{"nop"}, | ||
Exporters: []component.Type{"nop"}, | ||
Extensions: []component.Type{"nop"}, | ||
} | ||
ExpectedOutput, err := yaml.Marshal(ExpectedYamlStruct) | ||
require.NoError(t, err) | ||
|
||
b := bytes.NewBufferString("") | ||
cmd.SetOut(b) | ||
err = cmd.Execute() | ||
require.NoError(t, err) | ||
|
||
// Trim new line at the end of the two strings to make a better comparison as string() adds an extra new | ||
// line that makes the test fail. | ||
assert.Equal(t, strings.Trim(string(ExpectedOutput), "\n"), strings.Trim(b.String(), "\n")) | ||
} |