Skip to content

Commit 6fa1837

Browse files
committed
Refactor
1 parent 3dc69e0 commit 6fa1837

File tree

5 files changed

+30
-26
lines changed

5 files changed

+30
-26
lines changed

packages/telemetry/rollup.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ import { emitModulePackageFile } from '../../scripts/build/rollup_emit_module_pa
2525

2626
const deps = [
2727
...Object.keys(Object.assign({}, pkg.peerDependencies, pkg.dependencies)),
28-
'./constants/auto-constants'
28+
'./auto-constants'
2929
];
3030

3131
function replaceSource(path) {
3232
return replacePlugin({
33-
'./src/constants/auto-constants': `'${path}'`,
34-
'../constants/auto-constants': `'${path}'`,
33+
'./src/auto-constants': `'${path}'`,
34+
'../auto-constants': `'${path}'`,
3535
delimiters: ["'", "'"],
3636
preventAssignment: true
3737
});
@@ -191,7 +191,7 @@ const angularBuilds = [
191191

192192
const autoinitBuild = [
193193
{
194-
input: './src/constants/auto-constants.ts',
194+
input: './src/auto-constants.ts',
195195
output: {
196196
file: './dist/auto-constants.js',
197197
format: 'cjs'

packages/telemetry/src/api.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {
4242
import { TelemetryService } from './service';
4343
import { registerTelemetry } from './register';
4444
import { _FirebaseInstallationsInternal } from '@firebase/installations';
45-
import { AUTO_CONSTANTS } from './constants/auto-constants';
45+
import { AUTO_CONSTANTS } from './auto-constants';
4646

4747
const PROJECT_ID = 'my-project';
4848
const APP_ID = 'my-appid';

packages/telemetry/src/api.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@
1616
*/
1717

1818
import { _getProvider, FirebaseApp, getApp } from '@firebase/app';
19-
import {
20-
LOG_ENTRY_ATTRIBUTE_KEYS,
21-
TELEMETRY_SESSION_ID_KEY,
22-
TELEMETRY_TYPE
23-
} from './constants';
19+
import { LOG_ENTRY_ATTRIBUTE_KEYS, TELEMETRY_TYPE } from './constants';
2420
import { Telemetry, TelemetryOptions } from './public-types';
2521
import { Provider } from '@firebase/component';
2622
import { AnyValueMap, SeverityNumber } from '@opentelemetry/api-logs';
2723
import { trace } from '@opentelemetry/api';
2824
import { TelemetryService } from './service';
29-
import { getAppVersion } from './helpers';
25+
import { getAppVersion, getSessionId } from './helpers';
3026

3127
declare module '@firebase/component' {
3228
interface NameServiceMapping {
@@ -101,20 +97,9 @@ export function captureError(
10197
customAttributes['app.version'] = getAppVersion(telemetry);
10298

10399
// Add session ID metadata
104-
if (
105-
typeof sessionStorage !== 'undefined' &&
106-
typeof crypto?.randomUUID === 'function'
107-
) {
108-
try {
109-
let sessionId = sessionStorage.getItem(TELEMETRY_SESSION_ID_KEY);
110-
if (!sessionId) {
111-
sessionId = crypto.randomUUID();
112-
sessionStorage.setItem(TELEMETRY_SESSION_ID_KEY, sessionId);
113-
}
114-
customAttributes[LOG_ENTRY_ATTRIBUTE_KEYS.SESSION_ID] = sessionId;
115-
} catch (e) {
116-
// Ignore errors accessing sessionStorage (e.g. security restrictions)
117-
}
100+
const sessionId = getSessionId();
101+
if (sessionId) {
102+
customAttributes[LOG_ENTRY_ATTRIBUTE_KEYS.SESSION_ID] = sessionId;
118103
}
119104

120105
if (error instanceof Error) {

packages/telemetry/src/helpers.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18-
import * as constants from './constants/auto-constants';
18+
import * as constants from './auto-constants';
19+
import { TELEMETRY_SESSION_ID_KEY } from './constants';
1920
import { Telemetry } from './public-types';
2021
import { TelemetryService } from './service';
2122

@@ -27,3 +28,21 @@ export function getAppVersion(telemetry: Telemetry): string {
2728
}
2829
return 'unset';
2930
}
31+
32+
export function getSessionId(): string | undefined {
33+
if (
34+
typeof sessionStorage !== 'undefined' &&
35+
typeof crypto?.randomUUID === 'function'
36+
) {
37+
try {
38+
let sessionId = sessionStorage.getItem(TELEMETRY_SESSION_ID_KEY);
39+
if (!sessionId) {
40+
sessionId = crypto.randomUUID();
41+
sessionStorage.setItem(TELEMETRY_SESSION_ID_KEY, sessionId);
42+
}
43+
return sessionId;
44+
} catch (e) {
45+
// Ignore errors accessing sessionStorage (e.g. security restrictions)
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)