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 all commits
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
16 changes: 16 additions & 0 deletions .chloggen/build-info-flag.yaml
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:
31 changes: 31 additions & 0 deletions service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,37 @@ For more technical details about how configuration is resolved you can read the

`./otelcorecol --config=file:examples/local/otel-config.yaml --config="yaml:exporters::logging::loglevel: info"`


## How to check components available in a distribution

Use the sub command build-info. Below is an example:

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

```yaml

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


```
## How to override config properties?

The `--set` flag allows to set arbitrary config property. The `--set` values are merged into the final configuration
Expand Down
2 changes: 1 addition & 1 deletion service/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewCommand(set CollectorSettings) *cobra.Command {
return col.Run(cmd.Context())
},
}

rootCmd.AddCommand(newBuildSubCommand(set))
rootCmd.Flags().AddGoFlagSet(flagSet)
return rootCmd
}
65 changes: 65 additions & 0 deletions service/command_build_info.go
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
}
66 changes: 66 additions & 0 deletions service/command_build_info_test.go
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"))
}