DataPrime表達式語言 (DPXL) 參考
DataPrime Expression Language(或稱 DPXL)是一種基於 DataPrime 表達式的表達式語言 syntax.You,可以使用它定義豐富的基於表達式的篩選器,例如在設定串流時。
DPXL 運算式是DataPrime運算式的子集,例如篩選器運算子中使用的運算式。
DPXL 表達式經過版本控制以保持可預測性和穩定性。 透過版本控制,DPXL 可以隨著時間的推移而增強,而無需更改現有表達式的語義。
每個 DPXL 表達式都以版本標識符 <vX> 開頭,目前為 <v1>。 接下來是實際的布林表達式,包括文字、邏輯和比較結構、鍵路徑存取和函數。
<v1> <boolean-expression>
使用 UI 時會自動包含 <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 支援不同的資料類型。
| 資料類型 | 範例 |
|---|---|
| 字串 | 'us-east-1’’dev-’ |
| 數字 | 23-12.32 |
| bool | truefalse |
| 時間戳記 | @(’2023-01-01T00:00Z’)@’now’ |
| 正規表示式 | /H.*o$//^prod-.*/ |
| 嚴重性 | VERBOSEDEBUGINFOWARNINGERRORCRITICAL |
此外,還有一個 null 文字,可以與所有其他類型一起使用。
運算子
DPXL 支援多個營運商。
| 操作員 | 意義 | 範例 | 範例說明 |
|---|---|---|---|
&& |
邏輯 AND | country == ‘us’ && region == ‘us-south’ |
如果 country 是 us 且 region 是 us-south 則傳回 true。 |
|| |
邏輯 OR | age > 40 || country == ‘us’ |
如果 age 高於 40,或 country 是 us,則傳回 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 <= 20lastName <= ‘Smith’ |
== |
等於 | region == 'us-south' |
!= |
不等於 | first_name != 'joe' |
鍵路徑
鍵路徑分為三個不同的部分,每個部分都有一個單獨的前綴:
$m- Meta 資料
$l- 標籤,例如
applicationname或subsystemName $d- 用戶資料(預設前綴)
$m – 元資料鍵路徑
| 鍵路徑 | 資料類型 | 說明 |
|---|---|---|
$m.timestamp |
時間戳記 | 包含事件的時間戳記 |
$m.severity |
嚴重性 | 包含事件的嚴重性 |
$l – 標籤鍵路徑
| 鍵路徑 | 資料類型 |
|---|---|
$l.applicationname |
字串 |
$l.subsystemname |
字串 |
$d – 使用者資料鍵路徑
任何使用者鍵路徑都可以使用 $d.<keypath> 訪問,包括嵌套鍵路徑。
$d 是預設前綴。 任何不包含前綴的鍵路徑都將被視為使用者資料欄位。
Functions
函數在 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')