Skip to content

Commit

Permalink
fix: Sentry examples and default payload (#116)
Browse files Browse the repository at this point in the history
* fix: Sentry examples and default payload

* fix: Apply suggestions from code review

Co-authored-by: Anna Geller <[email protected]>

---------

Co-authored-by: Anna Geller <[email protected]>
  • Loading branch information
loicmathieu and anna-geller authored Dec 20, 2023
1 parent 457b99e commit 989d9bd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package io.kestra.plugin.notifications.sentry;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.kestra.core.models.annotations.Example;
import io.kestra.core.models.annotations.Plugin;
import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.models.tasks.Task;
import io.kestra.core.models.tasks.VoidOutput;
import io.kestra.core.runners.RunContext;
import io.kestra.core.serializers.JacksonMapper;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.netty.DefaultHttpClient;
import io.swagger.v3.oas.annotations.media.Schema;
Expand All @@ -20,11 +18,6 @@

import javax.validation.constraints.NotBlank;
import java.net.URI;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@SuperBuilder
@ToString
Expand Down Expand Up @@ -74,6 +67,9 @@
"level": "error",
"transaction": "/execution/id/{{ execution.id }}",
"server_name": "localhost:8080",
"message": {
"message": "Execution {{ execution.id }} failed"
},
"extra": {
"Namespace": "{{ flow.namespace }}",
"Flow ID": "{{ flow.id }}",
Expand All @@ -85,11 +81,25 @@
}
)
public class SentryAlert extends Task implements RunnableTask<VoidOutput> {

public static final String SENTRY_VERSION = "7";
public static final String SENTRY_CLIENT = "java";
public static final String SENTRY_DSN_REGEXP = "^(https?://[a-f0-9]+@o[0-9]+\\.ingest\\.sentry\\.io/[0-9]+)$";

private static final String DEFAULT_PAYLOAD = """
{
"timestamp": "{{ execution.startDate }}",
"platform": "java",
"level": "error",
"message": {
"message": "Execution {{ execution.id }} failed"
},
"extra": {
"Namespace": "{{ flow.namespace }}",
"Flow ID": "{{ flow.id }}",
"Execution ID": "{{ execution.id }}"
}
}""";

@Schema(
title = "Sentry DSN"
)
Expand Down Expand Up @@ -123,10 +133,7 @@ public VoidOutput run(RunContext runContext) throws Exception {
}

try (DefaultHttpClient client = new DefaultHttpClient(URI.create(url))) {
if (this.payload == null) {
setDefaultPayload();
}
String payload = runContext.render(this.payload);
String payload = this.payload != null ? runContext.render(this.payload) : runContext.render(DEFAULT_PAYLOAD.strip());

runContext.logger().debug("Sent the following Sentry event: {}", payload);

Expand All @@ -135,20 +142,4 @@ public VoidOutput run(RunContext runContext) throws Exception {

return null;
}

private void setDefaultPayload() throws JsonProcessingException {
Map<String, Object> map = new HashMap<>();

map.put("event_id", UUID.randomUUID().toString().toLowerCase().replace("-", ""));
map.put("timestamp", Instant.now().toString());
map.put("platform", Platform.JAVA.name().toLowerCase());
map.put("level", ErrorLevel.INFO.name().toLowerCase());

map.put("exception", Map.of("values", List.of(Map.of("type", "Kestra Alert"))));
if (this.id != null) {
map.put("extra", Map.of("Execution ID", this.id));
}

this.payload = JacksonMapper.ofJson().writeValueAsString(map);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ The alert message will include a link to the execution page in the UI along with
tasks:
- id: send_alert
type: io.kestra.plugin.notifications.sentry.SentryExecution
executionId: "{{ trigger.executionId} }"
transaction: "/execution/id/{{ trigger.executionId} }"
dsn: "{{ secret('SENTRY_DSN') }}" # format: https://[email protected]/xxx
executionId: "{{ trigger.executionId }}"
transaction: "/execution/id/{{ trigger.executionId }}"
dsn: "{{ secret('SENTRY_DSN') }}"
level: ERROR
executionId: "{{ trigger.executionId }}"
Expand Down

0 comments on commit 989d9bd

Please sign in to comment.