Skip to content

Commit

Permalink
Migrate telemetry/recordEvent (#2661)
Browse files Browse the repository at this point in the history
This PR is a part of the protocol migration. Some endpoints are written
by hand. We are switching to the protocol generated from Cody. In this
PR:
- [Migrate
telemetry/recordEvent](1e91c0b)

## Test plan
1. All migrated endpoints have been verified with debugger (and reviewed
in the trace log).
  • Loading branch information
mkondratek authored Nov 19, 2024
1 parent bbb60f7 commit 6cf4688
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 72 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@

public class CodeEditorFactory {

public static final int spaceBetweenButtons = 5;

public static volatile String lastCopiedText = null;
}
10 changes: 5 additions & 5 deletions src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package com.sourcegraph.cody.agent

import com.sourcegraph.cody.agent.protocol.NetworkRequest
import com.sourcegraph.cody.agent.protocol.TelemetryEvent
import com.sourcegraph.cody.agent.protocol_generated.AutocompleteParams
import com.sourcegraph.cody.agent.protocol_generated.AutocompleteResult
import com.sourcegraph.cody.agent.protocol_generated.Chat_ImportParams
Expand Down Expand Up @@ -135,6 +134,11 @@ interface _SubsetGeneratedCodyAgentServer {
@JsonRequest("testing/ignore/overridePolicy")
fun testing_ignore_overridePolicy(params: ContextFilters?): CompletableFuture<Null?>

@JsonRequest("telemetry/recordEvent")
fun telemetry_recordEvent(
params: com.sourcegraph.cody.agent.protocol_generated.TelemetryEvent
): CompletableFuture<Null?>

// // =============
// // Notifications
// // =============
Expand Down Expand Up @@ -182,10 +186,6 @@ interface _SubsetGeneratedCodyAgentServer {
* works similar to JavaScript Proxy.
*/
interface _LegacyAgentServer {

@JsonRequest("telemetry/recordEvent")
fun recordEvent(event: TelemetryEvent): CompletableFuture<Void?>

@JsonRequest("testing/requestErrors")
fun testingRequestErrors(): CompletableFuture<List<NetworkRequest>>
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sourcegraph.cody.agent.protocol_extensions

object BillingMetadata {
object Product {
const val CODY = "cody"
}

object Category {
const val CORE = "core"
const val BILLABLE = "billable"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.JBPopupMenu
import com.intellij.ui.awt.RelativePoint
import com.sourcegraph.cody.agent.protocol.BillingCategory
import com.sourcegraph.cody.agent.protocol.BillingMetadata
import com.sourcegraph.cody.agent.protocol.BillingProduct
import com.sourcegraph.cody.agent.protocol.TelemetryEventParameters
import com.sourcegraph.cody.agent.protocol_extensions.BillingMetadata
import com.sourcegraph.cody.agent.protocol_generated.BillingMetadataParams
import com.sourcegraph.cody.agent.protocol_generated.ParametersParams
import com.sourcegraph.cody.auth.ui.AccountsListModel
import com.sourcegraph.cody.auth.ui.AccountsListModelBase
import com.sourcegraph.cody.telemetry.TelemetryV2
Expand Down Expand Up @@ -71,8 +70,10 @@ class CodyAccountListModel(private val project: Project) :
project,
"auth.signin.token",
"clicked",
TelemetryEventParameters(
billingMetadata = BillingMetadata(BillingProduct.CODY, BillingCategory.BILLABLE)))
ParametersParams(
billingMetadata =
BillingMetadataParams(
BillingMetadata.Product.CODY, BillingMetadata.Category.BILLABLE)))

val account = CodyAccount(login, displayName, server, id)
if (accountsListModel.isEmpty) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.sourcegraph.cody.config

import com.intellij.openapi.project.Project
import com.sourcegraph.cody.agent.protocol.BillingCategory
import com.sourcegraph.cody.agent.protocol.BillingMetadata
import com.sourcegraph.cody.agent.protocol.BillingProduct
import com.sourcegraph.cody.agent.protocol.TelemetryEventParameters
import com.sourcegraph.cody.agent.protocol_extensions.BillingMetadata
import com.sourcegraph.cody.agent.protocol_generated.BillingMetadataParams
import com.sourcegraph.cody.agent.protocol_generated.ParametersParams
import com.sourcegraph.cody.telemetry.TelemetryV2

class CodyPersistentAccountsHost(private val project: Project) : CodyAccountsHost {
Expand All @@ -19,8 +18,10 @@ class CodyPersistentAccountsHost(private val project: Project) : CodyAccountsHos
project,
"auth.signin.token",
"clicked",
TelemetryEventParameters(
billingMetadata = BillingMetadata(BillingProduct.CODY, BillingCategory.BILLABLE)))
ParametersParams(
billingMetadata =
BillingMetadataParams(
BillingMetadata.Product.CODY, BillingMetadata.Category.BILLABLE)))

val codyAccount = CodyAccount(login, displayName, server, id)
val authManager = CodyAuthenticationManager.getInstance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.PlatformCoreDataKeys
import com.intellij.openapi.project.Project
import com.intellij.util.ui.JBUI
import com.sourcegraph.cody.agent.protocol.BillingCategory
import com.sourcegraph.cody.agent.protocol.BillingMetadata
import com.sourcegraph.cody.agent.protocol.BillingProduct
import com.sourcegraph.cody.agent.protocol.TelemetryEventParameters
import com.sourcegraph.cody.agent.protocol_extensions.BillingMetadata
import com.sourcegraph.cody.agent.protocol_generated.BillingMetadataParams
import com.sourcegraph.cody.agent.protocol_generated.ParametersParams
import com.sourcegraph.cody.api.SourcegraphApiRequestExecutor
import com.sourcegraph.cody.auth.SsoAuthMethod
import com.sourcegraph.cody.telemetry.TelemetryV2
Expand All @@ -28,8 +27,10 @@ class LogInToSourcegraphAction : BaseAddAccountWithTokenAction() {
it,
"auth.login",
"clicked",
TelemetryEventParameters(
billingMetadata = BillingMetadata(BillingProduct.CODY, BillingCategory.BILLABLE)))
ParametersParams(
billingMetadata =
BillingMetadataParams(
BillingMetadata.Product.CODY, BillingMetadata.Category.BILLABLE)))
}

val accountsHost = getCodyAccountsHost(e) ?: return
Expand Down Expand Up @@ -61,8 +62,10 @@ class AddCodyEnterpriseAccountAction : BaseAddAccountWithTokenAction() {
it,
"auth.login",
"clicked",
TelemetryEventParameters(
billingMetadata = BillingMetadata(BillingProduct.CODY, BillingCategory.BILLABLE)))
ParametersParams(
billingMetadata =
BillingMetadataParams(
BillingMetadata.Product.CODY, BillingMetadata.Category.BILLABLE)))
}

