Skip to content

Commit

Permalink
[component] make Type implement MarshalText (#9856)
Browse files Browse the repository at this point in the history
**Description:**
Adds `MarshalText` function so that `Type` can be properly marshaled as yaml.

**Link to tracking Issue:** Fixes #9855
  • Loading branch information
TylerHelmuth authored Mar 27, 2024
1 parent 1038b67 commit 9c5bf54
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 54 deletions.
25 changes: 25 additions & 0 deletions .chloggen/fix-componets-command.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: component

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix issue where the `components` command wasn't properly printing the component type.

# One or more tracking issues or pull requests related to the change
issues: [9856]

# (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:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
5 changes: 5 additions & 0 deletions component/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ func (t Type) String() string {
return t.name
}

// MarshalText marshals returns the Type name.
func (t Type) MarshalText() ([]byte, error) {
return []byte(t.name), nil
}

// typeRegexp is used to validate the type of a component.
// A type must start with an ASCII alphabetic character and
// can only contain ASCII alphanumeric characters and '_'.
Expand Down
57 changes: 3 additions & 54 deletions otelcol/command_components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ package otelcol

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

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"

"go.opentelemetry.io/collector/component"
)

var nopType = component.MustNewType("nop")

func TestNewBuildSubCommand(t *testing.T) {
cfgProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-nop.yaml")}))
require.NoError(t, err)
Expand All @@ -30,56 +28,7 @@ func TestNewBuildSubCommand(t *testing.T) {
cmd := NewCommand(set)
cmd.SetArgs([]string{"components"})

ExpectedYamlStruct := componentsOutput{
BuildInfo: component.NewDefaultBuildInfo(),
Receivers: []componentWithStability{{
Name: nopType,
Stability: map[string]string{
"logs": "Stable",
"metrics": "Stable",
"traces": "Stable",
},
}},
Processors: []componentWithStability{{
Name: nopType,
Stability: map[string]string{
"logs": "Stable",
"metrics": "Stable",
"traces": "Stable",
},
}},
Exporters: []componentWithStability{{
Name: nopType,
Stability: map[string]string{
"logs": "Stable",
"metrics": "Stable",
"traces": "Stable",
},
}},
Connectors: []componentWithStability{{
Name: nopType,
Stability: map[string]string{
"logs-to-logs": "Development",
"logs-to-metrics": "Development",
"logs-to-traces": "Development",

"metrics-to-logs": "Development",
"metrics-to-metrics": "Development",
"metrics-to-traces": "Development",

"traces-to-logs": "Development",
"traces-to-metrics": "Development",
"traces-to-traces": "Development",
},
}},
Extensions: []componentWithStability{{
Name: nopType,
Stability: map[string]string{
"extension": "Stable",
},
}},
}
ExpectedOutput, err := yaml.Marshal(ExpectedYamlStruct)
ExpectedOutput, err := os.ReadFile(filepath.Join("testdata", "components-output.yaml"))
require.NoError(t, err)

b := bytes.NewBufferString("")
Expand All @@ -89,5 +38,5 @@ func TestNewBuildSubCommand(t *testing.T) {

// 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"))
assert.Equal(t, strings.ReplaceAll(strings.ReplaceAll(string(ExpectedOutput), "\n", ""), "\r", ""), strings.ReplaceAll(strings.ReplaceAll(b.String(), "\n", ""), "\r", ""))
}
38 changes: 38 additions & 0 deletions otelcol/testdata/components-output.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
buildinfo:
command: otelcol
description: OpenTelemetry Collector
version: latest
receivers:
- name: nop
stability:
logs: Stable
metrics: Stable
traces: Stable
processors:
- name: nop
stability:
logs: Stable
metrics: Stable
traces: Stable
exporters:
- name: nop
stability:
logs: Stable
metrics: Stable
traces: Stable
connectors:
- name: nop
stability:
logs-to-logs: Development
logs-to-metrics: Development
logs-to-traces: Development
metrics-to-logs: Development
metrics-to-metrics: Development
metrics-to-traces: Development
traces-to-logs: Development
traces-to-metrics: Development
traces-to-traces: Development
extensions:
- name: nop
stability:
extension: Stable

0 comments on commit 9c5bf54

Please sign in to comment.