Skip to content

Commit 6282d02

Browse files
authored
Merge pull request #2298 from digma-ai/fix-force-update
fix force update Closes #2266
2 parents 058a2fb + e4a5a17 commit 6282d02

File tree

7 files changed

+202
-57
lines changed

7 files changed

+202
-57
lines changed

ide-common/src/main/java/org/digma/intellij/plugin/analytics/AnalyticsService.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,30 @@ synchronized void replaceClient(String url) {
125125
Log.log(LOGGER::debug, "AuthManager.withAuth successfully wrapped AnalyticsProvider for url {}", url);
126126
analyticsProviderProxy = newAnalyticsProviderProxy(analyticsProvider);
127127

128+
tryRegisterServerVersionEarly();
128129
environment.refreshNowOnBackground();
129130

130131
project.getMessageBus().syncPublisher(ApiClientChangedEvent.getAPI_CLIENT_CHANGED_TOPIC()).apiClientChanged(url);
131132

132133
}
133134

135+
//usually BackendInfoHolder registers the server version,but it depends on AnalyticsService.
136+
// it may happen that getEnvironments will fail and register error but there will be no server version yet
137+
// in ActivityMonitor.
138+
// this is an attempt to register server version as early as possible before any errors occurs.
139+
// BackendInfoHolder will continue monitoring the server info for changes.
140+
private void tryRegisterServerVersionEarly() {
141+
Backgroundable.executeOnPooledThread(() -> {
142+
try {
143+
var about = getAbout();
144+
if (about != null) {
145+
ActivityMonitor.getInstance(project).registerServerInfo(about);
146+
}
147+
} catch (Throwable e) {
148+
Log.debugWithException(LOGGER, project, e, "getAbout failed");
149+
}
150+
});
151+
}
134152

135153

