All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.tinkoff.plugins.buildmetrics.gradle.metrics.BuildResultMetricsFactory.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC2
Show newest version
package ru.tinkoff.plugins.buildmetrics.gradle.metrics

import org.gradle.api.flow.BuildWorkResult
import ru.tinkoff.plugins.buildmetrics.api.builds.BuildWorkResultListener
import ru.tinkoff.plugins.buildmetrics.api.factories.Factory
import ru.tinkoff.plugins.buildmetrics.api.labels.Label
import ru.tinkoff.plugins.buildmetrics.api.metrics.Metric
import kotlin.jvm.optionals.getOrNull

/**
 * Metrics:
 * - gradle_build_success
 *     - gradle_build_failure - label with build error text;
 */
class BuildResultMetricsFactory : Factory.Metrics, BuildWorkResultListener {

    @Transient
    private var failure: String? = null

    override fun reinitialize() {
        failure = null
    }

    override fun onBuildWorkResult(result: BuildWorkResult) {
        result.failure.getOrNull()?.let { buildFailure ->
            failure = buildFailure.toString()
        }
    }

    override fun create(): List> {
        return listOf(
            buildSuccessMetric()
        )
    }

    private fun buildSuccessMetric(): Metric = Metric(
        name = "gradle_build_success",
        value = failure == null,
        labels = failure?.let { buildFailure ->
            listOf(Label(name = "gradle_build_failure", value = buildFailure))
        } ?: emptyList()
    )
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy