From fd36d05133d0f090bcf037f5ee7ede7b9c1ca8da Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Mon, 1 Jul 2024 09:04:13 -0700 Subject: [PATCH] [otlpexporter] support validation for dns:// and dns:/// (#10450) This allows users to continue using the recommended dns://authority/host:port notation when needing to specify a custom authority. Fixes #10449 --------- Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> Co-authored-by: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com> --- .../codeboten_remove-additional-slash.yaml | 25 +++++++++++++++++++ exporter/otlpexporter/config.go | 6 +++-- exporter/otlpexporter/config_test.go | 13 +++++++++- 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 .chloggen/codeboten_remove-additional-slash.yaml diff --git a/.chloggen/codeboten_remove-additional-slash.yaml b/.chloggen/codeboten_remove-additional-slash.yaml new file mode 100644 index 00000000000..72fbe623fdb --- /dev/null +++ b/.chloggen/codeboten_remove-additional-slash.yaml @@ -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: otlpexporter + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Update validation to support both dns:// and dns:/// + +# One or more tracking issues or pull requests related to the change +issues: [10449] + +# (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: [user] diff --git a/exporter/otlpexporter/config.go b/exporter/otlpexporter/config.go index 62956f293ed..f1d5b668e54 100644 --- a/exporter/otlpexporter/config.go +++ b/exporter/otlpexporter/config.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "net" + "regexp" "strconv" "strings" @@ -49,8 +50,9 @@ func (c *Config) sanitizedEndpoint() string { return strings.TrimPrefix(c.Endpoint, "http://") case strings.HasPrefix(c.Endpoint, "https://"): return strings.TrimPrefix(c.Endpoint, "https://") - case strings.HasPrefix(c.Endpoint, "dns:///"): - return strings.TrimPrefix(c.Endpoint, "dns:///") + case strings.HasPrefix(c.Endpoint, "dns://"): + r := regexp.MustCompile("^dns://[/]?") + return r.ReplaceAllString(c.Endpoint, "") default: return c.Endpoint } diff --git a/exporter/otlpexporter/config_test.go b/exporter/otlpexporter/config_test.go index 2167f803ef3..a29d0182860 100644 --- a/exporter/otlpexporter/config_test.go +++ b/exporter/otlpexporter/config_test.go @@ -134,6 +134,17 @@ func TestUnmarshalInvalidConfig(t *testing.T) { func TestValidDNSEndpoint(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig().(*Config) - cfg.Endpoint = "dns:///backend.example.com:4317" + cfg.Endpoint = "dns://authority/backend.example.com:4317" assert.NoError(t, cfg.Validate()) } + +func TestSanitizeEndpoint(t *testing.T) { + factory := NewFactory() + cfg := factory.CreateDefaultConfig().(*Config) + cfg.Endpoint = "dns://authority/backend.example.com:4317" + assert.Equal(t, "authority/backend.example.com:4317", cfg.sanitizedEndpoint()) + cfg.Endpoint = "dns:///backend.example.com:4317" + assert.Equal(t, "backend.example.com:4317", cfg.sanitizedEndpoint()) + cfg.Endpoint = "dns:////backend.example.com:4317" + assert.Equal(t, "/backend.example.com:4317", cfg.sanitizedEndpoint()) +}