136154
public List<Env> getEnvironments() {
@@ -691,15 +709,15 @@ private void handleInvocationTargetException(InvocationTargetException invocatio
691709
} else if (isSslConnectionException(invocationTargetException)) {
692710
message = getSslExceptionMessage(invocationTargetException);
693711
} else {
694-
message = invocationTargetException.getCause() != null ? invocationTargetException.getCause().getMessage() : invocationTargetException.getMessage();
712+
message = ExceptionUtils.getNonEmptyMessage(invocationTargetException);
695713
}
696714

697715
if (message == null) {
698716
message = invocationTargetException.toString();
699717
}
700718

701719

702-
ErrorReporter.getInstance().reportAnalyticsServiceError(project, "AnalyticsInvocationHandler.invoke." + message, method.getName(), invocationTargetException, isConnectionException);
720+
ErrorReporter.getInstance().reportAnalyticsServiceError(project, "AnalyticsInvocationHandler.invoke", method.getName(), invocationTargetException, isConnectionException);
703721

704722

705723
if (isConnectionOK()) {

ide-common/src/main/java/org/digma/intellij/plugin/settings/SettingsComponent.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import com.intellij.ui.*;
55
import com.intellij.ui.components.*;
66
import com.intellij.util.ui.FormBuilder;
7+
import org.digma.intellij.plugin.analytics.BackendInfoHolder;
78
import org.digma.intellij.plugin.auth.account.*;
9+
import org.digma.intellij.plugin.common.ProjectUtilsKt;
810
import org.jetbrains.annotations.*;
911

1012
import javax.swing.*;
@@ -160,6 +162,15 @@ public boolean verify(JComponent input) {
160162
var userIdLabel = new JBLabel(userId);
161163
userIdLabel.setCopyable(true);
162164

165+
var backendVersionLabel = new JBLabel("Unknown");
166+
var someProject = ProjectUtilsKt.findActiveProject();
167+
if (someProject != null) {
168+
var about = BackendInfoHolder.getInstance(someProject).getAbout();
169+
if (about != null) {
170+
backendVersionLabel.setText(about.getApplicationVersion());
171+
}
172+
}
173+
163174
myMainPanel = FormBuilder.createFormBuilder()
164175
.addLabeledComponent(myUrlLabel, myApiUrlText, 1, false)
165176
.addLabeledComponent(new JBLabel("Api token:"), myApiToken, 1, false)
@@ -174,6 +185,7 @@ public boolean verify(JComponent input) {
174185
.addLabeledComponent("Extended Observability Exclude (beta)", extendedObservabilityExcludeTextBox, 1, false)
175186
.addComponent(resetButton)
176187
.addLabeledComponent(new JBLabel("User Id"), userIdLabel)
188+
.addLabeledComponent(new JBLabel("Backend version"), backendVersionLabel)
177189
.addComponentFillVertically(new JPanel(), 0)
178190
.getPanel();
179191
}

ide-common/src/main/kotlin/org/digma/intellij/plugin/analytics/BackendInfoHolder.kt

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ class BackendInfoHolder(val project: Project) : DisposableAdaptor {
4141

4242

4343
init {
44+
45+
//update now so that about exists as part of the object instantiation
46+
updateAboutInBackgroundNowWithTimeout()
47+
48+
@Suppress("UnstableApiUsage")
49+
disposingScope().launch {
50+
while (isActive) {
51+
update()
52+
delay(1.minutes.inWholeMilliseconds)
53+
}
54+
}
55+
4456
project.messageBus.connect(this)
4557
.subscribe(
4658
AnalyticsServiceConnectionEvent.ANALYTICS_SERVICE_CONNECTION_EVENT_TOPIC,
@@ -61,14 +73,6 @@ class BackendInfoHolder(val project: Project) : DisposableAdaptor {
6173
updateInBackground()
6274
})
6375

64-
65-
@Suppress("UnstableApiUsage")
66-
disposingScope().launch {
67-
while (isActive) {
68-
update()
69-
delay(1.minutes.inWholeMilliseconds)
70-
}
71-
}
7276
}
7377

7478

@@ -83,11 +87,14 @@ class BackendInfoHolder(val project: Project) : DisposableAdaptor {
8387
//must be called in background coroutine
8488
private fun update() {
8589
try {
90+
Log.log(logger::trace, "updating backend info")
8691
aboutRef.set(AnalyticsService.getInstance(project).about)
8792
aboutRef.get()?.let {
8893
ActivityMonitor.getInstance(project).registerServerInfo(it)
8994
}
95+
Log.log(logger::trace, "backend info updated {}", aboutRef.get())
9096
} catch (e: Throwable) {
97+
Log.warnWithException(logger, project, e, "error in update")
9198
val isConnectionException = ExceptionUtils.isAnyConnectionException(e)
9299
if (!isConnectionException) {
93100
ErrorReporter.getInstance().reportError(project, "BackendInfoHolder.update", e)
@@ -126,22 +133,33 @@ class BackendInfoHolder(val project: Project) : DisposableAdaptor {
126133

127134

128135
private fun getAboutInBackgroundNowWithTimeout(): AboutResult? {
136+
updateAboutInBackgroundNowWithTimeout()
137+
return aboutRef.get()
138+
}
139+
140+
141+
private fun updateAboutInBackgroundNowWithTimeout() {
142+
143+
Log.log(logger::trace, "updating backend info in background with timeout")
129144

130145
@Suppress("UnstableApiUsage")
131146
val deferred = disposingScope().async {
132147
update()
133148
}
134149

135-
return runBlocking {
150+
runBlocking {
136151
try {
137-
withTimeout(5000) {
152+
withTimeout(2000) {
138153
deferred.await()
139154
}
140-
aboutRef.get()
141155
} catch (e: Throwable) {
142-
null
156+
Log.warnWithException(logger, project, e, "error in updateAboutInBackgroundNowWithTimeout")
157+
ErrorReporter.getInstance().reportError("BackendInfoHolder.updateAboutInBackgroundNowWithTimeout", e)
143158
}
144159
}
160+
161+
Log.log(logger::trace, "backend info updated in background with timeout {}", aboutRef.get())
145162
}
146163

164+
147165
}

ide-common/src/main/kotlin/org/digma/intellij/plugin/posthog/ActivityMonitor.kt

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import kotlinx.coroutines.isActive
1313
import kotlinx.coroutines.launch
1414
import kotlinx.datetime.toJavaInstant
1515
import org.digma.intellij.plugin.analytics.BackendConnectionMonitor
16-
import org.digma.intellij.plugin.analytics.BackendInfoHolder
1716
import org.digma.intellij.plugin.common.ExceptionUtils
1817
import org.digma.intellij.plugin.common.UniqueGeneratedUserId
1918
import org.digma.intellij.plugin.common.objectToJson
@@ -71,6 +70,7 @@ class ActivityMonitor(private val project: Project) : Disposable {
7170
}
7271

7372

73+
private var serverInfo: AboutResult? = null
7474
private val simpleEventInterceptor = SimpleEventInterceptor(project)
7575

7676
private val userId: String = UniqueGeneratedUserId.userId
@@ -333,7 +333,7 @@ class ActivityMonitor(private val project: Project) : Disposable {
333333
"ide.version" to ideVersion,
334334
"ide.build" to ideBuildNumber,
335335
"plugin.version" to pluginVersion,
336-
"server.version" to BackendInfoHolder.getInstance(project).getAbout()?.applicationVersion.toString(),
336+
"server.version" to serverInfo?.applicationVersion.toString(),
337337
"user.type" to if (isDevUser) "internal" else "external"
338338
)
339339

@@ -363,9 +363,16 @@ class ActivityMonitor(private val project: Project) : Disposable {
363363
details
364364
)
365365
} catch (e: Exception) {
366+
val exceptionStackTrace = e.let {
367+
val stringWriter = StringWriter()
368+
it.printStackTrace(PrintWriter(stringWriter))
369+
stringWriter.toString()
370+
}
366371
registerCustomEvent(
367372
"error in registerError", mapOf(
368-
"message" to e.message.toString()
373+
"original message" to message,
374+
"exception type" to e.javaClass,
375+
"stack trace" to exceptionStackTrace
369376
)
370377
)
371378
}
@@ -413,7 +420,7 @@ class ActivityMonitor(private val project: Project) : Disposable {
413420
"ide.version" to ideVersion,
414421
"ide.build" to ideBuildNumber,
415422
"plugin.version" to pluginVersion,
416-
"server.version" to BackendInfoHolder.getInstance(project).getAbout()?.applicationVersion.toString(),
423+
"server.version" to serverInfo?.applicationVersion.toString(),
417424
"user.type" to if (isDevUser) "internal" else "external"
418425
)
419426
)
@@ -656,13 +663,17 @@ class ActivityMonitor(private val project: Project) : Disposable {
656663

657664

658665
fun registerServerInfo(serverInfo: AboutResult) {
659-
postHog?.set(
660-
userId,
661-
mapOf(
662-
"server.version" to serverInfo.applicationVersion,
663-
"server.deploymentType" to (serverInfo.deploymentType ?: BackendDeploymentType.Unknown)
666+
//AboutResult is a data class and equals should work correctly
667+
if (this.serverInfo != serverInfo) {
668+
this.serverInfo = serverInfo
669+
postHog?.set(
670+
userId,
671+
mapOf(
672+
"server.version" to serverInfo.applicationVersion,
673+
"server.deploymentType" to (serverInfo.deploymentType ?: BackendDeploymentType.Unknown)
674+
)
664675
)
665-
)
676+
}
666677
}
667678

668679

0 commit comments

Comments
 (0)