Skip to content

Commit 1b29fc7

Browse files
authored
Merge 7566d79 into 1dc534a
2 parents 1dc534a + 7566d79 commit 1b29fc7

File tree

5 files changed

+29
-227
lines changed

5 files changed

+29
-227
lines changed

packages/performance/src/services/remote_config_service.test.ts

Lines changed: 24 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { SettingsService } from './settings_service';
2020
import { CONFIG_EXPIRY_LOCAL_STORAGE_KEY } from '../constants';
2121
import { setupApi, Api } from './api_service';
2222
import * as iidService from './iid_service';
23-
import { getConfig, isDestFl } from './remote_config_service';
23+
import { getConfig } from './remote_config_service';
2424
import { FirebaseApp } from '@firebase/app-types';
2525
import '../../test/setup';
2626

@@ -37,7 +37,6 @@ describe('Performance Monitoring > remote_config_service', () => {
3737
"fpr_log_endpoint_url":"https://firebaselogging.test.com",\
3838
"fpr_log_transport_key":"pseudo-transport-key",\
3939
"fpr_log_source":"2","fpr_vc_network_request_sampling_rate":"0.250000",\
40-
"fpr_log_transport_web_percent":"100.0",\
4140
"fpr_vc_session_sampling_rate":"0.250000","fpr_vc_trace_sampling_rate":"0.500000"},\
4241
"state":"UPDATE"}`;
4342
const PROJECT_ID = 'project1';
@@ -135,7 +134,6 @@ describe('Performance Monitoring > remote_config_service', () => {
135134
TRANSPORT_KEY
136135
);
137136
expect(SettingsService.getInstance().logSource).to.equal(LOG_SOURCE);
138-
expect(SettingsService.getInstance().shouldSendToFl).to.be.true;
139137
expect(
140138
SettingsService.getInstance().networkRequestsSamplingRate
141139
).to.equal(NETWORK_SAMPLIG_RATE);
@@ -176,7 +174,6 @@ describe('Performance Monitoring > remote_config_service', () => {
176174
TRANSPORT_KEY
177175
);
178176
expect(SettingsService.getInstance().logSource).to.equal(LOG_SOURCE);
179-
expect(SettingsService.getInstance().shouldSendToFl).to.be.true;
180177
expect(
181178
SettingsService.getInstance().networkRequestsSamplingRate
182179
).to.equal(NETWORK_SAMPLIG_RATE);
@@ -240,131 +237,38 @@ describe('Performance Monitoring > remote_config_service', () => {
240237
expect(SettingsService.getInstance().loggingEnabled).to.be.true;
241238
});
242239

243-
it('marks event destination to cc if there is no template', async () => {
244-
setup(
245-
{
246-
// Expired local config.
247-
expiry: '1556524895320',
248-
config: NOT_VALID_CONFIG
249-
},
250-
{ reject: false, value: new Response('{"state":"NO_TEMPLATE"}') }
251-
);
252-
await getConfig(IID);
253-
254-
// If no template, will send to cc.
255-
expect(SettingsService.getInstance().shouldSendToFl).to.be.false;
256-
});
257-
258-
it('marks event destination to cc if instance state unspecified', async () => {
259-
setup(
260-
{
261-
// Expired local config.
262-
expiry: '1556524895320',
263-
config: NOT_VALID_CONFIG
264-
},
265-
{
266-
reject: false,
267-
value: new Response('{"state":"INSTANCE_STATE_UNSPECIFIED"}')
268-
}
269-
);
270-
await getConfig(IID);
271-
272-
// If instance state unspecified, will send to cc.
273-
expect(SettingsService.getInstance().shouldSendToFl).to.be.false;
274-
});
240+
it('gets the config from RC even with deprecated transport flag', async () => {
241+
// Expired local config.
242+
const EXPIRY_LOCAL_STORAGE_VALUE = '1556524895320';
243+
const STRINGIFIED_CUSTOM_CONFIG = `{"entries":{\
244+
"fpr_vc_network_request_sampling_rate":"0.250000",\
245+
"fpr_log_transport_web_percent":"100.0",\
246+
"fpr_vc_session_sampling_rate":"0.250000","fpr_vc_trace_sampling_rate":"0.500000"},\
247+
"state":"UPDATE"}`;
275248

276-
it("marks event destination to cc if state doesn't exist", async () => {
277-
setup(
249+
const { storageGetItemStub: getItemStub } = setup(
278250
{
279-
// Expired local config.
280-
expiry: '1556524895320',
281-
config: NOT_VALID_CONFIG
251+
expiry: EXPIRY_LOCAL_STORAGE_VALUE,
252+
config: STRINGIFIED_CUSTOM_CONFIG
282253
},
283-
{ reject: false, value: new Response('{}') }
254+
{ reject: false, value: new Response(STRINGIFIED_CONFIG) }
284255
);
285-
await getConfig(IID);
286256

287-
// If "state" doesn't exist, will send to cc.
288-
expect(SettingsService.getInstance().shouldSendToFl).to.be.false;
289-
});
290-
291-
it('marks event destination to Fl if template exists but no rollout flag', async () => {
292-
const CONFIG_WITHOUT_ROLLOUT_FLAG = `{"entries":{"fpr_enabled":"true",\
293-
"fpr_log_endpoint_url":"https://firebaselogging.test.com",\
294-
"fpr_log_source":"2","fpr_vc_network_request_sampling_rate":"0.250000",\
295-
"fpr_vc_session_sampling_rate":"0.250000","fpr_vc_trace_sampling_rate":"0.500000"},\
296-
"state":"UPDATE"}`;
297-
setup(
298-
{
299-
// Expired local config.
300-
expiry: '1556524895320',
301-
config: NOT_VALID_CONFIG
302-
},
303-
{ reject: false, value: new Response(CONFIG_WITHOUT_ROLLOUT_FLAG) }
304-
);
305257
await getConfig(IID);
306258

307-
// If template exists but no rollout flag, will send to Fl.
308-
expect(SettingsService.getInstance().shouldSendToFl).to.be.true;
309-
});
310-
311-
it('marks event destination to cc when instance is outside of rollout range', async () => {
312-
const CONFIG_WITH_ROLLOUT_FLAG_10 = `{"entries":{"fpr_enabled":"true",\
313-
"fpr_log_endpoint_url":"https://firebaselogging.test.com",\
314-
"fpr_log_source":"2","fpr_vc_network_request_sampling_rate":"0.250000",\
315-
"fpr_log_transport_web_percent":"10.0",\
316-
"fpr_vc_session_sampling_rate":"0.250000","fpr_vc_trace_sampling_rate":"0.500000"},\
317-
"state":"UPDATE"}`;
318-
setup(
319-
{
320-
// Expired local config.
321-
expiry: '1556524895320',
322-
config: NOT_VALID_CONFIG
323-
},
324-
{ reject: false, value: new Response(CONFIG_WITH_ROLLOUT_FLAG_10) }
259+
expect(getItemStub).to.be.calledOnce;
260+
expect(SettingsService.getInstance().loggingEnabled).to.be.true;
261+
expect(SettingsService.getInstance().logEndPointUrl).to.equal(LOG_URL);
262+
expect(SettingsService.getInstance().transportKey).to.equal(
263+
TRANSPORT_KEY
325264
);
326-
await getConfig(IID);
327-
328-
// If rollout flag exists, will send to cc when this instance is out of rollout scope.
329-
expect(SettingsService.getInstance().shouldSendToFl).to.be.false;
330-
});
331-
332-
it('marks event destination to Fl when instance is within rollout range', async () => {
333-
const CONFIG_WITH_ROLLOUT_FLAG_40 = `{"entries":{"fpr_enabled":"true",\
334-
"fpr_log_endpoint_url":"https://firebaselogging.test.com",\
335-
"fpr_log_source":"2","fpr_vc_network_request_sampling_rate":"0.250000",\
336-
"fpr_log_transport_web_percent":"40.0",\
337-
"fpr_vc_session_sampling_rate":"0.250000","fpr_vc_trace_sampling_rate":"0.500000"},\
338-
"state":"UPDATE"}`;
339-
setup(
340-
{
341-
// Expired local config.
342-
expiry: '1556524895320',
343-
config: NOT_VALID_CONFIG
344-
},
345-
{ reject: false, value: new Response(CONFIG_WITH_ROLLOUT_FLAG_40) }
265+
expect(SettingsService.getInstance().logSource).to.equal(LOG_SOURCE);
266+
expect(
267+
SettingsService.getInstance().networkRequestsSamplingRate
268+
).to.equal(NETWORK_SAMPLIG_RATE);
269+
expect(SettingsService.getInstance().tracesSamplingRate).to.equal(
270+
TRACE_SAMPLING_RATE
346271
);
347-
await getConfig(IID);
348-
349-
// If rollout flag exists, will send to Fl when this instance is within rollout scope.
350-
expect(SettingsService.getInstance().shouldSendToFl).to.be.true;
351-
});
352-
});
353-
354-
describe('isDestFl', () => {
355-
it('marks traffic to cc when rollout percentage is 0', () => {
356-
const shouldSendToFl = isDestFl('abc', 0); // Hash percentage of "abc" is 38%.
357-
expect(shouldSendToFl).to.be.false;
358-
});
359-
360-
it('marks traffic to Fl when rollout percentage is 100', () => {
361-
const shouldSendToFl = isDestFl('abc', 100); // Hash percentage of "abc" is 38%.
362-
expect(shouldSendToFl).to.be.true;
363-
});
364-
365-
it('marks traffic to Fl if hash percentage is lower than rollout percentage 50%', () => {
366-
const shouldSendToFl = isDestFl('abc', 50); // Hash percentage of "abc" is 38%.
367-
expect(shouldSendToFl).to.be.true;
368272
});
369273
});
370274
});

packages/performance/src/services/remote_config_service.ts

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,14 @@ interface SecondaryConfig {
3434
logSource?: number;
3535
logEndPointUrl?: string;
3636
transportKey?: string;
37-
shouldSendToFl?: boolean;
3837
tracesSamplingRate?: number;
3938
networkRequestsSamplingRate?: number;
4039
}
4140

4241
// These values will be used if the remote config object is successfully
4342
// retrieved, but the template does not have these fields.
4443
const DEFAULT_CONFIGS: SecondaryConfig = {
45-
loggingEnabled: true,
46-
shouldSendToFl: true
47-
};
48-
49-
// These values will be used if the remote config object is successfully
50-
// retrieved, but the config object state shows unspecified or no template.
51-
const NO_TEMPLATE_CONFIGS: SecondaryConfig = {
52-
shouldSendToFl: false
44+
loggingEnabled: true
5345
};
5446

5547
/* eslint-disable camelcase */
@@ -75,12 +67,12 @@ const FIS_AUTH_PREFIX = 'FIREBASE_INSTALLATIONS_AUTH';
7567
export function getConfig(iid: string): Promise<void> {
7668
const config = getStoredConfig();
7769
if (config) {
78-
processConfig(iid, config);
70+
processConfig(config);
7971
return Promise.resolve();
8072
}
8173

8274
return getRemoteConfig(iid)
83-
.then(config => processConfig(iid, config))
75+
.then(processConfig)
8476
.then(
8577
config => storeConfig(config),
8678
/** Do nothing for error, use defaults set in settings service. */
@@ -170,15 +162,13 @@ function getRemoteConfig(
170162
* is valid.
171163
*/
172164
function processConfig(
173-
iid: string,
174-
config: RemoteConfigResponse | undefined
165+
config?: RemoteConfigResponse
175166
): RemoteConfigResponse | undefined {
176167
if (!config) {
177168
return config;
178169
}
179170
const settingsServiceInstance = SettingsService.getInstance();
180171
const entries = config.entries || {};
181-
const state = config.state;
182172
if (entries.fpr_enabled !== undefined) {
183173
// TODO: Change the assignment of loggingEnabled once the received type is
184174
// known.
@@ -208,32 +198,6 @@ function processConfig(
208198
settingsServiceInstance.transportKey = DEFAULT_CONFIGS.transportKey;
209199
}
210200

211-
// If config object state indicates that no template has been set, that means it is new user of
212-
// Performance Monitoring and should use the old log endpoint, because it is guaranteed to work.
213-
if (
214-
state === undefined ||
215-
state === 'INSTANCE_STATE_UNSPECIFIED' ||
216-
state === 'NO_TEMPLATE'
217-
) {
218-
if (NO_TEMPLATE_CONFIGS.shouldSendToFl !== undefined) {
219-
settingsServiceInstance.shouldSendToFl =
220-
NO_TEMPLATE_CONFIGS.shouldSendToFl;
221-
}
222-
} else if (entries.fpr_log_transport_web_percent) {
223-
// If config object state doesn't indicate no template, it can only be UPDATE for now.
224-
// - Performance Monitoring doesn't set etag in request, therefore state cannot be NO_CHANGE.
225-
// - Sampling rate flags and master flag are required, therefore state cannot be EMPTY_CONFIG.
226-
// If config object state is UPDATE and rollout flag is present, determine endpoint by iid.
227-
settingsServiceInstance.shouldSendToFl = isDestFl(
228-
iid,
229-
Number(entries.fpr_log_transport_web_percent)
230-
);
231-
} else if (DEFAULT_CONFIGS.shouldSendToFl !== undefined) {
232-
// If config object state is UPDATE and rollout flag is not present, that means rollout is
233-
// complete and rollout flag is deprecated, therefore dispatch events to new transport endpoint.
234-
settingsServiceInstance.shouldSendToFl = DEFAULT_CONFIGS.shouldSendToFl;
235-
}
236-
237201
if (entries.fpr_vc_network_request_sampling_rate !== undefined) {
238202
settingsServiceInstance.networkRequestsSamplingRate = Number(
239203
entries.fpr_vc_network_request_sampling_rate
@@ -267,30 +231,3 @@ function configValid(expiry: string): boolean {
267231
function shouldLogAfterSampling(samplingRate: number): boolean {
268232
return Math.random() <= samplingRate;
269233
}
270-
271-
/**
272-
* True if event should be sent to Fl transport endpoint rather than CC transport endpoint.
273-
* rolloutPercent is in range [0.0, 100.0].
274-
* @param iid Installation ID which identifies a web app installed on client.
275-
* @param rolloutPercent the possibility of this app sending events to Fl endpoint.
276-
* @return true if this installation should send events to Fl endpoint.
277-
*/
278-
export function isDestFl(iid: string, rolloutPercent: number): boolean {
279-
if (iid.length === 0) {
280-
return false;
281-
}
282-
return getHashPercent(iid) < rolloutPercent;
283-
}
284-
/**
285-
* Generate integer value range in [0, 99]. Implementation from String.hashCode() in Java.
286-
* @param seed Same seed will generate consistent hash value using this algorithm.
287-
* @return Hash value in range [0, 99], generated from seed and hash algorithm.
288-
*/
289-
function getHashPercent(seed: string): number {
290-
let hash = 0;
291-
for (let i = 0; i < seed.length; i++) {
292-
hash = (hash << 3) + hash - seed.charCodeAt(i);
293-
}
294-
hash = Math.abs(hash % 100);
295-
return hash;
296-
}

packages/performance/src/services/settings_service.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ export class SettingsService {
4747

4848
transportKey = mergeStrings('AzSC8r6ReiGqFMyfvgow', 'Iayx0u-XT3vksVM-pIV');
4949

50-
shouldSendToFl = false;
51-
5250
// Source type for performance event logs.
5351
logSource = 462;
5452

packages/performance/src/services/transport_service.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ describe('Firebase Performance > transport_service', () => {
4747
fetchStub.restore();
4848
clock.restore();
4949
resetTransportService();
50-
SettingsService.getInstance().shouldSendToFl = false;
5150
});
5251

5352
it('throws an error when logging an empty message', () => {
@@ -87,7 +86,6 @@ describe('Firebase Performance > transport_service', () => {
8786
const setting = SettingsService.getInstance();
8887
const flTransportFullUrl =
8988
setting.flTransportEndpointUrl + '?key=' + setting.transportKey;
90-
setting.shouldSendToFl = true;
9189
fetchStub.withArgs(flTransportFullUrl, match.any).resolves(
9290
// DELETE_REQUEST means event dispatch is successful.
9391
new Response(

packages/performance/src/services/transport_service.ts

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function dispatchQueueEvents(): void {
112112
};
113113
/* eslint-enable camelcase */
114114

115-
postToEndpoint(data, staged).catch(() => {
115+
sendEventsToFl(data, staged).catch(() => {
116116
// If the request fails for some reason, add the events that were attempted
117117
// back to the primary queue to retry later.
118118
queue = [...staged, ...queue];
@@ -122,18 +122,6 @@ function dispatchQueueEvents(): void {
122122
});
123123
}
124124

125-
function postToEndpoint(
126-
data: TransportBatchLogFormat,
127-
staged: BatchEvent[]
128-
): Promise<void> {
129-
// Gradually rollout traffic from cc to transport using remote config.
130-
if (SettingsService.getInstance().shouldSendToFl) {
131-
return sendEventsToFl(data, staged);
132-
} else {
133-
return sendEventsToCc(data);
134-
}
135-
}
136-
137125
function sendEventsToFl(
138126
data: TransportBatchLogFormat,
139127
staged: BatchEvent[]
@@ -171,29 +159,6 @@ function sendEventsToFl(
171159
});
172160
}
173161

174-
function sendEventsToCc(data: TransportBatchLogFormat): Promise<void> {
175-
return fetch(SettingsService.getInstance().logEndPointUrl, {
176-
method: 'POST',
177-
body: JSON.stringify(data)
178-
})
179-
.then(res => {
180-
if (!res.ok) {
181-
consoleLogger.info('Call to Firebase backend failed.');
182-
}
183-
return res.json();
184-
})
185-
.then(res => {
186-
const wait = Number(res.next_request_wait_millis);
187-
// Find the next call wait time from the response.
188-
const requestOffset = isNaN(wait)
189-
? DEFAULT_SEND_INTERVAL_MS
190-
: Math.max(DEFAULT_SEND_INTERVAL_MS, wait);
191-
remainingTries = DEFAULT_REMAINING_TRIES;
192-
// Schedule the next process.
193-
processQueue(requestOffset);
194-
});
195-
}
196-
197162
function postToFlEndpoint(data: TransportBatchLogFormat): Promise<Response> {
198163
const flTransportFullUrl = SettingsService.getInstance().getFlTransportFullUrl();
199164
return fetch(flTransportFullUrl, {

0 commit comments

Comments
 (0)