Skip to content

Commit

Permalink
feat(metrics): pass useragent and client ip to Glean
Browse files Browse the repository at this point in the history
  • Loading branch information
akkomar authored and chenba committed Aug 14, 2023
1 parent c58acc6 commit 195ca61
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/fxa-auth-server/lib/metrics/glean/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ const createEventFn =
const metricsContext = await request.app.metricsContext;

const metrics = {
user_agent: request.headers['user-agent'],
ip_address: request.app.clientAddress,
account_user_id_sha256: '',
event_name: eventName,
event_reason: '',
Expand Down
14 changes: 12 additions & 2 deletions packages/fxa-auth-server/lib/metrics/glean/server_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// AUTOGENERATED BY glean_parser v8.1.1. DO NOT EDIT. DO NOT COMMIT.
// AUTOGENERATED BY glean_parser v7.2.2.dev8+g91d4c811. DO NOT EDIT. DO NOT COMMIT.

// This requires `uuid` and `mozlog` libraries to be in the environment
// @types/uuid and mozlog types definitions are required in devDependencies
Expand All @@ -25,6 +25,7 @@ class AccountsEventsServerEvent {
* @param {string} applicationId - The application ID.
* @param {string} appDisplayVersion - The application display version.
* @param {string} channel - The channel.
* @param {LoggerOptions} logger_options - The logger options.
*/
constructor(
applicationId: string,
Expand All @@ -50,6 +51,9 @@ class AccountsEventsServerEvent {
* Record and submit a server event object.
* Event is logged using internal mozlog logger.
*
* @param {string} user_agent - The user agent.
* @param {string} ip_address - The IP address. Will be used to decode Geo
* information and scrubbed at ingestion.
* @param {string} account_user_id_sha256 - A hex string of a sha256 hash of the account's uid.
* @param {string} event_name - The name of the event.
* @param {string} event_reason - additional context-dependent (on event.name) info, e.g. the cause of an error.
Expand All @@ -65,6 +69,8 @@ class AccountsEventsServerEvent {
* @param {string} utm_term - This metric is similar to the `utm.source`; it is used in the Firefox browser. For example, if the user started from about:welcome, then the value could be 'aboutwelcome-default-screen'. The value has a max length of 128 characters with the alphanumeric characters, _ (underscore), forward slash (/), . (period), % (percentage sign), and - (hyphen) in the allowed set of characters..
*/
record({
user_agent,
ip_address,
account_user_id_sha256,
event_name,
event_reason,
Expand All @@ -79,6 +85,8 @@ class AccountsEventsServerEvent {
utm_source,
utm_term,
}: {
user_agent: string;
ip_address: string;
account_user_id_sha256: string;
event_name: string;
event_reason: string;
Expand Down Expand Up @@ -119,7 +127,7 @@ class AccountsEventsServerEvent {
},
// `Unknown` fields below are required in the Glean schema, however they are not useful in server context
client_info: {
telemetry_sdk_build: 'glean_parser v8.1.1',
telemetry_sdk_build: 'glean_parser v7.2.2.dev8+g91d4c811',
first_run_date: 'Unknown',
os: 'Unknown',
os_version: 'Unknown',
Expand All @@ -137,6 +145,8 @@ class AccountsEventsServerEvent {
document_type: 'accounts-events',
document_version: '1',
document_id: uuidv4(),
user_agent: user_agent,
ip_address: ip_address,
payload: eventPayloadSerialized,
};

Expand Down
19 changes: 17 additions & 2 deletions packages/fxa-auth-server/test/local/metrics/glean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ const config = {
};

const request = {
app: { isMetricsEnabled: true, metricsContext: {}, ua: {} },
app: {
isMetricsEnabled: true,
metricsContext: {},
ua: {},
clientAddress: '10.10.10.10',
},
auth: { credentials: {} },
headers: {
'user-agent': 'ELinks/0.9.3 (textmode; SunOS)',
},
payload: {},
};

Expand Down Expand Up @@ -72,10 +80,17 @@ describe('Glean server side events', () => {
glean = gleanMetrics(config);
});

it('defaults to empty strings', async () => {
it('defaults', async () => {
await glean.login.success(request);
const metrics = recordStub.args[0][0];
assert.equal(metrics.user_agent, request.headers['user-agent']);
assert.equal(metrics.ip_address, request.app.clientAddress);

delete metrics.event_name; // there's always a name of course
delete metrics.user_agent;
delete metrics.ip_address;

// the rest should default to an empty string
assert.isTrue(Object.values(metrics).every((x) => x === ''));
});

Expand Down

0 comments on commit 195ca61

Please sign in to comment.