feat: 🔇 complete removal of telemetry when allow_anonymous_telemetry false

This commit is contained in:
Nate Sesti 2023-08-29 10:39:25 -07:00
parent 047dad3152
commit ae7dffa211
5 changed files with 82 additions and 29 deletions

View file

@ -1,8 +1,8 @@
import os
import socket
from typing import Any
from dotenv import load_dotenv
from posthog import Posthog
from ..constants.main import CONTINUE_SERVER_VERSION_FILE
from .commonregex import clean_pii_from_any
@ -13,10 +13,20 @@ in_codespaces = os.getenv("CODESPACES") == "true"
POSTHOG_API_KEY = "phc_JS6XFROuNbhJtVCEdTSYk6gl5ArRrTNMpCcguAXlSPs"
def is_connected():
try:
# connect to the host -- tells us if the host is actually reachable
socket.create_connection(("www.google.com", 80))
return True
except OSError:
pass
return False
class PostHogLogger:
unique_id: str = "NO_UNIQUE_ID"
allow_anonymous_telemetry: bool = False
posthog: Posthog = None
posthog = None
def __init__(self, api_key: str):
self.api_key = api_key
@ -36,6 +46,8 @@ class PostHogLogger:
print(f"Failed to capture event: {e}")
pass
_found_disconnected: bool = False
def _capture_event(self, event_name: str, event_properties: Any):
# logger.debug(
# f"Logging to PostHog: {event_name} ({self.unique_id}, {self.allow_anonymous_telemetry}): {event_properties}")
@ -72,10 +84,17 @@ class PostHogLogger:
# Send event to PostHog
if self.posthog is None:
from posthog import Posthog
# The personal API key is necessary only if you want to use local evaluation of feature flags.
self.posthog = Posthog(self.api_key, host="https://app.posthog.com")
self.posthog.capture(self.unique_id, event_name, event_properties)
if is_connected():
self.posthog.capture(self.unique_id, event_name, event_properties)
else:
if not self._found_disconnected:
self._found_disconnected = True
raise ConnectionError("No internet connection")
posthog_logger = PostHogLogger(api_key=POSTHOG_API_KEY)

View file

@ -0,0 +1,34 @@
import posthog from "posthog-js";
import { PostHogProvider } from "posthog-js/react";
import { PropsWithChildren, useEffect } from "react";
import { RootStore } from "../redux/store";
import { useSelector } from "react-redux";
import React from "react";
const CustomPostHogProvider = ({ children }: PropsWithChildren) => {
const allowAnonymousTelemetry = useSelector(
(store: RootStore) => store?.serverState?.config?.allow_anonymous_telemetry
);
const [client, setClient] = React.useState<any>(undefined);
useEffect(() => {
if (allowAnonymousTelemetry) {
posthog.init("phc_JS6XFROuNbhJtVCEdTSYk6gl5ArRrTNMpCcguAXlSPs", {
api_host: "https://app.posthog.com",
disable_session_recording: true,
});
setClient(client);
} else {
setClient(undefined);
}
}, [allowAnonymousTelemetry]);
return allowAnonymousTelemetry ? (
<PostHogProvider client={client}>{children}</PostHogProvider>
) : (
<>{children}</>
);
};
export default CustomPostHogProvider;

View file

@ -5,22 +5,14 @@ import { Provider } from "react-redux";
import store from "./redux/store";
import "./index.css";
import posthog from "posthog-js";
import { PostHogProvider } from "posthog-js/react";
console.log("Starting React");
posthog.init("phc_JS6XFROuNbhJtVCEdTSYk6gl5ArRrTNMpCcguAXlSPs", {
api_host: "https://app.posthog.com",
disable_session_recording: true,
});
import CustomPostHogProvider from "./hooks/CustomPostHogProvider";
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<PostHogProvider client={posthog}>
<Provider store={store}>
<Provider store={store}>
<CustomPostHogProvider>
<App />
</Provider>
</PostHogProvider>
</CustomPostHogProvider>
</Provider>
</React.StrictMode>
);

View file

@ -4,17 +4,23 @@
import * as vscode from "vscode";
import { getExtensionVersion } from "./activation/environmentSetup";
import { PostHog } from "posthog-node";
const client = new PostHog(
"phc_JS6XFROuNbhJtVCEdTSYk6gl5ArRrTNMpCcguAXlSPs",
let client: any = undefined;
async function capture(args: any) {
console.log("Capturing posthog event: ", args);
if (!client) {
const { PostHog } = await import("posthog-node");
client = new PostHog("phc_JS6XFROuNbhJtVCEdTSYk6gl5ArRrTNMpCcguAXlSPs", {
host: "https://app.posthog.com",
});
}
client.capture(args);
}
{ host: "https://app.posthog.com" }
);
async function dynamicImportAndActivate(context: vscode.ExtensionContext) {
if (!context.globalState.get("hasBeenInstalled")) {
context.globalState.update("hasBeenInstalled", true);
client.capture({
capture({
distinctId: vscode.env.machineId,
event: "install",
properties: {
@ -50,7 +56,7 @@ export function activate(context: vscode.ExtensionContext) {
}
export function deactivate() {
client.capture({
capture({
distinctId: vscode.env.machineId,
event: "deactivate",
properties: {
@ -58,5 +64,5 @@ export function deactivate() {
},
});
client.shutdown();
client?.shutdown();
}

12
run.py
View file

@ -1,12 +1,14 @@
from continuedev.main import main
import os
import sys
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
ca_bundle_path = os.path.join(sys._MEIPASS, 'ca_bundle', 'cacert.pem')
from continuedev.main import main
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
ca_bundle_path = os.path.join(sys._MEIPASS, "ca_bundle", "cacert.pem")
print("Certificates at: ", ca_bundle_path)
os.environ['SSL_CERT_FILE'] = ca_bundle_path
os.environ['REQUESTS_CA_BUNDLE'] = ca_bundle_path
os.environ["SSL_CERT_FILE"] = ca_bundle_path
os.environ["REQUESTS_CA_BUNDLE"] = ca_bundle_path
if __name__ == "__main__":
print("Running Continue server version 0.0.350")
main()