Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ all: dev-setup

setup-build-tools:
go install github.com/bufbuild/buf/cmd/buf@v1.32.2
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.34.2
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.36.10
go install connectrpc.com/connect/cmd/protoc-gen-connect-go@v1.16.2
go install gotest.tools/gotestsum@v1.13.0

Expand Down
44 changes: 26 additions & 18 deletions cli/src/core/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import crypto from 'node:crypto';
import os from 'node:os';
import { PostHog } from 'posthog-node';
import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb';
import { config, getBaseHeaders, getLoginDetails } from './config.js';
import { config, getBaseHeaders } from './config.js';
import { CreateClient } from './client/client.js';

// Environment variables to allow opting out of telemetry
Expand Down Expand Up @@ -30,6 +30,11 @@ type PostHogFetchResponse = {
json: () => Promise<any>;
};

type TelemetryIdentity = {
userEmail?: string;
organizationId: string;
};

const buildPostHogOkResponse = () => ({
status: 200,
text: () => Promise.resolve(''),
Expand Down Expand Up @@ -116,19 +121,12 @@ export const initTelemetry = () => {
* Generate a consistent distinct ID
* Uses the platform API to get the organization slug if available
*/
const getIdentity = async (): Promise<string> => {
const getIdentity = async (): Promise<TelemetryIdentity> => {
try {
// First try to get the identity from the config file
const loginDetails = getLoginDetails();
if (loginDetails?.organizationSlug) {
return loginDetails.organizationSlug;
}

// If not found, the user might be using an API key.
// Call the whoAmI API to get organization information

if (!apiClient) {
return 'anonymous';
return {
organizationId: 'anonymous',
};
}

const resp = await apiClient.platform.whoAmI(
Expand All @@ -139,13 +137,20 @@ const getIdentity = async (): Promise<string> => {
);

if (resp.response?.code === EnumStatusCode.OK) {
return resp.organizationSlug;
return {
organizationId: resp.organizationId || 'anonymous',
userEmail: resp.userEmail || undefined,
};
}
} catch (err) {
// skip catch, returning anonymous identity if any error occurs (e.g. network issues, not logged in, etc.)
if (process.env.DEBUG) {
console.debug('Failed to get identity for telemetry, using anonymous.', err);
}

return 'anonymous';
} catch {
return 'anonymous';
}
return {
organizationId: 'anonymous',
};
};

/**
Expand All @@ -161,7 +166,10 @@ export const capture = async (eventName: string, properties: Record<string, any>
const metadata = getMetadata();

client.capture({
distinctId: identity,
distinctId: identity.userEmail ?? identity.organizationId,
groups: {
cosmo_organization: identity.organizationId ?? '',
},
event: eventName,
properties: {
...metadata,
Expand Down
112 changes: 38 additions & 74 deletions connect-go/gen/proto/wg/cosmo/common/common.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading