Skip to content

Commit

Permalink
[pkg/ottl] parse uri string to url.* SemConv attributes (open-telemet…
Browse files Browse the repository at this point in the history
…ry#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:** open-telemetry#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:https://www.example.com")`

results in 
```
  "original": "http:https://www.example.com",
  "scheme":   "http",
  "domain":   "www.example.com",
  "path":     "",
```

-
`Uri("http:https://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:https://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
3 people committed Jun 17, 2024
1 parent 8838019 commit 6f30561
Show file tree
Hide file tree
Showing 47 changed files with 1,159 additions and 563 deletions.
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]
1 change: 1 addition & 0 deletions connector/countconnector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ require (
github.com/prometheus/procfs v0.15.0 // indirect
go.opentelemetry.io/collector v0.102.2-0.20240611143128-7dfb57b9ad1c // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.102.2-0.20240611143128-7dfb57b9ad1c // indirect
go.opentelemetry.io/collector/semconv v0.102.2-0.20240611143128-7dfb57b9ad1c // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.49.0 // indirect
go.opentelemetry.io/otel/sdk v1.27.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions connector/countconnector/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions connector/routingconnector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ require (
github.com/prometheus/procfs v0.15.0 // indirect
go.opentelemetry.io/collector v0.102.2-0.20240611143128-7dfb57b9ad1c // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.102.2-0.20240611143128-7dfb57b9ad1c // indirect
go.opentelemetry.io/collector/semconv v0.102.2-0.20240611143128-7dfb57b9ad1c // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.49.0 // indirect
go.opentelemetry.io/otel/sdk v1.27.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions connector/routingconnector/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions exporter/honeycombmarkerexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
go.opentelemetry.io/collector/confmap v0.102.2-0.20240611143128-7dfb57b9ad1c
go.opentelemetry.io/collector/exporter v0.102.2-0.20240611143128-7dfb57b9ad1c
go.opentelemetry.io/collector/pdata v1.9.1-0.20240611143128-7dfb57b9ad1c
go.opentelemetry.io/collector/semconv v0.102.2-0.20240611143128-7dfb57b9ad1c // indirect
go.opentelemetry.io/otel/metric v1.27.0
go.opentelemetry.io/otel/trace v1.27.0
go.uber.org/goleak v1.3.0
Expand Down
2 changes: 2 additions & 0 deletions exporter/honeycombmarkerexporter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

164 changes: 164 additions & 0 deletions internal/coreinternal/parseutils/uri.go
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
}
Loading

0 comments on commit 6f30561

Please sign in to comment.