Skip to content

Commit

Permalink
(pkg/ottl) Add extracted OS info from UserAgent
Browse files Browse the repository at this point in the history
Signed-off-by: Ioannis Androulidakis <androulidakis.ioannis@gmail.com>
  • Loading branch information
ioandr committed Oct 20, 2024
1 parent 33a5457 commit dcdbd5b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/ottl/ottlfuncs/func_useragent.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ func userAgent[K any](userAgentSource ottl.StringGetter[K]) ottl.ExprFunc[K] { /
return nil, err
}
parsedUserAgent := parser.ParseUserAgent(userAgentString)
parsedOS := parser.ParseOs(userAgentString)
return map[string]any{
semconv.AttributeUserAgentName: parsedUserAgent.Family,
semconv.AttributeUserAgentOriginal: userAgentString,
semconv.AttributeUserAgentVersion: parsedUserAgent.ToVersionString(),
semconv.AttributeOSName: parsedOS.Family,
semconv.AttributeOSVersion: parsedOS.ToVersionString(),
}, nil
}
}
49 changes: 49 additions & 0 deletions pkg/ottl/ottlfuncs/func_useragent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,26 @@ func TestUserAgentParser(t *testing.T) {
UAString string
ExpectedMap map[string]any
}{
{
Name: "Firefox-Android",
UAString: "Mozilla/5.0 (Linux; Android 4.1.1; SPH-L710 Build/JRO03L) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19",
ExpectedMap: map[string]any{
semconv.AttributeUserAgentOriginal: "Mozilla/5.0 (Linux; Android 4.1.1; SPH-L710 Build/JRO03L) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19",
semconv.AttributeUserAgentName: "Chrome Mobile",
semconv.AttributeUserAgentVersion: "18.0.1025",
semconv.AttributeOSName: "Android",
semconv.AttributeOSVersion: "4.1.1",
},
},
{
Name: "Firefox",
UAString: "Mozilla/5.0 (X11; Linux x86_64; rv:126.0) Gecko/20100101 Firefox/126.0",
ExpectedMap: map[string]any{
semconv.AttributeUserAgentOriginal: "Mozilla/5.0 (X11; Linux x86_64; rv:126.0) Gecko/20100101 Firefox/126.0",
semconv.AttributeUserAgentName: "Firefox",
semconv.AttributeUserAgentVersion: "126.0",
semconv.AttributeOSName: "Linux",
semconv.AttributeOSVersion: "",
},
},
{
Expand All @@ -35,6 +48,8 @@ func TestUserAgentParser(t *testing.T) {
semconv.AttributeUserAgentOriginal: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36",
semconv.AttributeUserAgentName: "Chrome",
semconv.AttributeUserAgentVersion: "51.0.2704",
semconv.AttributeOSName: "Linux",
semconv.AttributeOSVersion: "",
},
},
{
Expand All @@ -44,6 +59,8 @@ func TestUserAgentParser(t *testing.T) {
semconv.AttributeUserAgentOriginal: "Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1",
semconv.AttributeUserAgentName: "Mobile Safari",
semconv.AttributeUserAgentVersion: "13.1.1",
semconv.AttributeOSName: "iOS",
semconv.AttributeOSVersion: "13.5.1",
},
},
{
Expand All @@ -53,6 +70,8 @@ func TestUserAgentParser(t *testing.T) {
semconv.AttributeUserAgentOriginal: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.59",
semconv.AttributeUserAgentName: "Edge",
semconv.AttributeUserAgentVersion: "91.0.864",
semconv.AttributeOSName: "Windows",
semconv.AttributeOSVersion: "10",
},
},
{
Expand All @@ -62,6 +81,8 @@ func TestUserAgentParser(t *testing.T) {
semconv.AttributeUserAgentOriginal: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 OPR/38.0.2220.41",
semconv.AttributeUserAgentName: "Opera",
semconv.AttributeUserAgentVersion: "38.0.2220",
semconv.AttributeOSName: "Linux",
semconv.AttributeOSVersion: "",
},
},
{
Expand All @@ -71,6 +92,8 @@ func TestUserAgentParser(t *testing.T) {
semconv.AttributeUserAgentOriginal: "curl/7.81.0",
semconv.AttributeUserAgentName: "curl",
semconv.AttributeUserAgentVersion: "7.81.0",
semconv.AttributeOSName: "Other",
semconv.AttributeOSVersion: "",
},
},
{
Expand All @@ -80,6 +103,8 @@ func TestUserAgentParser(t *testing.T) {
semconv.AttributeUserAgentOriginal: "foobar/1.2.3 (foo; bar baz)",
semconv.AttributeUserAgentName: "Other",
semconv.AttributeUserAgentVersion: "",
semconv.AttributeOSName: "Other",
semconv.AttributeOSVersion: "",
},
},
{
Expand All @@ -89,6 +114,30 @@ func TestUserAgentParser(t *testing.T) {
semconv.AttributeUserAgentOriginal: "OpenTelemetry Collector Contrib/0.106.1 (linux/amd64)",
semconv.AttributeUserAgentName: "Other",
semconv.AttributeUserAgentVersion: "",
semconv.AttributeOSName: "Linux",
semconv.AttributeOSVersion: "",
},
},
{
Name: "ViaFree iOS",
UAString: "ViaFree-DK/3.8.3 (com.MTGx.ViaFree.dk; build:7383; iOS 12.1.0) Alamofire/4.7.0",
ExpectedMap: map[string]any{
semconv.AttributeUserAgentOriginal: "ViaFree-DK/3.8.3 (com.MTGx.ViaFree.dk; build:7383; iOS 12.1.0) Alamofire/4.7.0",
semconv.AttributeUserAgentName: "ViaFree",
semconv.AttributeUserAgentVersion: "3.8.3",
semconv.AttributeOSName: "iOS",
semconv.AttributeOSVersion: "12.1.0",
},
},
{
Name: "Java SDK Linux",
UAString: "ibm-cos-sdk-java/2.3.0 Linux/4.9.0-8-amd64 Java_HotSpot(TM)_64-Bit_Server_VM/9.0.4+11/9.0.4'",
ExpectedMap: map[string]any{
semconv.AttributeUserAgentOriginal: "ibm-cos-sdk-java/2.3.0 Linux/4.9.0-8-amd64 Java_HotSpot(TM)_64-Bit_Server_VM/9.0.4+11/9.0.4'",
semconv.AttributeUserAgentName: "ibm-cos-sdk-java",
semconv.AttributeUserAgentVersion: "2.3.0",
semconv.AttributeOSName: "Linux",
semconv.AttributeOSVersion: "4.9.0",
},
},
}
Expand Down

0 comments on commit dcdbd5b

Please sign in to comment.