forked from open-telemetry/opentelemetry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unify OTLP path parsing/default Logic (open-telemetry#2639)
* unify otlp path parsing/default logic * add changelog * add license and unit test * remove else branch * increase unitt test coverage * add vanity import * Update exporters/otlp/internal/config.go Co-authored-by: Tyler Yahn <[email protected]> * Update exporters/otlp/internal/config.go Co-authored-by: Tyler Yahn <[email protected]> * Update exporters/otlp/internal/config.go Co-authored-by: Tyler Yahn <[email protected]> * Update CHANGELOG.md Co-authored-by: Tyler Yahn <[email protected]> * Update exporters/otlp/internal/config_test.go Co-authored-by: Tyler Yahn <[email protected]> * Update exporters/otlp/internal/config_test.go Co-authored-by: Tyler Yahn <[email protected]> * format the config_test.go * Update exporters/otlp/internal/config_test.go Co-authored-by: Sam Xie <[email protected]> * Update exporters/otlp/internal/config_test.go Co-authored-by: Sam Xie <[email protected]> * Update exporters/otlp/internal/config_test.go Co-authored-by: Sam Xie <[email protected]> * Update exporters/otlp/internal/config_test.go Co-authored-by: Sam Xie <[email protected]> * Update exporters/otlp/internal/config.go Co-authored-by: Sam Xie <[email protected]> * Update exporters/otlp/internal/config.go Co-authored-by: Sam Xie <[email protected]> * change URLPath to urlPath Co-authored-by: Tyler Yahn <[email protected]> Co-authored-by: Sam Xie <[email protected]>
- Loading branch information
1 parent
061572b
commit 0919652
Showing
5 changed files
with
122 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 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 internal contains common functionality for all OTLP exporters. | ||
package internal // import "go.opentelemetry.io/otel/exporters/otlp/internal" | ||
|
||
import ( | ||
"fmt" | ||
"path" | ||
"strings" | ||
) | ||
|
||
// CleanPath returns a path with all spaces trimmed and all redundancies removed. If urlPath is empty or cleaning it results in an empty string, defaultPath is returned instead. | ||
func CleanPath(urlPath string, defaultPath string) string { | ||
tmp := path.Clean(strings.TrimSpace(urlPath)) | ||
if tmp == "." { | ||
return defaultPath | ||
} | ||
if !path.IsAbs(tmp) { | ||
tmp = fmt.Sprintf("/%s", tmp) | ||
} | ||
return tmp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// 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 internal | ||
|
||
import "testing" | ||
|
||
func TestCleanPath(t *testing.T) { | ||
type args struct { | ||
urlPath string | ||
defaultPath string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{ | ||
name: "clean empty path", | ||
args: args{ | ||
urlPath: "", | ||
defaultPath: "DefaultPath", | ||
}, | ||
want: "DefaultPath", | ||
}, | ||
{ | ||
name: "clean metrics path", | ||
args: args{ | ||
urlPath: "/prefix/v1/metrics", | ||
defaultPath: "DefaultMetricsPath", | ||
}, | ||
want: "/prefix/v1/metrics", | ||
}, | ||
{ | ||
name: "clean traces path", | ||
args: args{ | ||
urlPath: "https://env_endpoint", | ||
defaultPath: "DefaultTracesPath", | ||
}, | ||
want: "/https:/env_endpoint", | ||
}, | ||
{ | ||
name: "spaces trimmed", | ||
args: args{ | ||
urlPath: " /dir", | ||
}, | ||
want: "/dir", | ||
}, | ||
{ | ||
name: "clean path empty", | ||
args: args{ | ||
urlPath: "dir/..", | ||
defaultPath: "DefaultTracesPath", | ||
}, | ||
want: "DefaultTracesPath", | ||
}, | ||
{ | ||
name: "make absolute", | ||
args: args{ | ||
urlPath: "dir/a", | ||
}, | ||
want: "/dir/a", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := CleanPath(tt.args.urlPath, tt.args.defaultPath); got != tt.want { | ||
t.Errorf("CleanPath() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters