Skip to content

Commit

Permalink
Hide settings inside "private" field of type "object"
Browse files Browse the repository at this point in the history
...so they are not exposed to users.
  • Loading branch information
ctcjab committed Oct 27, 2024
1 parent 0af0b5a commit dd9e1f4
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 195 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

NOTE: Forked from https://github.com/team-monolith-product/jupyterlab-google-analytics

## Instructions

Use `overrides.json` to configure the extension with
the settings that it should initialize the DD_RUM object with:

```json
{
"jupyterlab-datadog-rum:plugin": {
"private": {
"applicationId": "...",
"clientToken": "...",
...
}
}
}
```

Ref: https://docs.datadoghq.com/real_user_monitoring/browser/

## Requirements

### 0.\*.\*
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = {
'!src/**/.ipynb_checkpoints/*'
],
coverageReporters: ['lcov', 'text'],
modulePathIgnorePatterns: ['<rootDir>/jupyterlab_datadog_rum/'],
testRegex: 'src/.*/.*.spec.ts[x]?$',
transformIgnorePatterns: [`/node_modules/(?!${esModules}).+`]
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"stylelint-config-standard": "^34.0.0",
"stylelint-csstree-validator": "^3.0.0",
"stylelint-prettier": "^4.0.0",
"ts-jest": "^26.0.0",
"ts-jest": "^29.2.5",
"typescript": "~5.0.2",
"yjs": "^13.5.40"
},
Expand Down
74 changes: 7 additions & 67 deletions schema/plugin.json
Original file line number Diff line number Diff line change
@@ -1,75 +1,15 @@
{
"additionalProperties": false,
"description": "Ref: https://docs.datadoghq.com/real_user_monitoring/browser/",
"description": "",
"jupyter.lab.shortcuts": [],
"title": "jupyterlab-datadog-rum",
"title": "",
"type": "object",
"properties": {
"applicationId": {
"default": "",
"private": {
"default": {},
"description": "",
"title": "applicationId",
"type": "string"
},
"clientToken": {
"default": "",
"description": "",
"title": "clientToken",
"type": "string"
},
"defaultPrivacyLevel": {
"default": "mask-user-input",
"description": "",
"title": "defaultPrivacyLevel",
"type": "string"
},
"env": {
"default": "",
"description": "Leave blank to infer from URL, e.g. jupyterhub-env.example.com",
"title": "env",
"type": "string"
},
"version": {
"default": "",
"description": "",
"title": "version",
"type": "string"
},
"service": {
"default": "jupyterhub",
"description": "",
"title": "service",
"type": "string"
},
"sessionReplaySampleRate": {
"default": 20,
"description": "",
"title": "sessionReplaySampleRate",
"type": "number"
},
"sessionSampleRate": {
"default": 100,
"description": "",
"title": "sessionSampleRate",
"type": "number"
},
"trackLongTasks": {
"default": true,
"description": "",
"title": "trackLongTasks",
"type": "boolean"
},
"trackResources": {
"default": true,
"description": "",
"title": "trackResources",
"type": "boolean"
},
"trackUserInteractions": {
"default": true,
"description": "",
"title": "trackUserInteractions",
"type": "boolean"
"title": "",
"type": "object"
}
}
}
}
82 changes: 50 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,88 @@ import {
import { ISettingRegistry } from '@jupyterlab/settingregistry';

declare global {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface Window {
DD_RUM: IDDRum;
}
}
import { Token } from '@lumino/coreutils';

export const IDDRum = new Token<IDDRum>('jupyterlab-datadog-rum:plugin:IDDRum');
export const PLUGIN_ID = 'jupyterlab-datadog-rum:plugin';
export const IDDRum = new Token<IDDRum>(`${PLUGIN_ID}:IDDRum`);
export interface IDDRum {
q: any[];
onReady: (f: (c: any) => void) => void;
init: (c: any) => void;
setUser: (c: any) => void;
}

// Ref: https://docs.datadoghq.com/real_user_monitoring/browser/
interface ISettings {
applicationId: string;
clientToken: string;
env: string;
version: string;
service: string;
sessionSampleRate: number;
sessionReplaySampleRate: number;
trackUserInteractions: boolean;
trackResources: boolean;
trackLongTasks: boolean;
defaultPrivacyLevel: string;
site: string;
}

const plugin: JupyterFrontEndPlugin<IDDRum> = {
id: 'jupyterlab-datadog-rum:plugin',
id: PLUGIN_ID,
autoStart: true,
provides: IDDRum,
requires: [ISettingRegistry],
activate: async (
app: JupyterFrontEnd,
settingRegistry: ISettingRegistry
): Promise<IDDRum> => {
const setting = await settingRegistry.load(plugin.id);
const clientToken = setting.get('clientToken').composite as string;
const applicationId = setting.get('applicationId').composite as string;
const env = setting.get('env').composite as string;
const version = setting.get('version').composite as string;
const service = setting.get('service').composite as string;
const sessionSampleRate = setting.get('sessionSampleRate').composite as number;
const sessionReplaySampleRate = setting.get('sessionReplaySampleRate').composite as number;
const trackUserInteractions = setting.get('trackUserInteractions').composite as boolean;
const trackResources = setting.get('trackResources').composite as boolean;
const trackLongTasks = setting.get('trackLongTasks').composite as boolean;
const defaultPrivacyLevel = setting.get('defaultPrivacyLevel').composite as string;
const site = setting.get('site').composite as string;
console.debug(`${PLUGIN_ID}: activated`);
const settings = (await settingRegistry.load(plugin.id)).composite
.private as unknown as ISettings;
if (!settings.applicationId || !settings.clientToken) {
throw new Error(`${PLUGIN_ID}: applicationId or clientToken not set`);
}
window.DD_RUM = {
q: [],
onReady: (c: any) => window.DD_RUM.q.push(c),
init: (c: any) => {},
setUser: (c: any) => {},
setUser: (c: any) => {}
};
const script = document.createElement('script');
script.async = true;
script.src = 'https://www.datadoghq-browser-agent.com/us1/v5/datadog-rum.js';
document.head.appendChild(script)
script.src =
'https://www.datadoghq-browser-agent.com/us1/v5/datadog-rum.js';
document.head.appendChild(script);
window.DD_RUM.onReady(() => {
window.DD_RUM.init({
applicationId: applicationId,
clientToken: clientToken,
site: site,
service: service,
env: env || getMatch(/jupyterhub-([^.]+)/, window.location.hostname),
version: version,
sessionSampleRate: sessionSampleRate,
sessionReplaySampleRate: sessionReplaySampleRate,
trackUserInteractions: trackUserInteractions,
trackResources: trackResources,
trackLongTasks: trackLongTasks,
defaultPrivacyLevel: defaultPrivacyLevel,
applicationId: settings.applicationId,
clientToken: settings.clientToken,
site: settings.site,
service: settings.service,
env:
settings.env ||
getMatch(/jupyterhub-([^.]+)/, window.location.hostname),
version: settings.version,
sessionSampleRate: settings.sessionSampleRate,
sessionReplaySampleRate: settings.sessionReplaySampleRate,
trackUserInteractions: settings.trackUserInteractions,
trackResources: settings.trackResources,
trackLongTasks: settings.trackLongTasks,
defaultPrivacyLevel: settings.defaultPrivacyLevel
});
console.debug(`${PLUGIN_ID}: RUM initialized`);
// On JupyterHub, the userId will appear in the URL path after /user/
const userId = getMatch(/\/user\/([^\/]+)\//, window.location.pathname);
if (userId) window.DD_RUM.setUser({ id: userId });
const userId = getMatch(/\/user\/([^/]+)\//, window.location.pathname);
if (userId) {
window.DD_RUM.setUser({ id: userId });
console.debug(`${PLUGIN_ID}: detected user: ${userId}`);
}
});
return window.DD_RUM;
}
Expand Down
Loading

0 comments on commit dd9e1f4

Please sign in to comment.