日志服务数据加工最佳实践: 事件判断

本文涉及的产品
对象存储 OSS,标准 - 本地冗余存储 20GB 3个月
日志服务 SLS,月写入数据量 50GB 1个月
对象存储 OSS,内容安全 1000 次 1年
简介: 本篇介绍日志服务数据加工: 事件判断最佳实践汇总

场景1:判断字段是否存在

原始日志

a: a_vlue
b:    // 空串

LOG DSL编排

方案一:使用e_has, e_not_has

e_if(e_has("a"), e_set("has_a", true))
e_if(e_has("b"), e_set("has_b", true))
e_if(e_has("c"), e_set("has_c", true))
e_if(e_not_has("a"), e_set("not_has_a", true))
e_if(e_not_has("b"), e_set("not_has_b", true))
e_if(e_not_has("c"), e_set("not_has_c", true))

方案二:使用e_search

e_if(e_search("a: *"), e_set("has_a", true))
e_if(e_search("b: *"), e_set("has_b", true))
e_if(e_search("c: *"), e_set("has_c", true))
e_if(e_search("not a: *"), e_set("not_has_a", true))
e_if(e_search("not b: *"), e_set("not_has_b", true))
e_if(e_search("not c: *"), e_set("not_has_c", true))

加工后日志

a: a_vlue
b:    // 空串
has_a: true
has_b: true
has_c: false
not_has_a: false
not_has_b: false
not_has_c: true
  • 以上两种方案都可用于判断字段是否存在,但是方案一更直观、易理解。
  • 以上加工规则中的两个e_if可通过e_if(条件1,操作1,条件2,操作2)的形式合并为一个e_if,此处为了易于读者阅读,拆成两个。

场景2:判断字段值存在且不为空

原始日志

a: a_vlue
b:     // 空串

LOG DSL编排

方案一:使用字段取值函数v

e_if(v("a"), e_set("not_empty_a", true))
e_if(v("b"), e_set("not_empty_b", true))
e_if(v("c"), e_set("not_empty_c", true))
  • 字段取值函数v,当对应字段存在且值不为空时,其自动转换的Bool值为true,否则为false

方案二:使用e_search

# 至少一个字符
e_if(e_search('a: "?"'), e_set("not_empty_a", true))
e_if(e_search('b: "?"'), e_set("not_empty_b", true))
e_if(e_search('c: "?"'), e_set("not_empty_c", true))

# 正则
e_if(e_search('a~=".+"'), e_set("not_empty_a", true))
e_if(e_search('b~=".+"'), e_set("not_empty_b", true))
e_if(e_search('c~=".+"'), e_set("not_empty_c", true))

# 存在且不为空
e_if(e_search('a: * and not a==""'), e_set("not_empty_a", true))
e_if(e_search('b: * and not b==""'), e_set("not_empty_b", true))
e_if(e_search('c: * and not c==""'), e_set("not_empty_b", true))

加工后日志

a: a_vlue
b:     // 空串
not_empty_a: true
not_empty_b: false
not_empty_c: false
  • 以上三方案都可用于判断字段是否存在,但是方案一更简洁。
  • 以上加工规则中的两个e_if可通过e_if(条件1,操作1,条件2,操作2)的形式合并为一个e_if,此处为了易于读者阅读,拆成两个。

场景3:判断字段值存在但为空

原始日志

a: a_vlue
b:   // 空串

LOG DSL编排

方案一:使用字段取值函数v

e_if(op_and(e_has("a"), op_not(v("a"))), e_set("empty_a", true))
e_if(op_and(e_has("b"), op_not(v("b"))), e_set("empty_b", true))
e_if(op_and(e_has("c"), op_not(v("c"))), e_set("empty_c", true))

# 错误的方案
e_if(op_not(v("a")), e_set("empty_a", true))
e_if(op_not(v("b")), e_set("empty_b", true))
e_if(op_not(v("c")), e_set("empty_c", true))
  • 字段取值函数v,当对应字段存在且值不为空时,其自动转换的Bool值为true,否则为false. 但是只不存在是, 其返回None, op_not(None)时也是返回True.

方案二:使用e_search

e_if(e_search('a==""'), e_set("empty_a", true))
e_if(e_search('b==""'), e_set("empty_b", true))
e_if(e_search('c==""'), e_set("empty_c", true))

# 以下是错误调用
e_if(e_search('a:""'), e_set("empty_a", true))
e_if(e_search('b:""'), e_set("empty_b", true))

注意, 以上错误调用中, 因为:e_search是部分查询, 字段存在时, 无论是否空串的情况下, 空串a: ""永远会真.

