-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
551 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,5 @@ services: | |
SMTP_FROM_ADDRESS: '[email protected]' | ||
SMTP_TO_ADDRESS: '[email protected]' | ||
SMTP_TRANSPORT_STRATEGY: 'SMTP_TLS' | ||
ENABLE_SLACK_EVENT_HANDLER: 'true' | ||
SLACK_WEBHOOK_URL: 'https://your.slack-webhook.url' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,5 @@ data: | |
smtp_from_address: "[email protected]" | ||
smtp_to_address: "[email protected]" | ||
smtp_transport_strategy: "SMTP_TLS" | ||
slack_event_handler_enabled: "true" | ||
slack_webhook_url: "https://your.slack-webhook.url" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/kuvaszuptime/kuvasz/config/handlers/SlackEventHandlerConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.kuvaszuptime.kuvasz.config.handlers | ||
|
||
import com.kuvaszuptime.kuvasz.models.dto.Validation.URI_REGEX | ||
import io.micronaut.context.annotation.ConfigurationProperties | ||
import io.micronaut.core.annotation.Introspected | ||
import javax.inject.Singleton | ||
import javax.validation.constraints.Pattern | ||
|
||
@ConfigurationProperties("handler-config.slack-event-handler") | ||
@Singleton | ||
@Introspected | ||
class SlackEventHandlerConfig { | ||
@Pattern(regexp = URI_REGEX) | ||
var webhookUrl: String? = null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/kotlin/com/kuvaszuptime/kuvasz/handlers/SlackEventHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.kuvaszuptime.kuvasz.handlers | ||
|
||
import com.kuvaszuptime.kuvasz.models.MonitorDownEvent | ||
import com.kuvaszuptime.kuvasz.models.MonitorUpEvent | ||
import com.kuvaszuptime.kuvasz.models.SlackWebhookMessage | ||
import com.kuvaszuptime.kuvasz.models.UptimeMonitorEvent | ||
import com.kuvaszuptime.kuvasz.models.runWhenStateChanges | ||
import com.kuvaszuptime.kuvasz.models.toEmoji | ||
import com.kuvaszuptime.kuvasz.models.toStructuredMessage | ||
import com.kuvaszuptime.kuvasz.services.EventDispatcher | ||
import com.kuvaszuptime.kuvasz.services.SlackWebhookService | ||
import io.micronaut.context.annotation.Context | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.http.HttpResponse | ||
import io.micronaut.http.client.exceptions.HttpClientResponseException | ||
import io.micronaut.scheduling.TaskExecutors | ||
import io.micronaut.scheduling.annotation.ExecuteOn | ||
import io.reactivex.Flowable | ||
import org.slf4j.LoggerFactory | ||
import javax.inject.Inject | ||
|
||
@Context | ||
@Requires(property = "handler-config.slack-event-handler.enabled", value = "true") | ||
class SlackEventHandler @Inject constructor( | ||
private val slackWebhookService: SlackWebhookService, | ||
private val eventDispatcher: EventDispatcher | ||
) { | ||
companion object { | ||
private val logger = LoggerFactory.getLogger(SlackEventHandler::class.java) | ||
} | ||
|
||
init { | ||
subscribeToEvents() | ||
} | ||
|
||
@ExecuteOn(TaskExecutors.IO) | ||
private fun subscribeToEvents() { | ||
eventDispatcher.subscribeToMonitorUpEvents { event -> | ||
logger.debug("A MonitorUpEvent has been received for monitor with ID: ${event.monitor.id}") | ||
event.runWhenStateChanges { slackWebhookService.sendMessage(it.toSlackMessage()).handleResponse() } | ||
} | ||
eventDispatcher.subscribeToMonitorDownEvents { event -> | ||
logger.debug("A MonitorDownEvent has been received for monitor with ID: ${event.monitor.id}") | ||
event.runWhenStateChanges { slackWebhookService.sendMessage(it.toSlackMessage()).handleResponse() } | ||
} | ||
} | ||
|
||
private fun UptimeMonitorEvent.toSlackMessage() = SlackWebhookMessage(text = "${toEmoji()} ${toMessage()}") | ||
|
||
private fun Flowable<HttpResponse<String>>.handleResponse() = | ||
subscribe( | ||
{ | ||
logger.debug("A Slack message to your configured webhook has been successfully sent") | ||
}, | ||
{ ex -> | ||
if (ex is HttpClientResponseException) { | ||
val responseBody = ex.response.getBody(String::class.java) | ||
logger.error("Slack message cannot be sent to your configured webhook: $responseBody") | ||
} | ||
} | ||
) | ||
|
||
private fun UptimeMonitorEvent.toMessage() = | ||
when (this) { | ||
is MonitorUpEvent -> toStructuredMessage().let { details -> | ||
listOfNotNull( | ||
"*${details.summary}*", | ||
"_${details.latency}_", | ||
details.previousDownTime.orNull() | ||
) | ||
} | ||
is MonitorDownEvent -> toStructuredMessage().let { details -> | ||
listOfNotNull( | ||
"*${details.summary}*", | ||
"_${details.error}_", | ||
details.previousUpTime.orNull() | ||
) | ||
} | ||
}.joinToString("\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.kuvaszuptime.kuvasz.models | ||
|
||
object Emoji { | ||
const val ALERT = "🚨" | ||
const val CHECK_OK = "✅" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/kuvaszuptime/kuvasz/models/SlackWebhookMessage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.kuvaszuptime.kuvasz.models | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import java.net.URI | ||
|
||
data class SlackWebhookMessage( | ||
val username: String = "KuvaszBot", | ||
@JsonProperty("icon_url") | ||
val iconUrl: URI = URI(ICON_URL), | ||
val text: String | ||
) { | ||
companion object { | ||
const val ICON_URL = "https://raw.githubusercontent.com/kuvasz-uptime/kuvasz/main/docs/kuvasz_avatar.png" | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/com/kuvaszuptime/kuvasz/models/dto/Validation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package com.kuvaszuptime.kuvasz.models.dto | ||
|
||
internal object Validation { | ||
object Validation { | ||
const val MIN_UPTIME_CHECK_INTERVAL = 60L | ||
const val URI_REGEX = "^(https?)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]" | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/kotlin/com/kuvaszuptime/kuvasz/services/SlackWebhookService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.kuvaszuptime.kuvasz.services | ||
|
||
import com.kuvaszuptime.kuvasz.config.handlers.SlackEventHandlerConfig | ||
import com.kuvaszuptime.kuvasz.models.SlackWebhookMessage | ||
import io.micronaut.context.event.ShutdownEvent | ||
import io.micronaut.core.type.Argument | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.HttpResponse | ||
import io.micronaut.http.client.RxHttpClient | ||
import io.micronaut.runtime.event.annotation.EventListener | ||
import io.reactivex.Flowable | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class SlackWebhookService @Inject constructor( | ||
private val slackEventHandlerConfig: SlackEventHandlerConfig, | ||
private val httpClient: RxHttpClient | ||
) { | ||
|
||
companion object { | ||
private const val RETRY_COUNT = 3L | ||
} | ||
|
||
fun sendMessage(message: SlackWebhookMessage): Flowable<HttpResponse<String>> { | ||
val request: HttpRequest<SlackWebhookMessage> = HttpRequest.POST(slackEventHandlerConfig.webhookUrl, message) | ||
|
||
return httpClient | ||
.exchange(request, Argument.STRING, Argument.STRING) | ||
.retry(RETRY_COUNT) | ||
} | ||
|
||
@EventListener | ||
@Suppress("UNUSED_PARAMETER") | ||
internal fun onShutdownEvent(event: ShutdownEvent) { | ||
httpClient.close() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.