Skip to content

Commit

Permalink
Add telemetry for Elastic Cloud (#102390)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover authored Jun 24, 2021
1 parent fb7b596 commit fbcf405
Show file tree
Hide file tree
Showing 21 changed files with 973 additions and 21 deletions.
4 changes: 4 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

---
Portions of this code are licensed under the following license:
For license information please see https://edge.fullstory.com/s/fs.js.LICENSE.txt

---
This product bundles bootstrap@3.3.6 which is available under a
"MIT" license.
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,26 @@
"@kbn/analytics": "link:bazel-bin/packages/kbn-analytics",
"@kbn/apm-config-loader": "link:bazel-bin/packages/kbn-apm-config-loader",
"@kbn/apm-utils": "link:bazel-bin/packages/kbn-apm-utils",
"@kbn/common-utils": "link:bazel-bin/packages/kbn-common-utils",
"@kbn/config": "link:bazel-bin/packages/kbn-config",
"@kbn/config-schema": "link:bazel-bin/packages/kbn-config-schema",
"@kbn/crypto": "link:bazel-bin/packages/kbn-crypto",
"@kbn/mapbox-gl": "link:bazel-bin/packages/kbn-mapbox-gl",
"@kbn/i18n": "link:bazel-bin/packages/kbn-i18n",
"@kbn/interpreter": "link:bazel-bin/packages/kbn-interpreter",
"@kbn/io-ts-utils": "link:bazel-bin/packages/kbn-io-ts-utils",
"@kbn/legacy-logging": "link:bazel-bin/packages/kbn-legacy-logging",
"@kbn/logging": "link:bazel-bin/packages/kbn-logging",
"@kbn/mapbox-gl": "link:bazel-bin/packages/kbn-mapbox-gl",
"@kbn/monaco": "link:bazel-bin/packages/kbn-monaco",
"@kbn/rule-data-utils": "link:bazel-bin/packages/kbn-rule-data-utils",
"@kbn/securitysolution-list-constants": "link:bazel-bin/packages/kbn-securitysolution-list-constants",
"@kbn/securitysolution-es-utils": "link:bazel-bin/packages/kbn-securitysolution-es-utils",
"@kbn/securitysolution-hook-utils": "link:bazel-bin/packages/kbn-securitysolution-hook-utils",
"@kbn/securitysolution-io-ts-types": "link:bazel-bin/packages/kbn-securitysolution-io-ts-types",
"@kbn/securitysolution-io-ts-alerting-types": "link:bazel-bin/packages/kbn-securitysolution-io-ts-alerting-types",
"@kbn/securitysolution-io-ts-list-types": "link:bazel-bin/packages/kbn-securitysolution-io-ts-list-types",
"@kbn/securitysolution-io-ts-types": "link:bazel-bin/packages/kbn-securitysolution-io-ts-types",
"@kbn/securitysolution-io-ts-utils": "link:bazel-bin/packages/kbn-securitysolution-io-ts-utils",
"@kbn/securitysolution-list-api": "link:bazel-bin/packages/kbn-securitysolution-list-api",
"@kbn/securitysolution-list-constants": "link:bazel-bin/packages/kbn-securitysolution-list-constants",
"@kbn/securitysolution-list-hooks": "link:bazel-bin/packages/kbn-securitysolution-list-hooks",
"@kbn/securitysolution-list-utils": "link:bazel-bin/packages/kbn-securitysolution-list-utils",
"@kbn/securitysolution-t-grid": "link:bazel-bin/packages/kbn-securitysolution-t-grid",
Expand All @@ -158,7 +159,6 @@
"@kbn/ui-framework": "link:bazel-bin/packages/kbn-ui-framework",
"@kbn/ui-shared-deps": "link:bazel-bin/packages/kbn-ui-shared-deps",
"@kbn/utility-types": "link:bazel-bin/packages/kbn-utility-types",
"@kbn/common-utils": "link:bazel-bin/packages/kbn-common-utils",
"@kbn/utils": "link:bazel-bin/packages/kbn-utils",
"@loaders.gl/core": "^2.3.1",
"@loaders.gl/json": "^2.3.1",
Expand Down Expand Up @@ -273,6 +273,7 @@
"jquery": "^3.5.0",
"js-levenshtein": "^1.1.6",
"js-search": "^1.4.3",
"js-sha256": "^0.9.0",
"js-yaml": "^3.14.0",
"json-stable-stringify": "^1.0.1",
"json-stringify-pretty-compact": "1.2.0",
Expand Down
100 changes: 100 additions & 0 deletions x-pack/plugins/cloud/public/fullstory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { sha256 } from 'js-sha256';
import type { IBasePath, PackageInfo } from '../../../../src/core/public';

export interface FullStoryDeps {
basePath: IBasePath;
orgId: string;
packageInfo: PackageInfo;
userIdPromise: Promise<string | undefined>;
}

interface FullStoryApi {
identify(userId: string, userVars?: Record<string, any>): void;
event(eventName: string, eventProperties: Record<string, any>): void;
}

export const initializeFullStory = async ({
basePath,
orgId,
packageInfo,
userIdPromise,
}: FullStoryDeps) => {
// @ts-expect-error
window._fs_debug = false;
// @ts-expect-error
window._fs_host = 'fullstory.com';
// @ts-expect-error
window._fs_script = basePath.prepend(`/internal/cloud/${packageInfo.buildNum}/fullstory.js`);
// @ts-expect-error
window._fs_org = orgId;
// @ts-expect-error
window._fs_namespace = 'FSKibana';

/* eslint-disable */
(function(m,n,e,t,l,o,g,y){
if (e in m) {if(m.console && m.console.log) { m.console.log('FullStory namespace conflict. Please set window["_fs_namespace"].');} return;}
// @ts-expect-error
g=m[e]=function(a,b,s){g.q?g.q.push([a,b,s]):g._api(a,b,s);};g.q=[];
// @ts-expect-error
o=n.createElement(t);o.async=1;o.crossOrigin='anonymous';o.src=_fs_script;
// @ts-expect-error
y=n.getElementsByTagName(t)[0];y.parentNode.insertBefore(o,y);
// @ts-expect-error
g.identify=function(i,v,s){g(l,{uid:i},s);if(v)g(l,v,s)};g.setUserVars=function(v,s){g(l,v,s)};g.event=function(i,v,s){g('event',{n:i,p:v},s)};
// @ts-expect-error
g.anonymize=function(){g.identify(!!0)};
// @ts-expect-error
g.shutdown=function(){g("rec",!1)};g.restart=function(){g("rec",!0)};
// @ts-expect-error
g.log = function(a,b){g("log",[a,b])};
// @ts-expect-error
g.consent=function(a){g("consent",!arguments.length||a)};
// @ts-expect-error
g.identifyAccount=function(i,v){o='account';v=v||{};v.acctId=i;g(o,v)};
// @ts-expect-error
g.clearUserCookie=function(){};
// @ts-expect-error
g.setVars=function(n, p){g('setVars',[n,p]);};
// @ts-expect-error
g._w={};y='XMLHttpRequest';g._w[y]=m[y];y='fetch';g._w[y]=m[y];
// @ts-expect-error
if(m[y])m[y]=function(){return g._w[y].apply(this,arguments)};
// @ts-expect-error
g._v="1.3.0";
// @ts-expect-error
})(window,document,window['_fs_namespace'],'script','user');
/* eslint-enable */

// @ts-expect-error
const fullstory: FullStoryApi = window.FSKibana;

// Record an event that Kibana was opened so we can easily search for sessions that use Kibana
// @ts-expect-error
window.FSKibana.event('Loaded Kibana', {
kibana_version_str: packageInfo.version,
});

// Use a promise here so we don't have to wait to retrieve the user to start recording the session
userIdPromise
.then((userId) => {
if (!userId) return;
// Do the hashing here to keep it at clear as possible in our source code that we do not send literal user IDs
const hashedId = sha256(userId.toString());
// @ts-expect-error
window.FSKibana.identify(hashedId);
})
.catch((e) => {
// eslint-disable-next-line no-console
console.error(
`[cloud.full_story] Could not call FS.identify due to error: ${e.toString()}`,
e
);
});
};
13 changes: 13 additions & 0 deletions x-pack/plugins/cloud/public/plugin.test.mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { FullStoryDeps } from './fullstory';

export const initializeFullStoryMock = jest.fn<void, [FullStoryDeps]>();
jest.doMock('./fullstory', () => {
return { initializeFullStory: initializeFullStoryMock };
});
Loading

0 comments on commit fbcf405

Please sign in to comment.