[gradle] fixes in gradle sync action collector IDEA-312923

Report overall sync duration by caching sync start timestamps for last 100 tasks, report project when it is available.

GitOrigin-RevId: c078356cbfd13d047291112673c2599e83982468
This commit is contained in:
Nikita.Skvortsov
2023-08-31 16:55:09 +02:00
committed by intellij-monorepo-bot
parent 4ba35f1c46
commit 9c2ae69ef4
2 changed files with 31 additions and 11 deletions

View File

@@ -31,13 +31,13 @@ enum class Phase { GRADLE_CALL, PROJECT_RESOLVERS, DATA_SERVICES, WORKSPACE_MODE
object ExternalSystemSyncActionsCollector : CounterUsagesCollector() {
override fun getGroup(): EventLogGroup = GROUP
val GROUP = EventLogGroup("build.gradle.import", 7)
val GROUP = EventLogGroup("build.gradle.import", 8)
private val activityIdField = EventFields.Long("ide_activity_id")
private val importPhaseField = EventFields.Enum<Phase>("phase")
val syncStartedEvent = GROUP.registerEvent("gradle.sync.started", activityIdField)
val syncFinishedEvent = GROUP.registerEvent("gradle.sync.finished", activityIdField, Boolean("sync_successful"))
val syncFinishedEvent = GROUP.registerEvent("gradle.sync.finished", activityIdField, DurationMs, Boolean("sync_successful"))
private val phaseStartedEvent = GROUP.registerEvent("phase.started", activityIdField, importPhaseField)
val phaseFinishedEvent = GROUP.registerVarargEvent("phase.finished",
activityIdField,
@@ -60,11 +60,30 @@ object ExternalSystemSyncActionsCollector : CounterUsagesCollector() {
private val ourErrorsRateThrottle = EventsRateThrottle(100, 5L * 60 * 1000) // 100 errors per 5 minutes
@JvmStatic
fun logSyncStarted(project: Project?, activityId: Long) = syncStartedEvent.log(project, activityId)
private const val tsCacheCapasity = 100
private val idToStartTS = object: LinkedHashMap<Long, Long>(tsCacheCapasity) {
override fun removeEldestEntry(eldest: MutableMap.MutableEntry<Long, Long>?): Boolean {
return size > tsCacheCapasity
}
}
@JvmStatic
fun logSyncFinished(project: Project?, activityId: Long, success: Boolean) = syncFinishedEvent.log(project, activityId, success)
fun logSyncStarted(project: Project?, activityId: Long) {
idToStartTS[activityId] = System.currentTimeMillis()
syncStartedEvent.log(project, activityId)
}
@JvmStatic
fun logSyncFinished(project: Project?, activityId: Long, success: Boolean) {
val nowTS = System.currentTimeMillis()
val startTS = idToStartTS[activityId]
val duration = if (startTS == null) {
-1
} else {
nowTS - startTS
}
syncFinishedEvent.log(project, activityId, duration, success)
}
@JvmStatic
fun logPhaseStarted(project: Project?, activityId: Long, phase: Phase) = phaseStartedEvent.log(project, activityId, phase)

View File

@@ -149,7 +149,7 @@ public class GradleProjectResolver implements ExternalSystemProjectResolver<Grad
myCancellationMap.putValue(resolverContext.getExternalSystemTaskId(), cancellationTokenSource);
final long activityId = resolverContext.getExternalSystemTaskId().getId();
ExternalSystemSyncActionsCollector.logSyncStarted(null, activityId);
ExternalSystemSyncActionsCollector.logSyncStarted(resolverContext.getExternalSystemTaskId().findProject(), activityId);
syncMetrics.getOrStartSpan(ExternalSystemSyncDiagnostic.gradleSyncSpanName);
try {
@@ -844,9 +844,10 @@ public class GradleProjectResolver implements ExternalSystemProjectResolver<Grad
@Override
public DataNode<ProjectData> fun(ProjectConnection connection) {
final long activityId = myResolverContext.getExternalSystemTaskId().getId();
ExternalSystemTaskId taskId = myResolverContext.getExternalSystemTaskId();
final long activityId = taskId.getId();
try {
myCancellationMap.putValue(myResolverContext.getExternalSystemTaskId(), myResolverContext.getCancellationTokenSource());
myCancellationMap.putValue(taskId, myResolverContext.getCancellationTokenSource());
myResolverContext.setConnection(connection);
return doResolveProjectInfo(myResolverContext, myProjectResolverChain, myIsBuildSrcProject);
}
@@ -859,15 +860,15 @@ public class GradleProjectResolver implements ExternalSystemProjectResolver<Grad
if (esException != null && esException != e) {
LOG.info("\nCaused by: " + esException.getOriginalReason());
}
ExternalSystemSyncActionsCollector.logError(null, activityId, extractCause(e));
ExternalSystemSyncActionsCollector.logSyncFinished(null, activityId, false);
ExternalSystemSyncActionsCollector.logError(taskId.findProject(), activityId, extractCause(e));
ExternalSystemSyncActionsCollector.logSyncFinished(taskId.findProject(), activityId, false);
syncMetrics.endSpan(ExternalSystemSyncDiagnostic.gradleSyncSpanName, (span) -> span.setAttribute("project", ""));
throw myProjectResolverChain.getUserFriendlyError(
myResolverContext.getBuildEnvironment(), e, myResolverContext.getProjectPath(), null);
}
finally {
myCancellationMap.remove(myResolverContext.getExternalSystemTaskId(), myResolverContext.getCancellationTokenSource());
myCancellationMap.remove(taskId, myResolverContext.getCancellationTokenSource());
}
}
}