From 195ca6113ac0d53662450e0d3e07bc27648fe975 Mon Sep 17 00:00:00 2001 From: Arkadiusz Komarzewski Date: Wed, 9 Aug 2023 17:34:12 +0200 Subject: [PATCH] feat(metrics): pass useragent and client ip to Glean --- .../lib/metrics/glean/index.ts | 2 ++ .../lib/metrics/glean/server_events.ts | 14 ++++++++++++-- .../test/local/metrics/glean.ts | 19 +++++++++++++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/packages/fxa-auth-server/lib/metrics/glean/index.ts b/packages/fxa-auth-server/lib/metrics/glean/index.ts index aba86490111..12cd7f6a8c3 100644 --- a/packages/fxa-auth-server/lib/metrics/glean/index.ts +++ b/packages/fxa-auth-server/lib/metrics/glean/index.ts @@ -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: '', diff --git a/packages/fxa-auth-server/lib/metrics/glean/server_events.ts b/packages/fxa-auth-server/lib/metrics/glean/server_events.ts index 8145abf076f..a99f6bffa5c 100644 --- a/packages/fxa-auth-server/lib/metrics/glean/server_events.ts +++ b/packages/fxa-auth-server/lib/metrics/glean/server_events.ts @@ -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 @@ -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, @@ -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. @@ -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, @@ -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; @@ -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', @@ -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, }; diff --git a/packages/fxa-auth-server/test/local/metrics/glean.ts b/packages/fxa-auth-server/test/local/metrics/glean.ts index f44da20abd1..4531942c4e9 100644 --- a/packages/fxa-auth-server/test/local/metrics/glean.ts +++ b/packages/fxa-auth-server/test/local/metrics/glean.ts @@ -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: {}, }; @@ -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 === '')); });