|
| 1 | +package com.shadiramadan.sbt.elastic.apm |
| 2 | + |
| 3 | +import sbt._ |
| 4 | +import sbt.Keys._ |
| 5 | +import sbt.plugins.JvmPlugin |
| 6 | +import com.typesafe.sbt.SbtNativePackager._ |
| 7 | +import com.typesafe.sbt.packager.archetypes.scripts.BashStartScriptPlugin.autoImport.bashScriptExtraDefines |
| 8 | +import com.typesafe.sbt.packager.archetypes.scripts.{BashStartScriptPlugin, BatStartScriptPlugin} |
| 9 | +import sbt.librarymanagement.DependencyFilter |
| 10 | + |
| 11 | +object ElasticApmPlugin extends AutoPlugin { |
| 12 | + |
| 13 | + override def trigger = allRequirements |
| 14 | + override def requires = JvmPlugin && BashStartScriptPlugin && BatStartScriptPlugin |
| 15 | + |
| 16 | + object autoImport { |
| 17 | + lazy val elasticApmVersion = settingKey[String]("Elastic APM Agent version") |
| 18 | + lazy val elasticApmJavaAgent = taskKey[File]("Elastic APM agent jar location") |
| 19 | + lazy val elasticApmServiceName = taskKey[String]( |
| 20 | + "This is used to keep all the errors and transactions of your service together and is the primary filter in the Elastic APM user interface. The default value is the sbt project name.") |
| 21 | + lazy val elasticApmAgentApplicationPackages = taskKey[Seq[String]]( |
| 22 | + "Used to determine whether a stack trace frame is an in-app frame or a library frame. Multiple packages can be set as a comma-separated list. Setting this option can also improve the startup time.") |
| 23 | + lazy val elasticApmAgentServerUrls = taskKey[Seq[URL]]( |
| 24 | + "The URLs must be fully qualified, including protocol (http or https) and port.") |
| 25 | + } |
| 26 | + |
| 27 | + import autoImport._ |
| 28 | + |
| 29 | + val ElasticApmConfig = config("elastic-apm-agent").hide |
| 30 | + |
| 31 | + override lazy val projectSettings = Seq( |
| 32 | + ivyConfigurations += ElasticApmConfig, |
| 33 | + elasticApmVersion := "1.6.1", |
| 34 | + elasticApmJavaAgent := findElasticApmJavaAgent(update.value), |
| 35 | + elasticApmServiceName := name.value, |
| 36 | + elasticApmAgentApplicationPackages := Seq("com.example"), |
| 37 | + elasticApmAgentServerUrls := Seq(url("http://localhost:8200")), |
| 38 | + libraryDependencies += "co.elastic.apm" % "elastic-apm-agent" % elasticApmVersion.value % ElasticApmConfig, |
| 39 | + mappings in Universal += elasticApmJavaAgent.value -> "elasticApm/elastic-apm-agent.jar", |
| 40 | + bashScriptExtraDefines += """addJava "-javaagent:${app_home}/../elasticApm/elastic-apm-agent.jar"""", |
| 41 | + bashScriptExtraDefines += s"""addJava "-Delastic.apm.service_name=${elasticApmServiceName.value}"""", |
| 42 | + bashScriptExtraDefines += s"""addJava "-Delastic.apm.application_packages=${elasticApmAgentApplicationPackages.value.mkString(",")}"""", |
| 43 | + bashScriptExtraDefines += s"""addJava "-Delastic.apm.server_urls=${elasticApmAgentServerUrls.value.mkString(",")}"""" |
| 44 | + ) |
| 45 | + |
| 46 | + private[this] def findElasticApmJavaAgent(report: UpdateReport) = report.matching(elasticApmFilter).head |
| 47 | + |
| 48 | + private[this] val elasticApmFilter: DependencyFilter = |
| 49 | + configurationFilter("elastic-apm-agent") && artifactFilter(`type` = "jar") |
| 50 | + |
| 51 | + override lazy val buildSettings = Seq() |
| 52 | + |
| 53 | + override lazy val globalSettings = Seq() |
| 54 | +} |
0 commit comments