加工后日志

a: a_vlue
b:    // 空串
empty_a: false
empty_b: true
empty_b: false
  • 以上方案中显然方案二更简洁.
  • 以上加工规则中的两个e_if可通过e_if(条件1,操作1,条件2,操作2)的形式合并为一个e_if,此处为了易于读者阅读,拆成两个。

场景4:基于字段值的逻辑查询判断

原始日志

"日志1"
http_host:  m1.abcd.com
status:  200
request_method:  GET
scheme:  https
header_length: 700
body_length: 1200

"日志2"
http_host:  m2.abcd.com
status:  200
request_method:  POST
scheme:  https
header_length: 100
body_length: 800

"日志3"
http_host:  m3.abcd.com
status:  200
request_method:  GET
scheme:  http
header_length: 700
body_length: 800

"日志4"
http_host:  m4.abcd.com
status:  404
request_method:  GET
scheme:  https
header_length: 100
body_length: 300

加工需求1

  • 为所有status字段值为200的日志事件, 添加一个字段type,其值为normal

LOG DSL编排

e_if(e_match("status", "200"), e_set("type", "normal))
或者
e_if(e_search("status==200"), e_set("type", "normal"))
  • 在此相对简单的场景下,以上两种方式都可以,并无太大差别。
  • 一般情况下status: 200也可以, 只是==更精准一些.

加工后的日志

"日志1"
type: normal
http_host:  m1.abcd.com
status:  200
request_method:  GET
scheme:  https
header_length: 700
body_length: 1200

"日志2"
type: normal
http_host:  m2.abcd.com
status:  200
request_method:  POST
scheme:  https
header_length: 100
body_length: 800

"日志3"
type: normal
http_host:  m3.abcd.com
status:  200
request_method:  GET
scheme:  http
header_length: 700
body_length: 800

"日志4"
http_host:  m4.abcd.com
status:  404
request_method:  GET
scheme:  https
header_length: 100
body_length: 300

加工需求2

  • 为所有status字段值为200 并且 request_method字段值为GET 并且 scheme字段值为https 的日志事件, 添加一个字段type,其值为normal

LOG DSL编排

e_if(e_search("status==200 and request_method==GET and scheme==https"), e_set("type", "normal"))
或者
e_if(e_match_all("status", "200", "request_method","GET", "scheme", "https"), e_set("type", "normal"))
  • 在此场景下,需要同时满足多个字段的匹配条件,可以使用e_searche_match_all
  • e_search用法相对更简洁一些。
  • 一般情况下e_search==也可以换成:status: 200也可以, 只是==更精准一些.

加工后的日志

"日志1"
type: normal
http_host:  m1.abcd.com
status:  200
request_method:  GET
scheme:  https
header_length: 700
body_length: 1200

"日志2"
http_host:  m2.abcd.com
status:  200
request_method:  POST
scheme:  https
header_length: 100
body_length: 800

"日志3"
http_host:  m3.abcd.com
status:  200
request_method:  GET
scheme:  http
header_length: 700
body_length: 800

"日志4"
http_host:  m4.abcd.com
status:  404
request_method:  GET
scheme:  https
header_length: 100
body_length: 300

加工需求3

  • 为所有status字段值为200 或者 request_method字段值为GET 或者 scheme字段值为https 的日志事件, 添加一个字段type,其值为normal

LOG DSL编排

e_if(e_search("status==200 or request_method==GET or scheme==https"), e_set("type", "normal"))
或者
e_if(e_match_any("status", "200", "request_method","GET", "scheme", "https"), e_set("type", "normal"))
  • 在此场景下,需要满足多个字段的匹配条件中的其中一个,可以使用e_searche_match_any
  • e_search用法相对更简洁一些。

加工后的日志

"日志1"
type: normal
http_host:  m1.abcd.com
status:  200
request_method:  GET
scheme:  https
header_length: 700
body_length: 100

"日志2"
type: normal
http_host:  m2.abcd.com
status:  200
request_method:  POST
scheme:  https
header_length: 100
body_length: 800

"日志3"
type: normal
http_host:  m3.abcd.com
status:  200
request_method:  GET
scheme:  http
header_length: 700
body_length: 800

"日志4"
type: normal
http_host:  m4.abcd.com
status:  404
request_method:  GET
scheme:  https
header_length: 100
body_length: 1300

加工需求4

  • 为所有status字段值为200 并且 request_method字段值为GET 并且 header_lengthbody_length的字段值之和小于等于1000的日志事件, 添加一个字段type,其值为normal

LOG DSL编排

e_if(op_and(e_search("status: 200 and request_method: GET"), op_le(op_sum(v("header_length"), v("body_length")), 1000)), e_set("type", "normal"))
  • 在更复杂的逻辑场景下,可通过e_search和其他表达式函数的组合来完成。

加工后的日志

"日志1"
type: normal
http_host:  m1.abcd.com
status:  200
request_method:  GET
scheme:  https
header_length: 700
body_length: 100

"日志2"
http_host:  m2.abcd.com
status:  200
request_method:  POST
scheme:  https
header_length: 100
body_length: 800

"日志3"
http_host:  m3.abcd.com
status:  200
request_method:  GET
scheme:  http
header_length: 700
body_length: 800

"日志4"
http_host:  m4.abcd.com
status:  404
request_method:  GET
scheme:  https
header_length: 100
body_length: 1300

进一步参考

欢迎扫码加入官方钉钉群获得实时更新与阿里云工程师的及时直接的支持:
image

相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
目录
相关文章
|
2月前
|
SQL 人工智能 监控
SLS Copilot 实践:基于 SLS 灵活构建 LLM 应用的数据基础设施
本文将分享我们在构建 SLS SQL Copilot 过程中的工程实践,展示如何基于阿里云 SLS 打造一套完整的 LLM 应用数据基础设施。
650 53
|
2月前
|
数据采集 运维 监控
不重启、不重写、不停机:SLS 软删除如何实现真正的“无感数据急救”?
SLS 全新推出的「软删除」功能,以接近索引查询的性能,解决了数据应急删除与脏数据治理的痛点。2 分钟掌握这一数据管理神器。
234 28
|
2月前
|
Prometheus 监控 Java
日志收集和Spring 微服务监控的最佳实践
在微服务架构中,日志记录与监控对系统稳定性、问题排查和性能优化至关重要。本文介绍了在 Spring 微服务中实现高效日志记录与监控的最佳实践,涵盖日志级别选择、结构化日志、集中记录、服务ID跟踪、上下文信息添加、日志轮转,以及使用 Spring Boot Actuator、Micrometer、Prometheus、Grafana、ELK 堆栈等工具进行监控与可视化。通过这些方法,可提升系统的可观测性与运维效率。
305 1
日志收集和Spring 微服务监控的最佳实践
|
2月前
|
负载均衡 监控 安全
5 个 IIS 日志记录最佳实践
IIS日志记录是监控Web服务器性能与安全的关键。本文介绍启用日志、应用池配置、负载均衡、敏感数据防护、日志集中管理及保留策略等五大最佳实践,助力高效分析与合规审计。
233 1
|
3月前
|
存储 缓存 Apache
StarRocks+Paimon 落地阿里日志采集:万亿级实时数据秒级查询
A+流量分析平台是阿里集团统一的全域流量数据分析平台,致力于通过埋点、采集、计算构建流量数据闭环,助力业务提升流量转化。面对万亿级日志数据带来的写入与查询挑战,平台采用Flink+Paimon+StarRocks技术方案,实现高吞吐写入与秒级查询,优化存储成本与扩展性,提升日志分析效率。
480 1
|
3月前
|
存储 关系型数据库 数据库
【赵渝强老师】PostgreSQL数据库的WAL日志与数据写入的过程
PostgreSQL中的WAL(预写日志)是保证数据完整性的关键技术。在数据修改前,系统会先将日志写入WAL,确保宕机时可通过日志恢复数据。它减少了磁盘I/O,提升了性能,并支持手动切换日志文件。WAL文件默认存储在pg_wal目录下,采用16进制命名规则。此外,PostgreSQL提供pg_waldump工具解析日志内容。
321 0
|
3月前
|
数据采集 运维 监控
|
6月前
|
监控 容灾 算法
阿里云 SLS 多云日志接入最佳实践:链路、成本与高可用性优化
本文探讨了如何高效、经济且可靠地将海外应用与基础设施日志统一采集至阿里云日志服务(SLS),解决全球化业务扩展中的关键挑战。重点介绍了高性能日志采集Agent(iLogtail/LoongCollector)在海外场景的应用,推荐使用LoongCollector以获得更优的稳定性和网络容错能力。同时分析了多种网络接入方案,包括公网直连、全球加速优化、阿里云内网及专线/CEN/VPN接入等,并提供了成本优化策略和多目标发送配置指导,帮助企业构建稳定、低成本、高可用的全球日志系统。
796 54

相关产品

  • 日志服务
  • 下一篇
    oss云网关配置