跳转至

Log Streaming


启动一个 HTTP Server,接收日志文本数据,上报到观测云。HTTP URL 固定为:/v1/write/logstreaming,即 http://Datakit_IP:PORT/v1/write/logstreaming

注:如果 DataKit 以 DaemonSet 方式部署在 Kubernetes 中,可以使用 Service 方式访问,地址为 http://datakit-service.datakit:9529

配置

进入 DataKit 安装目录下的 conf.d/samples 目录,复制 logstreaming.conf.sample 并命名为 logstreaming.conf。示例如下:

[inputs.logstreaming]
  ignore_url_tags = false
  json_as_fields = false

  ## Threads config controls how many goroutines an agent cloud start to handle HTTP request.
  ## buffer is the size of jobs' buffering of worker channel.
  ## threads is the total number fo goroutines at running time.
  # [inputs.logstreaming.threads]
  #   buffer = 100
  #   threads = 8

  ## Storage config a local storage space in hard dirver to cache trace data.
  ## path is the local file path used to cache data.
  ## capacity is total space size(MB) used to store data.
  # [inputs.logstreaming.storage]
  #   path = "./log_storage"
  #   capacity = 5120

配置好后,重启 DataKit 即可。

目前可以通过 ConfigMap 方式注入采集器配置来开启采集器。

JSON 字段模式

在 [inputs.logstreaming] 中设置 json_as_fields = true,可将默认文本类型中每行日志的 JSON 根对象转换为字段,详细规则见 JSON 字段模式。该配置不作用于 influxdb、firelens 和 firehose 类型,也不是 HTTP URL 参数。

支持参数

Log-Streaming 支持在 HTTP URL 中添加参数,对日志数据进行操作。参数列表如下:

  • type:数据格式,支持 influxdb、firelens 和 firehose。
    • 当 type 为 influxdb 时(/v1/write/logstreaming?type=influxdb),说明数据本身就是行协议格式(默认 precision 是 s),将只添加内置 Tags,不再做其他操作
    • 当 type 为 firelens 时 (/v1/write/logstreaming?type=firelens),数据格式应是 JSON 格式的多条日志
    • 当 type 为 firehose 时 (/v1/write/logstreaming?type=firehose),数据格式为 AWS Firehose 格式。
    • 当此值为空时,会对数据做分行和 Pipeline 等处理
  • source:标识数据来源,即行协议的 measurement。例如 nginx 或者 redis(/v1/write/logstreaming?source=nginx)。当 type 是 influxdb 时,此值无效。默认为 default
  • service:添加 service 标签字段,例如(/v1/write/logstreaming?service=nginx_service)。默认为 source 参数值。
  • pipeline:指定数据需要使用的 pipeline 名称,例如 nginx.p(/v1/write/logstreaming?pipeline=nginx.p)
  • tags:添加自定义 tag,以英文逗号 , 分割,例如 key1=value1 和 key2=value2(/v1/write/logstreaming?tags=key1=value1,key2=value2)
  • storage_index:指定日志存储的索引名(注意,索引名称必须以字母开头,并且只能包含小写字母、数字或 _ 字符) Version-1.76.0

FireLens 数据源类型

该类型数据中的 log, source, date 字段将会特殊处理,数据示例:

[
  {
    "date": 1705485197.93957,
    "container_id": "xxxxxxxxxxx-xxxxxxx",
    "container_name": "nginx_demo",
    "source": "stdout",
    "log": "127.0.0.1 - - [19/Jan/2024:11:49:48 +0800] \"GET / HTTP/1.1\" 403 162 \"-\" \"curl/7.81.0\"",
    "ecs_cluster": "Cluster_demo"
  },
  {
    "date": 1705485197.943546,
    "container_id": "f68a9aeb3d64493595e89f8821fa3f86-4093234565",
    "container_name": "javatest",
    "source": "stdout",
    "log": "2024/01/19 11:49:48 [error] 1316#1316: *1 directory index of \"/var/www/html/\" is forbidden, client: 127.0.0.1, server: _, request: \"GET / HTTP/1.1\", host: \"localhost\"",
    "ecs_cluster": "Cluster_Demo"
  }
]

在提取出列表中的两条日志后,其中 log 将作为数据的 message 字段,date 将转换为日志的时间,source 将被重命名为 firelens_source。

AWS Firehose 数据源类型

使用 POST /v1/write/logstreaming?type=firehose 发送 Firehose 格式的数据。records[].data 是原始记录字节的 Base64 编码,例如:

{
  "requestId": "ed46c141-8086-455a-85bc-6644837c7875",
  "timestamp": 1677654321000,
  "records": [
    {
      "data": "dGhpcyBpcyBsb2cgbWVzc2FnZQ=="
    },
    {
      "data": "bG9nIGRhdGEuLi4="
    }
  ]
}

解码后分别得到 this is log message 和 log data...,每条 record 生成一条日志,记录内的换行保留在 message 中。

建议设置 source、service,方便匹配 Pipeline。Firehose common attributes 通过 X-Amz-Firehose-Common-Attributes 请求头传入,其中的 source 优先于 URL 参数。

云端接入及可选的记录内换行拆分见 AWS Firehose HTTP Endpoint。该页面的 /v1/input/firehose 是云端接收接口;其 firehose_split=newline common attribute 不适用于本 DataKit 接口,DataKit 不会据此拆分 record。

使用方式

  • Fluentd 使用 Influxdb Output 文档
  • Fluentd 使用 HTTP Output 文档
  • Logstash 使用 Influxdb Output 文档
  • Logstash 使用 HTTP Output 文档

只需要将 Output Host 配置为 Log-Streaming URL(http://Datakit_IP:PORT/v1/write/logstreaming)并添加对应参数即可。

日志

default

Remote log entries received through the logstreaming HTTP endpoint. The point name comes from the source query parameter and defaults to default.

Tags & Fields Description
collector_source_ip
(tag)
Remote client IP address resolved from the HTTP request.
ip_or_hostname
(tag)
Hostname part of the logstreaming request URL.
service
(tag)
Service name. Using the service parameter in the URL.
message Log message text parsed from the request body or protocol-specific payload.
Type: string
Unit: N/A
status Log status string assigned to the streamed log entry before pipeline processing.
Type: string
Unit: N/A

文档评价

文档内容是否对您有帮助?