# HTTP

## 简介

`flusher_http` `flusher`插件可以实现将采集到的数据，经过处理后，通过http格式发送到指定的地址。

## 版本

[Alpha](https://ilogtail.gitbook.io/ilogtail-docs/pre-release/plugins/stability-level)

## 版本说明

* 推荐版本：iLogtail v1.4.0 及以上

## 配置参数

| 参数                           | 类型                  | 是否必选 | 说明                                                                                                                                                                                                                                 |
| ---------------------------- | ------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Type                         | string              | 是    | 插件类型，固定为`flusher_http`                                                                                                                                                                                                             |
| RemoteURL                    | string              | 是    | 要发送到的URL地址，示例：`http://localhost:8086/write`                                                                                                                                                                                        |
| Headers                      | Map\<string,string> | 否    | <p>发送时附加的http请求header，如可添加 Authorization、Content-Type等信息，支持动态变量写法，如<code>{"x-db":"%{tag.db}"}</code></p><p>v2版本支持从Group的Metadata或者Group.Tags中获取动态变量，如<code>{"x-db":"%{metadata.db}"}</code>或者<code>{"x-db":"%{tag.db}"}</code></p> |
| Query                        | Map\<string,string> | 否    | <p>发送时附加到url上的query参数，支持动态变量写法，如<code>{"db":"%{tag.db}"}</code></p><p>v2版本支持从Group的Metadata或者Group.Tags中获取动态变量，如<code>{"db":"%{metadata.db}"}</code>或者<code>{"db":"%{tag.db}"}</code></p>                                          |
| Timeout                      | string              | 否    | 请求的超时时间，默认 `60s`                                                                                                                                                                                                                   |
| Retry.Enable                 | bool                | 否    | 是否开启失败重试，默认为 `true`                                                                                                                                                                                                                |
| Retry.MaxRetryTimes          | int                 | 否    | 最大重试次数，默认为 `3`                                                                                                                                                                                                                     |
| Retry.InitialDelay           | string              | 否    | 首次重试时间间隔，默认为 `1s`，重试间隔以会2的倍数递增                                                                                                                                                                                                     |
| Retry.MaxDelay               | string              | 否    | 最大重试时间间隔，默认为 `30s`                                                                                                                                                                                                                 |
| Encoder                      | Struct              | 否    | LoongCollector 数据编码协议配置，优先级高于 Convert                                                                                                                                                                                              |
| Encoder.Type                 | string              | 否    | encoder 插件的类型                                                                                                                                                                                                                      |
| Encoder.Options              | Map\<string,Struct> | 否    | encoder 插件的配置                                                                                                                                                                                                                      |
| Convert                      | Struct              | 否    | LoongCollector 数据转换协议配置                                                                                                                                                                                                            |
| Convert.Protocol             | string              | 否    | <p>LoongCollector 数据转换协议，可选值：<code>custom\_single</code>,<code>influxdb</code>, <code>jsonline</code>。默认值：<code>custom\_single</code></p><p>v2版本可选值：<code>raw</code></p>                                                           |
| Convert.Encoding             | string              | 否    | LoongCollector Flusher 数据转换编码，可选值：`json`, `custom`，默认值：`json`                                                                                                                                                                      |
| Convert.Separator            | string              | 否    | <p>LoongCollector 数据转换时，PipelineGroupEvents中多个Events之间拼接使用的分隔符。如<code>\n</code>。若不设置，则默认不拼接Events，即每个Event作为独立请求向后发送。 默认值为空。</p><p>当前仅在<code>Convert.Protocol: raw</code>有效。</p>                                                   |
| Convert.IgnoreUnExpectedData | bool                | 否    | LoongCollector 数据转换时，遇到非预期的数据的行为，true 跳过，false 报错。默认值 true                                                                                                                                                                         |
| Convert.TagFieldsRename      | Map\<string,string> | 否    | 对日志中tags中的json字段重命名                                                                                                                                                                                                                |
| Convert.ProtocolFieldsRename | Map\<string,string> | 否    | LoongCollector 日志协议字段重命名，可当前可重命名的字段：`contents`,`tags`和`time`                                                                                                                                                                       |
| Concurrency                  | int                 | 否    | 向url发起请求的并发数，默认为`1`                                                                                                                                                                                                                |
| MaxConnsPerHost              | int                 | 否    | <p>每个host上的最大HTTP连接数（包含了拨号阶段的、活跃的、空闲的），默认<code>0</code>，表示不限制</p><p>当其值大于http.DefaultTransport.(\*http.Transport).MaxConnsPerHost时（当前是<code>0</code>），会采用该值</p>                                                                    |
| MaxIdleConnsPerHost          | int                 | 否    | <p>每个host上的最大空闲的HTTP连接数，默认<code>0</code>，表示不限制</p><p>当其值大于http.DefaultTransport.(\*http.Transport).MaxIdleConnsPerHost时（当前是<code>0</code>），会采用该值</p>                                                                               |
| IdleConnTimeout              | string              | 否    | <p>HTTP连接在关闭前保持闲置状态的最长时间，默认<code>90s</code></p><p>当其值大于http.DefaultTransport.(\*http.Transport).IdleConnTimeout时（当前是<code>90s</code>），会采用该值</p>                                                                                    |
| WriteBufferSize              | int                 | 否    | <p>写缓冲区的大小，不填不会给http.DefaultTransport.(\*http.Transport).WriteBufferSize赋值，此时采用默认的<code>4KB</code></p><p>当其值大于0时，会采用该值</p>                                                                                                         |
| QueueCapacity                | int                 | 否    | 内部channel的缓存大小，默认为1024                                                                                                                                                                                                             |
| Authenticator                | Struct              | 否    | 鉴权扩展插件配置                                                                                                                                                                                                                           |
| Authenticator.Type           | string              | 否    | 鉴权扩展插件类型                                                                                                                                                                                                                           |
| Authenticator.Options        | Map\<string,Struct> | 否    | 鉴权扩展插件配置内容                                                                                                                                                                                                                         |
| AsyncIntercept               | bool                | 否    | 异步过滤数据，默认为否                                                                                                                                                                                                                        |
| DropEventWhenQueueFull       | bool                | 否    | 当队列满时是否丢弃数据，否则需要等待，默认为不丢弃                                                                                                                                                                                                          |
| Compression                  | string              | 否    | 压缩策略，目前支持gzip和snappy，默认不开启                                                                                                                                                                                                         |

## 样例

采集`/home/test-log/`路径下的所有文件名匹配`*.log`规则的文件，并将采集结果以 `custom_single` 协议、`json`格式提交到 `http://localhost:8086/write`。 且提交时，附加 header x-filepath，其值使用log中的 **Tag**:**path** 的值

```yaml
enable: true
inputs:
  - Type: input_file
    FilePaths: 
      - /home/test-log/*.log
flushers:
  - Type: flusher_http
    RemoteURL: "http://localhost:8086/write"
    Headers:
      x-filepath: "%{tag.__path__}"
    Convert:
      Protocol: custom_single
      Encoding: json
```

采集Docker日志，并将采集结果以`jsonline`协议发送到`http://localhost:9428/insert/jsonline`。

```yaml
enable: true
inputs:
  - Type: service_docker_stdout
    Stderr: true
    Stdout: true
processors:
  - Type: processor_json
    SourceKey: content
    KeepSource: true
    ExpandDepth: 1
    ExpandConnector: ""
    KeepSourceIfParseError: true
flushers:
  - Type: flusher_http
    RemoteURL: http://localhsot:9428/insert/jsonline
    QueueCapacity: 64
    Convert:
      Protocol: jsonline
      Encoding: json
```

需要注意的是，由于使用`jsonline`协议（会将日志的content和tag打平），所以仅支持使用`json`格式进行提交。 由于`jsonline`默认会批量提交日志，所以建议调低`QueueCapacity`，避免在日志量较大的情况下，发生内存占用过多或OOM的问题。

采集Prometheus指标，并将指标以Prometheus协议发送到`PROMETHEUS_REMOTEWRITE_ADDRESS`。 这里用到了`ext_default_encoder`插件，该插件可以配置使用Prometheus Encoder，从而支持将采集到的数据转换为Prometheus协议。

```yaml
enable: true
global:
  StructureType: v2
inputs:
- Type: service_prometheus
  ConfigFilePath: '/etc/prometheus/prometheus.yml'
flushers:
- Type: flusher_http
  RemoteURL: 'http://PROMETHEUS_REMOTEWRITE_ADDRESS/api/v1/write'
  Concurrency: 10
  QueueCapacity: 4096
  DropEventWhenQueueFull: true
  Encoder:
    Type: ext_default_encoder
    Format: 'prometheus'
    SeriesLimit: 1024
  Authenticator:
    Type: ext_basicauth
  Compression: 'snappy'
extensions:
- Type: ext_basicauth
  Username: 'YOUR_USERNAME'
  Password: 'YOUR_PASSWORD'
```
