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

Add "components" Command #6322

Merged
merged 25 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f0c3ead
Created a fix for #4671
Chinwendu20 Oct 15, 2022
f459bbc
Added unit test and file in chloggen
Chinwendu20 Oct 18, 2022
2ce77ed
Merge branch 'main' into build-info-flag
Chinwendu20 Oct 18, 2022
37eb34d
Update service/flags.go
Chinwendu20 Oct 19, 2022
363bca9
Remove whitespace
Chinwendu20 Oct 19, 2022
8cd528a
Added build info sub command
Chinwendu20 Oct 20, 2022
72da9b6
fix merge conflict
Chinwendu20 Oct 20, 2022
03fa3d5
Added test
Chinwendu20 Oct 20, 2022
df35afd
Added comments in test
Chinwendu20 Oct 20, 2022
560d5c1
Fixed CI errors
Chinwendu20 Oct 21, 2022
0084318
Separated new command test in separate file
Chinwendu20 Oct 25, 2022
cd495bf
Update service/README.md
codeboten Oct 26, 2022
6f9c455
Update service/command_build_info.go
codeboten Oct 26, 2022
80551fd
Corrected wrong values chlog file
Chinwendu20 Oct 27, 2022
3b69252
Apply suggestions from code review
codeboten Oct 27, 2022
cfc13a2
Merge branch 'build-info-flag' of https://github.com/Chinwendu20/open…
Chinwendu20 Oct 28, 2022
76b26e9
Added license
Chinwendu20 Nov 5, 2022
b6583e4
Changed command name to components
Chinwendu20 Nov 5, 2022
6a3cbe1
vanity import
Chinwendu20 Nov 7, 2022
c9be8b7
Merge branch 'main' into build-info-flag
Chinwendu20 Nov 8, 2022
42ae192
Repalced deprecated config.Type with component.Type
Chinwendu20 Nov 8, 2022
75cd2e4
Fixed build info test
Chinwendu20 Nov 17, 2022
bcd3003
use cmd.OutOrStdout for output, makes testing easier
Nov 21, 2022
c3aad30
Fixed lint test
Chinwendu20 Nov 22, 2022
b2bba0b
Update .chloggen/build-info-flag.yaml
codeboten Nov 25, 2022
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
Prev Previous commit
Next Next commit
Separated new command test in separate file
Signed-off-by: Maureen <[email protected]>
  • Loading branch information
Chinwendu20 committed Oct 25, 2022
commit 008431875a1037b1de3fdc9a34bdec6049234f04
15 changes: 9 additions & 6 deletions service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,29 @@ For more technical details about how configuration is resolved you can read the
Use the sub command build-info. Below is an example:

```bash
.\otelcol build-info
./otelcorecol build-info
```
Sample output:

```yaml

version: 0.62.1-dev
buildinfo:
command: otelcorecol
description: Local OpenTelemetry Collector binary, testing only.
version: 0.62.1-dev
receivers:
- otlp
processors:
- memory_limiter
- batch
exporters:
- logging
- otlp
- otlphttp
- logging
extensions:
- memory_ballast
- zpages
- memory_ballast


```
## How to override config properties?
Expand Down Expand Up @@ -121,5 +125,4 @@ key:

1. Does not support setting a key that contains a dot `.`.
2. Does not support setting a key that contains a equal sign `=`.
3. The configuration key separator inside the value part of the property is "::". For example `--set "name={a::b: c}"` is equivalent with `--set name.a.b=c`.

3. The configuration key separator inside the value part of the property is "::". For example `--set "name={a::b: c}"` is equivalent with `--set name.a.b=c`.
47 changes: 0 additions & 47 deletions service/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,59 +16,12 @@ package service // import "go.opentelemetry.io/collector/service"

import (
"errors"
"fmt"

"go.opentelemetry.io/collector/component"

"github.com/spf13/cobra"

Copy link
Member

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 :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does

Copy link
Contributor Author

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

"gopkg.in/yaml.v3"

"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/featuregate"
)

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.
func newBuildSubCommand(set CollectorSettings) *cobra.Command {
buildCmd := &cobra.Command{
Use: "build-info",
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
}

// NewCommand constructs a new cobra.Command using the given CollectorSettings.
func NewCommand(set CollectorSettings) *cobra.Command {
flagSet := flags()
Expand Down
52 changes: 52 additions & 0 deletions service/command_build_info.go
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpkrohling @codeboten

What about just "info" or "components"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm in favour of components but don't have a strong preference here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

components would be my choice as well

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
}
71 changes: 71 additions & 0 deletions service/command_build_info_test.go
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"))
}
61 changes: 0 additions & 61 deletions service/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,9 @@
package service

import (
"bytes"
"io"
"os"
"path/filepath"
"strings"
"testing"

"gopkg.in/yaml.v3"

"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/featuregate"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -57,55 +48,3 @@ func TestNewCommandInvalidComponent(t *testing.T) {
cmd := NewCommand(CollectorSettings{Factories: factories, ConfigProvider: cfgProvider})
require.Error(t, cmd.Execute())
}

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"))
}
4 changes: 2 additions & 2 deletions service/testdata/otelcol-nop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ processors:
nop:

exporters:
nop:
nop: l
Copy link
Contributor

Choose a reason for hiding this comment

The 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:
Expand Down