Skip to content

Commit 3dc69e0

Browse files
committed
Switch auto constants to object for extensibility
1 parent 8f7f3f5 commit 3dc69e0

File tree

4 files changed

+62
-11
lines changed

4 files changed

+62
-11
lines changed

packages/telemetry/rollup.config.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ 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+
'./constants/auto-constants'
2929
];
3030

3131
function replaceSource(path) {
@@ -37,10 +37,7 @@ function replaceSource(path) {
3737
});
3838
}
3939

40-
const buildPlugins = [
41-
typescriptPlugin({ typescript }),
42-
json(),
43-
];
40+
const buildPlugins = [typescriptPlugin({ typescript }), json()];
4441

4542
const browserBuilds = [
4643
{
@@ -83,7 +80,11 @@ const nodeBuilds = [
8380
format: 'es',
8481
sourcemap: true
8582
},
86-
plugins: [...buildPlugins, emitModulePackageFile(), replaceSource('../auto-constants.js')],
83+
plugins: [
84+
...buildPlugins,
85+
emitModulePackageFile(),
86+
replaceSource('../auto-constants.js')
87+
],
8788
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
8889
}
8990
];
@@ -196,7 +197,7 @@ const autoinitBuild = [
196197
format: 'cjs'
197198
},
198199
plugins: buildPlugins
199-
},
200+
}
200201
];
201202

202203
export default [

packages/telemetry/src/api.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +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';
4546

4647
const PROJECT_ID = 'my-project';
4748
const APP_ID = 'my-appid';
@@ -278,6 +279,7 @@ describe('Top level API', () => {
278279
});
279280

280281
it('should use explicit app version when provided', () => {
282+
AUTO_CONSTANTS.appVersion = '1.2.3'; // Unused
281283
const telemetry = new TelemetryService(
282284
fakeTelemetry.app,
283285
fakeTelemetry.loggerProvider
@@ -296,6 +298,19 @@ describe('Top level API', () => {
296298
});
297299
});
298300

301+
it('should use auto constants if available', () => {
302+
AUTO_CONSTANTS.appVersion = '1.2.3';
303+
304+
captureError(fakeTelemetry, 'a string error');
305+
306+
expect(emittedLogs.length).to.equal(1);
307+
const log = emittedLogs[0];
308+
expect(log.attributes).to.deep.equal({
309+
[LOG_ENTRY_ATTRIBUTE_KEYS.APP_VERSION]: '1.2.3',
310+
[LOG_ENTRY_ATTRIBUTE_KEYS.SESSION_ID]: MOCK_SESSION_ID
311+
});
312+
});
313+
299314
describe('Session Metadata', () => {
300315
it('should generate and store a new session ID if none exists', () => {
301316
captureError(fakeTelemetry, 'error');
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
export const AUTO_APP_VERSION = '';
1+
/**
2+
* @license
3+
* Copyright 2025 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
19+
export const AUTO_CONSTANTS: any = {};

packages/telemetry/src/helpers.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
118
import * as constants from './constants/auto-constants';
219
import { Telemetry } from './public-types';
320
import { TelemetryService } from './service';
421

522
export function getAppVersion(telemetry: Telemetry): string {
623
if ((telemetry as TelemetryService).options?.appVersion) {
724
return (telemetry as TelemetryService).options!.appVersion!;
8-
} else if (constants.AUTO_APP_VERSION) {
9-
return constants.AUTO_APP_VERSION;
25+
} else if (constants.AUTO_CONSTANTS?.appVersion) {
26+
return constants.AUTO_CONSTANTS.appVersion;
1027
}
1128
return 'unset';
12-
}
29+
}

0 commit comments

Comments
 (0)