loki.source.cloudflare
loki.source.cloudflare 从 Cloudflare Logpull API 拉取日志并将其转发到其他 loki.* 组件。
这些日志包含与连接客户端、通过 Cloudflare 网络的请求路径以及源 Web 服务器响应相关的数据,可用于丰富源服务器上的现有日志。
您可以通过为它们指定不同的标签来使用多个 loki.source.cloudflare 组件。
用法
loki.source.cloudflare "<LABEL>" {
zone_id = "<ZONE_ID>"
api_token = "<API_TOKEN>"
forward_to = <RECEIVER_LIST>
}参数
您可以将以下参数与 loki.source.cloudflare 一起使用
| 名称 | 类型 | 描述 | 默认 | 必需 |
|---|---|---|---|---|
api_token | string | 用于认证的 API token。 | 是 | |
forward_to | list(LogsReceiver) | 要发送日志条目的接收器列表。 | 是 | |
zone_id | string | 要使用的 Cloudflare 区域 ID。 | 是 | |
additional_fields | list(string) | 用于补充通过 fields_type 提供的字段的附加字段列表。 | 否 | |
fields_type | string | 要为日志条目提取的字段集。 | "default" | 否 |
labels | map(string) | 与传入日志条目关联的标签。 | {} | 否 |
pull_range | duration | 每个拉取请求的时间范围。 | "1m" | 否 |
workers | int | 用于解析日志的 worker 数量。 | 3 | 否 |
默认情况下,loki.source.cloudflare 使用 default 字段集提取日志。以下列表显示了可供选择的不同 fields_type 集合以及它们包含的字段
default包括"ClientIP""ClientRequestHost""ClientRequestMethod""ClientRequestURI""EdgeEndTimestamp""EdgeResponseBytes""EdgeRequestHost""EdgeResponseStatus""EdgeStartTimestamp""RayID"
以及通过
additional_fields参数提供的任何额外字段。minimal包括所有default字段并添加以及通过"ZoneID""ClientSSLProtocol""ClientRequestProtocol""ClientRequestPath""ClientRequestUserAgent""ClientRequestReferer""EdgeColoCode""ClientCountry""CacheCacheStatus""CacheResponseStatus""EdgeResponseContentType"
additional_fields参数提供的任何额外字段。extended包括所有minimal字段并添加以及通过"ClientSSLCipher""ClientASN""ClientIPClass""CacheResponseBytes""EdgePathingOp""EdgePathingSrc""EdgePathingStatus""ParentRayID""WorkerCPUTime""WorkerStatus""WorkerSubrequest""WorkerSubrequestCount""OriginIP""OriginResponseStatus""OriginSSLProtocol""OriginResponseHTTPExpires""OriginResponseHTTPLastModified"
additional_fields参数提供的任何额外字段。all包括所有extended字段并添加以及通过"BotScore""BotScoreSrc""BotTags""ClientRequestBytes""ClientSrcPort""ClientXRequestedWith""CacheTieredFill""EdgeResponseCompressionRatio""EdgeServerIP""FirewallMatchesSources""FirewallMatchesActions""FirewallMatchesRuleIDs""OriginResponseBytes""OriginResponseTime""ClientDeviceType""WAFFlags""WAFMatchedVar""EdgeColoID""RequestHeaders""ResponseHeaders""ClientRequestSource"
additional_fields参数提供的任何额外字段。即使 Cloudflare API 提供了新的字段但尚未包含在all中,这仍然是相关的。custom仅包含在additional_fields中定义的字段。
该组件将其最后一次成功提取的时间戳保存在其位置文件中。如果在文件中找到给定区域 ID 的位置,则组件将从该时间戳重新开始拉取日志。如果找不到位置,则组件将从当前时间开始拉取日志。
日志使用多个 workers 提取,这些 worker 重复请求最新的可用 pull_range。由于每次拉取需要处理的日志行过多,可能会导致落后。增加 worker 数量、缩短拉取范围或减少提取的字段数量可以缓解此性能问题。
组件提取的最后一个时间戳记录在 loki_source_cloudflare_target_last_requested_end_timestamp 调试指标中。
所有传入的 Cloudflare 日志条目都是 JSON 格式。您可以使用 loki.process 组件和 JSON 处理阶段来提取更多标签或更改日志行格式。示例日志如下所示
{
"CacheCacheStatus": "miss",
"CacheResponseBytes": 8377,
"CacheResponseStatus": 200,
"CacheTieredFill": false,
"ClientASN": 786,
"ClientCountry": "gb",
"ClientDeviceType": "desktop",
"ClientIP": "100.100.5.5",
"ClientIPClass": "noRecord",
"ClientRequestBytes": 2691,
"ClientRequestHost": "www.foo.com",
"ClientRequestMethod": "GET",
"ClientRequestPath": "/comments/foo/",
"ClientRequestProtocol": "HTTP/1.0",
"ClientRequestReferer": "https://www.foo.com/foo/168855/?offset=8625",
"ClientRequestURI": "/foo/15248108/",
"ClientRequestUserAgent": "some bot",
"ClientRequestSource": "1"
"ClientSSLCipher": "ECDHE-ECDSA-AES128-GCM-SHA256",
"ClientSSLProtocol": "TLSv1.2",
"ClientSrcPort": 39816,
"ClientXRequestedWith": "",
"EdgeColoCode": "MAN",
"EdgeColoID": 341,
"EdgeEndTimestamp": 1637336610671000000,
"EdgePathingOp": "wl",
"EdgePathingSrc": "macro",
"EdgePathingStatus": "nr",
"EdgeRateLimitAction": "",
"EdgeRateLimitID": 0,
"EdgeRequestHost": "www.foo.com",
"EdgeResponseBytes": 14878,
"EdgeResponseCompressionRatio": 1,
"EdgeResponseContentType": "text/html",
"EdgeResponseStatus": 200,
"EdgeServerIP": "8.8.8.8",
"EdgeStartTimestamp": 1637336610517000000,
"FirewallMatchesActions": [],
"FirewallMatchesRuleIDs": [],
"FirewallMatchesSources": [],
"OriginIP": "8.8.8.8",
"OriginResponseBytes": 0,
"OriginResponseHTTPExpires": "",
"OriginResponseHTTPLastModified": "",
"OriginResponseStatus": 200,
"OriginResponseTime": 123000000,
"OriginSSLProtocol": "TLSv1.2",
"ParentRayID": "00",
"RayID": "6b0a...",
"RequestHeaders": [],
"ResponseHeaders": [
"x-foo": "bar"
],
"SecurityLevel": "med",
"WAFAction": "unknown",
"WAFFlags": "0",
"WAFMatchedVar": "",
"WAFProfile": "unknown",
"WAFRuleID": "",
"WAFRuleMessage": "",
"WorkerCPUTime": 0,
"WorkerStatus": "unknown",
"WorkerSubrequest": false,
"WorkerSubrequestCount": 0,
"ZoneID": 1234
}块
loki.source.cloudflare 组件不支持任何块。您可以使用参数配置此组件。
导出的字段
loki.source.cloudflare 不导出任何字段。
组件健康状态
仅当给出无效配置时,loki.source.cloudflare 才会被报告为不健康。
调试信息
loki.source.cloudflare 暴露以下调试信息
- 目标是否就绪并从 API 读取日志。
- Cloudflare 区域 ID。
- 报告的最后一个错误(如果有)。
- 存储的位置文件条目,是
zone_id、标签和最后一次提取的时间戳的组合。 - 最后一次提取的时间戳。
- 正在提取的字段集。
调试指标
loki_source_cloudflare_target_entries_total(counter): 通过 cloudflare 目标成功发送的条目总数。loki_source_cloudflare_target_last_requested_end_timestamp(gauge): 最后一次 cloudflare 请求结束的时间戳,用于计算目标落后多少。
示例
此示例从 Cloudflare 的 API 拉取日志并将其转发到 loki.write 组件。
loki.source.cloudflare "dev" {
zone_id = sys.env("CF_ZONE_ID")
api_token = local.file.api.content
forward_to = [loki.write.local.receiver]
}
loki.write "local" {
endpoint {
url = "loki:3100/api/v1/push"
}
}兼容组件
loki.source.cloudflare 可以接受来自以下组件的参数
- 导出 Loki
LogsReceiver的组件
注意
连接某些组件可能不合理,或者组件可能需要进一步配置才能使连接正常工作。有关更多详细信息,请参阅链接的文档。



