I'm using scala, sbt, sbt-native-package, and potentially sbt-java-agent to conditionally activate a datadog java agent at runtime w/ kubernetes.
By adding the dd-java-agent
as a dependency and adding a script snippet, I'm able to activate datadog only when a specific env. variable is set, but this is also adding the dd-java-agent to the classpath, which I'm trying to avoid:
val DataDogAgentVersion = "0.70.0"
libraryDependencies += "com.datadoghq" % "dd-java-agent" % DataDogAgentVersion % "runtime"
bashScriptExtraDefines += """if [ "$DD_PROFILING_ENABLED" = "true" ]; then addJava "-javaagent:${app_home}/../lib/dd-java-agent-""" + DataDogAgentVersion + """.jar"; fi""""
Is there a way to have sbt manage the downloading of dd-java-agent.jar, include this jar in the lib
directory (or a different directory if that's what it takes), but exclude from classpath?
I've tried using sbt-java-agent
which puts the jar in a dd-java-agent
directory and excludes the jar from the classpath, but I can not figure out how to wrap the addJava
statement in an if
check when using that plugin.
Thanks for any help you can provide!
I ended up going with the sbt-javaagent
plugin to avoid extra code to exclude the agent jar from the classpath, which the plugin handles automatically.
The trick/hack was to filter out the default addJava -javaagent
line the sbt-javaagent
plugin adds automatically, and then appending a new script snippent to only enable the javaagent when a certain env. variable is set.
lazy val dataDogAgentName = "dd-java-agent"
lazy val dataDogAgentVersion = "0.70.0"
lazy val distProject = project
.enablePlugins(JavaAgent, JavaAppPackaging)
.settings(
javaAgents += "com.datadoghq" % dataDogAgentName % dataDogAgentVersion,
bashScriptExtraDefines := bashScriptExtraDefines.value.filterNot(_.contains("javaagent")) :+ s"""
|if [[ "$$DD_PROFILING_ENABLED" = "true" ]]; then
| addJava "-javaagent:$${app_home}/../$dataDogAgentName/$dataDogAgentName-$dataDogAgentVersion.jar";
|fi
|""".stripMargin,
)