菜单
文档breadcrumb arrow Grafana Alloybreadcrumb arrow 参考breadcrumb arrow 组件breadcrumb arrow lokibreadcrumb arrow loki.source.cloudflare
通用可用 (GA) 开源

loki.source.cloudflare

loki.source.cloudflare 从 Cloudflare Logpull API 拉取日志并将其转发到其他 loki.* 组件。

这些日志包含与连接客户端、通过 Cloudflare 网络的请求路径以及源 Web 服务器响应相关的数据,可用于丰富源服务器上的现有日志。

您可以通过为它们指定不同的标签来使用多个 loki.source.cloudflare 组件。

用法

alloy
loki.source.cloudflare "<LABEL>" {
  zone_id   = "<ZONE_ID>"
  api_token = "<API_TOKEN>"

  forward_to = <RECEIVER_LIST>
}

参数

您可以将以下参数与 loki.source.cloudflare 一起使用

名称类型描述默认必需
api_tokenstring用于认证的 API token。
forward_tolist(LogsReceiver)要发送日志条目的接收器列表。
zone_idstring要使用的 Cloudflare 区域 ID。
additional_fieldslist(string)用于补充通过 fields_type 提供的字段的附加字段列表。
fields_typestring要为日志条目提取的字段集。"default"
labelsmap(string)与传入日志条目关联的标签。{}
pull_rangeduration每个拉取请求的时间范围。"1m"
workersint用于解析日志的 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 处理阶段来提取更多标签或更改日志行格式。示例日志如下所示

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 组件。

alloy
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 可以接受来自以下组件的参数

注意

连接某些组件可能不合理,或者组件可能需要进一步配置才能使连接正常工作。有关更多详细信息,请参阅链接的文档。