Skip to content

Commit

Permalink
Normalize trailing and leading slashes
Browse files Browse the repository at this point in the history
Updated per PR feedback to normalize URLs
  • Loading branch information
fredthomsen committed May 12, 2023
1 parent 27a51e2 commit 9d68924
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
3 changes: 3 additions & 0 deletions receiver/otlpreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package otlpreceiver // import "go.opentelemetry.io/collector/receiver/otlprecei

import (
"errors"
"strings"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configgrpc"
Expand Down Expand Up @@ -71,6 +72,8 @@ func (cfg *Config) Unmarshal(conf *confmap.Conf) error {

if !conf.IsSet(protoHTTP) {
cfg.HTTP = nil
} else {
cfg.HTTP.PathPrefix = strings.Trim(cfg.HTTP.PathPrefix, "/")
}

return nil
Expand Down
14 changes: 13 additions & 1 deletion receiver/otlpreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ func TestUnmarshalConfigOnlyHTTP(t *testing.T) {
assert.Equal(t, defaultOnlyHTTP, cfg)
}

func TestUnmarshalConfigOnlyHTTPSlashPrefixPath(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "only_http_slash_prefix.yaml"))
require.NoError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalConfig(cm, cfg))

defaultOnlyHTTP := factory.CreateDefaultConfig().(*Config)
defaultOnlyHTTP.GRPC = nil
assert.Equal(t, defaultOnlyHTTP, cfg)
}

func TestUnmarshalConfigOnlyHTTPNull(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "only_http_null.yaml"))
require.NoError(t, err)
Expand Down Expand Up @@ -140,7 +152,7 @@ func TestUnmarshalConfig(t *testing.T) {
MaxAge: 7200,
},
},
PathPrefix: "testprefix",
PathPrefix: "otlp",
},
},
}, cfg)
Expand Down
2 changes: 1 addition & 1 deletion receiver/otlpreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ protocols:
- https://*.test.com # Wildcard subdomain. Allows domains like https://www.test.com and https://foo.test.com but not https://wwwtest.com.
- https://test.com # Fully qualified domain name. Allows https://test.com only.
max_age: 7200
path_prefix: "testprefix"
path_prefix: "/otlp"
4 changes: 4 additions & 0 deletions receiver/otlpreceiver/testdata/only_http_slash_prefix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# The following entry initializes the default OTLP receiver with only http support and no-op slash path prefix.
protocols:
http:
path_prefix: /

0 comments on commit 9d68924

Please sign in to comment.