
com.riskalyze.alerts.drivers.opsgenie.OpsGenieAdapter.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alerts-kotlin Show documentation
Show all versions of alerts-kotlin Show documentation
This alerts module is used to standardize alerting for JVM-based application. It uses a modular driver system to allow easily swapping out alerting services.
The newest version!
package com.riskalyze.alerts.drivers.opsgenie
import com.kstruct.gethostname4j.Hostname
import com.riskalyze.alerts.drivers.opsgenie.dtos.OpsGenieAlertDTO
import com.riskalyze.alerts.drivers.opsgenie.dtos.OpsGenieAlertResponseDTO
import com.riskalyze.alerts.enumerations.Priority
import com.riskalyze.alerts.enumerations.Priority.*
import com.riskalyze.alerts.models.Alert
import com.riskalyze.alerts.models.AlertResponse
internal class OpsGenieAdapter {
fun convertAlertToDTO(alert: Alert): OpsGenieAlertDTO {
val hostname = alert.hostname ?: Hostname.getHostname()
val appName = alert.appName ?: determineAppName(hostname)
val alias = alert.alias ?: generateAlias(appName, alert.message)
return OpsGenieAlertDTO(
message = alert.message,
priority = mapToOpsGeniePriority(alert.priority),
description = alert.description,
alias = alias,
tags = alert.tags,
source = hostname,
entity = appName
)
}
fun mapToOpsGeniePriority(priority: Priority): String = when (priority) {
INFO -> "P5"
LOW -> "P4"
MODERATE -> "P3"
HIGH -> "P2"
CRITICAL -> "P1"
}
fun convertResponse(response: OpsGenieAlertResponseDTO) = AlertResponse(response.requestId, response.took)
private fun determineAppName(hostname: String) = when {
isRiskalyzeHostname(hostname) -> {
// Converts "east-pronode-web-prod-1a" to "pronode-web"
hostname.split("-").subList(1, 3).joinToString("-")
}
else -> hostname
}
private fun generateAlias(appName: String, message: String): String {
val generatedAlias = message
.replace("[^\\w\\s]+".toRegex(), "")
.replace("\\s+".toRegex(), "-")
.trim('-')
.toLowerCase()
return if (generatedAlias.isBlank()) appName else "$appName-$generatedAlias"
}
/**
* Expected standard format is "region-slug-type-env-series"
* For example: east-pronode-web-prod-1a
*/
private fun isRiskalyzeHostname(hostname: String) = hostname.count { it == '-' } == 4
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy