Skip to content

Commit

Permalink
telemetry: Add billing categorization to cody events 2 (#2345)
Browse files Browse the repository at this point in the history
Adds billing metadata to some events per RFC 978:
https://docs.google.com/document/d/1EvyH1kaU-fsn59h-DyoaO2Qy4C2TYLmDGjG73bdb2V4/edit#heading=h.trqab8y0kufp

follow on to sourcegraph/cody#5493 and
sourcegraph/sourcegraph#529

## Test plan

CI

---------

Co-authored-by: Dan Adler <5589410+dadlerj@users.noreply.github.com>
  • Loading branch information
dadlerj and dadlerj authored Oct 4, 2024
1 parent 99d9e50 commit 873011e
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 8 deletions.
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(
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))
}
}
}

0 comments on commit 873011e

Please sign in to comment.