-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(telemetry): Add telemetry on signals INTELLIJ-180 #119
Merged
+588
−41
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6f3d104
chore: tmp
kmruiz cb7b558
chore: add error message when querying mongodb fails
kmruiz 3ed204d
Merge branch 'main' into feat/INTELLIJ-180
kmruiz ace3453
chore: add to changelog
kmruiz e26d663
chore: remove refs to deprecated methods
kmruiz 1493e93
chore: fix finished processing inspections, now it tracks resolved in…
kmruiz 0508c95
chore: fix linter
kmruiz 282f91d
chore: fix flaky tests
kmruiz ec4f776
chore: this test was a bit flaky
kmruiz 58ee880
chore: fail safe in case telemetry fails
kmruiz 143e5dd
Merge branch 'main' into feat/INTELLIJ-180
kmruiz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,9 @@ | ||
#!/bin/bash | ||
|
||
./etc/plugin-logs.sh |\ | ||
grep 'inspection_id' |\ | ||
jq '{ inspection: .inspection_type, id: .inspection_id, status: .error_status, meta: { error_field_type: .error_field_type, actual_field_type: .actual_field_type } }' |\ | ||
jq -s |\ | ||
jq 'group_by(.id) | map({ id: .[0].id, inspection: .[0].inspection, status: map(.status), meta: map(.meta) })' | | ||
jq '.[]' | ||
|
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,3 @@ | ||
#!/bin/bash | ||
|
||
cat ./packages/jetbrains-plugin/build/idea-sandbox/system/log/idea.log | grep "pluginVersion" |
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
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
198 changes: 198 additions & 0 deletions
198
.../src/main/kotlin/com/mongodb/jbplugin/observability/probe/InspectionStatusChangedProbe.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,198 @@ | ||
package com.mongodb.jbplugin.observability.probe | ||
|
||
import com.intellij.codeInspection.ProblemsHolder | ||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.openapi.components.Service | ||
import com.intellij.openapi.diagnostic.Logger | ||
import com.intellij.openapi.diagnostic.logger | ||
import com.intellij.openapi.rd.util.launchChildNonUrgentBackground | ||
import com.intellij.psi.PsiElement | ||
import com.mongodb.jbplugin.meta.service | ||
import com.mongodb.jbplugin.mql.Node | ||
import com.mongodb.jbplugin.mql.components.HasSourceDialect | ||
import com.mongodb.jbplugin.observability.TelemetryEvent | ||
import com.mongodb.jbplugin.observability.TelemetryEvent.InspectionStatusChangeEvent.InspectionType | ||
import com.mongodb.jbplugin.observability.TelemetryService | ||
import com.mongodb.jbplugin.observability.probe.InspectionStatusChangedProbe.UniqueInspection | ||
import com.mongodb.jbplugin.observability.useLogMessage | ||
import kotlinx.coroutines.CoroutineScope | ||
import java.lang.ref.WeakReference | ||
import java.util.* | ||
import java.util.concurrent.ConcurrentHashMap | ||
import java.util.concurrent.CopyOnWriteArrayList | ||
import java.util.concurrent.locks.ReentrantLock | ||
import kotlin.concurrent.withLock | ||
|
||
private val logger: Logger = logger<InspectionStatusChangedProbe>() | ||
|
||
internal typealias ProblemsByInspectionType = MutableMap<InspectionType, MutableList<UniqueInspection>> | ||
|
||
@Service | ||
class InspectionStatusChangedProbe( | ||
private val cs: CoroutineScope | ||
) { | ||
internal data class UniqueInspection(val id: UUID, val on: WeakReference<Node<PsiElement>>) { | ||
companion object { | ||
fun new(query: Node<PsiElement>): UniqueInspection { | ||
return UniqueInspection(UUID.randomUUID(), WeakReference(query)) | ||
} | ||
} | ||
} | ||
|
||
private val mutex: ReentrantLock = ReentrantLock() | ||
private val problemsByInspectionType: ProblemsByInspectionType = ConcurrentHashMap() | ||
|
||
/** | ||
* We are using this function because we don't really care much about exceptions here. It's just | ||
* telemetry, and we will just fail safely and wait for the next event to happen. This is better | ||
* than raising an exception to the user. | ||
*/ | ||
private fun runSafe(fn: () -> Unit) { | ||
runCatching { | ||
fn() | ||
}.getOrNull() | ||
} | ||
|
||
fun inspectionChanged(inspectionType: InspectionType, query: Node<PsiElement>) = runSafe { | ||
val dialect = query.component<HasSourceDialect>() ?: return@runSafe | ||
val psiElement = query.source | ||
|
||
cs.launchChildNonUrgentBackground { | ||
mutex.withLock { | ||
val elementsWithProblems = problemsByInspectionType(inspectionType) | ||
|
||
// check if the element is already in the list | ||
if (isElementRegistered(elementsWithProblems) { psiElement }) { | ||
// do nothing, it's already registered | ||
return@launchChildNonUrgentBackground | ||
} | ||
|
||
// it's a new error, send a telemetry event and store it | ||
val inspection = UniqueInspection.new(query) | ||
elementsWithProblems.add(inspection) | ||
|
||
val telemetry by service<TelemetryService>() | ||
val event = TelemetryEvent.InspectionStatusChangeEvent( | ||
dialect = dialect.name, | ||
inspectionType = inspectionType, | ||
inspectionStatus = TelemetryEvent.InspectionStatusChangeEvent.InspectionStatus.ACTIVE, | ||
null, | ||
null | ||
) | ||
|
||
telemetry.sendEvent(event) | ||
logger.info( | ||
useLogMessage("New inspection triggered") | ||
.put("inspection_id", inspection.id.toString()) | ||
.mergeTelemetryEventProperties(event) | ||
.build() | ||
) | ||
} | ||
} | ||
} | ||
|
||
fun typeMismatchInspectionActive(query: Node<PsiElement>, actualType: String, expectedType: String) = runSafe { | ||
val inspectionType = InspectionType.TYPE_MISMATCH | ||
val dialect = query.component<HasSourceDialect>() ?: return@runSafe | ||
val psiElement = query.source | ||
|
||
cs.launchChildNonUrgentBackground { | ||
mutex.withLock { | ||
val elementsWithProblems = problemsByInspectionType(inspectionType) | ||
|
||
if (isElementRegistered(elementsWithProblems) { psiElement }) { | ||
// do nothing, it's already registered | ||
return@launchChildNonUrgentBackground | ||
} | ||
|
||
// it's a new error, send a telemetry event and store it | ||
val inspection = UniqueInspection.new(query) | ||
elementsWithProblems.add(inspection) | ||
|
||
val telemetry by service<TelemetryService>() | ||
val event = TelemetryEvent.InspectionStatusChangeEvent( | ||
dialect = dialect.name, | ||
inspectionType = inspectionType, | ||
inspectionStatus = TelemetryEvent.InspectionStatusChangeEvent.InspectionStatus.ACTIVE, | ||
actualFieldType = actualType, | ||
expectedFieldType = expectedType, | ||
) | ||
|
||
telemetry.sendEvent(event) | ||
logger.info( | ||
useLogMessage("New inspection triggered") | ||
.put("inspection_id", inspection.id.toString()) | ||
.mergeTelemetryEventProperties(event) | ||
.build() | ||
) | ||
} | ||
} | ||
} | ||
|
||
fun finishedProcessingInspections(inspectionType: InspectionType, problemsHolder: ProblemsHolder) = runSafe { | ||
cs.launchChildNonUrgentBackground { | ||
val results = problemsHolder.results | ||
// check all our registered problems | ||
// if at the end of the processing cycle it's empty | ||
// we will assume they are | ||
mutex.withLock { | ||
val elementsWithProblems = problemsByInspectionType(inspectionType) | ||
|
||
for (elementWithProblem in elementsWithProblems) { | ||
val findEquivalentProblem = results.find { | ||
isElementRegistered(elementsWithProblems, it::getPsiElement) | ||
} | ||
if (findEquivalentProblem != null) { | ||
// the problem is still there, so don't do anything | ||
// do nothing, it's already registered | ||
continue | ||
} | ||
|
||
elementsWithProblems.remove(elementWithProblem) | ||
|
||
val dialect = | ||
elementWithProblem.on.get()?.component<HasSourceDialect>() ?: continue | ||
val telemetry by service<TelemetryService>() | ||
val event = TelemetryEvent.InspectionStatusChangeEvent( | ||
dialect = dialect.name, | ||
inspectionType = inspectionType, | ||
inspectionStatus = TelemetryEvent.InspectionStatusChangeEvent.InspectionStatus.RESOLVED, | ||
null, | ||
null | ||
) | ||
|
||
telemetry.sendEvent(event) | ||
logger.info( | ||
useLogMessage("Inspection resolved") | ||
.put("inspection_id", elementWithProblem.id.toString()) | ||
.mergeTelemetryEventProperties(event) | ||
.build() | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun isElementRegistered( | ||
elementsWithProblems: MutableList<UniqueInspection>, | ||
psiElement: () -> PsiElement | ||
): Boolean = runCatching { | ||
ApplicationManager.getApplication().runReadAction<Boolean> { | ||
elementsWithProblems.find { | ||
val isStrictlyEqual = it.on.get()?.source == psiElement() | ||
val isEquivalent = it.on.get()?.source?.isEquivalentTo(psiElement()) == true | ||
|
||
isStrictlyEqual || isEquivalent | ||
} != null | ||
} | ||
}.getOrDefault(false) | ||
|
||
private fun problemsByInspectionType(inspectionType: InspectionType): MutableList<UniqueInspection> { | ||
val result = problemsByInspectionType.computeIfAbsent(inspectionType) { | ||
CopyOnWriteArrayList() | ||
} | ||
|
||
result.removeAll { it.on.get() == null } | ||
return result | ||
} | ||
} |
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
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
153 changes: 153 additions & 0 deletions
153
.../test/kotlin/com/mongodb/jbplugin/observability/probe/InspectionStatusChangedProbeTest.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,153 @@ | ||
package com.mongodb.jbplugin.observability.probe | ||
|
||
import com.intellij.codeInspection.ProblemsHolder | ||
import com.intellij.openapi.application.Application | ||
import com.intellij.psi.PsiElement | ||
import com.mongodb.jbplugin.fixtures.IntegrationTest | ||
import com.mongodb.jbplugin.fixtures.mockLogMessage | ||
import com.mongodb.jbplugin.fixtures.withMockedService | ||
import com.mongodb.jbplugin.mql.Node | ||
import com.mongodb.jbplugin.mql.components.HasSourceDialect | ||
import com.mongodb.jbplugin.observability.TelemetryEvent | ||
import com.mongodb.jbplugin.observability.TelemetryService | ||
import kotlinx.coroutines.test.TestScope | ||
import kotlinx.coroutines.test.advanceUntilIdle | ||
import org.junit.jupiter.api.Test | ||
import org.mockito.Mockito.`when` | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.timeout | ||
import org.mockito.kotlin.times | ||
import org.mockito.kotlin.verify | ||
|
||
@IntegrationTest | ||
internal class InspectionStatusChangedProbeTest { | ||
@Test | ||
fun `should send a InspectionStatusChangeEvent event when found for the first time`( | ||
application: Application, | ||
testScope: TestScope | ||
) { | ||
val telemetryService = mock<TelemetryService>() | ||
val dialect = HasSourceDialect.DialectName.entries.toTypedArray().random() | ||
|
||
val query = Node<PsiElement?>(null, listOf(HasSourceDialect(dialect))) as Node<PsiElement> | ||
|
||
application.withMockedService(telemetryService) | ||
.withMockedService(mockLogMessage()) | ||
|
||
val probe = InspectionStatusChangedProbe(testScope) | ||
|
||
probe.inspectionChanged( | ||
TelemetryEvent.InspectionStatusChangeEvent.InspectionType.FIELD_DOES_NOT_EXIST, | ||
query | ||
) | ||
probe.inspectionChanged( | ||
TelemetryEvent.InspectionStatusChangeEvent.InspectionType.FIELD_DOES_NOT_EXIST, | ||
query | ||
) | ||
|
||
testScope.advanceUntilIdle() | ||
|
||
verify(telemetryService, timeout(1000).times(1)).sendEvent(any()) | ||
} | ||
|
||
@Test | ||
fun `should send a InspectionStatusChangeEvent event when multiple types of events`( | ||
application: Application, | ||
testScope: TestScope | ||
) { | ||
val telemetryService = mock<TelemetryService>() | ||
val dialect = HasSourceDialect.DialectName.entries.toTypedArray().random() | ||
|
||
val query = Node<PsiElement?>(null, listOf(HasSourceDialect(dialect))) as Node<PsiElement> | ||
|
||
application.withMockedService(telemetryService) | ||
.withMockedService(mockLogMessage()) | ||
|
||
val probe = InspectionStatusChangedProbe(testScope) | ||
|
||
probe.inspectionChanged( | ||
TelemetryEvent.InspectionStatusChangeEvent.InspectionType.FIELD_DOES_NOT_EXIST, | ||
query | ||
) | ||
probe.inspectionChanged( | ||
TelemetryEvent.InspectionStatusChangeEvent.InspectionType.FIELD_DOES_NOT_EXIST, | ||
query | ||
) | ||
probe.inspectionChanged( | ||
TelemetryEvent.InspectionStatusChangeEvent.InspectionType.NO_NAMESPACE_INFERRED, | ||
query | ||
) | ||
probe.inspectionChanged( | ||
TelemetryEvent.InspectionStatusChangeEvent.InspectionType.NO_NAMESPACE_INFERRED, | ||
query | ||
) | ||
|
||
testScope.advanceUntilIdle() | ||
|
||
verify(telemetryService, timeout(1000).times(2)).sendEvent(any()) | ||
} | ||
|
||
@Test | ||
fun `should send a InspectionStatusChangeEvent event with type checking issues`( | ||
application: Application, | ||
testScope: TestScope | ||
) { | ||
val telemetryService = mock<TelemetryService>() | ||
val dialect = HasSourceDialect.DialectName.entries.toTypedArray().random() | ||
|
||
val query = Node<PsiElement?>(null, listOf(HasSourceDialect(dialect))) as Node<PsiElement> | ||
|
||
application.withMockedService(telemetryService) | ||
.withMockedService(mockLogMessage()) | ||
|
||
val probe = InspectionStatusChangedProbe(testScope) | ||
|
||
probe.typeMismatchInspectionActive(query, "actual", "expected") | ||
|
||
testScope.advanceUntilIdle() | ||
|
||
verify(telemetryService, timeout(1000).times(1)).sendEvent( | ||
TelemetryEvent.InspectionStatusChangeEvent( | ||
dialect, | ||
TelemetryEvent.InspectionStatusChangeEvent.InspectionType.TYPE_MISMATCH, | ||
TelemetryEvent.InspectionStatusChangeEvent.InspectionStatus.ACTIVE, | ||
"actual", | ||
"expected" | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun `should send a resolved InspectionStatusChangeEvent when there is no problem anymore`( | ||
application: Application, | ||
testScope: TestScope | ||
) { | ||
val telemetryService = mock<TelemetryService>() | ||
val problemsHolder = mock<ProblemsHolder>() | ||
|
||
val dialect = HasSourceDialect.DialectName.entries.toTypedArray().random() | ||
|
||
`when`(problemsHolder.results).thenReturn(emptyList()) | ||
|
||
val query = Node<PsiElement?>(null, listOf(HasSourceDialect(dialect))) as Node<PsiElement> | ||
|
||
application.withMockedService(telemetryService) | ||
.withMockedService(mockLogMessage()) | ||
|
||
val probe = InspectionStatusChangedProbe(testScope) | ||
|
||
probe.inspectionChanged( | ||
TelemetryEvent.InspectionStatusChangeEvent.InspectionType.FIELD_DOES_NOT_EXIST, | ||
query | ||
) | ||
probe.finishedProcessingInspections( | ||
TelemetryEvent.InspectionStatusChangeEvent.InspectionType.FIELD_DOES_NOT_EXIST, | ||
problemsHolder | ||
) | ||
|
||
testScope.advanceUntilIdle() | ||
|
||
verify(telemetryService, timeout(1000).times(2)).sendEvent(any()) | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice catch 👁️