Skip to content

Commit

Permalink
chore: improve telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
devcatalin committed Jun 21, 2023
1 parent d90e76d commit 4a6a68b
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 11 deletions.
17 changes: 12 additions & 5 deletions src/editor/editor.instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let isRecreatingModel = false;
let editorType: 'local' | 'cluster' | undefined;
let hasTypedInEditor = false;
let hasModelContentChanged = false;
let currentResourceKind: string | undefined;

export const didEditorContentChange = () => hasModelContentChanged;

Expand All @@ -36,7 +37,7 @@ export const mountEditor = (props: {element: HTMLElement; type: 'local' | 'clust
if (hasTypedInEditor) {
return;
}
trackEvent('edit/code_changes', {from: getEditorType()});
trackEvent('edit/code_changes', {from: getEditorType(), resourceKind: currentResourceKind});
hasTypedInEditor = true;
});
EDITOR.onDidChangeModelContent(e => {
Expand Down Expand Up @@ -81,6 +82,11 @@ export const setEditorNextSelection = (range: monaco.IRange) => {
};

export function recreateEditorModel(editor: monaco.editor.ICodeEditor, text: string, language: string = 'yaml') {
const kindMatch = text.match(/kind:\s*(\w+)/);
if (kindMatch?.length === 1) {
currentResourceKind = kindMatch[1];
}

isRecreatingModel = true;
resetEditor();
editor.getModel()?.dispose();
Expand Down Expand Up @@ -113,11 +119,11 @@ export const clearEditorLinks = () => {
};

export const addEditorCommand = (payload: EditorCommand['payload'], supportHtml?: boolean) => {
const {text, altText, handler, beforeText, afterText} = payload;
const {text, altText, handler, beforeText, afterText, type} = payload;

const id = `cmd_${uuidv4()}`;
const wrappedHandler = () => {
trackEvent('editor/run_command');
trackEvent('editor/run_command', {type, resourceKind: currentResourceKind});
handler();
};
const disposable: monaco.IDisposable = monaco.editor.registerCommand(id, wrappedHandler);
Expand Down Expand Up @@ -172,7 +178,8 @@ export const clearEditorDecorations = () => {

monaco.languages.registerHoverProvider('yaml', {
provideHover: (model, position) => {
trackEvent('editor/hover');
// We're sending this event on any hover because this method might be triggered by a hover created by Monaco itself
trackEvent('editor/hover', {resourceKind: currentResourceKind});
const positionHovers = editorHovers.filter(hover => isPositionInRange(position, hover.range));
if (positionHovers.length === 0) {
return null;
Expand All @@ -196,7 +203,7 @@ monaco.languages.registerLinkProvider('yaml', {
};
},
resolveLink: async link => {
trackEvent('editor/follow_link');
trackEvent('editor/follow_link', {resourceKind: currentResourceKind});
const linksToResolve = editorLinks.filter(({range}) => isRangeInRange(range, link.range));
const promises = linksToResolve.map(({handler}) => Promise.resolve(handler()));
await Promise.all(promises);
Expand Down
4 changes: 4 additions & 0 deletions src/editor/editor.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export type EditorLink = {

export type EditorCommand = {
payload: {
/**
* The type of the command is only used for telemetry.
*/
type: string;
text: string;
altText: string;
handler: monaco.editor.ICommandHandler;
Expand Down
1 change: 1 addition & 0 deletions src/editor/enhancers/helm/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const helmTemplateFileEnhancer = createEditorEnhancer(({state, resourceId
typeof keyPathInFile.value === 'object' ? JSON.stringify(keyPathInFile.value, null, 4) : keyPathInFile.value;

const newCommand = addEditorCommand({
type: 'go_to_helm_values_file',
text: `${keyPathInFile.filePath}`,
altText: 'Select file',
handler: () => {
Expand Down
1 change: 1 addition & 0 deletions src/editor/enhancers/helm/valuesFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const helmValuesFileEnhancer = createEditorEnhancer(({state, resourceIden
decorations.push(createInlineDecoration(placeUsed.locationInValueFile, InlineDecorationTypes.SatisfiedRef));
placeUsed.uses.forEach(use => {
const newCommand = addEditorCommand({
type: 'go_to_helm_template_file',
text: `${use.filePath}`,
altText: 'Select file',
beforeText: 'Found in: ',
Expand Down
3 changes: 3 additions & 0 deletions src/editor/enhancers/k8sResource/refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ const addEditorCommandForRef = (args: {resourceMeta: ResourceMeta; ref: Resource
if (ref.target.type === 'resource' && ref.target.resourceId) {
command = addEditorCommand(
{
type: 'go_to_resource',
text: 'Open resource',
altText: 'Open resource',
handler: () => {
Expand All @@ -215,6 +216,7 @@ const addEditorCommandForRef = (args: {resourceMeta: ResourceMeta; ref: Resource
} else if (ref.target.type === 'file') {
command = addEditorCommand(
{
type: 'go_to_file',
text: `Open file`,
altText: 'Open file',
handler: () => {
Expand All @@ -229,6 +231,7 @@ const addEditorCommandForRef = (args: {resourceMeta: ResourceMeta; ref: Resource
} else if (ref.target.type === 'image') {
command = addEditorCommand(
{
type: 'go_to_image',
text: `Open image`,
altText: 'Open image',
handler: () => {
Expand Down
5 changes: 5 additions & 0 deletions src/editor/enhancers/k8sResource/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function addNamespaceFilterLink(
const namespace = getSymbolValue(lines, symbol);
if (namespace) {
const newCommand = addEditorCommand({
type: 'filter_namespace',
text: `Apply or remove`,
altText: 'Add/remove namespace to/from current filter',
handler: () => {
Expand Down Expand Up @@ -88,6 +89,7 @@ function addKindFilterLink(
const kind = getSymbolValue(lines, symbol);
if (kind) {
const newCommand = addEditorCommand({
type: 'filter_kind',
text: `Apply or remove`,
altText: 'Add/remove kind to/from current filter',
handler: () => {
Expand Down Expand Up @@ -121,6 +123,7 @@ function addLabelFilterLink(
const value = label.substring(symbol.name.length + 1).trim();

const newCommand = addEditorCommand({
type: 'filter_label',
text: `Apply or remove`,
altText: 'Add/remove label to/from current filter',
handler: () => {
Expand Down Expand Up @@ -156,6 +159,7 @@ function addAnnotationFilterLink(
const value = annotation.substring(symbol.name.length + 1).trim();

const newCommand = addEditorCommand({
type: 'filter_annotation',
text: `${annotation}`,
altText: 'Add/remove annotation to/from current filter',
handler: () => {
Expand Down Expand Up @@ -190,6 +194,7 @@ function addDecodeSecretHover(
const decoded = Buffer.from(value, 'base64').toString('utf-8');

const newCommand = addEditorCommand({
type: 'secret_copy_to_clipboard',
text: 'Copy to clipboard',
altText: 'Copy decoded secret to clipboard',
handler: () => {
Expand Down
7 changes: 6 additions & 1 deletion src/redux/services/clusterDashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ export const getClusterUtilization = async (kubeconfig: string, context: string)

const nodeMetrics: k8s.NodeMetric[] = (await metricClient.getNodeMetrics()).items;
const nodes = await k8s.topNodes(k8sApiClient);
let kubeletVersion: string | undefined;

if (lastContext !== context) {
const providers = uniq(
nodes
.map(node => {
if (!kubeletVersion) {
kubeletVersion = node.Node.status?.nodeInfo?.kubeletVersion;
}

const providerId = node.Node?.spec?.providerID;
// ID of the node assigned by the cloud provider in the format: <ProviderName>://<ProviderSpecificNodeID>
const providerParts = providerId?.split('://');
Expand All @@ -35,7 +40,7 @@ export const getClusterUtilization = async (kubeconfig: string, context: string)
})
.filter(isDefined)
);
trackEvent('cluster/metrics', {providers});
trackEvent('cluster/info', {providers, kubeletVersion});
}

return nodeMetrics.map(m => ({
Expand Down
10 changes: 5 additions & 5 deletions src/shared/models/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ export type EventMap = {
'explore/quick_search': undefined;
'graph/select_resource': {kind: string};
'graph/select_image': undefined;
'edit/code_changes': {from?: 'local' | 'cluster'};
'edit/code_changes': {from?: 'local' | 'cluster'; resourceKind?: string};
'edit/template_use': {templateID: string};
'edit/form_editor': {resourceKind?: string};
'edit/source': {resourceKind?: string};
'edit/side_by_side_editor': {resourceKind: string};
'edit/select_hover_link': {type: 'resource' | 'image' | 'file'};
'edit/graphview': {resourceKind?: string};
'editor/hover': undefined;
'editor/follow_link': undefined;
'editor/run_command': undefined;
'editor/hover': {resourceKind?: string};
'editor/follow_link': {resourceKind?: string};
'editor/run_command': {type: string; resourceKind?: string};
'create/file': undefined;
'create/folder': undefined;
'create/resource': {resourceKind: string};
Expand Down Expand Up @@ -123,7 +123,7 @@ export type EventMap = {
'cluster/actions/scale': {replicasNumber: number};
'cluster/actions/restart': undefined;
'cluster/actions/delete': {kind: string};
'cluster/metrics': {providers: string[]};
'cluster/info': {kubeletVersion?: string; providers: string[]};
'compare/opened': {from?: string};
'compare/compared': {left?: string; right?: string; operation: string};
'compare/inspected': {type?: string};
Expand Down

0 comments on commit 4a6a68b

Please sign in to comment.