├── project ├── build.properties ├── PublishSettings.scala ├── ReleaseSettings.scala └── plugins.sbt ├── .scalafmt.conf ├── .gitmodules ├── .gitlab-ci.yml ├── publish.sbt ├── LICENSE ├── .gitignore ├── README.md └── src └── main └── scala └── com └── colisweb └── sbt └── DatadogAPM.scala /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version = 1.9.6 2 | -------------------------------------------------------------------------------- /project/PublishSettings.scala: -------------------------------------------------------------------------------- 1 | ../scala-common/scala/PublishSettings.scala -------------------------------------------------------------------------------- /project/ReleaseSettings.scala: -------------------------------------------------------------------------------- 1 | ../scala-common/scala/ReleaseSettings.scala -------------------------------------------------------------------------------- /.scalafmt.conf: -------------------------------------------------------------------------------- 1 | version = 3.0.0 2 | style = defaultWithAlign 3 | maxColumn = 120 4 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "scala-common"] 2 | path = scala-common 3 | url = ../scala-common.git 4 | branch = v5.1.4 5 | -------------------------------------------------------------------------------- /.gitlab-ci.yml: -------------------------------------------------------------------------------- 1 | include: 2 | - https://colisweb-idl.gitlab.io/colisweb-open-source/ci-common/v17.8.0/templates/scala.yml 3 | 4 | test sbt-datadog: 5 | extends: .sbt-test 6 | 7 | version sbt-datadog: 8 | extends: .version 9 | 10 | publish sbt-datadog: 11 | extends: .sonatype-publish 12 | 13 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.21") 2 | addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.2.1") 3 | addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2") 4 | addSbtPlugin("com.github.cb372" % "sbt-explicit-dependencies" % "0.3.1") 5 | -------------------------------------------------------------------------------- /publish.sbt: -------------------------------------------------------------------------------- 1 | ReleaseSettings.globalReleaseSettings 2 | ReleaseSettings.buildReleaseSettings( 3 | details = "Utilities to use tracing alongside a comprehensive instrumentation, like when using Datadog's java-agent.", 4 | licenseName = "MIT", 5 | licenseUrl = "http://opensource.org/licenses/MIT", 6 | projectName = "sbt-datadog" 7 | ) 8 | 9 | ThisBuild / developers := List(Developers.julesIvanic) 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Colisweb 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### SBT template 3 | # Simple Build Tool 4 | # http://www.scala-sbt.org/release/docs/Getting-Started/Directories.html#configuring-version-control 5 | 6 | dist/* 7 | target/ 8 | lib_managed/ 9 | src_managed/ 10 | project/boot/ 11 | project/plugins/project/ 12 | .history 13 | .cache 14 | sbt-cache 15 | .lib/ 16 | ### Scala template 17 | *.class 18 | *.log 19 | ### JetBrains template 20 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 21 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 22 | 23 | # User-specific stuff 24 | .idea/**/workspace.xml 25 | .idea/**/tasks.xml 26 | .idea/**/dictionaries 27 | .idea/**/shelf 28 | 29 | # Sensitive or high-churn files 30 | .idea/**/dataSources/ 31 | .idea/**/dataSources.ids 32 | .idea/**/dataSources.local.xml 33 | .idea/**/sqlDataSources.xml 34 | .idea/**/dynamic.xml 35 | .idea/**/uiDesigner.xml 36 | .idea/**/dbnavigator.xml 37 | 38 | # Gradle 39 | .idea/**/gradle.xml 40 | .idea/**/libraries 41 | 42 | # CMake 43 | cmake-build-debug/ 44 | cmake-build-release/ 45 | 46 | # Mongo Explorer plugin 47 | .idea/**/mongoSettings.xml 48 | 49 | # File-based project format 50 | *.iws 51 | 52 | # IntelliJ 53 | out/ 54 | 55 | # mpeltonen/sbt-idea plugin 56 | .idea_modules/ 57 | 58 | # JIRA plugin 59 | atlassian-ide-plugin.xml 60 | 61 | # Cursive Clojure plugin 62 | .idea/replstate.xml 63 | 64 | # Crashlytics plugin (for Android Studio and IntelliJ) 65 | com_crashlytics_export_strings.xml 66 | crashlytics.properties 67 | crashlytics-build.properties 68 | fabric.properties 69 | 70 | # Editor-based Rest Client 71 | .idea/httpRequests 72 | /.idea/ 73 | .gitsecret/keys/random_seed 74 | !*.secret 75 | secrets-env.sh 76 | 77 | .metals 78 | .bloop 79 | .bsp -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sbt-datadog 2 | 3 | [ ![Download](https://api.bintray.com/packages/colisweb/sbt-plugins/sbt-datadog/images/download.svg) ](https://bintray.com/colisweb/sbt-plugins/sbt-datadog/_latestVersion) 4 | 5 | This project is highly inspired by the fantastic [sbt-newrelic](https://github.com/gilt/sbt-newrelic) project. 6 | 7 | We want to thanks [Gilt](http://tech.gilt.com) for their work on `sbt-newrelic` that allowed us to create this project really quickly. 8 | 9 | Prerequisites 10 | ------------- 11 | 12 | The plugin assumes that sbt-native-packager has been included in your SBT build configuration. 13 | This can be done by adding the plugin following instructions at http://www.scala-sbt.org/sbt-native-packager/ or by adding 14 | another plugin that includes and initializes it (e.g. the SBT plugin for Play 2.6.x). 15 | 16 | 17 | Installation 18 | ------------ 19 | 20 | Add the following to your `project/plugins.sbt` file: 21 | 22 | ```scala 23 | 24 | addSbtPlugin("com.colisweb" % "sbt-datadog" % "2.1.0") 25 | ``` 26 | 27 | To enable the Datadog APM for your project, add the `DatadogAPM` auto-plugin to your project. 28 | 29 | ```scala 30 | enablePlugins(DatadogAPM) 31 | ``` 32 | 33 | Configuration 34 | ------------- 35 | 36 | #### `datadogApmVersion` 37 | 38 | To use a specific Datadog Java APM Agent version, add the following to your `build.sbt` file: 39 | 40 | ```scala 41 | datadogApmVersion := "0.74.1" 42 | ``` 43 | 44 | #### `datadogServiceName` 45 | 46 | By default, the agent will use the sbt project `name` value as `service.name`. 47 | 48 | To use another value, add the following to your `build.sbt` file: 49 | 50 | ```scala 51 | datadogServiceName := "another name" 52 | ``` 53 | 54 | You can use your **host** (where you code run) environment variables in the value: 55 | 56 | ```scala 57 | datadogServiceName := "another name ${MY_HOST_ENV_VAR}" 58 | ``` 59 | 60 | #### `datadogAgentHost` 61 | 62 | By default, the agent `agent.host` value is `localhost`. 63 | 64 | To use another value, add the following to your `build.sbt` file: 65 | 66 | ```scala 67 | datadogAgentHost := "127.0.0.1" 68 | ``` 69 | 70 | You can use your **host** (where you code run) environment variables in the value: 71 | 72 | ```scala 73 | datadogAgentHost := "${MY_DD_HOST_IP}" 74 | ``` 75 | 76 | #### `datadogAgentPort` 77 | 78 | By default, the agent `agent.port` value is `"8126"`. 79 | 80 | To use another value, add the following to your `build.sbt` file: 81 | 82 | ```scala 83 | datadogAgentPort := "9999" 84 | ``` 85 | 86 | You can use your **host** (where you code run) environment variables in the value: 87 | 88 | ```scala 89 | datadogAgentPort := "${MY_DD_PORT}" 90 | ``` 91 | 92 | #### `datadogEnableNetty` 93 | 94 | Netty Http Server and Client Instrumentation. Default value is `false`. 95 | 96 | To use another value, add the following to your `build.sbt` file: 97 | 98 | ```scala 99 | datadogEnableNetty := true 100 | ``` 101 | 102 | #### `datadogEnableAkkaHttp` 103 | 104 | Akka-Http Server and Lagom Framework Instrumentation. Default value is `false`. 105 | 106 | To use another value, add the following to your `build.sbt` file: 107 | 108 | ```scala 109 | datadogEnableAkkaHttp := true 110 | ``` 111 | 112 | #### `datadogEnableDebug` 113 | 114 | To return debug level application logs, enable debug mode with this flag. Default value is `false`. 115 | 116 | To use another value, add the following to your `build.sbt` file: 117 | 118 | ```scala 119 | datadogEnableDebug := true 120 | ``` 121 | 122 | #### `datadogGlobalTags` 123 | 124 | A list of default tags to be added to every span and every JMX metric. Default value is an empty list. 125 | 126 | To add global tags: 127 | 128 | ```scala 129 | datadogGlobalTags := Map("env" -> "testing", "version" -> "testing-4c84587e") 130 | ``` 131 | 132 | #### Other possible settings 133 | 134 | For more configuration option, look at the Datadog Java APM agent documentation: https://docs.datadoghq.com/tracing/setup/java/ -------------------------------------------------------------------------------- /src/main/scala/com/colisweb/sbt/DatadogAPM.scala: -------------------------------------------------------------------------------- 1 | package com.colisweb.sbt 2 | 3 | import com.typesafe.sbt.SbtNativePackager._ 4 | import com.typesafe.sbt.packager.archetypes.scripts.BashStartScriptPlugin.autoImport.bashScriptExtraDefines 5 | import com.typesafe.sbt.packager.archetypes.scripts.{BashStartScriptPlugin, BatStartScriptPlugin} 6 | import sbt.Keys._ 7 | import sbt._ 8 | import sbt.librarymanagement.DependencyFilter 9 | 10 | /** Eagerly inspired by https://github.com/gilt/sbt-newrelic 11 | */ 12 | object DatadogAPM extends AutoPlugin { 13 | 14 | object autoImport { 15 | lazy val datadogApmVersion = settingKey[String]("Datadog APM agent version") 16 | lazy val datadogJavaAgent = taskKey[File]("Datadog agent jar location") 17 | lazy val datadogServiceName = taskKey[String]( 18 | "The name of a set of processes that do the same job. Used for grouping stats for your application. Default value is the sbt project name" 19 | ) 20 | lazy val datadogAgentHost = taskKey[String]( 21 | """Hostname for where to send traces to. If using a containerized environment, configure this to be the host ip. See our docker docs for additional detail. Default value: "localhost"""" 22 | ) 23 | lazy val datadogAgentPort = 24 | taskKey[String]("Port number the Agent is listening on for configured host. Default value: 8126") 25 | lazy val datadogEnableNetty = taskKey[Boolean]("Netty Http Server and Client Instrumentation. Default value: false") 26 | lazy val datadogEnableAkkaHttp = 27 | taskKey[Boolean]("Akka-Http Server and Lagom Framework Instrumentation. Default value: false") 28 | lazy val datadogEnableDebug = 29 | taskKey[Boolean]("To return debug level application logs, enable debug mode. Default value: false") 30 | lazy val datadogGlobalTags = taskKey[Map[String, String]]( 31 | "A list of default tags to be added to every span and every JMX metric. Default value: Empty List" 32 | ) 33 | } 34 | import autoImport._ 35 | 36 | override def requires = BashStartScriptPlugin && BatStartScriptPlugin 37 | 38 | val DatadogConfig = config("dd-java-agent").hide 39 | 40 | override lazy val projectSettings = Seq( 41 | ivyConfigurations += DatadogConfig, 42 | datadogApmVersion := "0.105.0", 43 | datadogJavaAgent := findDatadogJavaAgent(update.value), 44 | datadogServiceName := name.value, 45 | datadogAgentHost := "localhost", 46 | datadogAgentPort := "8126", 47 | datadogEnableNetty := false, 48 | datadogEnableAkkaHttp := false, 49 | datadogEnableDebug := false, 50 | datadogGlobalTags := Map.empty, 51 | libraryDependencies += "com.datadoghq" % "dd-java-agent" % datadogApmVersion.value % DatadogConfig, 52 | mappings in Universal += datadogJavaAgent.value -> "datadog/dd-java-agent.jar", 53 | bashScriptExtraDefines += """addJava "-javaagent:${app_home}/../datadog/dd-java-agent.jar"""", 54 | bashScriptExtraDefines += s"""addJava "-Ddd.service.name=${datadogServiceName.value}"""", 55 | bashScriptExtraDefines += s"""addJava "-Ddd.agent.host=${datadogAgentHost.value}"""", 56 | bashScriptExtraDefines += s"""addJava "-Ddd.agent.port=${datadogAgentPort.value}"""", 57 | bashScriptExtraDefines += s"""addJava "-Ddd.integration.netty.enabled=${datadogEnableNetty.value}"""", 58 | bashScriptExtraDefines += s"""addJava "-Ddd.integration.akka-http.enabled=${datadogEnableAkkaHttp.value}"""", 59 | bashScriptExtraDefines += { 60 | val debugEnabled = datadogEnableDebug.value 61 | if (debugEnabled) s"""addJava "-Ddatadog.slf4j.simpleLogger.defaultLogLevel=debug"""" 62 | else """echo "Datadog debug mode disabled"""" 63 | }, 64 | bashScriptExtraDefines += { 65 | val globalTags = datadogGlobalTags.value 66 | (globalTags.contains("env"), globalTags.contains("version")) match { 67 | case (false, false) => """echo "Datadog env and app version are not set"""" 68 | case (false, true) => """echo "Datadog env is not set"""" 69 | case (true, false) => """echo "App version is not set"""" 70 | case (true, true) => 71 | val tags = globalTags.map { case (key, value) => s"$key:$value" }.mkString(",") 72 | s"""addJava -Ddd.trace.global.tags=$tags""" 73 | } 74 | } 75 | ) 76 | 77 | private[this] def findDatadogJavaAgent(report: UpdateReport) = report.matching(datadogFilter).head 78 | 79 | private[this] val datadogFilter: DependencyFilter = 80 | configurationFilter("dd-java-agent") && artifactFilter(`type` = "jar") 81 | 82 | } 83 | --------------------------------------------------------------------------------