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

Unify OTLP path parsing/default Logic #2639

Merged
merged 24 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8aaa60d
unify otlp path parsing/default logic
hanyuancheung Feb 24, 2022
db2a08d
add changelog
hanyuancheung Feb 24, 2022
6d6cf9e
add license and unit test
hanyuancheung Feb 24, 2022
16be419
remove else branch
hanyuancheung Feb 24, 2022
d6c7059
increase unitt test coverage
hanyuancheung Feb 24, 2022
bd48ca6
add vanity import
hanyuancheung Feb 24, 2022
2701534
Update exporters/otlp/internal/config.go
hanyuancheung Feb 25, 2022
8a99844
Update exporters/otlp/internal/config.go
hanyuancheung Feb 25, 2022
9877a21
Update exporters/otlp/internal/config.go
hanyuancheung Feb 25, 2022
cdd7d1e
Update CHANGELOG.md
hanyuancheung Feb 25, 2022
0e7b6e8
Update exporters/otlp/internal/config_test.go
hanyuancheung Feb 25, 2022
cf86c84
Update exporters/otlp/internal/config_test.go
hanyuancheung Feb 25, 2022
a0fb2cf
format the config_test.go
hanyuancheung Feb 25, 2022
b2566ce
Merge branch 'main' into unify-otlp-path-logic
hanyuancheung Feb 25, 2022
5f6d4a0
Merge branch 'main' into unify-otlp-path-logic
hanyuancheung Feb 27, 2022
14fb4e5
Merge branch 'main' into unify-otlp-path-logic
hanyuancheung Mar 1, 2022
535493d
Merge branch 'main' into unify-otlp-path-logic
hanyuancheung Mar 2, 2022
85a2a20
Update exporters/otlp/internal/config_test.go
hanyuancheung Mar 2, 2022
d53ed18
Update exporters/otlp/internal/config_test.go
hanyuancheung Mar 2, 2022
c781a1a
Update exporters/otlp/internal/config_test.go
hanyuancheung Mar 2, 2022
2a191b1
Update exporters/otlp/internal/config_test.go
hanyuancheung Mar 2, 2022
896889c
Update exporters/otlp/internal/config.go
hanyuancheung Mar 2, 2022
d59525c
Update exporters/otlp/internal/config.go
hanyuancheung Mar 2, 2022
9d6cb63
change URLPath to urlPath
hanyuancheung Mar 2, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- For tracestate's members, prepend the new element and remove the oldest one, which is over capacity (#2592)
- Add event and link drop counts to the exported data from the `oltptrace` exporter. (#2601)
- Unify path cleaning functionally in the `otlpmetric` and `otlptrace` config. (#2639)
- Change the debug message from the `sdk/trace.BatchSpanProcessor` to reflect the count is cumulative. (#2640)

### Fixed
Expand Down
34 changes: 34 additions & 0 deletions exporters/otlp/internal/config.go
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
}
83 changes: 83 additions & 0 deletions exporters/otlp/internal/config_test.go
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",
},
hanyuancheung marked this conversation as resolved.
Show resolved Hide resolved
{
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)
}
})
}
}
15 changes: 2 additions & 13 deletions exporters/otlp/otlpmetric/internal/otlpconfig/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package otlpconfig // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric
import (
"crypto/tls"
"fmt"
"path"
"strings"
"time"

"google.golang.org/grpc"
Expand All @@ -27,6 +25,7 @@ import (
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/encoding/gzip"

"go.opentelemetry.io/otel/exporters/otlp/internal"
"go.opentelemetry.io/otel/exporters/otlp/internal/retry"
)

Expand Down Expand Up @@ -90,17 +89,7 @@ func NewHTTPConfig(opts ...HTTPOption) Config {
for _, opt := range opts {
cfg = opt.ApplyHTTPOption(cfg)
}

tmp := strings.TrimSpace(cfg.Metrics.URLPath)
if tmp == "" {
tmp = DefaultMetricsPath
} else {
tmp = path.Clean(tmp)
if !path.IsAbs(tmp) {
tmp = fmt.Sprintf("/%s", tmp)
}
}
cfg.Metrics.URLPath = tmp
cfg.Metrics.URLPath = internal.CleanPath(cfg.Metrics.URLPath, DefaultMetricsPath)
return cfg
}

Expand Down
15 changes: 2 additions & 13 deletions exporters/otlp/otlptrace/internal/otlpconfig/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package otlpconfig // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace/
import (
"crypto/tls"
"fmt"
"path"
"strings"
"time"

"google.golang.org/grpc"
Expand All @@ -27,6 +25,7 @@ import (
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/encoding/gzip"

"go.opentelemetry.io/otel/exporters/otlp/internal"
"go.opentelemetry.io/otel/exporters/otlp/internal/retry"
)

Expand Down Expand Up @@ -83,17 +82,7 @@ func NewHTTPConfig(opts ...HTTPOption) Config {
for _, opt := range opts {
cfg = opt.ApplyHTTPOption(cfg)
}

tmp := strings.TrimSpace(cfg.Traces.URLPath)
if tmp == "" {
tmp = DefaultTracesPath
} else {
tmp = path.Clean(tmp)
if !path.IsAbs(tmp) {
tmp = fmt.Sprintf("/%s", tmp)
}
}
cfg.Traces.URLPath = tmp
cfg.Traces.URLPath = internal.CleanPath(cfg.Traces.URLPath, DefaultTracesPath)
return cfg
}

Expand Down