Skip to content

Commit

Permalink
Merge pull request #38 from takahirom/takahirom/add-repoters-to-Measu…
Browse files Browse the repository at this point in the history
…reBuildsExtension/2024-03-18

Add Customizable MetricsReporters to the Measure-Builds Gradle Plugin
  • Loading branch information
wzieba authored Mar 26, 2024
2 parents bad994f + 1accd07 commit a38c5e9
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.automattic.android.measure
import com.automattic.android.measure.lifecycle.BuildFinishedFlowAction
import com.automattic.android.measure.lifecycle.BuildTaskService
import com.automattic.android.measure.lifecycle.ConfigurationPhaseObserver
import com.automattic.android.measure.networking.MetricsReporter
import com.automattic.android.measure.providers.BuildDataProvider
import com.automattic.android.measure.providers.UsernameProvider
import com.automattic.android.measure.reporters.InMemoryMetricsReporter
import com.gradle.scan.plugin.BuildScanExtension
import kotlinx.coroutines.runBlocking
import org.gradle.StartParameter
Expand Down Expand Up @@ -34,41 +34,39 @@ class BuildTimePlugin @Inject constructor(
val extension =
project.extensions.create("measureBuilds", MeasureBuildsExtension::class.java, project)

val analyticsReporter = MetricsReporter(project.logger, extension.authToken, project.buildDir)
val metricsDispatcher = InMemoryMetricsReporter
metricsDispatcher.buildMetricsPreparedAction = extension.buildMetricsPreparedAction

val encodedUser: String = UsernameProvider.provide(project, extension)

project.afterEvaluate {
if (extension.enable.orNull == true) {
if (extension.enable.convention(false).get() == true) {
val configurationProvider: Provider<Boolean> = project.providers.of(
ConfigurationPhaseObserver::class.java
) { }
ConfigurationPhaseObserver.init()

prepareBuildData(project, extension, encodedUser)
prepareBuildData(project, encodedUser)
prepareBuildFinishedAction(
project.gradle.startParameter,
extension,
analyticsReporter,
buildInitiatedTime,
configurationProvider
)
}
}

prepareBuildTaskService(project)
prepareBuildScanListener(project, extension, analyticsReporter)
prepareBuildScanListener(project, extension, metricsDispatcher)
}

private fun prepareBuildData(
project: Project,
extension: MeasureBuildsExtension,
encodedUser: String,
) {
InMemoryReport.setBuildData(
BuildDataProvider.provide(
project,
extension.automatticProject.get(),
encodedUser,
)
)
Expand All @@ -77,12 +75,14 @@ class BuildTimePlugin @Inject constructor(
private fun prepareBuildScanListener(
project: Project,
extension: MeasureBuildsExtension,
analyticsReporter: MetricsReporter,
analyticsReporter: InMemoryMetricsReporter,
) {
val buildScanExtension = project.extensions.findByType(BuildScanExtension::class.java)
val extensionEnable = extension.enable
val attachGradleScanId = extension.attachGradleScanId
buildScanExtension?.buildScanPublished {
runBlocking {
if (extension.enable.orNull == true && extension.attachGradleScanId.get()) {
if (extensionEnable.get() == true && attachGradleScanId.get()) {
analyticsReporter.report(InMemoryReport, it.buildScanId)
}
}
Expand All @@ -92,7 +92,6 @@ class BuildTimePlugin @Inject constructor(
private fun prepareBuildFinishedAction(
startParameter: StartParameter,
extension: MeasureBuildsExtension,
analyticsReporter: MetricsReporter,
buildInitiatedTime: Long,
configurationPhaseObserver: Provider<Boolean>,
) {
Expand All @@ -102,7 +101,6 @@ class BuildTimePlugin @Inject constructor(
spec.parameters.apply {
this.buildWorkResult.set(flowProviders.buildWorkResult)
this.attachGradleScanId.set(extension.attachGradleScanId)
this.analyticsReporter.set(analyticsReporter)
this.initiationTime.set(buildInitiatedTime)
this.configurationPhaseExecuted.set(configurationPhaseObserver)
this.startParameter.set(startParameter)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package com.automattic.android.measure

import com.automattic.android.measure.reporters.MetricsReport
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.provider.Property

abstract class MeasureBuildsExtension(project: Project) {

private val objects = project.objects

val automatticProject: Property<AutomatticProject> =
objects.property(AutomatticProject::class.java)

val enable: Property<Boolean> = objects.property(Boolean::class.java)

val obfuscateUsername: Property<Boolean> = objects.property(Boolean::class.java)

@Suppress("UNCHECKED_CAST")
internal val buildMetricsPreparedAction: Property<Action<MetricsReport>> =
objects.property(Action::class.java) as Property<Action<MetricsReport>>

fun buildMetricsPrepared(action: Action<MetricsReport>) {
buildMetricsPreparedAction.set(action)
}

/**
* If `true`, then the metrics will be sent at build finish,
* orchestrated by Gradle Enterprise plugin, attaching
Expand All @@ -23,10 +30,4 @@ abstract class MeasureBuildsExtension(project: Project) {
* @see io.github.wzieba.tracks.plugin.analytics.BuildFinishedFlowAction
*/
val attachGradleScanId: Property<Boolean> = objects.property(Boolean::class.java)

val authToken: Property<String> = objects.property(String::class.java)

enum class AutomatticProject {
WooCommerce, WordPress, DayOne, PocketCasts, Tumblr, FluxC
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ package com.automattic.android.measure.lifecycle

import com.automattic.android.measure.InMemoryReport
import com.automattic.android.measure.models.ExecutionData
import com.automattic.android.measure.networking.MetricsReporter
import kotlinx.coroutines.runBlocking
import com.automattic.android.measure.reporters.InMemoryMetricsReporter
import org.gradle.StartParameter
import org.gradle.api.flow.BuildWorkResult
import org.gradle.api.flow.FlowAction
Expand All @@ -30,9 +29,6 @@ class BuildFinishedFlowAction : FlowAction<BuildFinishedFlowAction.Parameters> {
@get:Input
val buildWorkResult: Property<Provider<BuildWorkResult>>

@get:Input
val analyticsReporter: Property<MetricsReporter>

@get:Input
val attachGradleScanId: Property<Boolean>

Expand Down Expand Up @@ -70,12 +66,10 @@ class BuildFinishedFlowAction : FlowAction<BuildFinishedFlowAction.Parameters> {
InMemoryReport.setExecutionData(executionData)

if (parameters.attachGradleScanId.get() == false) {
runBlocking {
parameters.analyticsReporter.get().report(
report = InMemoryReport,
gradleScanId = null
)
}
InMemoryMetricsReporter.report(
report = InMemoryReport,
gradleScanId = null
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.automattic.android.measure.models

import com.automattic.android.measure.MeasureBuildsExtension
import kotlinx.serialization.Serializable

/**
Expand All @@ -12,7 +11,6 @@ import kotlinx.serialization.Serializable
*/
@Serializable
data class BuildData(
val forProject: MeasureBuildsExtension.AutomatticProject,
val user: String,
val gradleVersion: String,
val operatingSystem: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import com.automattic.android.measure.models.MeasuredTask.State.EXECUTED
import com.automattic.android.measure.models.MeasuredTask.State.IS_FROM_CACHE
import com.automattic.android.measure.models.MeasuredTask.State.UP_TO_DATE

fun InMemoryReport.toAppsMetricsPayload(gradleScanId: String?): GroupedAppsMetrics {
val projectKey = buildData.forProject.name.lowercase()

fun InMemoryReport.toAppsMetricsPayload(
projectKey: String,
gradleScanId: String?
): GroupedAppsMetrics {
val meta = mapOf(
"user" to buildData.user,
"project" to projectKey,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.automattic.android.measure.providers

import com.automattic.android.measure.MeasureBuildsExtension
import com.automattic.android.measure.models.BuildData
import com.automattic.android.measure.models.Environment
import org.gradle.api.Project
Expand All @@ -10,15 +9,13 @@ object BuildDataProvider {

fun provide(
project: Project,
automatticProject: MeasureBuildsExtension.AutomatticProject,
username: String,
): BuildData {
val gradle = project.gradle
val startParameter = gradle.startParameter

@Suppress("UnstableApiUsage")
return BuildData(
forProject = automatticProject,
environment = gradle.environment(),
gradleVersion = gradle.gradleVersion,
operatingSystem = System.getProperty("os.name").lowercase(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.automattic.android.measure.reporters

import com.automattic.android.measure.InMemoryReport
import org.gradle.api.Action
import org.gradle.api.provider.Property

object InMemoryMetricsReporter {

lateinit var buildMetricsPreparedAction: Property<Action<MetricsReport>>

fun report(
report: InMemoryReport,
gradleScanId: String?
) {
val result = object : MetricsReport {
override val report: InMemoryReport
get() = report
override val gradleScanId: String?
get() = gradleScanId
}
buildMetricsPreparedAction.get().execute(result)
}
}
Loading

0 comments on commit a38c5e9

Please sign in to comment.