def process()

in src/main/scala/com/gu/elasticsearchmonitor/Lambda.scala [68:96]


  def process(env: Env, logger: LambdaLogger): Unit = {
    def resolveMasterHostName(masterInfo: MasterInformation): Either[String, String] =
      if (env.stage == "DEV") {
        logger.log(s"would have resolved with master node ${masterInfo.aRandomMasterUrl}, but forcing back to localhost as the lambda is running locally", LogLevel.INFO)
        Right("http://localhost:8000")
      } else Either.cond(masterInfo.aRandomMasterUrl.isDefined, masterInfo.aRandomMasterUrl.get, "No Master node!")

    def fetchAndSendMetrics = for {
      masterInfo <- masterDetector.detectMasters(env, logger)
    } yield {
      val masterMetrics = cloudwatchMetrics.buildMasterMetricData(env.clusterName, masterInfo)
      val clusterMetrics = for {
        masterHostName <- resolveMasterHostName(masterInfo)
        clusterHealth <- ClusterHealth.fetchAndParse(masterHostName, httpClient, mapper, logger)
        nodeStats <- NodeStats.fetchAndParse(masterHostName, httpClient, mapper, logger)
      } yield {
        cloudwatchMetrics.buildMetricData(env.clusterName, clusterHealth, nodeStats)
      }
      clusterMetrics.left.foreach(error => logger.log(s"Couldn't fetch cluster health: $error", LogLevel.ERROR))

      val allMetrics = masterMetrics ++ clusterMetrics.getOrElse(Nil)
      cloudwatchMetrics.sendMetrics(env.clusterName, allMetrics, logger)
    }

    Try(fetchAndSendMetrics) match {
      case Success(_) => logger.log(s"Successfully finished to send metrics to cloudwatch", LogLevel.INFO)
      case Failure(e) => logger.log(s"Unable to finish processing the metrics: $e", LogLevel.ERROR)
    }
  }