Explore Talaiot plugin: configure metrics
Talaiot is a Plugin written in Kotlin that helps to track the information of the Gradle tasks executed in your project during the build.
Talaiot has notions of metrics (what to measure) and publishers (where to store/publish measurements https://github.com/cdsap/Talaiot/#publishers).
In this article, I will cover two topics:
- how to customise what is measured
- how to add additional metrics
Initial setup
For the initial setup, I just follow the readme and use JsonPublisher
which stores all measurements in the build/reports/talaiot/data.json
file.
After executing /.gradlew tasks
data.json
contains a lot of information like OS version, Java version, user name, etc.
Customise what is measured
Depending on your use-case, you might need to disable some of the metrics. For example, username
and gitUser
are pretty much the same and you need only one of them. Or you simply might be not interested in some fields. Another example is when you store your measurement in InfluxDB/Prometheus and don’t want to blow up your storage with irrelevant data or fields with high cardinality.
Talaiot implements each measurement as a separate metric like GitUserMetric
, UserMetric
, OsMetric
etc. Metrics are combined into groups: default
, git
, environment
, performance
and gradleSwitches
. All that information is defined in MetricsConfiguration
. By default all five groups are included. Important: default configuration is applied only if there is no customisation. It means that if you do customisation, then you have to specify all metrics that needs to be executed.
In our case, we want to include all metrics from groups default
and perfomance
and one specific metric GitBranchMetric
. Talaiot configuration would look like
After executing /.gradlew tasks
data.json
contains much fewer data.
Add custom metrics
Talaiot has a lot of different metrics, but sometimes you are interested to measure something specific to your project. For that you have two mechanisms:
customBuildMetrics
andcustomTaskMetrics
are key-value maps that are defined at project configuration time https://github.com/cdsap/Talaiot/#metrics- fully custom metrics, implemented in your project.
I’ll focus only on the second option because the first one is straightforward and well documented.
Let’s track if the Gradle build was executed from IDE (Android Studio) or command line. As the second custom metric, we will identify the root folder of the project.
I implement custom metrics inside the buildSrc
folder, but you can use the composite build for that purpose too. It’s important to add the dependency on the Talaiot plugin.
Custom metric has to implement Metric<T, in Context>
and has to define provider
and assigner
function. provider
is a function that performs the calculation and assigner
is a function that assigns the value to the ExecutionReport
. There are several predefined base metrics like GradleMetric
, GradlePropertyMetric
, CmdMetric
, JvmArgsMetric
etc. In our case we will extend GradleMetric
which defines that provider
function receives Gradle project as aninput.
AndroidStudioMetric will look like
I know that IDE adds android.injected.invoked.from.ide
property to the build. Our metric simply checks if this property is present.
And ProjectFolderMetric will look like
It’s even simpler. We just retrieve root directory of the project.
The final step is to adjust the Talaiot configuration
After executing ./gradlew tasks
data.json
contains two new fields under customProperties.buildProperties
.
Source code
Source code can be found https://github.com/MyDogTom/ConfigureTalaiot
Each step was added as a separate commit:
- initial setup https://github.com/MyDogTom/ConfigureTalaiot/commit/63551db6f461befbeb7c46b90f126368a52357ae
- customise what is measured https://github.com/MyDogTom/ConfigureTalaiot/commit/1fbf8a80cfa49441011f223e92837b8689d95816
- add custom metrics https://github.com/MyDogTom/ConfigureTalaiot/commit/e46978427ab1c72a10773b56e1942f98fa0a89d9