请给出使用 ELK 清洗推送日志并可视化漏斗的 DSL。
解读
国内移动端测试面试中,推送链路漏斗是衡量消息到达率的核心指标,常见五层:
① 调用推送网关量 → ② 网关返回成功量 → ③ 设备在线送达量 → ④ 用户可见通知量 → ⑤ 点击通知量。
面试官不仅想看你会不会写 DSL,更关注你能否把乱序、多行、嵌套 JSON 的原始日志清洗成可聚合的宽表,并用一条 DSL 语句完成漏斗计算。
因此答案必须同时给出 Logstash 清洗脚本 与 Kibana Lens/TSVB 无法表达时的 Elasticsearch 聚合 DSL,并解释每一步的中文字段映射与时间窗口对齐策略,体现你对国内手机厂商 ROM 兼容、弱网重试、消息去重等真实痛点的理解。
知识点
- Logstash 条件判断与 Ruby 插件:国内推送日志常混有厂商回执,需用
if [fields][log_topic] == "push"隔离。 - Elasticsearch 管道聚合(pipeline aggregation):
bucket_script实现漏斗层之间的除法,得出转化率。 - 时间对齐:国内面试常追问“小时级漏斗与自然日漏斗如何切换”,需用
date_histogram的offset参数。 - 去重键:
device_id + msg_id组合键,防止厂商重推造成漏斗层虚高。 - Kibana 可视化限制:Lens 无法画漏斗图,需用 Vega-Lite DSL 嵌入 Kibana,体现你对高级可视化的掌握。
答案
以下示例基于国内最常见的 腾讯信鸽推送日志格式,字段均为中文,可直接落地。
1. Logstash 清洗脚本(push_funnel.conf)
input {
kafka {
bootstrap_servers => "kafka-inner:9092"
topics => ["app_push"]
codec => json { charset => "UTF-8" }
consumer_threads => 3
tags => ["push"]
}
}
filter {
# 只保留推送相关日志
if [fields][log_topic] != "push" { drop {} }
# 统一时间字段
date {
match => ["@timestamp", "yyyy-MM-dd HH:mm:ss.SSS"]
timezone => "Asia/Shanghai"
}
# 解析嵌套 JSON
json {
source => "message"
target => "push"
}
# 生成去重键
mutate {
add_field => {
"[@metadata][dedup_key]" => "%{[push][device_id]}_%{[push][msg_id]}"
}
}
# 映射漏斗阶段
if [push][stage] == "网关接收" {
mutate { add_field => { "funnel_step" => 1 } }
} else if [push][stage] == "网关回执成功" {
mutate { add_field => { "funnel_step" => 2 } }
} else if [push][stage] == "设备在线送达" {
mutate { add_field => { "funnel_step" => 3 } }
} else if [push][stage] == "通知展示" {
mutate { add_field => { "funnel_step" => 4 } }
} else if [push][stage] == "用户点击" {
mutate { add_field => { "funnel_step" => 5 } }
} else {
drop {}
}
# 去掉多余字段,节省磁盘
prune {
whitelist_names => ["@timestamp", "funnel_step", "[@metadata][dedup_key]", "[push][msg_id]", "[push][device_id]"]
}
}
output {
elasticsearch {
hosts => ["es-cluster:9200"]
index => "push_funnel-%{+YYYY.MM.dd}"
document_id => "%{[@metadata][dedup_key]}_%{funnel_step}" # 确保同一设备同一消息同一阶段只存一条
}
}
2. Elasticsearch 聚合 DSL(返回小时级漏斗)
GET push_funnel-*/_search
{
"size": 0,
"query": {
"range": {
"@timestamp": {
"gte": "now-7d/d",
"lte": "now/d",
"time_zone": "+08:00"
}
}
},
"aggs": {
"hour": {
"date_histogram": {
"field": "@timestamp",
"calendar_interval": "1h",
"time_zone": "+08:00",
"min_doc_count": 0
},
"aggs": {
"funnel": {
"terms": {
"field": "funnel_step",
"size": 5,
"order": { "_key": "asc" }
}
},
"step1": {
"filter": { "term": { "funnel_step": 1 } }
},
"step2": {
"filter": { "term": { "funnel_step": 2 } }
},
"step3": {
"filter": { "term": { "funnel_step": 3 } }
},
"step4": {
"filter": { "term": { "funnel_step": 4 } }
},
"step5": {
"filter": { "term": { "funnel_step": 5 } }
},
"cvt_2_1": {
"bucket_script": {
"buckets_path": {
"s2": "step2._count",
"s1": "step1._count"
},
"script": "params.s1 == 0 ? 0 : params.s2 / params.s1"
}
},
"cvt_3_2": {
"bucket_script": {
"buckets_path": {
"s3": "step3._count",
"s2": "step2._count"
},
"script": "params.s2 == 0 ? 0 : params.s3 / params.s2"
}
},
"cvt_4_3": {
"bucket_script": {
"buckets_path": {
"s4": "step4._count",
"s3": "step3._count"
},
"script": "params.s3 == 0 ? 0 : params.s4 / params.s3"
}
},
"cvt_5_4": {
"bucket_script": {
"buckets_path": {
"s5": "step5._count",
"s4": "step4._count"
},
"script": "params.s4 == 0 ? 0 : params.s5 / params.s4"
}
}
}
}
}
}
3. Kibana Vega-Lite 漏斗图 DSL(可直接粘到 Vega 插件)
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"title": "推送漏斗(近7天)",
"data": {
"url": {
"index": "push_funnel-*",
"body": {
"aggs": {
"step": {
"terms": {"field": "funnel_step", "size": 5, "order": {"_key": "asc"}},
"aggs": {"uv": {"cardinality": {"field": "[push][device_id]"}}}
}
},
"size": 0
}
},
"format": {"property": "aggregations.step.buckets"}
},
"mark": {"type": "bar", "tooltip": true, "color": "#1890ff"},
"encoding": {
"x": {"field": "key", "type": "ordinal", "title": "漏斗阶段"},
"y": {"field": "uv.value", "type": "quantitative", "title": "设备数"}
}
}
拓展思考
- 厂商离线消息补发会导致阶段 3 突增,面试时可提出用
msg_id的 TTL 去重窗口(如 24 h)过滤重复回执。 - 弱网场景下阶段 2→3 转化率骤降,可结合 Network Emulation 日志,在 DSL 中增加
network_type子聚合,证明你理解国内 2G/3G 长尾用户现状。 - 大屏实时漏斗可用 Elasticsearch Transform 持续聚合,每分钟输出一次结果,再让 Grafana 引用,展示你对 ELK+Grafana 混合架构的落地经验。