Skip to content

Commit adba96a

Browse files
Add additional telemetry (#63)
1 parent 379976d commit adba96a

File tree

15 files changed

+229
-14
lines changed

15 files changed

+229
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Added additional telemetry events. ([#63](https://github.com/sourcebot-dev/sourcebot/pull/63))
13+
1014
## [2.4.0] - 2024-11-06
1115

1216
### Added

Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ RUN echo "Sourcebot Version: $SOURCEBOT_VERSION"
5555
ENV SOURCEBOT_LOG_LEVEL=info
5656

5757
# @note: This is also set in .env
58-
ENV NEXT_PUBLIC_POSTHOG_KEY=phc_VFn4CkEGHRdlVyOOw8mfkoj1DKVoG6y1007EClvzAnS
58+
ENV POSTHOG_KEY=phc_VFn4CkEGHRdlVyOOw8mfkoj1DKVoG6y1007EClvzAnS
59+
ENV NEXT_PUBLIC_POSTHOG_KEY=$POSTHOG_KEY
5960

6061
# Sourcebot collects anonymous usage data using [PostHog](https://posthog.com/). Uncomment this line to disable.
6162
# ENV SOURCEBOT_TELEMETRY_DISABLED=1
6263

6364
# Configure dependencies
64-
RUN apk add --no-cache git ca-certificates bind-tools tini jansson wget supervisor uuidgen curl perl
65+
RUN apk add --no-cache git ca-certificates bind-tools tini jansson wget supervisor uuidgen curl perl jq
6566

6667
# Configure zoekt
6768
COPY vendor/zoekt/install-ctags-alpine.sh .

entrypoint.sh

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,48 @@ fi
1515

1616
# In order to detect if this is the first run, we create a `.installed` file in
1717
# the cache directory.
18-
FIRST_RUN_FILE="$DATA_CACHE_DIR/.installed"
18+
FIRST_RUN_FILE="$DATA_CACHE_DIR/.installedv2"
19+
1920
if [ ! -f "$FIRST_RUN_FILE" ]; then
2021
touch "$FIRST_RUN_FILE"
21-
22+
export SOURCEBOT_INSTALL_ID=$(uuidgen)
23+
2224
# If this is our first run, send a `install` event to PostHog
2325
# (if telemetry is enabled)
2426
if [ -z "$SOURCEBOT_TELEMETRY_DISABLED" ]; then
2527
curl -L -s --header "Content-Type: application/json" -d '{
26-
"api_key": "'"$NEXT_PUBLIC_POSTHOG_KEY"'",
28+
"api_key": "'"$POSTHOG_KEY"'",
2729
"event": "install",
28-
"distinct_id": "'"$(uuidgen)"'",
30+
"distinct_id": "'"$SOURCEBOT_INSTALL_ID"'",
2931
"properties": {
3032
"sourcebot_version": "'"$SOURCEBOT_VERSION"'"
3133
}
3234
}' https://us.i.posthog.com/capture/ > /dev/null
3335
fi
36+
else
37+
export SOURCEBOT_INSTALL_ID=$(cat "$FIRST_RUN_FILE" | jq -r '.install_id')
38+
PREVIOUS_VERSION=$(cat "$FIRST_RUN_FILE" | jq -r '.version')
39+
40+
# If the version has changed, we assume an upgrade has occurred.
41+
if [ "$PREVIOUS_VERSION" != "$SOURCEBOT_VERSION" ]; then
42+
echo -e "\e[34m[Info] Upgraded from version $PREVIOUS_VERSION to $SOURCEBOT_VERSION\e[0m"
43+
44+
if [ -z "$SOURCEBOT_TELEMETRY_DISABLED" ]; then
45+
curl -L -s --header "Content-Type: application/json" -d '{
46+
"api_key": "'"$POSTHOG_KEY"'",
47+
"event": "upgrade",
48+
"distinct_id": "'"$SOURCEBOT_INSTALL_ID"'",
49+
"properties": {
50+
"from_version": "'"$PREVIOUS_VERSION"'",
51+
"to_version": "'"$SOURCEBOT_VERSION"'"
52+
}
53+
}' https://us.i.posthog.com/capture/ > /dev/null
54+
fi
55+
fi
3456
fi
3557

58+
echo "{\"version\": \"$SOURCEBOT_VERSION\", \"install_id\": \"$SOURCEBOT_INSTALL_ID\"}" > "$FIRST_RUN_FILE"
59+
3660
# Fallback to sample config if a config does not exist
3761
if echo "$CONFIG_PATH" | grep -qE '^https?://'; then
3862
if ! curl --output /dev/null --silent --head --fail "$CONFIG_PATH"; then

packages/backend/.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
POSTHOG_HOST=https://us.i.posthog.com
2+
3+
# @note: This is also set in the Dockerfile
4+
POSTHOG_KEY=phc_VFn4CkEGHRdlVyOOw8mfkoj1DKVoG6y1007EClvzAnS

packages/backend/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
dist/
1+
dist/
2+
!.env

packages/backend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
"@octokit/rest": "^21.0.2",
2525
"argparse": "^2.0.1",
2626
"cross-fetch": "^4.0.0",
27+
"dotenv": "^16.4.5",
2728
"gitea-js": "^1.22.0",
2829
"lowdb": "^7.0.1",
2930
"micromatch": "^4.0.8",
31+
"posthog-node": "^4.2.1",
3032
"simple-git": "^3.27.0",
3133
"strip-json-comments": "^5.0.1",
3234
"winston": "^3.15.0"

packages/backend/src/environment.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1+
import dotenv from 'dotenv';
12

23
export const getEnv = (env: string | undefined, defaultValue = '') => {
34
return env ?? defaultValue;
45
}
56

7+
export const getEnvBoolean = (env: string | undefined, defaultValue: boolean) => {
8+
if (!env) {
9+
return defaultValue;
10+
}
11+
return env === 'true' || env === '1';
12+
}
13+
14+
dotenv.config({
15+
path: './.env',
16+
});
17+
618
export const SOURCEBOT_LOG_LEVEL = getEnv(process.env.SOURCEBOT_LOG_LEVEL, 'info');
19+
export const SOURCEBOT_TELEMETRY_DISABLED = getEnvBoolean(process.env.SOURCEBOT_TELEMETRY_DISABLED, false);
20+
export const SOURCEBOT_INSTALL_ID = getEnv(process.env.SOURCEBOT_INSTALL_ID, 'unknown');
21+
export const SOURCEBOT_VERSION = getEnv(process.env.SOURCEBOT_VERSION, 'unknown');
22+
export const POSTHOG_KEY = getEnv(process.env.POSTHOG_KEY);
23+
export const POSTHOG_HOST = getEnv(process.env.POSTHOG_HOST);

packages/backend/src/gitea.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const getGiteaReposFromConfig = async (config: GiteaConfig, ctx: AppConte
4747

4848
return {
4949
vcs: 'git',
50+
codeHost: 'gitea',
5051
name: repo.full_name!,
5152
id: repoId,
5253
cloneUrl: cloneUrl.toString(),

packages/backend/src/github.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export const getGitHubReposFromConfig = async (config: GitHubConfig, signal: Abo
7272

7373
return {
7474
vcs: 'git',
75+
codeHost: 'github',
7576
name: repo.full_name,
7677
id: repoId,
7778
cloneUrl: cloneUrl.toString(),

packages/backend/src/gitlab.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export const getGitLabReposFromConfig = async (config: GitLabConfig, ctx: AppCon
7575

7676
return {
7777
vcs: 'git',
78+
codeHost: 'gitlab',
7879
name: project.path_with_namespace,
7980
id: repoId,
8081
cloneUrl: cloneUrl.toString(),

0 commit comments

Comments
 (0)