Skip to content

Commit

Permalink
Initial naive impl for user-agent parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
pchila committed Jul 23, 2024
1 parent e32faef commit 2031ca3
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/ottl/ottlfuncs/func_useragent.go
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
}
}
54 changes: 54 additions & 0 deletions pkg/ottl/ottlfuncs/func_useragent_test.go
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])
}
}
})
}
}

0 comments on commit 2031ca3

Please sign in to comment.