Skip to content
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

telemetry: Add billing categorization to cody events 2 #2345

Merged
merged 3 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
package com.sourcegraph.cody.agent.protocol

enum class BillingProduct {
CODY
}

enum class BillingCategory {
CORE,
BILLABLE
}

data class BillingMetadata(val product: BillingProduct, val category: BillingCategory)

data class TelemetryEventParameters(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, this protocol class is not generated

interface TelemetryEvent {
    feature: string
    action: string
    parameters?:
        | TelemetryEventParameters<{ [key: string]: number }, BillingProduct, BillingCategory>
        | undefined
        | null
}

It looks like our generating tool does not support fields like parameters in this case. I'd be nice to get it working.

val version: Long? = null,
val interactionID: String? = null,
val metadata: Map<String, Long>? = null,
val billingMetadata: BillingMetadata? = null,
val privateMetadata: Map<String, String>? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ 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.auth.ui.AccountsListModel
import com.sourcegraph.cody.auth.ui.AccountsListModelBase
import com.sourcegraph.cody.telemetry.TelemetryV2
Expand Down Expand Up @@ -63,7 +67,12 @@ class CodyAccountListModel(private val project: Project) :
token: String,
id: String
) {
TelemetryV2.sendTelemetryEvent(project, "auth.signin.token", "clicked")
TelemetryV2.sendTelemetryEvent(
project,
"auth.signin.token",
"clicked",
TelemetryEventParameters(
billingMetadata = BillingMetadata(BillingProduct.CODY, BillingCategory.BILLABLE)))

val account = CodyAccount(login, displayName, server, id)
if (accountsListModel.isEmpty) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
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.telemetry.TelemetryV2

class CodyPersistentAccountsHost(private val project: Project) : CodyAccountsHost {
Expand All @@ -11,7 +15,12 @@ class CodyPersistentAccountsHost(private val project: Project) : CodyAccountsHos
token: String,
id: String
) {
TelemetryV2.sendTelemetryEvent(project, "auth.signin.token", "clicked")
TelemetryV2.sendTelemetryEvent(
project,
"auth.signin.token",
"clicked",
TelemetryEventParameters(
billingMetadata = BillingMetadata(BillingProduct.CODY, BillingCategory.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,6 +5,10 @@ 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.api.SourcegraphApiRequestExecutor
import com.sourcegraph.cody.auth.SsoAuthMethod
import com.sourcegraph.cody.telemetry.TelemetryV2
Expand All @@ -19,7 +23,14 @@ class LogInToSourcegraphAction : BaseAddAccountWithTokenAction() {
get() = SourcegraphServerPath.DEFAULT_HOST

override fun actionPerformed(e: AnActionEvent) {
e.project?.let { TelemetryV2.sendTelemetryEvent(it, "auth.login", "clicked") }
e.project?.let {
TelemetryV2.sendTelemetryEvent(
it,
"auth.login",
"clicked",
TelemetryEventParameters(
billingMetadata = BillingMetadata(BillingProduct.CODY, BillingCategory.BILLABLE)))
}

val accountsHost = getCodyAccountsHost(e) ?: return
val authMethod: SsoAuthMethod =
Expand All @@ -45,7 +56,14 @@ class AddCodyEnterpriseAccountAction : BaseAddAccountWithTokenAction() {
get() = ""

override fun actionPerformed(e: AnActionEvent) {
e.project?.let { TelemetryV2.sendTelemetryEvent(it, "auth.login", "clicked") }
e.project?.let {
TelemetryV2.sendTelemetryEvent(
it,
"auth.login",
"clicked",
TelemetryEventParameters(
billingMetadata = BillingMetadata(BillingProduct.CODY, BillingCategory.BILLABLE)))
}

val accountsHost = getCodyAccountsHost(e) ?: return
val dialog =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ class PostStartupActivity : ProjectActivity {
.connect(disposable)
.subscribe(AppTopics.FILE_DOCUMENT_SYNC, CodySettingsChangeListener(project))

TelemetryV2.sendTelemetryEvent(project, "cody.extension", "started")
TelemetryV2.sendTelemetryEvent(project, "extension", "started")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +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.CompletionItemParams
import com.sourcegraph.cody.agent.protocol.ProtocolTextDocument
import com.sourcegraph.cody.autocomplete.CodyAutocompleteManager
Expand All @@ -21,7 +24,11 @@ class CodyDocumentListener(val project: Project) : BulkAwareDocumentListener {
if (pastedCode.isNotBlank() && CodeEditorFactory.lastCopiedText == pastedCode) {
CodeEditorFactory.lastCopiedText = null
TelemetryV2.sendCodeGenerationEvent(
project, feature = "keyDown", action = "paste", pastedCode)
project,
feature = "keyDown",
action = "paste",
pastedCode,
billingMetadata = BillingMetadata(BillingProduct.CODY, BillingCategory.CORE))
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/main/kotlin/com/sourcegraph/cody/telemetry/TelemetryV2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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

Expand Down Expand Up @@ -46,7 +47,13 @@ class TelemetryV2 {
}
}

fun sendCodeGenerationEvent(project: Project, feature: String, action: String, code: String) {
fun sendCodeGenerationEvent(
project: Project,
feature: String,
action: String,
code: String,
billingMetadata: BillingMetadata? = null
) {
val op =
if (action.startsWith("copy")) "copy"
else if (action.startsWith("insert")) "insert" else "save"
Expand All @@ -61,7 +68,10 @@ class TelemetryV2 {
feature = feature,
action = action,
parameters =
TelemetryEventParameters(metadata = metadata, privateMetadata = privateMetadata))
TelemetryEventParameters(
metadata = metadata,
privateMetadata = privateMetadata,
billingMetadata = billingMetadata))
}
}
}
Loading