val accountsHost = getCodyAccountsHost(e) ?: return
Expand Down Expand Up @@ -91,12 +94,6 @@ abstract class BaseAddAccountWithTokenAction : DumbAwareEDTAction() {
?: DataManager.getInstance().loadFromDataContext(e.dataContext, CodyAccountsHost.KEY))
}

fun signInWithSourcegraphDialog(project: Project?, parent: Component?): BaseLoginDialog =
SourcegraphTokenLoginDialog(project, parent, SsoAuthMethod.DEFAULT).apply {
title = "Sign in with Sourcegraph"
setLoginButtonText("Sign in")
}

internal class SourcegraphTokenLoginDialog(
project: Project?,
parent: Component?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import com.intellij.openapi.editor.event.BulkAwareDocumentListener
import com.intellij.openapi.editor.event.DocumentEvent
import com.intellij.openapi.project.Project
import com.sourcegraph.cody.agent.CodyAgentService
import com.sourcegraph.cody.agent.protocol.BillingCategory
import com.sourcegraph.cody.agent.protocol.BillingMetadata
import com.sourcegraph.cody.agent.protocol.BillingProduct
import com.sourcegraph.cody.agent.protocol_extensions.BillingMetadata
import com.sourcegraph.cody.agent.protocol_extensions.ProtocolTextDocumentExt
import com.sourcegraph.cody.agent.protocol_generated.BillingMetadataParams
import com.sourcegraph.cody.agent.protocol_generated.CompletionItemParams
import com.sourcegraph.cody.autocomplete.CodyAutocompleteManager
import com.sourcegraph.cody.autocomplete.action.AcceptCodyAutocompleteAction
Expand All @@ -19,6 +18,7 @@ import com.sourcegraph.utils.CodyEditorUtil

class CodyDocumentListener(val project: Project) : BulkAwareDocumentListener {

// todo: it does not work after the migration to webview. see: linear.app/sourcegraph/issue/QA-78
private fun logCodeCopyPastedFromChat(event: DocumentEvent) {
val pastedCode = event.newFragment.toString()
if (pastedCode.isNotBlank() && CodeEditorFactory.lastCopiedText == pastedCode) {
Expand All @@ -28,7 +28,8 @@ class CodyDocumentListener(val project: Project) : BulkAwareDocumentListener {
feature = "keyDown",
action = "paste",
pastedCode,
billingMetadata = BillingMetadata(BillingProduct.CODY, BillingCategory.CORE))
billingMetadata =
BillingMetadataParams(BillingMetadata.Product.CODY, BillingMetadata.Category.CORE))
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/main/kotlin/com/sourcegraph/cody/telemetry/TelemetryV2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.sourcegraph.cody.telemetry
import com.intellij.openapi.application.ApplicationInfo
import com.intellij.openapi.project.Project
import com.sourcegraph.cody.agent.CodyAgentService
import com.sourcegraph.cody.agent.protocol.BillingMetadata
import com.sourcegraph.cody.agent.protocol.TelemetryEvent
import com.sourcegraph.cody.agent.protocol.TelemetryEventParameters
import com.sourcegraph.cody.agent.protocol_generated.BillingMetadataParams
import com.sourcegraph.cody.agent.protocol_generated.ParametersParams
import com.sourcegraph.cody.agent.protocol_generated.TelemetryEvent
import com.sourcegraph.config.ConfigUtil

class TelemetryV2 {
Expand All @@ -14,18 +14,18 @@ class TelemetryV2 {
project: Project,
feature: String,
action: String,
parameters: TelemetryEventParameters? = null
parameters: ParametersParams? = null
) {
val versionParameters =
mapOf(
"ideProductCode" to ConfigUtil.getIntellijProductCode(),
"ideBaselineVersion" to ApplicationInfo.getInstance().build.baselineVersion.toLong())
val baseParameters = parameters ?: TelemetryEventParameters()
val baseParameters = parameters ?: ParametersParams()
val newParameters =
baseParameters.copy(metadata = baseParameters.metadata?.plus(versionParameters))

CodyAgentService.withAgent(project) { agent ->
agent.server.recordEvent(
agent.server.telemetry_recordEvent(
TelemetryEvent(feature = "cody.$feature", action = action, parameters = newParameters))
}
}
Expand All @@ -35,7 +35,7 @@ class TelemetryV2 {
feature: String,
action: String,
code: String,
billingMetadata: BillingMetadata? = null
billingMetadata: BillingMetadataParams
) {
val op =
if (action.startsWith("copy")) "copy"
Expand All @@ -51,7 +51,7 @@ class TelemetryV2 {
feature = feature,
action = action,
parameters =
TelemetryEventParameters(
ParametersParams(
metadata = metadata,
privateMetadata = privateMetadata,
billingMetadata = billingMetadata))
Expand Down

0 comments on commit 6cf4688

Please sign in to comment.