Add only one warning for failed statistics

This commit is contained in:
nataliya.valtman
2022-02-21 13:49:25 +03:00
parent 10f43b9fd7
commit bbafa67122
4 changed files with 93 additions and 3 deletions
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle
import org.gradle.api.logging.LogLevel
import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.report.BuildReportType
import org.jetbrains.kotlin.gradle.testbase.*
import org.junit.jupiter.api.DisplayName
@DisplayName("Build statistics")
@SimpleGradlePluginTests
class BuildStatisticsIT : KGPBaseTest() {
@DisplayName("Http build report url problems are logged only ones")
@GradleTest
fun testHttpServiceWithInvalidUrl(gradleVersion: GradleVersion) {
project("incrementalMultiproject", gradleVersion) {
enableStatisticReports(BuildReportType.HTTP, "invalid/url")
build("assemble") {
assertOutputContainsExactTimes("Unable to open connection to")
}
}
}
}
@@ -19,6 +19,30 @@ fun BuildResult.assertOutputContains(
}
}
/**
* Asserts Gradle output contains [expectedSubString] string exact times.
*/
fun BuildResult.assertOutputContainsExactTimes(
expectedSubString: String,
expectedRepetitionTimes: Int = 1
) {
var currentOffset = 0
var count = 0
var nextIndex = output.indexOf(expectedSubString, currentOffset)
while (nextIndex != -1 && count < expectedRepetitionTimes + 1) {
count++
currentOffset = nextIndex + expectedSubString.length
nextIndex = output.indexOf(expectedSubString, currentOffset)
}
assert(count == expectedRepetitionTimes) {
printBuildOutput()
"Build output contains \"$expectedSubString\" $count times"
}
}
/**
* Asserts Gradle output does not contain [notExpectedSubString] string.
*
@@ -12,6 +12,7 @@ import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.BaseGradleIT.Companion.acceptAndroidSdkLicenses
import org.jetbrains.kotlin.gradle.model.ModelContainer
import org.jetbrains.kotlin.gradle.model.ModelFetcherBuildAction
import org.jetbrains.kotlin.gradle.report.BuildReportType
import org.jetbrains.kotlin.test.util.KtTestUtil
import java.io.File
import java.nio.file.*
@@ -179,6 +180,21 @@ fun TestProject.enableLocalBuildCache(
)
}
fun TestProject.enableStatisticReports(
type: BuildReportType,
url: String?
) {
gradleProperties.append(
"\nkotlin.build.report.output=${type.name}\n"
)
url?.also {
gradleProperties.append(
"\nkotlin.build.report.http.url=$url\n"
)
}
}
open class GradleProject(
val projectName: String,
val projectPath: Path
@@ -16,6 +16,7 @@ import org.gradle.tooling.events.OperationCompletionListener
import org.gradle.tooling.events.task.TaskFinishEvent
import org.jetbrains.kotlin.gradle.plugin.stat.CompileStatisticsData
import org.jetbrains.kotlin.gradle.plugin.statistics.KotlinBuildStatListener.Companion.prepareData
import java.io.IOException
import java.net.HttpURLConnection
import java.net.URL
import java.util.*
@@ -38,6 +39,10 @@ abstract class HttpReportService : BuildService<HttpReportService.Parameters>,
private val log = Logging.getLogger(this.javaClass)
// @Volatile for one thread executor it does not need
private var requestPreviousFailed = false
private var invalidUrl = false
override fun onFinish(event: FinishEvent?) {
if (event is TaskFinishEvent) {
val data = prepareData(event, parameters.projectName, parameters.uuid, parameters.label, parameters.kotlinVersion)
@@ -74,12 +79,22 @@ abstract class HttpReportService : BuildService<HttpReportService.Parameters>,
fun report(data: CompileStatisticsData) {
val elapsedTime = measureTimeMillis {
val connection = URL(parameters.httpSettings.url).openConnection() as HttpURLConnection
if (invalidUrl) {
return
}
val connection = try {
URL(parameters.httpSettings.url).openConnection() as HttpURLConnection
} catch (e: IOException) {
log.warn("Unable to open connection to ${parameters.httpSettings.url}: ${e.message}")
invalidUrl = true
return
}
try {
if (parameters.httpSettings.user != null && parameters.httpSettings.password != null) {
val auth = Base64.getEncoder()
.encode("${parameters.httpSettings.user}:${parameters.httpSettings.password}".toByteArray()).toString(Charsets.UTF_8)
.encode("${parameters.httpSettings.user}:${parameters.httpSettings.password}".toByteArray())
.toString(Charsets.UTF_8)
connection.addRequestProperty("Authorization", "Basic $auth")
}
connection.addRequestProperty("Content-Type", "application/json")
@@ -103,7 +118,13 @@ abstract class HttpReportService : BuildService<HttpReportService.Parameters>,
private fun checkResponseAndLog(connection: HttpURLConnection) {
val isResponseBad = connection.responseCode !in 200..299
if (isResponseBad) {
log.warn("Failed to send statistic to ${connection.url}: ${connection.responseMessage}")
val message = "Failed to send statistic to ${connection.url} with ${connection.responseCode}: ${connection.responseMessage}"
if (!requestPreviousFailed) {
log.warn(message)
} else {
log.debug(message)
}
requestPreviousFailed = true
}
}