From 748a20c705abdbd275ef0f7423649f21153a07f4 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 26 Jan 2021 09:44:44 +0100 Subject: [PATCH 1/4] Extending the alerting API to reload kapacitor tasks due to changed alerting rules --- .../kapacitor/KapacitorTaskController.java | 18 +- .../rest/alert/kapacitor/model/Task.java | 12 ++ .../KapacitorTaskControllerTest.java | 178 +++++++----------- 3 files changed, 84 insertions(+), 124 deletions(-) diff --git a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskController.java b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskController.java index 1f97de6d88..006d4fb481 100644 --- a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskController.java +++ b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskController.java @@ -25,9 +25,7 @@ public KapacitorTaskController(InspectitServerSettings settings) { @ApiOperation(value = "Provides a list with basic information about each kapacitor task. Only tasks based on templates will be listed.") @GetMapping("/alert/kapacitor/tasks") public List getAllTasks() { - ObjectNode response = kapacitor() - .getForEntity("/kapacitor/v1/tasks", ObjectNode.class) - .getBody(); + ObjectNode response = kapacitor().getForEntity("/kapacitor/v1/tasks", ObjectNode.class).getBody(); return StreamSupport.stream(response.path("tasks").spliterator(), false) .map(Task::fromKapacitorResponse) @@ -39,8 +37,7 @@ public List getAllTasks() { @GetMapping("/alert/kapacitor/tasks/{taskId}") public Task getTask(@PathVariable @ApiParam("The id of the task to query") String taskId) { - ObjectNode response = kapacitor() - .getForEntity("/kapacitor/v1/tasks/{taskId}", ObjectNode.class, taskId) + ObjectNode response = kapacitor().getForEntity("/kapacitor/v1/tasks/{taskId}", ObjectNode.class, taskId) .getBody(); return Task.fromKapacitorResponse(response); @@ -50,8 +47,7 @@ public Task getTask(@PathVariable @ApiParam("The id of the task to query") Strin @ApiOperation(value = "Inserts a new Kapacitor task") @PostMapping("/alert/kapacitor/tasks") public Task addTask(@RequestBody Task task) { - ObjectNode response = kapacitor() - .postForEntity("/kapacitor/v1/tasks", task.toKapacitorRequest(), ObjectNode.class) + ObjectNode response = kapacitor().postForEntity("/kapacitor/v1/tasks", task.toKapacitorRequest(), ObjectNode.class) .getBody(); return Task.fromKapacitorResponse(response); @@ -60,10 +56,10 @@ public Task addTask(@RequestBody Task task) { @Secured(UserRoleConfiguration.WRITE_ACCESS_ROLE) @ApiOperation(value = "Updates one or more settings of a kapacitor task") @PatchMapping("/alert/kapacitor/tasks/{taskId}") - public Task updateTask(@PathVariable @ApiParam("The id of the task to update") String taskId, - @RequestBody Task task) { - ObjectNode response = kapacitor() - .patchForObject("/kapacitor/v1/tasks/{taskId}", task.toKapacitorRequest(), ObjectNode.class, taskId); + public Task updateTask(@PathVariable @ApiParam("The id of the task to update") String taskId, @RequestBody Task task) { + ObjectNode response = kapacitor().patchForObject("/kapacitor/v1/tasks/{taskId}", task.toKapacitorRequest(), ObjectNode.class, taskId); + ObjectNode responseReload = kapacitor().patchForObject("/kapacitor/v1/templates/{templateID}", task.toKapacitorRequestTemplateUpdate(), ObjectNode.class, task + .getTemplate()); return Task.fromKapacitorResponse(response); } diff --git a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/model/Task.java b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/model/Task.java index 292e333ea1..a39525aed0 100644 --- a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/model/Task.java +++ b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/model/Task.java @@ -108,6 +108,18 @@ public ObjectNode toKapacitorRequest() { } + /** + * @return a JSON-Object which can be used for updating the template in PATCH request to kapacitor + */ + public ObjectNode toKapacitorRequestTemplateUpdate() { + ObjectMapper mapper = new ObjectMapper(); + ObjectNode result = mapper.createObjectNode(); + result.put("id", template); + + return result; + + } + public static Task fromKapacitorResponse(JsonNode task) { Task.TaskBuilder builder = Task.builder() .id(task.path("id").asText()) diff --git a/components/inspectit-ocelot-configurationserver/src/test/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskControllerTest.java b/components/inspectit-ocelot-configurationserver/src/test/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskControllerTest.java index 3725899e35..ae7e968d3f 100644 --- a/components/inspectit-ocelot-configurationserver/src/test/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskControllerTest.java +++ b/components/inspectit-ocelot-configurationserver/src/test/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskControllerTest.java @@ -47,32 +47,27 @@ void tasksListedCorrectly() { assertThat(task.getTemplate()).isEqualTo("my_template"); assertThat(task.getDescription()).isNull(); assertThat(task.getTopic()).isNull(); - assertThat(task.getVars()).containsExactlyInAnyOrder( - TemplateVariable.builder() - .name("some_string") - .type("string") - .value("test") - .description("") - .build(), - TemplateVariable.builder() - .name("some_float") - .type("float") - .value(4242.0) - .description("") - .build(), - TemplateVariable.builder() - .name("some_int") - .type("int") - .value(42.0) - .description("") - .build(), - TemplateVariable.builder() - .name("some_dur") - .type("duration") - .value("2h") - .description("") - .build() - ); + assertThat(task.getVars()).containsExactlyInAnyOrder(TemplateVariable.builder() + .name("some_string") + .type("string") + .value("test") + .description("") + .build(), TemplateVariable.builder() + .name("some_float") + .type("float") + .value(4242.0) + .description("") + .build(), TemplateVariable.builder() + .name("some_int") + .type("int") + .value(42.0) + .description("") + .build(), TemplateVariable.builder() + .name("some_dur") + .type("duration") + .value("2h") + .description("") + .build()); }); assertThat(result).anySatisfy(task -> { @@ -86,14 +81,12 @@ void tasksListedCorrectly() { assertThat(task.getTemplate()).isEqualTo("some_template"); assertThat(task.getDescription()).isEqualTo("My task description"); assertThat(task.getTopic()).isEqualTo("my_topic"); - assertThat(task.getVars()).containsExactlyInAnyOrder( - TemplateVariable.builder() - .name("some_string") - .type("string") - .value("test") - .description("") - .build() - ); + assertThat(task.getVars()).containsExactlyInAnyOrder(TemplateVariable.builder() + .name("some_string") + .type("string") + .value("test") + .description("") + .build()); }); @@ -122,14 +115,12 @@ void conflictingTemplateDefinitions() { assertThat(task.getTemplate()).isEqualTo("other_template"); assertThat(task.getDescription()).isEqualTo("My task description"); assertThat(task.getTopic()).isEqualTo("my_topic"); - assertThat(task.getVars()).containsExactlyInAnyOrder( - TemplateVariable.builder() - .name("some_string") - .type("string") - .value("test") - .description("") - .build() - ); + assertThat(task.getVars()).containsExactlyInAnyOrder(TemplateVariable.builder() + .name("some_string") + .type("string") + .value("test") + .description("") + .build()); mockKapacitor.verify(); } @@ -140,20 +131,11 @@ class AddTasks { @Test void addWithTemplate() { - Task toAdd = Task.builder() - .id("my_task") - .template("blub") - .build(); + Task toAdd = Task.builder().id("my_task").template("blub").build(); mockKapacitor.expect(requestTo("/kapacitor/v1/tasks")) .andExpect(method(HttpMethod.POST)) - .andExpect(content().json( - "{" + - "\"id\" : \"my_task\"," + - "\"template-id\" : \"blub\"," + - "\"vars\" : { \"inspectit_template_reference\" : {\"type\" : \"string\", \"value\" : \"blub\"} }" + - "}" - , true)) + .andExpect(content().json("{" + "\"id\" : \"my_task\"," + "\"template-id\" : \"blub\"," + "\"vars\" : { \"inspectit_template_reference\" : {\"type\" : \"string\", \"value\" : \"blub\"} }" + "}", true)) .andRespond(withSuccess(getTestJson("tasks_conflicting_template.json"), MediaType.APPLICATION_JSON)); Task result = controller.addTask(toAdd); @@ -177,15 +159,7 @@ void addWithDescriptionAndVariable() { mockKapacitor.expect(requestTo("/kapacitor/v1/tasks")) .andExpect(method(HttpMethod.POST)) - .andExpect(content().json( - "{" + - "\"id\" : \"my_task\"," + - "\"vars\" : { " + - "\"inspectit_template_description\" : {\"type\" : \"string\", \"value\" : \"blub\"}, " + - "\"my_dur\" : {\"type\" : \"duration\", \"value\" : 7000000000} " + - "}" + - "}" - , true)) + .andExpect(content().json("{" + "\"id\" : \"my_task\"," + "\"vars\" : { " + "\"inspectit_template_description\" : {\"type\" : \"string\", \"value\" : \"blub\"}, " + "\"my_dur\" : {\"type\" : \"duration\", \"value\" : 7000000000} " + "}" + "}", true)) .andRespond(withSuccess(getTestJson("tasks_conflicting_template.json"), MediaType.APPLICATION_JSON)); Task result = controller.addTask(toAdd); @@ -197,23 +171,11 @@ void addWithDescriptionAndVariable() { @Test void addWithTopicAndStatus() { - Task toAdd = Task.builder() - .id("my_task") - .topic("blub") - .status("disabled") - .build(); + Task toAdd = Task.builder().id("my_task").topic("blub").status("disabled").build(); mockKapacitor.expect(requestTo("/kapacitor/v1/tasks")) .andExpect(method(HttpMethod.POST)) - .andExpect(content().json( - "{" + - "\"id\" : \"my_task\"," + - "\"status\" : \"disabled\"," + - "\"vars\" : { " + - "\"topic\" : {\"type\" : \"string\", \"value\" : \"blub\"} " + - "}" + - "}" - , true)) + .andExpect(content().json("{" + "\"id\" : \"my_task\"," + "\"status\" : \"disabled\"," + "\"vars\" : { " + "\"topic\" : {\"type\" : \"string\", \"value\" : \"blub\"} " + "}" + "}", true)) .andRespond(withSuccess(getTestJson("tasks_conflicting_template.json"), MediaType.APPLICATION_JSON)); Task result = controller.addTask(toAdd); @@ -228,17 +190,16 @@ class UpdateTasks { @Test void changeId() { - Task toAdd = Task.builder() - .id("new_id") - .build(); + Task toAdd = Task.builder().id("new_id").build(); mockKapacitor.expect(requestTo("/kapacitor/v1/tasks/my_task")) .andExpect(method(HttpMethod.PATCH)) - .andExpect(content().json( - "{" + - "\"id\" : \"new_id\"" + - "}" - , true)) + .andExpect(content().json("{" + "\"id\" : \"new_id\"" + "}", true)) + .andRespond(withSuccess(getTestJson("tasks_conflicting_template.json"), MediaType.APPLICATION_JSON)); + + mockKapacitor.expect(requestTo("/kapacitor/v1/templates/")) + .andExpect(content().json("{}")) + .andExpect(method(HttpMethod.PATCH)) .andRespond(withSuccess(getTestJson("tasks_conflicting_template.json"), MediaType.APPLICATION_JSON)); Task result = controller.updateTask("my_task", toAdd); @@ -249,18 +210,16 @@ void changeId() { @Test void changeTemplate() { - Task toAdd = Task.builder() - .template("blub") - .build(); + Task toAdd = Task.builder().template("blub").build(); mockKapacitor.expect(requestTo("/kapacitor/v1/tasks/my_task")) .andExpect(method(HttpMethod.PATCH)) - .andExpect(content().json( - "{" + - "\"template-id\" : \"blub\"," + - "\"vars\" : { \"inspectit_template_reference\" : {\"type\" : \"string\", \"value\" : \"blub\"} }" + - "}" - , true)) + .andExpect(content().json("{" + "\"template-id\" : \"blub\"," + "\"vars\" : { \"inspectit_template_reference\" : {\"type\" : \"string\", \"value\" : \"blub\"} }" + "}", true)) + .andRespond(withSuccess(getTestJson("tasks_conflicting_template.json"), MediaType.APPLICATION_JSON)); + + mockKapacitor.expect(requestTo("/kapacitor/v1/templates/blub")) + .andExpect(content().json("{" + "\"id\" : \"blub\"}")) + .andExpect(method(HttpMethod.PATCH)) .andRespond(withSuccess(getTestJson("tasks_conflicting_template.json"), MediaType.APPLICATION_JSON)); Task result = controller.updateTask("my_task", toAdd); @@ -282,14 +241,12 @@ void changeDescriptionAndVariable() { mockKapacitor.expect(requestTo("/kapacitor/v1/tasks/my_task")) .andExpect(method(HttpMethod.PATCH)) - .andExpect(content().json( - "{" + - "\"vars\" : { " + - "\"inspectit_template_description\" : {\"type\" : \"string\", \"value\" : \"blub\"}, " + - "\"my_dur\" : {\"type\" : \"duration\", \"value\" : 7000000000} " + - "}" + - "}" - , true)) + .andExpect(content().json("{" + "\"vars\" : { " + "\"inspectit_template_description\" : {\"type\" : \"string\", \"value\" : \"blub\"}, " + "\"my_dur\" : {\"type\" : \"duration\", \"value\" : 7000000000} " + "}" + "}", true)) + .andRespond(withSuccess(getTestJson("tasks_conflicting_template.json"), MediaType.APPLICATION_JSON)); + + mockKapacitor.expect(requestTo("/kapacitor/v1/templates/")) + .andExpect(content().json("{}")) + .andExpect(method(HttpMethod.PATCH)) .andRespond(withSuccess(getTestJson("tasks_conflicting_template.json"), MediaType.APPLICATION_JSON)); Task result = controller.updateTask("my_task", toAdd); @@ -300,21 +257,16 @@ void changeDescriptionAndVariable() { @Test void changeTopicAndStatus() { - Task toAdd = Task.builder() - .topic("blub") - .status("disabled") - .build(); + Task toAdd = Task.builder().topic("blub").status("disabled").build(); mockKapacitor.expect(requestTo("/kapacitor/v1/tasks/my_task")) .andExpect(method(HttpMethod.PATCH)) - .andExpect(content().json( - "{" + - "\"status\" : \"disabled\"," + - "\"vars\" : { " + - "\"topic\" : {\"type\" : \"string\", \"value\" : \"blub\"} " + - "}" + - "}" - , true)) + .andExpect(content().json("{" + "\"status\" : \"disabled\"," + "\"vars\" : { " + "\"topic\" : {\"type\" : \"string\", \"value\" : \"blub\"} " + "}" + "}", true)) + .andRespond(withSuccess(getTestJson("tasks_conflicting_template.json"), MediaType.APPLICATION_JSON)); + + mockKapacitor.expect(requestTo("/kapacitor/v1/templates/")) + .andExpect(content().json("{}")) + .andExpect(method(HttpMethod.PATCH)) .andRespond(withSuccess(getTestJson("tasks_conflicting_template.json"), MediaType.APPLICATION_JSON)); Task result = controller.updateTask("my_task", toAdd); From 6c0c23515447440d5bdcfa2cd3ca8c7d8f4464e0 Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 28 Jan 2021 11:12:04 +0100 Subject: [PATCH 2/4] Implemented review comments --- .../rest/alert/kapacitor/KapacitorTaskController.java | 8 ++++++-- .../inspectit/ocelot/rest/alert/kapacitor/model/Task.java | 6 +++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskController.java b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskController.java index 006d4fb481..7cdc161f84 100644 --- a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskController.java +++ b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskController.java @@ -58,8 +58,7 @@ public Task addTask(@RequestBody Task task) { @PatchMapping("/alert/kapacitor/tasks/{taskId}") public Task updateTask(@PathVariable @ApiParam("The id of the task to update") String taskId, @RequestBody Task task) { ObjectNode response = kapacitor().patchForObject("/kapacitor/v1/tasks/{taskId}", task.toKapacitorRequest(), ObjectNode.class, taskId); - ObjectNode responseReload = kapacitor().patchForObject("/kapacitor/v1/templates/{templateID}", task.toKapacitorRequestTemplateUpdate(), ObjectNode.class, task - .getTemplate()); + triggerTaskReload(task); return Task.fromKapacitorResponse(response); } @@ -71,4 +70,9 @@ public void removeTask(@PathVariable @ApiParam("The id of the task to delete") S kapacitorRestTemplate.delete("/kapacitor/v1/tasks/{taskId}", taskId); } + private void triggerTaskReload(Task task) { + String taskTemplateId = task.getTemplate(); + kapacitor().patchForObject("/kapacitor/v1/templates/{templateID}", task.toKapacitorRequestTemplateUpdate(), ObjectNode.class, taskTemplateId); + } + } diff --git a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/model/Task.java b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/model/Task.java index a39525aed0..d582a4b913 100644 --- a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/model/Task.java +++ b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/model/Task.java @@ -60,11 +60,12 @@ public class Task { List vars; + private static final ObjectMapper mapper = new ObjectMapper(); + /** * @return a JSON-Object which can be used for adding or updating this task in POST/PATCH request to kapacitor */ public ObjectNode toKapacitorRequest() { - ObjectMapper mapper = new ObjectMapper(); ObjectNode result = mapper.createObjectNode(); ObjectNode varsNode = mapper.createObjectNode(); if (id != null) { @@ -112,10 +113,9 @@ public ObjectNode toKapacitorRequest() { * @return a JSON-Object which can be used for updating the template in PATCH request to kapacitor */ public ObjectNode toKapacitorRequestTemplateUpdate() { - ObjectMapper mapper = new ObjectMapper(); ObjectNode result = mapper.createObjectNode(); result.put("id", template); - + return result; } From 18bbcb073ad8095737b467d2f168d57184f5b909 Mon Sep 17 00:00:00 2001 From: Patrick Date: Fri, 29 Jan 2021 09:33:00 +0100 Subject: [PATCH 3/4] Implemented review comments --- .../ocelot/rest/alert/kapacitor/KapacitorTaskController.java | 5 +++++ .../inspectit/ocelot/rest/alert/kapacitor/model/Task.java | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskController.java b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskController.java index 7cdc161f84..60b91d7dc6 100644 --- a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskController.java +++ b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskController.java @@ -70,6 +70,11 @@ public void removeTask(@PathVariable @ApiParam("The id of the task to delete") S kapacitorRestTemplate.delete("/kapacitor/v1/tasks/{taskId}", taskId); } + /** + * Required to reload kapacitor tasks during runtime. + * + * @param task kapacitor task + */ private void triggerTaskReload(Task task) { String taskTemplateId = task.getTemplate(); kapacitor().patchForObject("/kapacitor/v1/templates/{templateID}", task.toKapacitorRequestTemplateUpdate(), ObjectNode.class, taskTemplateId); diff --git a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/model/Task.java b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/model/Task.java index d582a4b913..3b9c23b90d 100644 --- a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/model/Task.java +++ b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/model/Task.java @@ -25,6 +25,8 @@ @NoArgsConstructor public class Task { + private static final ObjectMapper mapper = new ObjectMapper(); + String id; /** @@ -60,8 +62,6 @@ public class Task { List vars; - private static final ObjectMapper mapper = new ObjectMapper(); - /** * @return a JSON-Object which can be used for adding or updating this task in POST/PATCH request to kapacitor */ From e4d6b0b217f00af2a651bf52744acf2e41b95686 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 2 Feb 2021 07:43:09 +0100 Subject: [PATCH 4/4] added more information about reloading tasks --- .../ocelot/rest/alert/kapacitor/KapacitorTaskController.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskController.java b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskController.java index 60b91d7dc6..7ba0029511 100644 --- a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskController.java +++ b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/rest/alert/kapacitor/KapacitorTaskController.java @@ -72,6 +72,9 @@ public void removeTask(@PathVariable @ApiParam("The id of the task to delete") S /** * Required to reload kapacitor tasks during runtime. + * A task cannot be reloaded and just doing patch request against the template API - without changing it - + * triggers kapacitor to reload the previously changed task. + * Note that all tasks created from this template will be reloaded. * * @param task kapacitor task */