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

feat: different countly keys for kubo and webui.ipfs.io deployments #2081

15 changes: 12 additions & 3 deletions src/bundles/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ACTIONS as FILES } from './files/consts'
import { ACTIONS as CONIFG } from './config-save'
import { ACTIONS as INIT } from './ipfs-provider'
import { ACTIONS as EXP } from './experiments'
import { getDeploymentEnv } from '../env'

/**
* @typedef {import('./ipfs-provider').Init} Init
Expand Down Expand Up @@ -91,13 +92,22 @@ const ASYNC_ACTIONS_TO_RECORD = [

const COUNTLY_KEY_WEBUI = '8fa213e6049bff23b08e5f5fbac89e7c27397612'
const COUNTLY_KEY_WEBUI_TEST = '700fd825c3b257e021bd9dbc6cbf044d33477531'
const COUNTLY_KEY_WEBUI_KUBO = 'c4524cc93fed92a5838d4ea27c5a65526b4e7558'

function pickAppKey () {
/**
* @see https://github.com/ipfs/ipfs-webui/issues/2078
* @returns {Promise<string>}
*/
async function pickAppKey () {
const isProd = process.env.NODE_ENV === 'production'

if (root.ipfsDesktop && root.ipfsDesktop.countlyAppKey) {
return root.ipfsDesktop.countlyAppKey
} else {
const env = await getDeploymentEnv()
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
if (env === 'kubo') {
return COUNTLY_KEY_WEBUI_KUBO
}
return isProd ? COUNTLY_KEY_WEBUI : COUNTLY_KEY_WEBUI_TEST
}
}
Expand Down Expand Up @@ -262,7 +272,6 @@ const actions = {

const createAnalyticsBundle = ({
countlyUrl = 'https://countly.ipfs.io',
countlyAppKey = pickAppKey(),
appVersion = process.env.REACT_APP_VERSION,
// @ts-ignore - declared but never used
appGitRevision = process.env.REACT_APP_GIT_REV,
Expand Down Expand Up @@ -294,7 +303,7 @@ const createAnalyticsBundle = ({

Countly.require_consent = true
Countly.url = countlyUrl
Countly.app_key = countlyAppKey
Countly.app_key = await pickAppKey()
Countly.app_version = appVersion
Countly.debug = debug

Expand Down
28 changes: 28 additions & 0 deletions src/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

/**
* @see https://github.com/ipfs/ipfs-webui/issues/2078#issuecomment-1384444232
* @see https://github.com/ipfs/ipfs-webui/issues/2078#issuecomment-1384605253
* @returns {Promise<'local'|'kubo'|'webui.ipfs'>}
*/
export async function getDeploymentEnv () {
const origin = globalThis.location.origin
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
if (origin.includes('webui-ipfs') || origin.includes('webui.ipfs')) {
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
if (origin.includes('localhost')) {
return 'local'
}
return 'webui.ipfs'
}
try {
const response = await fetch('/webui', { method: 'HEAD' })
if (response.redirected) {
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT:
is this kubo check necessary? if not local or webui.ipfs.io it would be kubo correct?
is this check just a safeguard for future?

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately, it is required as we can't really guarantee that it's kubo unless it meets the expectations of kubo. We should assume it's typical webui unless we can guarantee it's kubo. I added some tests to express this more clearly, but as lidel mentioned at #2078 (comment) it's susceptible to silent failures upon kubo's changing of behavior.

Copy link
Member Author

@SgtPooki SgtPooki Jan 27, 2023

Choose a reason for hiding this comment

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

that question was something i was wondering myself after addressing lidel’s latest comment. but i’d rather have some webui metrics for kubo under webui (when/if they change behavior) than non-kubo webui metrics listed for kubo

Copy link
Contributor

Choose a reason for hiding this comment

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

makes sense, tests help too. LGTM

/**
* @see https://github.com/ipfs/kubo/blob/f54b2bcf6061219f7f481e8660e6707ae3bc3c38/cmd/ipfs/daemon.go#L677
* @see https://github.com/ipfs/kubo/blob/15093a00116ecf6b550023155f31a33f4bba6403/core/corehttp/webui.go#L57
*/
return 'kubo'
}
return 'local'
} catch {
return 'webui.ipfs'
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

@lidel how does this look

1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@
"src/lib/count-dirs.js",
"src/lib/sort.js",
"src/lib/files.js",
"src/env.js"
]
}