/core/monitoring/user-events/src/main/scala/org/apache/openwhisk/core/monitoring/metrics/OpenWhiskEvents.scala

https://github.com/apache/incubator-openwhisk · Scala · 73 lines · 44 code · 13 blank · 16 comment · 2 complexity · f6a59796cce9784dc4e6147913f09cb9 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.openwhisk.core.monitoring.metrics
  18. import akka.actor.{ActorSystem, CoordinatedShutdown}
  19. import akka.event.slf4j.SLF4JLogging
  20. import akka.http.scaladsl.Http
  21. import akka.kafka.ConsumerSettings
  22. import akka.stream.ActorMaterializer
  23. import com.typesafe.config.Config
  24. import kamon.Kamon
  25. import kamon.prometheus.PrometheusReporter
  26. import org.apache.kafka.common.serialization.StringDeserializer
  27. import pureconfig._
  28. import pureconfig.generic.auto._
  29. import scala.concurrent.duration.FiniteDuration
  30. import scala.concurrent.{ExecutionContext, Future}
  31. object OpenWhiskEvents extends SLF4JLogging {
  32. case class MetricConfig(port: Int,
  33. enableKamon: Boolean,
  34. ignoredNamespaces: Set[String],
  35. renameTags: Map[String, String],
  36. retry: RetryConfig)
  37. case class RetryConfig(minBackoff: FiniteDuration, maxBackoff: FiniteDuration, randomFactor: Double, maxRestarts: Int)
  38. def start(config: Config)(implicit system: ActorSystem,
  39. materializer: ActorMaterializer): Future[Http.ServerBinding] = {
  40. implicit val ec: ExecutionContext = system.dispatcher
  41. val prometheusReporter = new PrometheusReporter()
  42. Kamon.registerModule("prometheus", prometheusReporter)
  43. Kamon.init(config)
  44. val metricConfig = loadConfigOrThrow[MetricConfig](config, "whisk.user-events")
  45. val prometheusRecorder = PrometheusRecorder(prometheusReporter, metricConfig)
  46. val recorders = if (metricConfig.enableKamon) Seq(prometheusRecorder, KamonRecorder) else Seq(prometheusRecorder)
  47. val eventConsumer = EventConsumer(eventConsumerSettings(defaultConsumerConfig(config)), recorders, metricConfig)
  48. CoordinatedShutdown(system).addTask(CoordinatedShutdown.PhaseBeforeServiceUnbind, "shutdownConsumer") { () =>
  49. eventConsumer.shutdown()
  50. }
  51. val port = metricConfig.port
  52. val api = new PrometheusEventsApi(eventConsumer, prometheusRecorder)
  53. val httpBinding = Http().bindAndHandle(api.routes, "0.0.0.0", port)
  54. httpBinding.foreach(_ => log.info(s"Started the http server on http://localhost:$port"))(system.dispatcher)
  55. httpBinding
  56. }
  57. def eventConsumerSettings(config: Config): ConsumerSettings[String, String] =
  58. ConsumerSettings(config, new StringDeserializer, new StringDeserializer)
  59. def defaultConsumerConfig(globalConfig: Config): Config = globalConfig.getConfig("akka.kafka.consumer")
  60. }