-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: send telemetry when there is a failure connecting to a cluster…
… INTELLIJ-14 (#10)
- Loading branch information
Showing
13 changed files
with
521 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ replay_pid* | |
/.gradle | ||
/build | ||
/.idea | ||
/**/.idea | ||
/**/build | ||
|
||
# This file is generated at compile time. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...plugin/src/main/kotlin/com/mongodb/jbplugin/observability/probe/ConnectionFailureProbe.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.mongodb.jbplugin.observability.probe | ||
|
||
import com.intellij.database.dataSource.DatabaseConnection | ||
import com.intellij.database.dataSource.DatabaseConnectionManager | ||
import com.intellij.database.dataSource.DatabaseConnectionPoint | ||
import com.intellij.execution.rmi.RemoteObject | ||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.openapi.diagnostic.Logger | ||
import com.intellij.openapi.diagnostic.logger | ||
import com.intellij.openapi.project.Project | ||
import com.mongodb.jbplugin.accessadapter.datagrip.DataGripBasedReadModelProvider | ||
import com.mongodb.jbplugin.accessadapter.datagrip.adapter.isMongoDbDataSource | ||
import com.mongodb.jbplugin.accessadapter.slice.BuildInfo | ||
import com.mongodb.jbplugin.observability.LogMessage | ||
import com.mongodb.jbplugin.observability.TelemetryEvent | ||
import com.mongodb.jbplugin.observability.TelemetryService | ||
import java.sql.SQLException | ||
|
||
private val logger: Logger = logger<NewConnectionActivatedProbe>() | ||
|
||
/** This probe is emitted when a connection error happens in DataGrip. It connects | ||
* directly into DataGrip extension points, so it shouldn't be instantiated directly. | ||
*/ | ||
class ConnectionFailureProbe : DatabaseConnectionManager.Listener { | ||
override fun connectionChanged( | ||
connection: DatabaseConnection, | ||
added: Boolean, | ||
) { | ||
} | ||
|
||
override fun connectionFailed( | ||
project: Project, | ||
connectionPoint: DatabaseConnectionPoint, | ||
th: Throwable, | ||
) { | ||
if (!connectionPoint.dataSource.isMongoDbDataSource()) { | ||
return | ||
} | ||
|
||
val exception = if (th is SQLException) th.cause else th | ||
|
||
val application = ApplicationManager.getApplication() | ||
val logMessage = application.getService(LogMessage::class.java) | ||
val telemetryService = application.getService(TelemetryService::class.java) | ||
|
||
val readModelProvider = project.getService(DataGripBasedReadModelProvider::class.java) | ||
val dataSource = connectionPoint.dataSource | ||
val serverInfo = readModelProvider.slice(dataSource, BuildInfo.Slice) | ||
val telemetryEvent = | ||
if (exception is RemoteObject.ForeignException) { | ||
connectionFailureEvent(extractMongoDbExceptionCode(exception), exception.originalClassName!!, | ||
serverInfo) | ||
} else { | ||
connectionFailureEvent("<unk>", th.javaClass.simpleName, serverInfo) | ||
} | ||
|
||
telemetryService.sendEvent(telemetryEvent) | ||
logger.warn( | ||
logMessage | ||
.message("Failure connecting to cluster") | ||
.put("isMongoDBException", exception is RemoteObject.ForeignException) | ||
.put("exceptionMessage", th.message ?: "<no-message>") | ||
.put("stackTrace", th.stackTrace.joinToString()) | ||
.mergeTelemetryEventProperties(telemetryEvent) | ||
.build(), | ||
) | ||
} | ||
|
||
private fun extractMongoDbExceptionCode(exception: RemoteObject.ForeignException): String { | ||
val originalCause = exception.cause?.message | ||
val errorCode = | ||
originalCause?.let { | ||
Regex("""\d+""").find(originalCause)?.value ?: "<unk>" | ||
} ?: "-1" | ||
return errorCode | ||
} | ||
|
||
private fun connectionFailureEvent( | ||
errorCode: String, | ||
errorName: String, | ||
serverInfo: BuildInfo, | ||
) = TelemetryEvent.ConnectionError( | ||
errorCode = errorCode, | ||
errorName = errorName, | ||
isAtlas = serverInfo.isAtlas, | ||
isLocalhost = serverInfo.isLocalhost, | ||
isEnterprise = serverInfo.isEnterprise, | ||
isGenuine = serverInfo.isGenuineMongoDb, | ||
nonGenuineServerName = serverInfo.nonGenuineVariant, | ||
serverOsFamily = serverInfo.buildEnvironment["target_os"], | ||
isLocalAtlas = serverInfo.isLocalAtlas, | ||
version = serverInfo.version, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...in/src/test/kotlin/com/mongodb/jbplugin/observability/probe/ConnectionFailureProbeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.mongodb.jbplugin.observability.probe | ||
|
||
import com.intellij.database.dataSource.DatabaseConnectionPoint | ||
import com.intellij.database.dataSource.LocalDataSource | ||
import com.intellij.openapi.application.Application | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.util.CachedValuesManager | ||
import com.intellij.util.CachedValuesManagerImpl | ||
import com.mongodb.jbplugin.fixtures.* | ||
import com.mongodb.jbplugin.fixtures.mockLogMessage | ||
import com.mongodb.jbplugin.observability.TelemetryProperty | ||
import com.mongodb.jbplugin.observability.TelemetryService | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.Test | ||
import org.mockito.Mockito.`when` | ||
import org.mockito.Mockito.mock | ||
import org.mockito.kotlin.argThat | ||
import org.mockito.kotlin.verify | ||
|
||
import java.rmi.server.RemoteObject | ||
import java.sql.SQLException | ||
|
||
import kotlinx.coroutines.runBlocking | ||
|
||
@IntegrationTest | ||
class ConnectionFailureProbeTest { | ||
@Test | ||
fun `should infer the relevant info from the connection string`( | ||
application: Application, | ||
project: Project, | ||
) = runBlocking { | ||
val telemetryService = mock<TelemetryService>() | ||
val logMessage = mockLogMessage() | ||
val connectionPoint = mock<DatabaseConnectionPoint>() | ||
val dataSource = mock<LocalDataSource>() | ||
|
||
`when`(connectionPoint.dataSource).thenReturn(dataSource) | ||
application.withMockedService(telemetryService) | ||
application.withMockedService(logMessage) | ||
|
||
project.withMockedUnconnectedMongoDbConnection(MongoDbServerUrl("mongodb://localhost")) | ||
val probe = ConnectionFailureProbe() | ||
|
||
project.withMockedService<Project, CachedValuesManager>(CachedValuesManagerImpl(project)) | ||
probe.connectionFailed(project, connectionPoint, Throwable()) | ||
|
||
verify(telemetryService).sendEvent( | ||
argThat { event -> | ||
event.properties[TelemetryProperty.IS_ATLAS] == false && | ||
event.properties[TelemetryProperty.IS_LOCAL_ATLAS] == false && | ||
event.properties[TelemetryProperty.IS_LOCALHOST] == true && | ||
event.properties[TelemetryProperty.IS_ENTERPRISE] == false && | ||
event.properties[TelemetryProperty.IS_GENUINE] == true && | ||
event.properties[TelemetryProperty.VERSION] == "<unknown>" && | ||
event.properties[TelemetryProperty.ERROR_CODE] == "<unk>" && | ||
event.properties[TelemetryProperty.ERROR_NAME] == "Throwable" | ||
}, | ||
) | ||
} | ||
|
||
@Test | ||
fun `should infer the relevant connection error from the exception message`( | ||
application: Application, | ||
project: Project, | ||
) = runBlocking { | ||
val telemetryService = mock<TelemetryService>() | ||
val logMessage = mockLogMessage() | ||
val connectionPoint = mock<DatabaseConnectionPoint>() | ||
val dataSource = mock<LocalDataSource>() | ||
|
||
`when`(connectionPoint.dataSource).thenReturn(dataSource) | ||
application.withMockedService(telemetryService) | ||
application.withMockedService(logMessage) | ||
|
||
project.withMockedUnconnectedMongoDbConnection(MongoDbServerUrl("mongodb://localhost")) | ||
val probe = ConnectionFailureProbe() | ||
|
||
project.withMockedService<Project, CachedValuesManager>(CachedValuesManagerImpl(project)) | ||
|
||
val innerException = Exception( | ||
"com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed):" | ||
) | ||
val remoteWrapper = | ||
com.intellij.execution.rmi.RemoteObject.ForeignException( | ||
"Error in the driver", | ||
"com.mongodb.MongoCommandException", | ||
) | ||
remoteWrapper.initCause(innerException) | ||
val sqlException = SQLException(remoteWrapper) | ||
|
||
probe.connectionFailed(project, connectionPoint, sqlException) | ||
|
||
verify(telemetryService).sendEvent( | ||
argThat { event -> | ||
event.properties[TelemetryProperty.IS_ATLAS] == false && | ||
event.properties[TelemetryProperty.ERROR_CODE] == "18" && | ||
event.properties[TelemetryProperty.ERROR_NAME] == "com.mongodb.MongoCommandException" | ||
}, | ||
) | ||
} | ||
} |
Oops, something went wrong.