Skip to content

Commit

Permalink
wire up UserAgentFactory and add e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
pchila committed Jul 23, 2024
1 parent 0e6bf76 commit 186f7b3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,15 @@ func Test_e2e_converters(t *testing.T) {
m.PutStr("bar", "pass")
},
},
{
statement: `set(attributes["test"], UserAgent("curl/7.81.0"))`,
want: func(tCtx ottllog.TransformContext) {
m := tCtx.GetLogRecord().Attributes().PutEmptyMap("test")
m.PutStr("user_agent.original", "curl/7.81.0")
m.PutStr("user_agent.name", "curl")
m.PutStr("user_agent.version", "7.81.0")
},
},
}

for _, tt := range tests {
Expand Down
18 changes: 18 additions & 0 deletions pkg/ottl/ottlfuncs/func_useragent.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,30 @@
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
import (
"context"
"fmt"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
"github.com/ua-parser/uap-go/uaparser"
semconv "go.opentelemetry.io/collector/semconv/v1.25.0"
)

type UserAgentArguments[K any] struct {
UserAgent ottl.StringGetter[K]
}

func NewUserAgentFactory[K any]() ottl.Factory[K] {
return ottl.NewFactory("UserAgent", &UserAgentArguments[K]{}, createUserAgentFunction[K])
}

func createUserAgentFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
args, ok := oArgs.(*UserAgentArguments[K])
if !ok {
return nil, fmt.Errorf("URLFactory args must be of type *URLArguments[K]")
}

return userAgent[K](args.UserAgent), nil
}

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)
Expand Down
1 change: 1 addition & 0 deletions pkg/ottl/ottlfuncs/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func converters[K any]() []ottl.Factory[K] {
NewUnixSecondsFactory[K](),
NewUUIDFactory[K](),
NewURLFactory[K](),
NewUserAgentFactory[K](),
NewAppendFactory[K](),
NewYearFactory[K](),
NewHexFactory[K](),
Expand Down

0 comments on commit 186f7b3

Please sign in to comment.