All Downloads are FREE. Search and download functionalities are using the official Maven repository.

team.supernova.graphite.reader.event.stream.flow.ClusterMetricsQueryBuilder.scala Maven / Gradle / Ivy

The newest version!
package team.supernova.graphite.reader.event.stream.flow

import akka.actor.ActorSystem
import akka.event.{Logging, LoggingAdapter}
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Flow
import com.trueaccord.scalapb.TextFormat
import org.owasp.encoder.Encode.forUriComponent
import team.supernova.graph.messages.GraphiteEvent
import team.supernova.graphite.reader.stream.MissingValueException

class ClusterMetricsQueryBuilder(actorSystem: ActorSystem) {

  implicit val system = actorSystem
  implicit val materializer = ActorMaterializer.create(system)
  private val log: LoggingAdapter = Logging.getLogger(system, this)

  val clusterMetricsMessageTypes = system.settings.config.getStringList("hubble.reader.event.graphite.message.types.cluster")

  private def validate(field: String, fieldName: String, ge: GraphiteEvent): GraphiteEvent = {
    forUriComponent(field)
    if (field.nonEmpty) ge
    else {
      log.error(s"discarding GraphiteEvent, since mandatory '$fieldName' field is missing - ${TextFormat.printToSingleLineUnicodeString(ge)} ")
      throw MissingValueException(fieldName, ge)
    }
  }

  val flow =
    Flow[GraphiteEvent]
      .map {
        case ge@GraphiteEvent(clusterName, _, dataCenterName, serverName, _, _, _, metricType, metricValue) =>
          validate(serverName, "serverName", ge)
          validate(dataCenterName, "dataCenterName", ge)
          validate(clusterName, "clusterName", ge)
          validate(metricType, "metricType", ge)
          validate(metricValue, "metricValue", ge)
        case x => throw new RuntimeException(s"GraphiteEvent expected. received $x")
      }
      .map {
        case GraphiteEvent(clusterName, _, dataCenterName, serverName, _, _, _, metricType, metricValue) =>
          s"""MERGE (cm:clusterMetric {name: '${forUriComponent(metricType)}'})
             |MERGE (cms:clusterMetrics {name: '${forUriComponent(clusterName)}'})
             |MERGE (cms)-[:HAS_CLMETRIC]->(cm)
             |MERGE (c:cluster {name: '${forUriComponent(clusterName)}'})
             |MERGE (c)-[:HAS_CLMETRICS]->(cms)
             |MERGE (n:node {name: '${forUriComponent(serverName)}'})
             |MERGE (c)-[:HAS_HOST]->(n)
             |MERGE (dc:dc {name: '${forUriComponent(dataCenterName)}'})
             |MERGE (n)-[:IS_HOSTED_IN]->(dc)
             |SET cm.value = '${forUriComponent(metricValue)}'""".stripMargin
        case x => throw new RuntimeException(s"GraphiteEvent expected. received $x")
      }
}

object ClusterMetricsQueryBuilder {
  def apply(system: ActorSystem) = new ClusterMetricsQueryBuilder(system)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy