-
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.
Initial naive impl for user-agent parsing
- Loading branch information
Showing
2 changed files
with
74 additions
and
0 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,20 @@ | ||
// 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" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
func userAgent[K any](userAgentSource ottl.StringGetter[K]) ottl.ExprFunc[K] { //revive:disable-line:var-naming | ||
return func(ctx context.Context, tCtx K) (any, error) { | ||
userAgentString, err := userAgentSource.Get(ctx, tCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return map[string]any{}, nil | ||
} | ||
} |
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,54 @@ | ||
// 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" | ||
"testing" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
semconv "go.opentelemetry.io/collector/semconv/v1.25.0" | ||
) | ||
|
||
func TestUserAgentParser(t *testing.T) { | ||
testCases := []struct { | ||
Name string | ||
Original string | ||
ExpectedMap map[string]any | ||
}{ | ||
{ | ||
Name: "Firefox on Linux", | ||
Original: "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: "", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range testCases { | ||
t.Run(tt.Name, func(t *testing.T) { | ||
source := &ottl.StandardStringGetter[any]{ | ||
Getter: func(_ context.Context, _ any) (any, error) { | ||
return tt.Original, nil | ||
}, | ||
} | ||
|
||
exprFunc := userAgent[any](source) //revive:disable-line:var-naming | ||
res, err := exprFunc(context.Background(), nil) | ||
require.NoError(t, err) | ||
require.IsType(t, map[string]any{}, res) | ||
resMap := res.(map[string]any) | ||
assert.Equal(t, tt.ExpectedMap, resMap) | ||
assert.Len(t, resMap, len(tt.ExpectedMap)) | ||
for k, v := range tt.ExpectedMap { | ||
if assert.Containsf(t, resMap, k, "key not found %q", k) { | ||
assert.Equal(t, v, resMap[k]) | ||
} | ||
} | ||
}) | ||
} | ||
} |