-
Notifications
You must be signed in to change notification settings - Fork 3k
[pkg/ottl] parse uri string to url.* SemConv attributes #32906
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
Merged
djaglowski
merged 61 commits into
open-telemetry:main
from
michalpristas:ottl/uri-parser
Jun 17, 2024
Merged
Changes from 15 commits
Commits
Show all changes
61 commits
Select commit
Hold shift + click to select a range
b721c09
initial uri bootstrap
michalpristas 4fa5b2d
Uri parser for ottl
michalpristas 44fbc78
changelog
michalpristas 2a75ec2
Update pkg/ottl/ottlfuncs/README.md
michalpristas 46f67a4
Merge branch 'main' into ottl/uri-parser
michalpristas 8ad8d9e
lint
michalpristas d1d942a
Merge branch 'main' of github.com:open-telemetry/opentelemetry-collec…
michalpristas fa3e18c
Merge branch 'ottl/uri-parser' of github.com:michalpristas/openteleme…
michalpristas 29d1e8b
Update pkg/ottl/ottlfuncs/func_uri.go
michalpristas bfbe183
Make uri uppercase"
michalpristas e71e65b
Merge branch 'main' of github.com:open-telemetry/opentelemetry-collec…
michalpristas 5bdda14
URI in tests
michalpristas bcd8b49
fixed import order for uri tests
michalpristas e4d8e58
Merge branch 'main' into ottl/uri-parser
michalpristas a1bde01
Merge branch 'main' into ottl/uri-parser
michalpristas 535d6bd
disable uri to URI lint for func_uri
michalpristas 1f88ccb
Merge branch 'ottl/uri-parser' of github.com:michalpristas/openteleme…
michalpristas e182d16
Merge branch 'main' of github.com:open-telemetry/opentelemetry-collec…
michalpristas 7bd764d
fix error is always nil check
michalpristas b266800
resolved conflicts with main
michalpristas 6bc8e8f
saved functions.go file
michalpristas f9adfbe
Merge branch 'main' into ottl/uri-parser
michalpristas 3809d16
Merge branch 'main' into ottl/uri-parser
michalpristas ae73009
Merge branch 'main' into ottl/uri-parser
michalpristas a128f7c
Merge branch 'main' into ottl/uri-parser
michalpristas f041030
Merge branch 'main' into ottl/uri-parser
michalpristas 8a843d7
Merge branch 'main' into ottl/uri-parser
michalpristas 63dc323
Merge branch 'main' into ottl/uri-parser
michalpristas 5c9e5c9
resolved conflicts in functions.go
michalpristas 8f4b7ae
resolved conflicts in functions.go
michalpristas 7884983
Merge branch 'main' of github.com:open-telemetry/opentelemetry-collec…
michalpristas 395fe2c
replace hardcoded strings with ottl convention
michalpristas 4369458
Merge branch 'main' into ottl/uri-parser
michalpristas ec00160
govulncheck issue addressed
michalpristas a544e05
Merge branch 'main' of github.com:open-telemetry/opentelemetry-collec…
michalpristas 1db9ffc
Merge branch 'ottl/uri-parser' of github.com:michalpristas/openteleme…
michalpristas d4e0b33
resovled conflicts with main
michalpristas 52b4c42
fixed go.mod for countconnector
michalpristas 5eb0620
indirect semconv
michalpristas d204d4a
lint
michalpristas db455ea
move logic to stanza uri parser
michalpristas ea2ef4f
more linting
michalpristas 800f741
more linting
michalpristas 747218b
fix typo
michalpristas 67746dd
Merge branch 'main' of github.com:open-telemetry/opentelemetry-collec…
michalpristas 29db7eb
check fixed
michalpristas 95b8704
crosslink
michalpristas 95e56e0
move parse uri to separate package, remove stanza dep from ottl
michalpristas 308c533
ottl crosslink
michalpristas 2cde9d7
stanza indirect dependencies removed
michalpristas 6c9ca14
Merge branch 'main' of github.com:open-telemetry/opentelemetry-collec…
michalpristas 41b5ea4
import added to parseutils/uri.go
michalpristas 7155d02
stanza go mod
michalpristas 69b6745
Update pkg/stanza/operator/parser/uri/parser.go
michalpristas 80773df
Update pkg/ottl/ottlfuncs/func_uri.go
michalpristas da2bc40
Update pkg/ottl/ottlfuncs/README.md
michalpristas d6a7114
Update pkg/ottl/ottlfuncs/README.md
michalpristas 3ae53fb
Merge branch 'main' into ottl/uri-parser
michalpristas 6236224
proper naming, URI vs URL
michalpristas 1e2c2da
updated docs
michalpristas e925180
missing files after rename
michalpristas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: pkg/ottl | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Introducing `Uri` converter parsing URI string into SemConv | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [32433] | ||
|
||
# (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: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# 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] |
This file contains hidden or 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 |
---|---|---|
|
@@ -1292,6 +1292,44 @@ Examples: | |
|
||
- `UnixSeconds(Time("02/04/2023", "%m/%d/%Y"))` | ||
|
||
### URI | ||
|
||
`URI(uri_string)` | ||
|
||
Parses a Uniform Resource Identifier (URI) string and extracts its components as an object. | ||
This URI object includes properties for the URI’s domain, path, fragment, port, query, scheme, user info, username, and password. | ||
|
||
`original`, `domain`, `scheme`, and `path` are always present. Other properties are present only if they have corresponding values. | ||
|
||
`uri_string` is a `string`. | ||
|
||
- `URI("http://www.example.com")` | ||
|
||
results in | ||
``` | ||
"original": "http://www.example.com", | ||
"scheme": "http", | ||
"domain": "www.example.com", | ||
"path": "", | ||
michalpristas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
- `URI("http://myusername:[email protected]:80/foo.gif?key1=val1&key2=val2#fragment")` | ||
|
||
results in | ||
``` | ||
"path": "/foo.gif", | ||
"fragment": "fragment", | ||
"extension": "gif", | ||
"password": "mypassword", | ||
"original": "http://myusername:[email protected]:80/foo.gif?key1=val1&key2=val2#fragment", | ||
"scheme": "http", | ||
"port": 80, | ||
"user_info": "myusername:mypassword", | ||
"domain": "www.example.com", | ||
"query": "key1=val1&key2=val2", | ||
"username": "myusername", | ||
michalpristas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
### UUID | ||
|
||
`UUID()` | ||
|
This file contains hidden or 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,93 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
type URIArguments[K any] struct { | ||
URI ottl.StringGetter[K] | ||
} | ||
|
||
func NewURIFactory[K any]() ottl.Factory[K] { | ||
return ottl.NewFactory("URI", &URIArguments[K]{}, createURIFunction[K]) | ||
} | ||
|
||
func createURIFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { | ||
args, ok := oArgs.(*URIArguments[K]) | ||
if !ok { | ||
return nil, fmt.Errorf("URIFactory args must be of type *URIArguments[K]") | ||
} | ||
|
||
return URI(args.URI) | ||
} | ||
|
||
func URI[K any](uriSource ottl.StringGetter[K]) (ottl.ExprFunc[K], error) { | ||
TylerHelmuth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return func(ctx context.Context, tCtx K) (any, error) { | ||
uriString, err := uriSource.Get(ctx, tCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if uriString == "" { | ||
return nil, fmt.Errorf("uri cannot be nil") | ||
michalpristas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
uriParts := make(map[string]any) | ||
|
||
parsedURI, err := url.Parse(uriString) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// always present fields | ||
uriParts["original"] = uriString | ||
uriParts["domain"] = parsedURI.Hostname() | ||
uriParts["scheme"] = parsedURI.Scheme | ||
uriParts["path"] = parsedURI.Path | ||
|
||
// optional fields included only if populated | ||
if port := parsedURI.Port(); len(port) > 0 { | ||
uriParts["port"], err = strconv.Atoi(port) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if fragment := parsedURI.Fragment; len(fragment) > 0 { | ||
uriParts["fragment"] = fragment | ||
} | ||
|
||
if parsedURI.User != nil { | ||
uriParts["user_info"] = parsedURI.User.String() | ||
|
||
if username := parsedURI.User.Username(); len(username) > 0 { | ||
uriParts["username"] = username | ||
} | ||
|
||
if pwd, isSet := parsedURI.User.Password(); isSet { | ||
uriParts["password"] = pwd | ||
} | ||
} | ||
|
||
if query := parsedURI.RawQuery; len(query) > 0 { | ||
uriParts["query"] = query | ||
} | ||
|
||
if periodIdx := strings.LastIndex(parsedURI.Path, "."); periodIdx != -1 { | ||
if periodIdx < len(parsedURI.Path)-1 { | ||
uriParts["extension"] = parsedURI.Path[periodIdx+1:] | ||
} | ||
} | ||
|
||
return uriParts, nil | ||
}, nil | ||
} |
This file contains hidden or 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,172 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
func TestURIParser(t *testing.T) { | ||
testCases := []struct { | ||
Name string | ||
Original string | ||
ExpectedMap map[string]any | ||
}{ | ||
{ | ||
"complete example", | ||
"http://myusername:[email protected]:80/foo.gif?key1=val1&key2=val2#fragment", | ||
map[string]any{ | ||
"path": "/foo.gif", | ||
"fragment": "fragment", | ||
"extension": "gif", | ||
"password": "mypassword", | ||
"original": "http://myusername:[email protected]:80/foo.gif?key1=val1&key2=val2#fragment", | ||
"scheme": "http", | ||
"port": 80, | ||
"user_info": "myusername:mypassword", | ||
"domain": "www.example.com", | ||
"query": "key1=val1&key2=val2", | ||
"username": "myusername", | ||
}, | ||
}, | ||
{ | ||
"simple example", | ||
"http://www.example.com", | ||
map[string]any{ | ||
"original": "http://www.example.com", | ||
"scheme": "http", | ||
"domain": "www.example.com", | ||
"path": "", | ||
}, | ||
}, | ||
{ | ||
"custom port", | ||
"http://www.example.com:77", | ||
map[string]any{ | ||
"original": "http://www.example.com:77", | ||
"scheme": "http", | ||
"domain": "www.example.com", | ||
"path": "", | ||
"port": 77, | ||
}, | ||
}, | ||
{ | ||
"file", | ||
"http://www.example.com:77/file.png", | ||
map[string]any{ | ||
"original": "http://www.example.com:77/file.png", | ||
"scheme": "http", | ||
"domain": "www.example.com", | ||
"path": "/file.png", | ||
"port": 77, | ||
"extension": "png", | ||
}, | ||
}, | ||
{ | ||
"fragment", | ||
"http://www.example.com:77/foo#bar", | ||
map[string]any{ | ||
"original": "http://www.example.com:77/foo#bar", | ||
"scheme": "http", | ||
"domain": "www.example.com", | ||
"path": "/foo", | ||
"port": 77, | ||
"fragment": "bar", | ||
}, | ||
}, | ||
{ | ||
"query example", | ||
"https://www.example.com:77/foo?key=val", | ||
map[string]any{ | ||
"original": "https://www.example.com:77/foo?key=val", | ||
"scheme": "https", | ||
"domain": "www.example.com", | ||
"path": "/foo", | ||
"port": 77, | ||
"query": "key=val", | ||
}, | ||
}, | ||
{ | ||
"user info", | ||
"https://user:[email protected]:77/foo", | ||
map[string]any{ | ||
"original": "https://user:[email protected]:77/foo", | ||
"scheme": "https", | ||
"domain": "www.example.com", | ||
"path": "/foo", | ||
"port": 77, | ||
"user_info": "user:pw", | ||
"username": "user", | ||
"password": "pw", | ||
}, | ||
}, | ||
{ | ||
"user info - no password", | ||
"https://user:@www.example.com:77/foo", | ||
map[string]any{ | ||
"original": "https://user:@www.example.com:77/foo", | ||
"scheme": "https", | ||
"domain": "www.example.com", | ||
"path": "/foo", | ||
"port": 77, | ||
"user_info": "user:", | ||
"username": "user", | ||
"password": "", | ||
}, | ||
}, | ||
{ | ||
"non-http scheme: ftp", | ||
"ftp://ftp.is.co.za/rfc/rfc1808.txt", | ||
map[string]any{ | ||
"original": "ftp://ftp.is.co.za/rfc/rfc1808.txt", | ||
"scheme": "ftp", | ||
"path": "/rfc/rfc1808.txt", | ||
"extension": "txt", | ||
"domain": "ftp.is.co.za", | ||
}, | ||
}, | ||
{ | ||
"non-http scheme: telnet", | ||
"telnet://192.0.2.16:80/", | ||
map[string]any{ | ||
"original": "telnet://192.0.2.16:80/", | ||
"scheme": "telnet", | ||
"path": "/", | ||
"port": 80, | ||
"domain": "192.0.2.16", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
source := &ottl.StandardStringGetter[any]{ | ||
Getter: func(_ context.Context, _ any) (any, error) { | ||
return tc.Original, nil | ||
}, | ||
} | ||
|
||
exprFunc, err := URI(source) | ||
require.NoError(t, err) | ||
|
||
res, err := exprFunc(context.Background(), nil) | ||
require.NoError(t, err) | ||
|
||
resMap, ok := res.(map[string]any) | ||
require.True(t, ok) | ||
|
||
require.Equal(t, len(tc.ExpectedMap), len(resMap)) | ||
for k, v := range tc.ExpectedMap { | ||
actualValue, found := resMap[k] | ||
require.True(t, found, "key not found %q", k) | ||
require.Equal(t, v, actualValue) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.