-
Notifications
You must be signed in to change notification settings - Fork 274
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
chore(analytics): include project and user metadata when running commands with noProject #4915
Open
mkhq
wants to merge
7
commits into
main
Choose a base branch
from
analytics-load-project-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bb2786f
chore: store the user id and org with the auth token on login
mkhq ec8f31a
chore(analytics): optionally include user and project metadata for no…
mkhq 532bfbc
test(analytics): introduced tests for noProject commands
mkhq 57d61dc
test(analytics): added cloud user id field to analytics struct
mkhq ada15d8
test(analytics): adding a best-effort verification of auth token vali…
mkhq ed4145c
chore(cloud): refactored save token api to use named params
mkhq 1d826c5
chore(analytics): make sure we use enterpriseDomain/projectId from co…
mkhq 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 |
---|---|---|
|
@@ -15,19 +15,21 @@ import { getPackageVersion, sleep, getDurationMsec } from "../util/util" | |
import { SEGMENT_PROD_API_KEY, SEGMENT_DEV_API_KEY, gardenEnv } from "../constants" | ||
import { Log } from "../logger/log-entry" | ||
import hasha = require("hasha") | ||
import { Garden } from "../garden" | ||
import { DummyGarden, Garden } from "../garden" | ||
import { AnalyticsCommandResult, AnalyticsEventType } from "./analytics-types" | ||
import dedent from "dedent" | ||
import { getGitHubUrl } from "../docs/common" | ||
import { Profile } from "../util/profiling" | ||
import { ModuleConfig } from "../config/module" | ||
import { UserResult } from "@garden-io/platform-api-types" | ||
import { uuidv4 } from "../util/random" | ||
import { GardenError, NodeJSErrnoErrorCodes, StackTraceMetadata } from "../exceptions" | ||
import { ActionConfigMap } from "../actions/types" | ||
import { actionKinds } from "../actions/types" | ||
import { getResultErrorProperties } from "./helpers" | ||
import segmentClient = require("analytics-node") | ||
import { findProjectConfig } from "../config/base" | ||
import { ProjectConfig } from "../config/project" | ||
import { CloudApi, CloudUserProfile, getGardenCloudDomain } from "../cloud/api" | ||
|
||
const CI_USER = "ci-user" | ||
|
||
|
@@ -113,7 +115,9 @@ interface PropertiesBase { | |
enterpriseDomainV2?: string | ||
isLoggedIn: boolean | ||
cloudUserId?: string | ||
cloudDomain?: string | ||
customer?: string | ||
organizationName?: string | ||
ciName: string | null | ||
system: SystemInfo | ||
isCI: boolean | ||
|
@@ -214,6 +218,7 @@ interface IdentifyEvent { | |
traits: { | ||
userIdV2: string | ||
customer?: string | ||
organizationName?: string | ||
platform: string | ||
platformVersion: string | ||
gardenVersion: string | ||
|
@@ -270,13 +275,15 @@ export class AnalyticsHandler { | |
private enterpriseProjectIdV2?: string | ||
private enterpriseDomainV2?: string | ||
private isLoggedIn: boolean | ||
// These are set for a logged in user | ||
private cloudUserId?: string | ||
private cloudCustomerName?: string | ||
private cloudOrganizationName?: string | ||
private cloudDomain?: string | ||
private ciName: string | null | ||
private systemConfig: SystemInfo | ||
private isCI: boolean | ||
private sessionId: string | ||
private pendingEvents: Map<string, SegmentEvent> | ||
private pendingEvents: Map<string, SegmentEvent | IdentifyEvent> | ||
protected garden: Garden | ||
private projectMetadata: ProjectMetadata | ||
public isEnabled: boolean | ||
|
@@ -292,6 +299,9 @@ export class AnalyticsHandler { | |
cloudUser, | ||
isEnabled, | ||
ciInfo, | ||
projectName, | ||
configuredCloudDomain, | ||
configuredCloudProjectId, | ||
}: { | ||
garden: Garden | ||
log: Log | ||
|
@@ -300,8 +310,11 @@ export class AnalyticsHandler { | |
moduleConfigs: ModuleConfig[] | ||
actionConfigs: ActionConfigMap | ||
isEnabled: boolean | ||
cloudUser?: UserResult | ||
cloudUser?: CloudUserProfile | ||
ciInfo: CiInfo | ||
projectName: string | ||
configuredCloudDomain?: string | ||
configuredCloudProjectId?: string | ||
}) { | ||
const segmentApiKey = gardenEnv.ANALYTICS_DEV ? SEGMENT_DEV_API_KEY : SEGMENT_PROD_API_KEY | ||
|
||
|
@@ -351,43 +364,43 @@ export class AnalyticsHandler { | |
|
||
const originName = this.garden.vcsInfo.originUrl | ||
|
||
const projectName = this.garden.projectName | ||
this.projectName = AnalyticsHandler.hash(projectName) | ||
this.projectNameV2 = AnalyticsHandler.hashV2(projectName) | ||
|
||
// Note, this is not the project id from the Project config, its referred to as enterpriseProjectId below | ||
const projectId = originName || this.projectName | ||
this.projectId = AnalyticsHandler.hash(projectId) | ||
this.projectIdV2 = AnalyticsHandler.hashV2(projectId) | ||
|
||
// The enterprise project ID is the UID for this project in Garden Cloud that the user puts | ||
// in the project level Garden configuration. Not to be confused with the anonymized project ID we generate from | ||
// the project name for the purpose of analytics. | ||
const enterpriseProjectId = this.garden.projectId | ||
if (enterpriseProjectId) { | ||
this.enterpriseProjectId = AnalyticsHandler.hash(enterpriseProjectId) | ||
this.enterpriseProjectIdV2 = AnalyticsHandler.hashV2(enterpriseProjectId) | ||
} | ||
|
||
const enterpriseDomain = this.garden.cloudDomain | ||
if (enterpriseDomain) { | ||
this.enterpriseDomain = AnalyticsHandler.hash(enterpriseDomain) | ||
this.enterpriseDomainV2 = AnalyticsHandler.hashV2(enterpriseDomain) | ||
if (configuredCloudProjectId && configuredCloudDomain) { | ||
this.enterpriseProjectId = AnalyticsHandler.hash(configuredCloudProjectId) | ||
this.enterpriseProjectIdV2 = AnalyticsHandler.hashV2(configuredCloudProjectId) | ||
this.enterpriseDomain = AnalyticsHandler.hash(configuredCloudDomain) | ||
this.enterpriseDomainV2 = AnalyticsHandler.hashV2(configuredCloudDomain) | ||
} | ||
|
||
// A user can be logged in to the community tier | ||
if (cloudUser) { | ||
this.cloudUserId = AnalyticsHandler.makeCloudUserId(cloudUser) | ||
this.cloudCustomerName = cloudUser.organization.name | ||
this.cloudUserId = AnalyticsHandler.makeUniqueCloudUserId(cloudUser) | ||
this.cloudOrganizationName = cloudUser.organizationName | ||
this.cloudDomain = this.garden.cloudDomain | ||
this.isLoggedIn = true | ||
} | ||
|
||
this.isRecurringUser = getIsRecurringUser(analyticsConfig.firstRunAt, analyticsConfig.latestRunAt) | ||
|
||
const userIdV2 = AnalyticsHandler.hashV2(anonymousUserId) | ||
|
||
this.identify({ | ||
userId: this.cloudUserId, | ||
anonymousId: anonymousUserId, | ||
traits: { | ||
userIdV2, | ||
customer: cloudUser?.organization.name, | ||
customer: cloudUser?.organizationName, | ||
organizationName: cloudUser?.organizationName, | ||
platform: platform(), | ||
platformVersion: release(), | ||
gardenVersion: getPackageVersion(), | ||
|
@@ -445,15 +458,58 @@ export class AnalyticsHandler { | |
const moduleConfigs = await garden.getRawModuleConfigs() | ||
const actionConfigs = await garden.getRawActionConfigs() | ||
|
||
let cloudUser: UserResult | undefined | ||
let cloudUser: CloudUserProfile | undefined | ||
if (garden.cloudApi) { | ||
try { | ||
cloudUser = await garden.cloudApi?.getProfile() | ||
const userProfile = await garden.cloudApi?.getProfile() | ||
|
||
if (userProfile && userProfile.id && userProfile.organization.name) { | ||
cloudUser = { | ||
userId: userProfile.id, | ||
organizationName: userProfile.organization.name, | ||
domain: garden.cloudApi.domain, | ||
} | ||
} | ||
} catch (err) { | ||
log.debug(`Getting profile from API failed with error: ${err}`) | ||
} | ||
} | ||
|
||
// best effort load the project if this is a dummy garden instance | ||
let projectName = garden.projectName | ||
|
||
let projectConfig: ProjectConfig | undefined | ||
|
||
if (garden instanceof DummyGarden) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we extract this to a separate function and update the control flow so that it doesn't have all these nested if statements? We can instead return early with something like:
|
||
// Not logged in and this is a dummy instance, try to best effort retrieve the config | ||
projectConfig = await findProjectConfig({ log, path: garden.projectRoot }) | ||
|
||
// override the project name since it will default to no-project | ||
if (projectConfig) { | ||
projectName = projectConfig.name | ||
|
||
if (!garden.cloudApi) { | ||
const fallbackCloudDomain = getGardenCloudDomain(projectConfig.domain) | ||
|
||
// fallback to the stored user profile (this is done without verifying the token and the content) | ||
const userProfile = await CloudApi.getAuthTokenUserProfile(log, garden.globalConfigStore, fallbackCloudDomain) | ||
|
||
if (userProfile) { | ||
cloudUser = { | ||
userId: userProfile.userId, | ||
organizationName: userProfile.organizationName, | ||
domain: fallbackCloudDomain, | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
projectConfig = garden.getProjectConfig() | ||
} | ||
|
||
const configuredCloudDomain = projectConfig?.domain | ||
const configuredCloudProjectId = projectConfig?.id | ||
|
||
if (isFirstRun && !ciInfo.isCi) { | ||
const gitHubUrl = getGitHubUrl("docs/misc/telemetry.md") | ||
const msg = dedent` | ||
|
@@ -502,6 +558,9 @@ export class AnalyticsHandler { | |
isEnabled, | ||
ciInfo, | ||
anonymousUserId, | ||
projectName, | ||
configuredCloudDomain, | ||
configuredCloudProjectId, | ||
}) | ||
} | ||
|
||
|
@@ -529,8 +588,8 @@ export class AnalyticsHandler { | |
} | ||
} | ||
|
||
static makeCloudUserId(cloudUser: UserResult) { | ||
return `${cloudUser.organization.name}_${cloudUser.id}` | ||
static makeUniqueCloudUserId(cloudUser: CloudUserProfile) { | ||
return `${cloudUser.organizationName}_${cloudUser.userId}` | ||
} | ||
|
||
/** | ||
|
@@ -547,8 +606,11 @@ export class AnalyticsHandler { | |
enterpriseDomain: this.enterpriseDomain, | ||
enterpriseDomainV2: this.enterpriseDomainV2, | ||
isLoggedIn: this.isLoggedIn, | ||
cloudUserId: this.cloudUserId, | ||
cloudDomain: this.cloudDomain, | ||
ciName: this.ciName, | ||
customer: this.cloudCustomerName, | ||
customer: this.cloudOrganizationName, | ||
organizationName: this.cloudOrganizationName, | ||
system: this.systemConfig, | ||
isCI: this.isCI, | ||
sessionId: this.sessionId, | ||
|
@@ -607,7 +669,20 @@ export class AnalyticsHandler { | |
if (!this.segment || !this.isEnabled) { | ||
return false | ||
} | ||
this.segment.identify(event) | ||
|
||
const eventUid = uuidv4() | ||
this.pendingEvents.set(eventUid, event) | ||
this.segment.identify(event, (err: any) => { | ||
this.pendingEvents.delete(eventUid) | ||
|
||
this.log.silly(dedent`Tracking identify event. | ||
Payload: | ||
${JSON.stringify(event)} | ||
`) | ||
if (err && this.log) { | ||
this.log.debug(`Error sending identify tracking event: ${err}`) | ||
} | ||
}) | ||
return event | ||
} | ||
|
||
|
@@ -774,7 +849,13 @@ export class AnalyticsHandler { | |
if (this.pendingEvents.size === 0 || retry >= 3) { | ||
if (this.pendingEvents.size > 0) { | ||
const pendingEvents = Array.from(this.pendingEvents.values()) | ||
.map((event) => event.event) | ||
.map((event: SegmentEvent | IdentifyEvent) => { | ||
if ("event" in event) { | ||
return event.event | ||
} else { | ||
return event | ||
} | ||
}) | ||
.join(", ") | ||
this.log.debug(`Timed out while waiting for events to flush: ${pendingEvents}`) | ||
} | ||
|
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
Oops, something went wrong.
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.
This is kind of an awkward name. Should we skip this for non-enterprise?