-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add "components" Command #6322
Add "components" Command #6322
Changes from 1 commit
f0c3ead
f459bbc
2ce77ed
37eb34d
363bca9
8cd528a
72da9b6
03fa3d5
df35afd
560d5c1
0084318
cd495bf
6f9c455
80551fd
3b69252
cfc13a2
76b26e9
b6583e4
6a3cbe1
c9be8b7
42ae192
75cd2e4
bcd3003
c3aad30
b2bba0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Signed-off-by: Maureen <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package service | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/config" | ||
) | ||
|
||
type componentsOutput struct { | ||
BuildInfo component.BuildInfo | ||
Receivers []config.Type | ||
Processors []config.Type | ||
Exporters []config.Type | ||
Extensions []config.Type | ||
} | ||
|
||
// newBuildCommand constructs a new cobra.Command sub command using the given CollectorSettings. | ||
codeboten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func newBuildSubCommand(set CollectorSettings) *cobra.Command { | ||
buildCmd := &cobra.Command{ | ||
Use: "build-info", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about just "info" or "components"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'm in favour of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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.Println(string(yamlData)) | ||
return nil | ||
}, | ||
} | ||
return buildCmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package service | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"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/config" | ||
"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{"build-info"}) | ||
|
||
ExpectedYamlStruct := componentsOutput{ | ||
BuildInfo: component.NewDefaultBuildInfo(), | ||
Receivers: []config.Type{"nop"}, | ||
Processors: []config.Type{"nop"}, | ||
Exporters: []config.Type{"nop"}, | ||
Extensions: []config.Type{"nop"}, | ||
} | ||
ExpectedOutput, err := yaml.Marshal(ExpectedYamlStruct) | ||
require.NoError(t, err) | ||
|
||
// Obtaining StdOutput of cmd.Execute() | ||
oldStdOut := os.Stdout | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w // Write to os.StdOut | ||
|
||
err = cmd.Execute() | ||
require.NoError(t, err) | ||
|
||
bufChan := make(chan string) | ||
|
||
go func() { | ||
var buf bytes.Buffer | ||
_, err = io.Copy(&buf, r) | ||
require.NoError(t, err) | ||
bufChan <- buf.String() | ||
}() | ||
|
||
err = w.Close() | ||
require.NoError(t, err) | ||
defer func() { os.Stdout = oldStdOut }() // Restore os.Stdout to old value after test | ||
output := <-bufChan | ||
// 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(output, "\n"), strings.Trim(string(ExpectedOutput), "\n")) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,10 @@ processors: | |
nop: | ||
|
||
exporters: | ||
nop: | ||
nop: l | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are the changes in this file intentional? |
||
|
||
extensions: | ||
nop: | ||
nop: 9 | ||
|
||
service: | ||
telemetry: | ||
|
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.
Those two imports should be together (
go.opentelemetry.io/collector/...
) but I think the linter will tell you that as well :-)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.
It does
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.
Thanks I attempted putting them together but when I ran "make fmt", it sorted them back in this order