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

[configopaque] configopaque.String implements fmt.Stringer #9214

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
25 changes: 25 additions & 0 deletions .chloggen/configopaque_stringer.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: enhancement
Copy link
Member

Choose a reason for hiding this comment

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

Given the impact you discovered in contrib, I would mark this as a breaking just to be safe and make sure more eyes are on this

Suggested change
change_type: enhancement
change_type: breaking

Copy link
Member

@bogdandrutu bogdandrutu Jan 5, 2024

Choose a reason for hiding this comment

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

For me to learn: Confused based on what golang rules this is breaking.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess Pablo is trying to make sure this is advertised as a change that can break some uses of the configopaque.String as part of its use with fmt.Stringer. This is not really a change impacting the go API.

Copy link
Member

Choose a reason for hiding this comment

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

For my to learn: Confused based on what golang rules this is breaking.

Yes, arguably it's a breakage in behavior, but it's not impacting the API


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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: configopaque.String implements `fmt.Stringer`, outputting [REDACTED] when used as `fmt.Sprintf("%s", opaquestring)`

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

# (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: [api]
7 changes: 7 additions & 0 deletions config/configopaque/opaque.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ package configopaque // import "go.opentelemetry.io/collector/config/configopaqu

import (
"encoding"
"fmt"
)

// String alias that is marshaled in an opaque way.
type String string

var _ fmt.Stringer = String("")

const maskedString = "[REDACTED]"

var _ encoding.TextMarshaler = String("")
Expand All @@ -18,3 +21,7 @@ var _ encoding.TextMarshaler = String("")
func (s String) MarshalText() ([]byte, error) {
return []byte(maskedString), nil
}

func (s String) String() string {
atoulme marked this conversation as resolved.
Show resolved Hide resolved
return maskedString
}
12 changes: 12 additions & 0 deletions config/configopaque/opaque_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package configopaque // import "go.opentelemetry.io/collector/config/configopaque"

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -18,3 +19,14 @@ func TestStringMarshalText(t *testing.T) {
assert.Equal(t, "[REDACTED]", string(opaque))
}
}

func TestStringFmt(t *testing.T) {
examples := []String{"opaque", "s", "veryveryveryveryveryveryveryveryveryverylong"}
for _, example := range examples {
// nolint S1025
assert.Equal(t, "[REDACTED]", fmt.Sprintf("%s", example))
// converting to a string allows to output as an unredacted string still:
// nolint S1025
assert.Equal(t, string(example), fmt.Sprintf("%s", string(example)))
}
}
Loading