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 739a54f
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 207 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
python -m pip install .[test]
jupyter labextension list
jupyter labextension list 2>&1 | grep -ie "jupyterlab-google-analytics.*OK"
jupyter labextension list 2>&1 | grep -ie "jupyterlab-datadog-rum.*OK"
python -m jupyterlab.browser_check
- name: Package the extension
Expand All @@ -46,13 +46,13 @@ jobs:
pip install build
python -m build
pip uninstall -y "jupyterlab_google_analytics" jupyterlab
pip uninstall -y "jupyterlab_datadog_rum" jupyterlab
- name: Upload extension packages
uses: actions/upload-artifact@v3
with:
name: extension-artifacts
path: dist/jupyterlab_google_analytics*
path: dist/jupyterlab_datadog_rum*
if-no-files-found: error

test_isolated:
Expand All @@ -75,11 +75,11 @@ jobs:
sudo rm -rf $(which node)
sudo rm -rf $(which node)
pip install "jupyterlab>=4.0.0,<5" jupyterlab_google_analytics*.whl
pip install "jupyterlab>=4.0.0,<5" jupyterlab_datadog_rum*.whl
jupyter labextension list
jupyter labextension list 2>&1 | grep -ie "jupyterlab-google-analytics.*OK"
jupyter labextension list 2>&1 | grep -ie "jupyterlab-datadog-rum.*OK"
python -m jupyterlab.browser_check --no-browser-test
integration-tests:
Expand All @@ -105,7 +105,7 @@ jobs:
- name: Install the extension
run: |
set -eux
python -m pip install "jupyterlab>=4.0.0,<5" jupyterlab_google_analytics*.whl
python -m pip install "jupyterlab>=4.0.0,<5" jupyterlab_datadog_rum*.whl
- name: Install dependencies
working-directory: ui-tests
Expand Down Expand Up @@ -134,7 +134,7 @@ jobs:
if: always()
uses: actions/upload-artifact@v3
with:
name: jupyterlab_google_analytics-playwright-tests
name: jupyterlab_datadog_rum-playwright-tests
path: |
ui-tests/test-results
ui-tests/playwright-report
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/check-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ jobs:
- name: Upload Distributions
uses: actions/upload-artifact@v3
with:
name: jupyterlab_google_analytics-releaser-dist-${{ github.run_number }}
name: jupyterlab_datadog_rum-releaser-dist-${{ github.run_number }}
path: .jupyter_releaser_checkout/dist
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"
}
}
}
}
83 changes: 51 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,89 @@ 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`);
window.DD_RUM = {
q: [],
onReady: (c: any) => window.DD_RUM.q.push(c),
init: (c: any) => {},
setUser: (c: any) => {},
setUser: (c: any) => {}
};
const settings = (await settingRegistry.load(plugin.id)).composite
.private as unknown as ISettings;
if (!settings.applicationId || !settings.clientToken) {
console.warn(`${PLUGIN_ID}: applicationId or clientToken not set`);
return window.DD_RUM;
}
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
5 changes: 1 addition & 4 deletions ui-tests/tests/jupyterlab_datadog_rum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ test('should emit an activation console message', async ({ page }) => {
await page.goto();

expect(
logs.filter(
s =>
s === 'JupyterLab extension jupyterlab-google-analytics is activated!'
)
logs.filter(s => s === 'jupyterlab-datadog-rum:plugin activated')
).toHaveLength(1);

Check failure on line 20 in ui-tests/tests/jupyterlab_datadog_rum.spec.ts

View workflow job for this annotation

GitHub Actions / Integration tests

tests/jupyterlab_datadog_rum.spec.ts:9:5 › should emit an activation console message

1) tests/jupyterlab_datadog_rum.spec.ts:9:5 › should emit an activation console message ────────── Error: expect(received).toHaveLength(expected) Expected length: 1 Received length: 0 Received array: [] 18 | expect( 19 | logs.filter(s => s === 'jupyterlab-datadog-rum:plugin activated') > 20 | ).toHaveLength(1); | ^ 21 | }); 22 | at /home/runner/work/jupyterlab-datadog-rum/jupyterlab-datadog-rum/ui-tests/tests/jupyterlab_datadog_rum.spec.ts:20:5
});
Loading

0 comments on commit 739a54f

Please sign in to comment.