-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg/ottl] parse uri string to url.* SemConv attributes (#32906)
**Description:** This ottl converter converts uri string into SemConv attributes. Instead of using regex it uses `uri.Parse` providing nicer shorthand. **Link to tracking Issue:** #32433 **Testing:** unit test added **Documentation:** readme updated with: ### 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 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": "", ``` - `Uri("http://myusername:mypassword@www.example.com:80/foo.gif?key1=val1&key2=val2#fragment")` results in ``` "path": "/foo.gif", "fragment": "fragment", "extension": "gif", "password": "mypassword", "original": "http://myusername:mypassword@www.example.com: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", ``` --------- Co-authored-by: Tiffany Hrabusa <[email protected]> Co-authored-by: Evan Bradley <[email protected]>
- Loading branch information
1 parent
8838019
commit 6f30561
Showing
47 changed files
with
1,159 additions
and
563 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
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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,164 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package parseutils // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/parseutils" | ||
|
||
import ( | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
|
||
semconv "go.opentelemetry.io/collector/semconv/v1.25.0" | ||
) | ||
|
||
const ( | ||
// replace once conventions includes these | ||
AttributeURLUserInfo = "url.user_info" | ||
AttributeURLUsername = "url.username" | ||
AttributeURLPassword = "url.password" | ||
) | ||
|
||
// parseURI takes an absolute or relative uri and returns the parsed values. | ||
func ParseURI(value string, semconvCompliant bool) (map[string]any, error) { | ||
m := make(map[string]any) | ||
|
||
if strings.HasPrefix(value, "?") { | ||
// remove the query string '?' prefix before parsing | ||
v, err := url.ParseQuery(value[1:]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return queryToMap(v, m), nil | ||
} | ||
|
||
var x *url.URL | ||
var err error | ||
var mappingFn func(*url.URL, map[string]any) (map[string]any, error) | ||
|
||
if semconvCompliant { | ||
mappingFn = urlToSemconvMap | ||
x, err = url.Parse(value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
x, err = url.ParseRequestURI(value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
mappingFn = urlToMap | ||
} | ||
return mappingFn(x, m) | ||
} | ||
|
||
// urlToMap converts a url.URL to a map, excludes any values that are not set. | ||
func urlToSemconvMap(parsedURI *url.URL, m map[string]any) (map[string]any, error) { | ||
m[semconv.AttributeURLOriginal] = parsedURI.String() | ||
m[semconv.AttributeURLDomain] = parsedURI.Hostname() | ||
m[semconv.AttributeURLScheme] = parsedURI.Scheme | ||
m[semconv.AttributeURLPath] = parsedURI.Path | ||
|
||
if portString := parsedURI.Port(); len(portString) > 0 { | ||
port, err := strconv.Atoi(portString) | ||
if err != nil { | ||
return nil, err | ||
} | ||
m[semconv.AttributeURLPort] = port | ||
} | ||
|
||
if fragment := parsedURI.Fragment; len(fragment) > 0 { | ||
m[semconv.AttributeURLFragment] = fragment | ||
} | ||
|
||
if parsedURI.User != nil { | ||
m[AttributeURLUserInfo] = parsedURI.User.String() | ||
|
||
if username := parsedURI.User.Username(); len(username) > 0 { | ||
m[AttributeURLUsername] = username | ||
} | ||
|
||
if pwd, isSet := parsedURI.User.Password(); isSet { | ||
m[AttributeURLPassword] = pwd | ||
} | ||
} | ||
|
||
if query := parsedURI.RawQuery; len(query) > 0 { | ||
m[semconv.AttributeURLQuery] = query | ||
} | ||
|
||
if periodIdx := strings.LastIndex(parsedURI.Path, "."); periodIdx != -1 { | ||
if periodIdx < len(parsedURI.Path)-1 { | ||
m[semconv.AttributeURLExtension] = parsedURI.Path[periodIdx+1:] | ||
} | ||
} | ||
|
||
return m, nil | ||
} | ||
|
||
// urlToMap converts a url.URL to a map, excludes any values that are not set. | ||
func urlToMap(p *url.URL, m map[string]any) (map[string]any, error) { | ||
scheme := p.Scheme | ||
if scheme != "" { | ||
m["scheme"] = scheme | ||
} | ||
|
||
user := p.User.Username() | ||
if user != "" { | ||
m["user"] = user | ||
} | ||
|
||
host := p.Hostname() | ||
if host != "" { | ||
m["host"] = host | ||
} | ||
|
||
port := p.Port() | ||
if port != "" { | ||
m["port"] = port | ||
} | ||
|
||
path := p.EscapedPath() | ||
if path != "" { | ||
m["path"] = path | ||
} | ||
|
||
return queryToMap(p.Query(), m), nil | ||
} | ||
|
||
// queryToMap converts a query string url.Values to a map. | ||
func queryToMap(query url.Values, m map[string]any) map[string]any { | ||
// no-op if query is empty, do not create the key m["query"] | ||
if len(query) == 0 { | ||
return m | ||
} | ||
|
||
/* 'parameter' will represent url.Values | ||
map[string]any{ | ||
"parameter-a": []any{ | ||
"a", | ||
"b", | ||
}, | ||
"parameter-b": []any{ | ||
"x", | ||
"y", | ||
}, | ||
} | ||
*/ | ||
parameters := map[string]any{} | ||
for param, values := range query { | ||
parameters[param] = queryParamValuesToMap(values) | ||
} | ||
m["query"] = parameters | ||
return m | ||
} | ||
|
||
// queryParamValuesToMap takes query string parameter values and | ||
// returns an []interface populated with the values | ||
func queryParamValuesToMap(values []string) []any { | ||
v := make([]any, len(values)) | ||
for i, value := range values { | ||
v[i] = value | ||
} | ||
return v | ||
} |
Oops, something went wrong.