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 53 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]
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