DataPrime 表达式语言 (DPXL) 引用

DataPrime 表达式语言,或称 DPXL,是一种基于 DataPrime 表达式的表达式语言 syntax.You,可用于定义丰富的基于表达式的过滤器,例如在设置流媒体时。

DPXL 表达式是 DataPrime 表达式的子集,例如过滤器操作符中使用的表达式。

DPXL 表达式经过版本控制,以保持可预测性和稳定性。 有了版本控制功能,DPXL 可以在不改变现有表达式语义的情况下不断改进。

每个 DPXL 表达式都以版本标识符 <vX> 开始,目前为 <v1>。 随后是实际的布尔表达式,包括字面、逻辑和比较结构、键路径访问和函数。

<v1> <boolean-expression>

使用用户界面时,<v> 前缀会自动包含,无需指定。 但是,在 API 中使用的 DPXL 表达式必须以 <v1> 前缀开头。

# A filter that returns true if my_text field has the value 'example'
<v1> $d.my_text == 'example'

# A filter that returns true if the event's timestamp is before the beginning of the year 2024
<v1> $m.timestamp < @'2024-01-01T00:00:00'

# A filter that returns true if the application name starts with 'dev-'
<v1> $l.applicationname.startsWith('dev-')

# A filter that returns true if the field region_id is us-east-1 or us-east-2
<v1> region_id:string.in('us-east-1', 'us-west-2')

数据类型

DPXL 支持不同的数据类型。

数据类型
数据类型 示例
string 'us-east-1’
’dev-’
数字 23
-12.32
布尔值 true
false
时间戳记 @(’2023-01-01T00:00Z’)
@’now’
正则表达式 /H.*o$/
/^prod-.*/
严重性 VERBOSE
DEBUG
INFO
WARNING
ERROR
CRITICAL

此外,还有一个 null 字面,可用于所有其他类型。

运算符

DPXL 支持多种操作符。

运算符
运算符 含义 示例 示例说明
&& 逻辑 AND country == ‘us’ && region == ‘us-south’ 如果 countryus,且 regionus-south,则返回 true。
|| 逻辑 OR age > 40 || country == ‘us’ 如果 age 超过 40,或者 countryus,则返回 true。
! 逻辑 NOT !region.contains(’us-’) 如果 region 不包含 us- 将返回 true

订购

您可以使用括号控制表达式内部的求值顺序。 例如:

region.startsWith('us-') &&
(country == 'us' && (age > 40 || age < 10)) || (country == 'il' && age > 25)

比较运算符

DPXL 支持多种比较运算符。

比较运算符
运算符 含义 示例
> 大于 duration > 40.5
$m.timestamp > @(’2023-01-01T00:00:00’)
>= 大于或等于 duration >= 40.5
< 小于 age < 20
<= 小于或等于 age <= 20
lastName <= ‘Smith’
== 等于 region == 'us-south'
!= 不等于 first_name != 'joe'

关键路径

关键路径分为三个不同的部分,每个部分都有单独的前缀:

$m
元数据
$l
标签,如 applicationnamesubsystemName
$d
用户数据(默认前缀)

$m- 元数据关键路径

元数据关键路径
关键路径 数据类型 描述
$m.timestamp 时间戳记 包含事件的时间戳
$m.severity 严重性 包含事件的严重程度

$l- 标签关键路径

标记日志的关键路径
关键路径 数据类型
$l.applicationname string
$l.subsystemname string

$d- 用户数据关键路径

可以使用 $d.<keypath> 访问任何用户键路径,包括嵌套键路径。

$d 是默认前缀。 任何不包含前缀的键路径都将被视为用户数据字段。

函数

函数为 DPXL 表达式提供了额外的功能。

函数
功能 描述 示例
<s>.startsWith(<substr>):bool 检查字符串 <s> 是否以指定子串 <substr> 开始 region.startsWith('us-')
<s>.endsWith(<substr>):bool 检查字符串 <s> 是否以指定子串 <substr> 结束 firstName.endsWith(’Jo’)
<s>.contains(<substr>):bool 检查字符串 <s> 是否包含指定子串 <substr> stream.contains(’err’)
<s>.matches(<regex>):bool 检查字符串 <s> 是否与 <regex> 提供的指定模式相匹配 hostname.matches(/prod-.*/)
<value>.in(<value1>,<value2>,...) 检查值是否为提供的值之一 value1-valueN region.in(’us-east’,’us-south’)

推断数据类型

DPXL 尝试推断键路径的预期数据类型。 例如,在处理 age > 50 时,它会推断出 age 应该是一个数字。 如果 DPXL 无法推断键路径的数据类型,则需要有关该类型的必要信息。 例如:

'123':number

region1:string == region2

my_key:number > my_other_key

示例

以下是 DPXL 示例,您可以以此为基础创建自己的 DPXL 表达式。

# Allow access only to logs where the application name is "production"
<v1> $l.applicationname == 'production'
# Allow access only to logs in which app name starts with dev, or the field "region_id" in the data is us-east
<v1> $l.applicationname.startsWith('dev-') && region_id == 'us-east'
# Allow access only to logs in which the field "country" is not one of the listed below.
<v1> !$d.country:string.in('us','il','gr')
# Allow access only to logs where the pod name matches the regex provided
<v1> kubernetes.pod_name.matches(/^kafka-[0-9]+/)
# Allow access only to logs that don't have a DEBUG severity
<v1> $m.severity != DEBUG
# Allow access only to logs in which some query duration is very large
<v1> query_duration_seconds > 100
# Allow access only to logs up to the beginning of the year 2024
<v1> $m.timestamp < @'2024-01-01T00:00:00'
# Disallow access to all logs entirely
<v1> false

推论的局限性

in 函数无法自动推断键路径的预期类型。 要使用 in 功能,您需要指明类型。 例如:

<v1> !$d.country:string.in('us','il','gr')