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

[pkg/ottl] parse uri string to url.* SemConv attributes #32906

Merged
merged 61 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
b721c09
initial uri bootstrap
michalpristas May 7, 2024
4fa5b2d
Uri parser for ottl
michalpristas May 7, 2024
44fbc78
changelog
michalpristas May 7, 2024
2a75ec2
Update pkg/ottl/ottlfuncs/README.md
michalpristas May 9, 2024
46f67a4
Merge branch 'main' into ottl/uri-parser
michalpristas May 9, 2024
8ad8d9e
lint
michalpristas May 9, 2024
d1d942a
Merge branch 'main' of github.com:open-telemetry/opentelemetry-collec…
michalpristas May 9, 2024
fa3e18c
Merge branch 'ottl/uri-parser' of github.com:michalpristas/openteleme…
michalpristas May 9, 2024
29d1e8b
Update pkg/ottl/ottlfuncs/func_uri.go
michalpristas May 10, 2024
bfbe183
Make uri uppercase"
michalpristas May 10, 2024
e71e65b
Merge branch 'main' of github.com:open-telemetry/opentelemetry-collec…
michalpristas May 10, 2024
5bdda14
URI in tests
michalpristas May 10, 2024
bcd8b49
fixed import order for uri tests
michalpristas May 10, 2024
e4d8e58
Merge branch 'main' into ottl/uri-parser
michalpristas May 13, 2024
a1bde01
Merge branch 'main' into ottl/uri-parser
michalpristas May 14, 2024
535d6bd
disable uri to URI lint for func_uri
michalpristas May 15, 2024
1f88ccb
Merge branch 'ottl/uri-parser' of github.com:michalpristas/openteleme…
michalpristas May 15, 2024
e182d16
Merge branch 'main' of github.com:open-telemetry/opentelemetry-collec…
michalpristas May 15, 2024
7bd764d
fix error is always nil check
michalpristas May 15, 2024
b266800
resolved conflicts with main
michalpristas May 26, 2024
6bc8e8f
saved functions.go file
michalpristas May 26, 2024
f9adfbe
Merge branch 'main' into ottl/uri-parser
michalpristas May 30, 2024
3809d16
Merge branch 'main' into ottl/uri-parser
michalpristas May 31, 2024
ae73009
Merge branch 'main' into ottl/uri-parser
michalpristas May 31, 2024
a128f7c
Merge branch 'main' into ottl/uri-parser
michalpristas Jun 3, 2024
f041030
Merge branch 'main' into ottl/uri-parser
michalpristas Jun 6, 2024
8a843d7
Merge branch 'main' into ottl/uri-parser
michalpristas Jun 7, 2024
63dc323
Merge branch 'main' into ottl/uri-parser
michalpristas Jun 10, 2024
5c9e5c9
resolved conflicts in functions.go
michalpristas Jun 11, 2024
8f4b7ae
resolved conflicts in functions.go
michalpristas Jun 11, 2024
7884983
Merge branch 'main' of github.com:open-telemetry/opentelemetry-collec…
michalpristas Jun 11, 2024
395fe2c
replace hardcoded strings with ottl convention
michalpristas Jun 11, 2024
4369458
Merge branch 'main' into ottl/uri-parser
michalpristas Jun 12, 2024
ec00160
govulncheck issue addressed
michalpristas Jun 12, 2024
a544e05
Merge branch 'main' of github.com:open-telemetry/opentelemetry-collec…
michalpristas Jun 12, 2024
1db9ffc
Merge branch 'ottl/uri-parser' of github.com:michalpristas/openteleme…
michalpristas Jun 12, 2024
d4e0b33
resovled conflicts with main
michalpristas Jun 13, 2024
52b4c42
fixed go.mod for countconnector
michalpristas Jun 13, 2024
5eb0620
indirect semconv
michalpristas Jun 13, 2024
d204d4a
lint
michalpristas Jun 13, 2024
db455ea
move logic to stanza uri parser
michalpristas Jun 13, 2024
ea2ef4f
more linting
michalpristas Jun 13, 2024
800f741
more linting
michalpristas Jun 13, 2024
747218b
fix typo
michalpristas Jun 13, 2024
67746dd
Merge branch 'main' of github.com:open-telemetry/opentelemetry-collec…
michalpristas Jun 13, 2024
29db7eb
check fixed
michalpristas Jun 13, 2024
95b8704
crosslink
michalpristas Jun 13, 2024
95e56e0
move parse uri to separate package, remove stanza dep from ottl
michalpristas Jun 14, 2024
308c533
ottl crosslink
michalpristas Jun 14, 2024
2cde9d7
stanza indirect dependencies removed
michalpristas Jun 14, 2024
6c9ca14
Merge branch 'main' of github.com:open-telemetry/opentelemetry-collec…
michalpristas Jun 14, 2024
41b5ea4
import added to parseutils/uri.go
michalpristas Jun 14, 2024
7155d02
stanza go mod
michalpristas Jun 14, 2024
69b6745
Update pkg/stanza/operator/parser/uri/parser.go
michalpristas Jun 17, 2024
80773df
Update pkg/ottl/ottlfuncs/func_uri.go
michalpristas Jun 17, 2024
da2bc40
Update pkg/ottl/ottlfuncs/README.md
michalpristas Jun 17, 2024
d6a7114
Update pkg/ottl/ottlfuncs/README.md
michalpristas Jun 17, 2024
3ae53fb
Merge branch 'main' into ottl/uri-parser
michalpristas Jun 17, 2024
6236224
proper naming, URI vs URL
michalpristas Jun 17, 2024
1e2c2da
updated docs
michalpristas Jun 17, 2024
e925180
missing files after rename
michalpristas Jun 17, 2024
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
27 changes: 27 additions & 0 deletions .chloggen/ottl_uri-parser.yaml
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]
38 changes: 38 additions & 0 deletions pkg/ottl/ottlfuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,44 @@ Examples:

- `UnixSeconds(Time("02/04/2023", "%m/%d/%Y"))`

### URI
djaglowski marked this conversation as resolved.
Show resolved Hide resolved

`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()`
Expand Down
93 changes: 93 additions & 0 deletions pkg/ottl/ottlfuncs/func_uri.go
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
}
172 changes: 172 additions & 0 deletions pkg/ottl/ottlfuncs/func_uri_test.go
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)
}
})
}
}
1 change: 1 addition & 0 deletions pkg/ottl/ottlfuncs/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@ func converters[K any]() []ottl.Factory[K] {
NewUnixNanoFactory[K](),
NewUnixSecondsFactory[K](),
NewUUIDFactory[K](),
NewURIFactory[K](),
}
}