Skip to content

User-Defined Functions

Inobi supports two types of user-defined functions:

Type Defined in Requires recompile
Lite functions YAML files No
Go UDFs Go source Yes

Lite functions are expression-only shortcuts defined in YAML files. They are inlined at compile time. Use them to define reusable expression logic.

Go UDFs are arbitrary Go methods compiled into the binary. Use them when you need external state, I/O, data structures, or logic that can't be expressed in expr.


Lite Functions

Lite functions are defined in YAML files within a directory and referenced in the config. They are parsed once at startup and inlined into rule expressions. Each file can contain multiple function definitions, and files can be organized into subdirectories.

Defining Lite Functions

Create YAML files in a directory where each top-level key is a function name:

functions/auth.yaml
isServiceAccount:
  args: [username]
  environments: [match, extended_match]
  expr: >
    iHasPrefix(username, "svc-")
      or iHasPrefix(username, "app-")

Fields

Field Required Description
args List of parameter names. Must have at least one
expr The expr expression that forms the function body.
environments Expression fields where this function is available. Default: [extended_match]

Valid environments values: match, extended_match, aggregation, projections, normalize.

A lite function can use any built-in function, helper, or variable (event, events, value, etc.) available in the target environment, including Go UDFs. It cannot call other lite functions.

Note

Function names must not conflict with built-in expr functions, Inobi helper functions, or other symbol names. Inobi will refuse to start if a conflict is detected.

Loading Lite Functions

Point the config at the functions directory:

config.yaml
functions:
  directory: functions/

inobi test loads the same functions directory from the config.

Function names must be unique across all files. Inobi will refuse to start if a duplicate is found.

Using Lite Functions in Rules

Call them by name like any other function:

match: >
    event.action == "login-success"
      and isServiceAccount(event.user.name)
      and isPublicIP(event.source.ip)

The function call is replaced with its body at rule compile time. The above compiles to the equivalent of:

match: >
    event.action == "login-success"
      and (iHasPrefix(event.user.name, "svc-")
           or iHasPrefix(event.user.name, "app-"))
      and isPublicIP(event.source.ip)

Go UDFs

Go UDFs let you extend rule expressions with arbitrary Go code, accessed via the udf. prefix. Defined UDFs are available in extended_match, aggregation, and projections expressions.

Defining a Go UDF

Add exported methods to the UDF struct in udf/udf.go and recompile. The following example loads a CSV of usernames and titles into a map, then exposes a LookupUser method that rules can call:

udf/udf.go
type UDF struct {
    mu    sync.RWMutex
    users map[string]string // username -> title
}

func (udf *UDF) LookupUser(username string) string {
    udf.mu.RLock()
    defer udf.mu.RUnlock()
    return udf.users[username]
}

The Init method is called at startup and can be used to initialize shared state (e.g., open a database connection, load a lookup table). Returning an error from Init prevents the engine from starting.

Avoid doing long-running work directly in Init as it will block the engine from starting. If you need a background task (e.g. periodically refreshing a lookup table), spawn a goroutine using wg.Go:

udf/udf.go
func (udf *UDF) Init(ctx context.Context, wg *sync.WaitGroup) error {
    if err := udf.loadCSV(); err != nil {
        return fmt.Errorf("failed to load users CSV: %w", err)
    }
    wg.Go(func() {
        ticker := time.NewTicker(60 * time.Second)
        defer ticker.Stop()
        for {
            select {
            case <-ctx.Done():
                return
            case <-ticker.C:
                udf.loadCSV()
            }
        }
    })
    return nil
}

The engine will not shut down until all goroutines spawned with wg.Go have returned. When the engine wants to shut down, it cancels ctx. Your goroutine should watch for ctx.Done() as the signal to release any resources and return.

Note

Go UDFs require a recompile to take effect. Return values must be JSON-serializable to appear in alerts.

Using Go UDFs in Rules

Call Go UDFs by name with the udf. prefix:

extended_match:
  - udf.LookupUser(event.user.name) | project("_user_title"); true