SPL Rule Syntax Comparison for Upgrades

Updated at:

Maps old DSL syntax to new SPL syntax for each data transformation scenario.

The Scenario comparison between SPL and SQL covers how SPL and SQL differ in data processing.

Data Synchronization (No Processing Logic Required)

Version

Script description

Old version

Leave the DSL script empty.

New version

Leave the SPL rule empty.

Data Filtering: Exact Text Match

Version

Script description

Old version

e_keep(v("level") == "ERROR") or

e_drop(v("level") != "ERROR") or

e_if(v("level") != "ERROR", e_drop()) or

e_keep(e_search("level==ERROR"))

New version

| where level='ERROR'

Data Filtering: Numeric Comparison

Version

Script description

Old version

e_keep(ct_int(v("status"))>=400)

New version

| where cast(status as bigint)>=400

Data Filtering: Fuzzy Match

Version

Script description

Old version

e_keep(op_in(v("level"), "ERROR")) or

e_keep(e_search("level: ERROR") or

e_if(op_not_in(v("level"), "ERROR"), e_drop())

New version

| where level like '%ERROR%'

Add a Field by Extraction or Construction

Version

Script description

Old version

  1. Extract a value using a regular expression.

    e_set("version", regex_select(v("data"), r'"version":\d+'))

  2. Extract a value using JSON. The old version uses JMESPath syntax for JSON queries.

    e_set("version", json_select(v("data"), "version"))

New version

  1. Extract a value using a regular expression.

    | extend version=regexp_extract(data, '"version":\d+')

  2. Extract a value using JSON. The new version uses JsonPath for JSON path references.

    | extend version=json_extract(data, '$.version')

Parse and Format Time Information

Version

Script description

Old version

  1. Extract the log time field __time__.

    e_set(
        "__time__", 
        dt_parsetimestamp(
            v("time"), 
            fmt="%Y/%m/%d %H-%M-%S",
        ),
    )
  2. Standardize the time format.

    e_set(
        "time",
        dt_strftime(
            dt_parse(
                v("time"), 
                fmt="%Y/%m/%d %H-%M-%S",
            ), 
            fmt="%Y-%m-%d %H:%M:%S",
        ),
    )

New version

  1. Extract the log time field __time__.

    | extend time=date_parse(time, '%Y/%m/%d %H-%i-%S')

    | extend __time__=cast(to_unixtime(time) as bigint)

  2. Standardize the time format.

    | extend time=date_parse(time, '%Y/%m/%d %H-%i-%S')

    | extend time=date_format(time, '%Y-%m-%d %H:%i:%S')

Field Processing and Filtering

Version

Script description

Old version

  1. Select fields precisely.

    e_keep_fields("__tag__:node", "path", regex=False)

  2. Select fields by pattern.

    e_keep_fields("__tag__:.*", regex=True)

  3. Rename fields.

    e_rename("__tag__:node", node)

  4. Exclude fields by pattern.

    e_drop_fields("__tag__:.*", regex=True)

New version

  1. Select fields precisely.

    | project node="__tag__:node", path

  2. Select fields by pattern.

    | project -wildcard "__tag__:*"

  3. Rename fields.

    | project-rename node="__tag__:node"

  4. Exclude fields by pattern.

    | project-away -wildcard "__tag__:*"

Extract Multiple Fields with Regex

Version

Script description

Old version

e_regex("data", r"(\S+)\s+(\w+)", ["time", "level"])

New version

| parse-regexp data, '(\S+)\s+(\w+)' as time, level

Expand JSON Key-Value Pairs into Fields

Version

Script description

Old version

The old version uses JMESPath syntax for JSON queries.

e_json("data", depth=1, jmes="x.y.z")

New version

The new version uses JsonPath for JSON path references.

| parse-json -path='$.x.y.z' data

Extract CSV Content into Fields

Version

Script description

Old version

e_csv("data", ["time", "addr", "user"], sep="\0", quote='"')

New version

  1. Single-character separators follow CSV RFC 4180.

    | parse-csv -delim='\0' -quote='"' data as time, addr, user

  2. Multi-character delimiters.

    | parse-csv -delim='^_^' data AS time, addr, user

Logical Branch Processing: Parallel Branches

Version

Script description

Old version

e_if(
    e_has("a"), e_set("mode_a", "1"), 
    e_has("b"), e_set("mode_b", "1"),
)

Equivalent Python structure:

if e_has("a"):
    e_set("mode_a", "1")
if e_has("b"):
    e_set("mode_b", "1")

New version

.let a = *
| where a is not null
| extend mode_a='1';

.let b = *
| where b is not null
| extend mode_b='1';

$a;
$b;

Logical Branch Processing: Mutually Exclusive Branches (if-else/switch)

Version

Script description

Old version

e_switch(
    e_has("a"), e_keep_fields("x", "y", "z"), 
    e_has("b"), e_keep_fields("u", "v"),
    default=e_keep_fields("w"),
)

Equivalent Python structure:

if e_has("a"):
    e_keep_fields("x", "y", "z")
elif e_has("b"):
    e_keep_fields("u", "v")
else:
    e_keep_fields("w")

New version

.let src = *
| extend mode=case
    when a is not null then 1
    when b is not null then 2
    else 0
  end;

.let a = $src | where mode=1 | project x, y, z;
.let b = $src | where mode=2 | project u, v;
.let c = $src | where mode=0 | project w;

$a;
$b;
$c;

Route Data to a Destination Project or Logstore

Version

Script description

Old version

e_output(project=v("dst_project"), logstore=v("dst_logstore"))

New version

| extend "__tag__:__sls_etl_output_project__"=dst_project

| extend "__tag__:__sls_etl_output_logstore__"=dst_logstore

Route Results to a Destination Shard by HashKey

Version

Script description

Old version

e_output(hash_key_field="key_field")

New version

| extend "__tag__:__sls_etl_output_hash_key__"=to_hex(md5(to_utf8(key_field))

Serialize Fields to JSON

Version

Script description

Old version

  1. Package all fields.

e_pack_fields("content", include="\w+")

  1. Extract and package values from the dict field into the name field using a regex.

e_regex("dict", r"(\w+):(\d+)", {r"k_\1": r"\2"}, pack_json="name")

New version

  1. Package all fields.

| pack-fields -include='\w+' as content

  1. Extract and package values from the dict field into the name field using a regex.

| parse-kv -prefix='k_' -regexp dict, '(\w+):(\d+)' | pack-fields -include='k_.*' as name

Convert Logs to Time Series Format

Version

Script description

Old version

e_to_metric(names="rt", labels="host")

New version

| log-to-metric -names='["rt"]' -labels='["host"]'