Skip to content

Commit

Permalink
Added customMetric in Custom Event beacon (#27)
Browse files Browse the repository at this point in the history
* Added customMetric

* Update lib/customEvents.js with indent

Co-authored-by: HUAI NAN ZHOU <[email protected]>

* Added e2e test for customMetric in weasel

* e2e test for exponential value cm

---------

Co-authored-by: Gouri-Hariharan1 <“[email protected]”>
Co-authored-by: HUAI NAN ZHOU <[email protected]>
  • Loading branch information
3 people authored and GitHub Enterprise committed Feb 28, 2024
1 parent b742b58 commit 53f9c2e
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/customEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,8 @@ function enrich(beacon: CustomEventBeacon, opts: CustomEventOptions) {
if (typeof opts['componentStack'] === 'string') {
beacon['cs'] = opts['componentStack'].substring(0, 4096);
}
}

if (typeof opts['customMetric'] === 'number') {
beacon['cm'] = opts['customMetric'];
}
}
10 changes: 8 additions & 2 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ export interface CustomEventBeacon extends Beacon {
// error stack
st: ?string,
// component stack
cs: ?string
cs: ?string,

//custom metric
cm: ?number
}

export interface CustomEventOptions {
Expand All @@ -265,7 +268,10 @@ export interface CustomEventOptions {

// to permit ['foo'] based access that doesn't break
// when pushed through the Closure compiler
[key: string]: any
[key: string]: any,

// Any custom metric that can be passed with custom events Example: 123.2342
customMetric: ?number
}

export interface PageChangeBeacon extends Beacon {
Expand Down
57 changes: 57 additions & 0 deletions test/e2e/09_customEvents/customEvents.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('09_customEvents', () => {
cexpect(beacon.m_kind).to.equal('experienced');
cexpect(beacon.m_state).to.equal('broken');
cexpect(beacon.bt).to.equal('ab87128a1ff99345');
cexpect(beacon.cm).to.equal('123.2342');
});
});
});
Expand Down Expand Up @@ -83,4 +84,60 @@ describe('09_customEvents', () => {
});
});
});

describe('simpleEventReportingWithoutCustomMetric', () => {
beforeEach(() => {
browser.get(getE2ETestBaseUrl('09_customEvents/simpleEventReportingWithoutCustomMetric'));
});

it('must report custom events', () => {
return retry(() => {
return getBeacons().then(beacons => {
const pageLoadBeacon = expectOneMatching(beacons, beacon => {
cexpect(beacon.ty).to.equal('pl');
});

expectOneMatching(beacons, beacon => {
cexpect(beacon.ty).to.equal('cus');
cexpect(beacon.ts).to.be.a('string');
cexpect(beacon.n).to.equal('testWithEmptyCustomMetric');
cexpect(beacon.e).to.match(/Testing 123/);
cexpect(beacon.pl).to.equal(pageLoadBeacon.t);
cexpect(beacon.bt).to.equal('ab87128a1ff99345');
cexpect(beacon.cm).to.be.undefined;
});

expectOneMatching(beacons, beacon => {
cexpect(beacon.ty).to.equal('cus');
cexpect(beacon.ts).to.be.a('string');
cexpect(beacon.n).to.equal('testWithNoCustomMetric');
cexpect(beacon.e).to.match(/something wrong/);
cexpect(beacon.pl).to.equal(pageLoadBeacon.t);
cexpect(beacon.bt).to.equal('ab87128a1ff99345');
cexpect(beacon.cm).to.be.undefined;
});

expectOneMatching(beacons, beacon => {
cexpect(beacon.ty).to.equal('cus');
cexpect(beacon.ts).to.be.a('string');
cexpect(beacon.n).to.equal('testWithIncorrectCustomMetric');
cexpect(beacon.e).to.match(/something wrong/);
cexpect(beacon.pl).to.equal(pageLoadBeacon.t);
cexpect(beacon.bt).to.equal('ab87128a1ff99345');
cexpect(beacon.cm).to.be.undefined;
});

expectOneMatching(beacons, beacon => {
cexpect(beacon.ty).to.equal('cus');
cexpect(beacon.ts).to.be.a('string');
cexpect(beacon.n).to.equal('testWithExponentialCustomMetric(1e5)');
cexpect(beacon.e).to.match(/something wrong/);
cexpect(beacon.pl).to.equal(pageLoadBeacon.t);
cexpect(beacon.bt).to.equal('ab87128a1ff99345');
cexpect(beacon.cm).to.equal('100000');
});
});
});
});
});
});
3 changes: 2 additions & 1 deletion test/e2e/09_customEvents/simpleEventReporting.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
// one-off overwrite
kind: 'experienced',
state: 'broken'
}
},
customMetric: 123.2342
});
</script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>error test</title>

<script src="/e2e/initializer.js"></script>
<script crossorigin="anonymous" defer src="/target/eum.min.js"></script>
<script>
eum('meta', 'name', 'Tom');
eum('meta', 'age', 23);
eum('meta', 'kind', 'newbie');

eum('reportEvent', 'testWithEmptyCustomMetric', {
duration: 42,
backendTraceId: 'ab87128a1ff99345',
error: new Error('Testing 123'),
componentStack: 'a component stack',
meta: {
// one-off overwrite
kind: 'experienced',
state: 'broken'
},
customMetric: null
});
eum('reportEvent', 'testWithNoCustomMetric', {
duration: 2,
backendTraceId: 'ab87128a1ff99345',
error: new Error('something wrong'),
componentStack: 'some stack',
meta: {
// one-off overwrite
kind: 'experienced',
state: 'broken'
}
});
eum('reportEvent', 'testWithIncorrectCustomMetric', {
duration: 2,
backendTraceId: 'ab87128a1ff99345',
error: new Error('something wrong'),
componentStack: 'some stack',
meta: {
// one-off overwrite
kind: 'experienced',
state: 'broken'
},
customMetric: 'some string'
});
eum('reportEvent', 'testWithExponentialCustomMetric(1e5)', {
duration: 2,
backendTraceId: 'ab87128a1ff99345',
error: new Error('something wrong'),
componentStack: 'some stack',
meta: {
// one-off overwrite
kind: 'experienced',
state: 'broken'
},
customMetric: 1e5
});
</script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.js"></script>
</head>
<body>
simple event reporting without proper CustomMetric
</body>
</html>

0 comments on commit 53f9c2e

Please sign in to comment.