-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ID-1276 Introduce Bard Service for sending metrics (#7434)
Co-authored-by: Tristan Garwood <[email protected]> Co-authored-by: Adam Nichols <[email protected]> Co-authored-by: Janet Gainer-Dewar <[email protected]>
- Loading branch information
1 parent
ea67a13
commit 748f488
Showing
30 changed files
with
720 additions
and
32 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
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
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
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
13 changes: 13 additions & 0 deletions
13
services/src/main/scala/cromwell/services/metrics/bard/BardConfig.scala
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,13 @@ | ||
package cromwell.services.metrics.bard | ||
|
||
import com.typesafe.config.Config | ||
import net.ceedubs.ficus.Ficus._ | ||
|
||
final case class BardConfig(enabled: Boolean, baseUrl: String, connectionPoolSize: Int) | ||
|
||
object BardConfig { | ||
def apply(config: Config): BardConfig = BardConfig(config.as[Boolean]("enabled"), | ||
config.as[String]("bard.base-url"), | ||
config.as[Int]("bard.connection-pool-size") | ||
) | ||
} |
13 changes: 13 additions & 0 deletions
13
services/src/main/scala/cromwell/services/metrics/bard/BardEventing.scala
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,13 @@ | ||
package cromwell.services.metrics.bard | ||
|
||
import cromwell.services.ServiceRegistryActor.ServiceRegistryMessage | ||
import cromwell.services.metrics.bard.model.BardEvent | ||
|
||
object BardEventing { | ||
sealed trait BardEventingMessage extends ServiceRegistryMessage { | ||
override def serviceName: String = "BardEventing" | ||
} | ||
|
||
case class BardEventRequest(event: BardEvent) extends BardEventingMessage | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
services/src/main/scala/cromwell/services/metrics/bard/BardService.scala
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,57 @@ | ||
package cromwell.services.metrics.bard | ||
|
||
import akka.actor.ActorRef | ||
import bio.terra.bard.api.DefaultApi | ||
import bio.terra.bard.client.ApiClient | ||
import bio.terra.bard.model.EventsEventLogRequest | ||
import cats.data.NonEmptyList | ||
import com.typesafe.scalalogging.LazyLogging | ||
import cromwell.services.instrumentation.CromwellInstrumentation | ||
import cromwell.services.metrics.bard.model.BardEvent | ||
import org.apache.http.impl.client.HttpClients | ||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager | ||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory | ||
import org.springframework.web.client.RestTemplate | ||
|
||
class BardService(bardUrl: String, connectionPoolSize: Int, serviceRegistry: ActorRef) | ||
extends LazyLogging | ||
with CromwellInstrumentation { | ||
|
||
private val restTemplate = makeRestTemplateWithPooling | ||
private val client = getEventApi(restTemplate) | ||
private val appId = "cromwell" | ||
|
||
override lazy val serviceRegistryActor: ActorRef = serviceRegistry | ||
|
||
def sendEvent(event: BardEvent): Unit = { | ||
try { | ||
val eventLogRequest = new EventsEventLogRequest().properties(event.getProperties) | ||
client.eventsEventLog(event.eventName, appId, eventLogRequest) | ||
increment(NonEmptyList.of("send_event", "success"), Some("bard")) | ||
} catch { | ||
case e: Exception => | ||
logger.error(s"Failed to send event to Bard: ${e.getMessage}", e) | ||
increment(NonEmptyList.of("send_event", "failure"), Some("bard")) | ||
} | ||
() | ||
} | ||
|
||
private def getEventApi(restTemplate: RestTemplate): DefaultApi = { | ||
val bardClient = new ApiClient(restTemplate) | ||
bardClient.setBasePath(bardUrl) | ||
new DefaultApi(bardClient) | ||
} | ||
|
||
/** | ||
* @return a new RestTemplate backed by a pooling connection manager | ||
*/ | ||
private def makeRestTemplateWithPooling: RestTemplate = { | ||
val poolingConnManager = new PoolingHttpClientConnectionManager() | ||
poolingConnManager.setMaxTotal(connectionPoolSize) | ||
poolingConnManager.setDefaultMaxPerRoute(connectionPoolSize) | ||
val httpClient = HttpClients.custom.setConnectionManager(poolingConnManager).build | ||
val factory = new HttpComponentsClientHttpRequestFactory(httpClient) | ||
new RestTemplate(factory) | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
services/src/main/scala/cromwell/services/metrics/bard/impl/BardEventingActor.scala
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,44 @@ | ||
package cromwell.services.metrics.bard.impl | ||
|
||
import akka.actor.{Actor, ActorRef, ActorSystem, Props} | ||
import com.typesafe.config.Config | ||
import com.typesafe.scalalogging.LazyLogging | ||
import common.util.StringUtil.EnhancedToStringable | ||
import cromwell.core.Dispatcher.ServiceDispatcher | ||
import cromwell.services.metrics.bard.BardEventing.BardEventRequest | ||
import cromwell.services.metrics.bard.{BardConfig, BardService} | ||
import cromwell.util.GracefulShutdownHelper.ShutdownCommand | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
class BardEventingActor(serviceConfig: Config, globalConfig: Config, serviceRegistry: ActorRef) | ||
extends Actor | ||
with LazyLogging { | ||
|
||
implicit val system: ActorSystem = context.system | ||
implicit val ec: ExecutionContext = context.dispatcher | ||
|
||
lazy val bardConfig: BardConfig = BardConfig(serviceConfig) | ||
lazy val bardService: BardService = | ||
new BardService(bardConfig.baseUrl, bardConfig.connectionPoolSize, serviceRegistry) | ||
|
||
override def receive: Receive = { | ||
case BardEventRequest(event) if bardConfig.enabled => bardService.sendEvent(event) | ||
// This service currently doesn't do any work on shutdown but the service registry pattern requires it | ||
// (see https://github.com/broadinstitute/cromwell/issues/2575) | ||
case ShutdownCommand => context stop self | ||
case other => | ||
logger.error( | ||
s"Programmer Error: Unexpected message ${other.toPrettyElidedString(1000)} received by ${this.self.path.name}." | ||
) | ||
} | ||
} | ||
|
||
object BardEventingActor { | ||
|
||
def props(serviceConfig: Config, globalConfig: Config, serviceRegistryActor: ActorRef): Props = | ||
Props(new BardEventingActor(serviceConfig, globalConfig, serviceRegistryActor)) | ||
.withDispatcher(ServiceDispatcher) | ||
.withMailbox("akka.bard-actor-mailbox") | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
services/src/main/scala/cromwell/services/metrics/bard/model/BardEvent.scala
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,14 @@ | ||
package cromwell.services.metrics.bard.model | ||
|
||
trait BardEvent extends Product { | ||
|
||
private val baseProperties: Map[String, Any] = Map("event" -> eventName, "pushToMixpanel" -> false) | ||
|
||
def eventName: String | ||
|
||
def assembleScalaProperties: Map[String, Any] = | ||
this.productElementNames.zip(this.productIterator).toMap ++ baseProperties | ||
|
||
def getProperties: java.util.Map[String, Any] | ||
|
||
} |
Oops, something went wrong.