Commit 179fb19e authored by Mikołaj Wawrzyniak's avatar Mikołaj Wawrzyniak 1️⃣
Browse files

feat(telemetry): Added Track Telemetry to Platform

Implements Snowplow tracker for IDE extension
that is platform independent and can be used
regardless of distribution (browser / desktop).

Fixes: gitlab-org/modelops/applied-ml/code-suggestions/ai-assist#254
parent 997f283c
Loading
Loading
Loading
Loading
+45 −28
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@
      "hasInstallScript": true,
      "license": "MIT",
      "dependencies": {
        "@snowplow/tracker-core": "3.14.0",
        "cross-fetch": "^3.1.8",
        "dayjs": "^1.10.7",
        "graphql": "^16.8.0",
@@ -1897,6 +1898,24 @@
      "integrity": "sha512-+iTbntw2IZPb/anVDbypzfQa+ay64MW0Zo8aJ8gZPWMMK6/OubMVb6lUPMagqjOPnmtauXnFCACVl3O7ogjeqQ==",
      "dev": true
    },
    "node_modules/@snowplow/tracker-core": {
      "version": "3.14.0",
      "resolved": "https://registry.npmjs.org/@snowplow/tracker-core/-/tracker-core-3.14.0.tgz",
      "integrity": "sha512-YRbYJLlkcVVTCOcFSrimXCAdj+NxP/Dw6kdptaDyLFRC4S+SNfnar/BGEGj4Bx5e4aAZPME3nLeRfj9EfJBZhA==",
      "dependencies": {
        "tslib": "^2.3.1",
        "uuid": "^3.4.0"
      }
    },
    "node_modules/@snowplow/tracker-core/node_modules/uuid": {
      "version": "3.4.0",
      "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
      "integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==",
      "deprecated": "Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.",
      "bin": {
        "uuid": "bin/uuid"
      }
    },
    "node_modules/@tootallnate/once": {
      "version": "1.1.2",
      "resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-1.1.2.tgz",
@@ -3336,12 +3355,6 @@
        "url": "https://github.com/sponsors/fb55"
      }
    },
    "node_modules/cheerio/node_modules/tslib": {
      "version": "2.3.1",
      "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.1.tgz",
      "integrity": "sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==",
      "dev": true
    },
    "node_modules/chokidar": {
      "version": "3.5.3",
      "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz",
@@ -11028,12 +11041,6 @@
        "tslib": "^2.1.0"
      }
    },
    "node_modules/rxjs/node_modules/tslib": {
      "version": "2.4.0",
      "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.0.tgz",
      "integrity": "sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==",
      "dev": true
    },
    "node_modules/safe-buffer": {
      "version": "5.2.1",
      "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
@@ -11938,6 +11945,11 @@
        "strip-bom": "^3.0.0"
      }
    },
    "node_modules/tslib": {
      "version": "2.6.1",
      "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.1.tgz",
      "integrity": "sha512-t0hLfiEKfMUoqhG+U1oid7Pva4bbDPHYfJNiB7BiIjRkj1pyC++4N3huJfqY6aRH6VTB0rvtzQwjM4K6qpfOig=="
    },
    "node_modules/tsscmp": {
      "version": "1.0.6",
      "resolved": "https://registry.npmjs.org/tsscmp/-/tsscmp-1.0.6.tgz",
@@ -14201,6 +14213,22 @@
      "integrity": "sha512-+iTbntw2IZPb/anVDbypzfQa+ay64MW0Zo8aJ8gZPWMMK6/OubMVb6lUPMagqjOPnmtauXnFCACVl3O7ogjeqQ==",
      "dev": true
    },
    "@snowplow/tracker-core": {
      "version": "3.14.0",
      "resolved": "https://registry.npmjs.org/@snowplow/tracker-core/-/tracker-core-3.14.0.tgz",
      "integrity": "sha512-YRbYJLlkcVVTCOcFSrimXCAdj+NxP/Dw6kdptaDyLFRC4S+SNfnar/BGEGj4Bx5e4aAZPME3nLeRfj9EfJBZhA==",
      "requires": {
        "tslib": "^2.3.1",
        "uuid": "^3.4.0"
      },
      "dependencies": {
        "uuid": {
          "version": "3.4.0",
          "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
          "integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A=="
        }
      }
    },
    "@tootallnate/once": {
      "version": "1.1.2",
      "resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-1.1.2.tgz",
@@ -15285,14 +15313,6 @@
        "parse5": "^6.0.1",
        "parse5-htmlparser2-tree-adapter": "^6.0.1",
        "tslib": "^2.2.0"
      },
      "dependencies": {
        "tslib": {
          "version": "2.3.1",
          "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.3.1.tgz",
          "integrity": "sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==",
          "dev": true
        }
      }
    },
    "cheerio-select": {
@@ -21085,14 +21105,6 @@
      "dev": true,
      "requires": {
        "tslib": "^2.1.0"
      },
      "dependencies": {
        "tslib": {
          "version": "2.4.0",
          "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.0.tgz",
          "integrity": "sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==",
          "dev": true
        }
      }
    },
    "safe-buffer": {
@@ -21790,6 +21802,11 @@
        "strip-bom": "^3.0.0"
      }
    },
    "tslib": {
      "version": "2.6.1",
      "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.1.tgz",
      "integrity": "sha512-t0hLfiEKfMUoqhG+U1oid7Pva4bbDPHYfJNiB7BiIjRkj1pyC++4N3huJfqY6aRH6VTB0rvtzQwjM4K6qpfOig=="
    },
    "tsscmp": {
      "version": "1.0.6",
      "resolved": "https://registry.npmjs.org/tsscmp/-/tsscmp-1.0.6.tgz",
+1 −0
Original line number Diff line number Diff line
@@ -237,6 +237,7 @@
    "webfont": "^11.2.26"
  },
  "dependencies": {
    "@snowplow/tracker-core": "3.14.0",
    "cross-fetch": "^3.1.8",
    "dayjs": "^1.10.7",
    "graphql": "^16.8.0",
+1 −0
Original line number Diff line number Diff line
@@ -76,6 +76,7 @@ module.exports = {
    clipboard: {
      writeText: jest.fn(),
    },
    onDidChangeTelemetryEnabled: jest.fn(),
  },
  CommentMode: { Editing: 0, Preview: 1 },
  StatusBarAlignment: { Left: 0 },
+3 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ import {
} from './code_suggestions/commands/code_suggestion_accepted';
import { COMMAND_SHOW_OUTPUT, createShowOutputCommand } from './show_output_command';
import { activateChat } from './chat/gitlab_chat';
import { setupTelemetry } from './setup_telemetry';

export const activateCommon = async (
  context: vscode.ExtensionContext,
@@ -16,6 +17,8 @@ export const activateCommon = async (
) => {
  context.subscriptions.push(initializeFeatureFlagContext());

  setupTelemetry();

  const commands = {
    [COMMAND_SHOW_OUTPUT]: createShowOutputCommand(outputChannel),
    [COMMAND_CODE_SUGGESTION_ACCEPTED]: codeSuggestionAccepted,
+44 −0
Original line number Diff line number Diff line
import fetch from 'cross-fetch';
import { setupTelemetry } from './setup_telemetry';

jest.mock('cross-fetch');

const structEvent = {
  category: 'test',
  action: 'test',
  label: 'test',
  value: 1,
};

describe('setupTelemetry', () => {
  beforeEach(() => {
    (fetch as jest.MockedFunction<typeof fetch>).mockResolvedValue({
      status: 200,
      statusText: 'OK',
    } as Response);

    (fetch as jest.MockedFunction<typeof fetch>).mockClear();
  });

  it('should return an instance of the snowplow telemetry provider', async () => {
    const sp = setupTelemetry();
    expect(sp).toBeDefined();
    await sp.stop();
  });

  it('should call the api when telemetry is enabled', async () => {
    const sp = setupTelemetry();
    await sp.trackStructEvent(structEvent);
    await sp.trackStructEvent(structEvent);
    await sp.trackStructEvent(structEvent);
    await sp.trackStructEvent(structEvent);
    await sp.trackStructEvent(structEvent);
    await sp.trackStructEvent(structEvent);
    await sp.trackStructEvent(structEvent);
    await sp.trackStructEvent(structEvent);
    await sp.trackStructEvent(structEvent);
    await sp.trackStructEvent(structEvent);
    expect(fetch).toHaveBeenCalledTimes(1);
    await sp.stop();
  });
});
Loading