Skip to content

Commit 95b1407

Browse files
authored
Restore Watcher execute watch request examples (#4754)
1 parent 58fbe0a commit 95b1407

File tree

2 files changed

+308
-0
lines changed

2 files changed

+308
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
summary: Run a watch with multiple action modes
2+
method_request: POST _watcher/watch/my_watch/_execute
3+
description: >
4+
Run `POST _watcher/watch/my_watch/_execute` and set a different mode for each action.
5+
# type: request
6+
value: |-
7+
{
8+
"action_modes" : {
9+
"action1" : "force_simulate",
10+
"action2" : "skip"
11+
}
12+
}
13+
alternatives:
14+
- language: Python
15+
code: |-
16+
resp = client.watcher.execute_watch(
17+
id="my_watch",
18+
action_modes={
19+
"action1": "force_simulate",
20+
"action2": "skip"
21+
},
22+
)
23+
- language: JavaScript
24+
code: |-
25+
const response = await client.watcher.executeWatch({
26+
id: "my_watch",
27+
action_modes: {
28+
action1: "force_simulate",
29+
action2: "skip",
30+
},
31+
});
32+
- language: Ruby
33+
code: |-
34+
response = client.watcher.execute_watch(
35+
id: "my_watch",
36+
body: {
37+
"action_modes": {
38+
"action1": "force_simulate",
39+
"action2": "skip"
40+
}
41+
}
42+
)
43+
- language: PHP
44+
code: |-
45+
$resp = $client->watcher()->executeWatch([
46+
"id" => "my_watch",
47+
"body" => [
48+
"action_modes" => [
49+
"action1" => "force_simulate",
50+
"action2" => "skip",
51+
],
52+
],
53+
]);
54+
- language: curl
55+
code:
56+
'curl -X POST -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d
57+
''{"action_modes":{"action1":"force_simulate","action2":"skip"}}''
58+
"$ELASTICSEARCH_URL/_watcher/watch/my_watch/_execute"'
59+
- language: Java
60+
code: |
61+
client.watcher().executeWatch(e -> e
62+
.actionModes(Map.of("action1", ActionExecutionMode.ForceSimulate,"action2", ActionExecutionMode.Skip))
63+
.id("my_watch")
64+
);
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
summary: Run a watch inline
2+
method_request: POST _watcher/watch/_execute
3+
description: >
4+
Run `POST _watcher/watch/_execute` to run a watch inline. All other settings for this API still apply when inlining a watch. In
5+
this example, while the inline watch defines a compare condition, during the execution this condition will be ignored.
6+
# type: request
7+
value: |-
8+
{
9+
"watch" : {
10+
"trigger" : { "schedule" : { "interval" : "10s" } },
11+
"input" : {
12+
"search" : {
13+
"request" : {
14+
"indices" : [ "logs" ],
15+
"body" : {
16+
"query" : {
17+
"match" : { "message": "error" }
18+
}
19+
}
20+
}
21+
}
22+
},
23+
"condition" : {
24+
"compare" : { "ctx.payload.hits.total" : { "gt" : 0 }}
25+
},
26+
"actions" : {
27+
"log_error" : {
28+
"logging" : {
29+
"text" : "Found {{ctx.payload.hits.total}} errors in the logs"
30+
}
31+
}
32+
}
33+
}
34+
}
35+
alternatives:
36+
- language: Python
37+
code: |-
38+
resp = client.watcher.execute_watch(
39+
watch={
40+
"trigger": {
41+
"schedule": {
42+
"interval": "10s"
43+
}
44+
},
45+
"input": {
46+
"search": {
47+
"request": {
48+
"indices": [
49+
"logs"
50+
],
51+
"body": {
52+
"query": {
53+
"match": {
54+
"message": "error"
55+
}
56+
}
57+
}
58+
}
59+
}
60+
},
61+
"condition": {
62+
"compare": {
63+
"ctx.payload.hits.total": {
64+
"gt": 0
65+
}
66+
}
67+
},
68+
"actions": {
69+
"log_error": {
70+
"logging": {
71+
"text": "Found {{ctx.payload.hits.total}} errors in the logs"
72+
}
73+
}
74+
}
75+
},
76+
)
77+
- language: JavaScript
78+
code: |-
79+
const response = await client.watcher.executeWatch({
80+
watch: {
81+
trigger: {
82+
schedule: {
83+
interval: "10s",
84+
},
85+
},
86+
input: {
87+
search: {
88+
request: {
89+
indices: ["logs"],
90+
body: {
91+
query: {
92+
match: {
93+
message: "error",
94+
},
95+
},
96+
},
97+
},
98+
},
99+
},
100+
condition: {
101+
compare: {
102+
"ctx.payload.hits.total": {
103+
gt: 0,
104+
},
105+
},
106+
},
107+
actions: {
108+
log_error: {
109+
logging: {
110+
text: "Found {{ctx.payload.hits.total}} errors in the logs",
111+
},
112+
},
113+
},
114+
},
115+
});
116+
- language: Ruby
117+
code: |-
118+
response = client.watcher.execute_watch(
119+
body: {
120+
"watch": {
121+
"trigger": {
122+
"schedule": {
123+
"interval": "10s"
124+
}
125+
},
126+
"input": {
127+
"search": {
128+
"request": {
129+
"indices": [
130+
"logs"
131+
],
132+
"body": {
133+
"query": {
134+
"match": {
135+
"message": "error"
136+
}
137+
}
138+
}
139+
}
140+
}
141+
},
142+
"condition": {
143+
"compare": {
144+
"ctx.payload.hits.total": {
145+
"gt": 0
146+
}
147+
}
148+
},
149+
"actions": {
150+
"log_error": {
151+
"logging": {
152+
"text": "Found {{ctx.payload.hits.total}} errors in the logs"
153+
}
154+
}
155+
}
156+
}
157+
}
158+
)
159+
- language: PHP
160+
code: |-
161+
$resp = $client->watcher()->executeWatch([
162+
"body" => [
163+
"watch" => [
164+
"trigger" => [
165+
"schedule" => [
166+
"interval" => "10s",
167+
],
168+
],
169+
"input" => [
170+
"search" => [
171+
"request" => [
172+
"indices" => array(
173+
"logs",
174+
),
175+
"body" => [
176+
"query" => [
177+
"match" => [
178+
"message" => "error",
179+
],
180+
],
181+
],
182+
],
183+
],
184+
],
185+
"condition" => [
186+
"compare" => [
187+
"ctx.payload.hits.total" => [
188+
"gt" => 0,
189+
],
190+
],
191+
],
192+
"actions" => [
193+
"log_error" => [
194+
"logging" => [
195+
"text" => "Found {{ctx.payload.hits.total}} errors in the logs",
196+
],
197+
],
198+
],
199+
],
200+
],
201+
]);
202+
- language: curl
203+
code:
204+
"curl -X POST -H \"Authorization: ApiKey $ELASTIC_API_KEY\" -H \"Content-Type: application/json\" -d
205+
'{\"watch\":{\"trigger\":{\"schedule\":{\"interval\":\"10s\"}},\"input\":{\"search\":{\"request\":{\"indices\":[\"logs\"],\"b\
206+
ody\":{\"query\":{\"match\":{\"message\":\"error\"}}}}}},\"condition\":{\"compare\":{\"ctx.payload.hits.total\":{\"gt\":0}}},\
207+
\"actions\":{\"log_error\":{\"logging\":{\"text\":\"Found {{ctx.payload.hits.total}} errors in the logs\"}}}}}'
208+
\"$ELASTICSEARCH_URL/_watcher/watch/_execute\""
209+
- language: Java
210+
code: |
211+
client.watcher().executeWatch(e -> e
212+
.watch(w -> w
213+
.actions("log_error", a -> a
214+
.logging(l -> l
215+
.text("Found {{ctx.payload.hits.total}} errors in the logs")
216+
)
217+
)
218+
.condition(c -> c
219+
.compare(NamedValue.of("ctx.payload.hits.total",Pair.of(ConditionOp.Gt,FieldValue.of(0))))
220+
)
221+
.input(i -> i
222+
.search(s -> s
223+
.request(r -> r
224+
.body(b -> b
225+
.query(q -> q
226+
.match(m -> m
227+
.field("message")
228+
.query(FieldValue.of("error"))
229+
)
230+
)
231+
)
232+
.indices("logs")
233+
)
234+
)
235+
)
236+
.trigger(t -> t
237+
.schedule(sc -> sc
238+
.interval(in -> in
239+
.time("10s")
240+
)
241+
)
242+
)
243+
)
244+
);

0 commit comments

Comments
 (0)