从 Vector 发送日志
Vector 是一款出色的软件(显然用 Rust 编写),为可观测性领域带来了新的清新之风, 它以从基础设施的各个部分收集日志、转换和聚合日志以及最终将日志转发到接收端而闻名。
在本指南中,我们将向您展示如何将其连接到 Quickwit。
启动 Quickwit 服务器
- CLI
- Docker
# Create Quickwit data dir.
mkdir qwdata
./quickwit run
# Create Quickwit data dir.
mkdir qwdata
docker run --rm -v $(pwd)/qwdata:/quickwit/qwdata -p 7280:7280 quickwit/quickwit run
利用 Quickwit 对日志的原生支持
让我们拥抱 OpenTelemetry 标准并利用 Quickwit 的功能。借助对 OpenTelemetry 标准的原生支持,Quickwit 已经带有一个名为 otel-logs_v0_7
的索引,与 OpenTelemetry 日志数据模型兼容。这意味着我们可以开始推送日志数据,无需通常的索引设置。
OpenTelemetry 索引配置可以在 quickwit-opentelemetry/src/otlp/logs.rs 源文件中找到。
设置 Vector
我们的接收端将是 Quickwit 的摄入 API http://127.0.0.1:7280/api/v1/otel-logs-v0_7/ingest
。
为了简化本教程,我们将使用一个名为 demo_logs
的日志源,该日志源以给定格式生成日志。让我们选择常见的 syslog
格式
(Vector 不直接生成 OpenTelemetry 格式的日志!)并使用转换功能将 syslog
格式映射到 OpenTelemetry 格式。
[sources.generate_syslog]
type = "demo_logs"
format = "syslog"
count = 100000
interval = 0.001
[transforms.remap_syslog]
inputs = [ "generate_syslog"]
type = "remap"
source = '''
structured = parse_syslog!(.message)
.timestamp_nanos = to_unix_timestamp!(structured.timestamp, unit: "nanoseconds")
.body = structured
.service_name = structured.appname
.resource_attributes.source_type = .source_type
.resource_attributes.host.hostname = structured.hostname
.resource_attributes.service.name = structured.appname
.attributes.syslog.procid = structured.procid
.attributes.syslog.facility = structured.facility
.attributes.syslog.version = structured.version
.severity_text = if includes(["emerg", "err", "crit", "alert"], structured.severity) {
"ERROR"
} else if structured.severity == "warning" {
"WARN"
} else if structured.severity == "debug" {
"DEBUG"
} else if includes(["info", "notice"], structured.severity) {
"INFO"
} else {
structured.severity
}
.scope_name = structured.msgid
del(.message)
del(.timestamp)
del(.service)
del(.source_type)
'''
# useful to see the logs in the terminal
# [sinks.emit_syslog]
# inputs = ["remap_syslog"]
# type = "console"
# encoding.codec = "json"
[sinks.quickwit_logs]
type = "http"
method = "post"
inputs = ["remap_syslog"]
encoding.codec = "json"
framing.method = "newline_delimited"
uri = "http://127.0.0.1:7280/api/v1/otel-logs-v0_7/ingest"
下载上述 Vector 配置文件。
curl -o vector.toml https://raw.githubusercontent.com/quickwit-oss/quickwit/main/config/tutorials/vector-otel-logs/vector.toml
现在让我们启动 Vector,以便我们能够开始将日志发送到 Quickwit。
docker run -v $(pwd)/vector.toml:/etc/vector/vector.toml:ro -p 8383:8383 --net=host timberio/vector:0.25.0-distroless-libc
搜索日志
Quickwit 现在正在从 Vector 接收日志,并且您可以使用 curl
或通过 UI 进行搜索:
curl -XGET http://127.0.0.1:7280/api/v1/otel-logs-v0_7/search?query=severity_text:ERROR
- 在浏览器中打开
http://127.0.0.1:7280/ui/search?query=severity_text:ERROR&index_id=otel-logs-v0_7&max_hits=10
并开始使用!
对 severity_text 计算聚合
对于聚合,我们暂时还不能使用 Quickwit UI,但我们可以通过 curl
使用。
让我们构造一个漂亮的聚合查询,计算每分钟有多少条 INFO
、DEBUG
、WARN
和 ERROR
(所有日期时间都以微秒存储,因此间隔为 60_000_000 微秒):
{
"query": "*",
"max_hits": 0,
"aggs": {
"count_per_minute": {
"histogram": {
"field": "timestamp_nanos",
"interval": 60000000
},
"aggs": {
"severity_text_count": {
"terms": {
"field": "severity_text"
}
}
}
}
}
}
curl -XPOST -H "Content-Type: application/json" http://127.0.0.1:7280/api/v1/otel-logs-v0_7/search --data @aggregation-query.json
进一步操作
现在您还可以部署 Grafana 并将 Quickwit 作为数据源用于查询、仪表板、警报等!