HTTP 输入
Elastic Stack Serverless
使用 http
输入可以向 HTTP 端点提交请求,并在触发监视时将响应加载到监视执行上下文中。 有关所有受支持的属性,请参阅 HTTP 输入属性。
使用 http
输入,您可以
- 查询外部 Elasticsearch 集群。
http
输入提供了一种将搜索请求提交到 Watcher 运行的集群以外的其他集群的方法。 如果您正在运行专用 Watcher 集群或者需要搜索运行不同 Elasticsearch 版本的集群,这将非常有用。 - 查询搜索 API 以外的 Elasticsearch API。 例如,您可能想要从 节点统计、集群健康或 集群状态 API 加载数据。
- 查询外部 Web 服务。
http
输入使您可以从公开 HTTP 端点的任何服务加载数据。 这在 Elasticsearch 集群和其他系统之间提供了一个桥梁。
要查询外部 Elasticsearch 集群,您需要指定集群的 host
和 port
属性,并将索引的搜索端点指定为 path
。 如果省略搜索正文,则请求会返回指定索引中的所有文档
"input" : {
"http" : {
"request" : {
"host" : "example.com",
"port" : 9200,
"path" : "/idx/_search"
}
}
}
您可以使用完整的 Elasticsearch Query DSL 来执行更复杂的搜索。 例如,以下 http
输入检索所有在 category
字段中包含 event
的文档
"input" : {
"http" : {
"request" : {
"host" : "host.domain",
"port" : 9200,
"path" : "/idx/_search",
"body" : "{\"query\" : { \"match\" : { \"category\" : \"event\"}}}"
}
}
}
要从其他 Elasticsearch API 加载数据,请将 API 端点指定为 path
属性。 使用 params
属性来指定查询字符串参数。 例如,以下 http
输入调用 集群统计 API 并启用 human
属性
"input" : {
"http" : {
"request" : {
"host" : "host.domain",
"port" : 9200,
"path" : "/_cluster/stats",
"params" : {
"human" : "true"
}
}
}
}
- 启用此属性会以人类可读的格式返回响应中的
bytes
值。
您可以使用 http
输入从任何外部 Web 服务获取数据。 http
输入支持基本身份验证。 例如,以下输入提供用户名和密码以访问 myservice
"input" : {
"http" : {
"request" : {
"host" : "host.domain",
"port" : 9200,
"path" : "/myservice",
"auth" : {
"basic" : {
"username" : "user",
"password" : "pass"
}
}
}
}
}
您还可以通过 params
属性传入特定于服务的 API 密钥和其他信息。 例如,以下 http
输入从 OpenWeatherMap 服务加载阿姆斯特丹的当前天气预报
"input" : {
"http" : {
"request" : {
"url" : "http://api.openweathermap.org/data/2.5/weather",
"params" : {
"lat" : "52.374031",
"lon" : "4.88969",
"appid" : "<your openweathermap appid>"
}
}
}
}
您还可以使用 Bearer token
而不是基本身份验证来调用 API。 request.headers
对象包含 HTTP 标头
"input" : {
"http" : {
"request" : {
"url": "https://api.example.com/v1/something",
"headers": {
"authorization" : "Bearer ABCD1234...",
"content-type": "application/json"
# other headers params..
},
"connection_timeout": "30s"
}
}
}
http
输入支持模板。 您可以在指定 path
、body
、标头值和参数值时使用模板。
例如,以下代码段使用模板来指定要查询的索引,并将结果限制为过去五分钟内添加的文档
"input" : {
"http" : {
"request" : {
"host" : "host.domain",
"port" : 9200,
"path" : "/{{ctx.watch_id}}/_search",
"body" : "{\"query\" : {\"range\": {\"@timestamp\" : {\"from\": \"{{ctx.trigger.triggered_time}}||-5m\",\"to\": \"{{ctx.trigger.triggered_time}}\"}}}}"
}
}
}
如果响应正文采用 JSON 或 YAML 格式,则会对其进行解析并加载到执行上下文中。 如果响应正文不是 JSON 或 YAML 格式,则会将其加载到有效负载的 _value
字段中。
条件、转换和操作通过执行上下文访问响应数据。 例如,如果响应包含 message
对象,则可以使用 ctx.payload.message
访问消息数据。
此外,还可以使用 ctx.payload._headers
字段访问响应中的所有标头,并使用 ctx.payload._status_code
访问响应的 HTTP 状态代码。
名称 | 必需 | 默认 | 描述 |
---|---|---|---|
request.scheme |
否 | http | URL 方案。 有效值为:http 或 https 。 |
request.host |
是 | - | 要连接的主机。 |
request.port |
是 | - | http 服务正在侦听的端口。 |
request.path |
否 | - | URL 路径。 该路径可以是静态文本,也可以包含 mustache 模板。 URL 查询字符串参数必须通过 request.params 属性指定。 |
request.method |
否 | get | HTTP 方法。 支持的值有:head 、get 、post 、put 和 delete 。 |
request.headers |
否 | - | HTTP 请求标头。 标头值可以是静态文本,也可以包含 mustache 模板。 |
request.params |
否 | - | URL 查询字符串参数。 参数值可以是静态文本,也可以包含 mustache 模板。 |
request.url |
否 | - | 允许您通过指定实际的 URL(例如 https://www.example.org:1234/mypath?foo=bar )来一次性设置 request.scheme 、request.host 、request.port 和 request.params 。 不能与这四个参数中的任何一个组合使用。 由于这些参数已设置,因此单独指定它们可能会覆盖它们。 |
request.auth.basic.username |
否 | - | HTTP 基本身份验证用户名 |
request.auth.basic.password |
否 | - | HTTP 基本身份验证密码 |
request.proxy.host |
否 | - | 连接到主机时要使用的代理主机。 |
request.proxy.port |
否 | - | 连接到主机时要使用的代理端口。 |
request.connection_timeout |
否 | 10s | 建立 http 连接的超时时间。 如果在此时间内无法建立连接,则输入将超时并失败。 |
request.read_timeout |
否 | 10s | 从 http 连接读取数据的超时时间。 如果在此时间内未收到任何响应,则输入将超时并失败。 |
request.body |
否 | - | HTTP 请求正文。 正文可以是静态文本,也可以包含 mustache 模板。 |
extract |
否 | - | 要从输入响应中提取并用作有效负载的 JSON 键数组。 如果输入生成较大的响应,则可以使用此选项来过滤响应的相关部分以用作有效负载。 |
response_content_type |
否 | json | 响应正文将包含的预期内容类型。 支持的值有 json 、yaml 和 text 。 如果格式为 text ,则 extract 属性不能存在。 请注意,这将覆盖 HTTP 响应中返回的标头。 如果此项设置为 text ,则响应的正文将被分配给有效负载的 _value 变量并可通过该变量访问。 |
在指定 path
、params
、headers
和 body
值时,您可以引用执行上下文中的以下变量
名称 | 描述 |
---|---|
ctx.watch_id |
当前正在执行的监视的 ID。 |
ctx.execution_time |
此监视开始执行的时间。 |
ctx.trigger.triggered_time |
触发此监视的时间。 |
ctx.trigger.scheduled_time |
计划触发此监视的时间。 |
ctx.metadata.* |
与监视关联的任何元数据。 |