-
Notifications
You must be signed in to change notification settings - Fork 34
Bug 1677443 - Implement initialize and core metrics #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * 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/. */ | ||
|
|
||
| import { KNOWN_CLIENT_ID, CLIENT_INFO_STORAGE } from "./constants"; | ||
| import UUIDMetricType from "metrics/types/uuid"; | ||
| import DatetimeMetricType from "metrics/types/datetime"; | ||
| import StringMetricType from "metrics/types/string"; | ||
| import { createMetric } from "metrics/utils"; | ||
| import TimeUnit from "metrics/time_unit"; | ||
| import { Lifetime } from "metrics"; | ||
| import Glean from "glean"; | ||
| import PlatformInfo from "platform_info"; | ||
|
|
||
| export class CoreMetrics { | ||
| readonly clientId: UUIDMetricType; | ||
| readonly firstRunDate: DatetimeMetricType; | ||
| readonly os: StringMetricType; | ||
| readonly osVersion: StringMetricType; | ||
| readonly architecture: StringMetricType; | ||
| readonly locale: StringMetricType; | ||
| // Provided by the user | ||
| readonly appBuild: StringMetricType; | ||
| readonly appDisplayVersion: StringMetricType; | ||
|
|
||
| constructor() { | ||
| this.clientId = new UUIDMetricType({ | ||
| name: "client_id", | ||
| category: "", | ||
| sendInPings: ["glean_client_info"], | ||
| lifetime: Lifetime.User, | ||
| disabled: false, | ||
| }); | ||
|
|
||
| this.firstRunDate = new DatetimeMetricType({ | ||
| name: "first_run_date", | ||
| category: "", | ||
| sendInPings: ["glean_client_info"], | ||
| lifetime: Lifetime.User, | ||
| disabled: false, | ||
| }, TimeUnit.Day); | ||
|
|
||
| this.os = new StringMetricType({ | ||
| name: "os", | ||
| category: "", | ||
| sendInPings: ["glean_client_info"], | ||
| lifetime: Lifetime.Application, | ||
| disabled: false, | ||
| }); | ||
|
|
||
| this.osVersion = new StringMetricType({ | ||
| name: "os_version", | ||
| category: "", | ||
| sendInPings: ["glean_client_info"], | ||
| lifetime: Lifetime.Application, | ||
| disabled: false, | ||
| }); | ||
|
|
||
| this.architecture = new StringMetricType({ | ||
| name: "architecture", | ||
| category: "", | ||
| sendInPings: ["glean_client_info"], | ||
| lifetime: Lifetime.Application, | ||
| disabled: false, | ||
| }); | ||
|
|
||
| this.locale = new StringMetricType({ | ||
| name: "locale", | ||
| category: "", | ||
| sendInPings: ["glean_client_info"], | ||
| lifetime: Lifetime.Application, | ||
| disabled: false, | ||
| }); | ||
|
|
||
| this.appBuild = new StringMetricType({ | ||
| name: "app_build", | ||
| category: "", | ||
| sendInPings: ["glean_client_info"], | ||
| lifetime: Lifetime.Application, | ||
| disabled: false, | ||
| }); | ||
|
|
||
| this.appDisplayVersion = new StringMetricType({ | ||
| name: "app_display_version", | ||
| category: "", | ||
| sendInPings: ["glean_client_info"], | ||
| lifetime: Lifetime.Application, | ||
| disabled: false, | ||
| }); | ||
| } | ||
|
|
||
| async initialize(appBuild?: string, appDisplayVersion?: string): Promise<void> { | ||
| await this.initializeClientId(); | ||
| await this.initializeFirstRunDate(); | ||
| await this.initializeOs(); | ||
| await this.initializeOsVersion(); | ||
| await this.initializeArchitecture(); | ||
| await this.initializeLocale(); | ||
| await this.appBuild.set(appBuild || "Unknown"); | ||
| await this.appDisplayVersion.set(appDisplayVersion || "Unkown"); | ||
| } | ||
|
|
||
| /** | ||
| * Generates and sets the client_id if it is not set, | ||
| * or if the current value is currepted. | ||
| */ | ||
| private async initializeClientId(): Promise<void> { | ||
| let needNewClientId = false; | ||
| const clientIdData = await Glean.metricsDatabase.getMetric(CLIENT_INFO_STORAGE, this.clientId); | ||
| if (clientIdData) { | ||
| try { | ||
| const currentClientId = createMetric("uuid", clientIdData); | ||
| if (currentClientId.payload() === KNOWN_CLIENT_ID) { | ||
| needNewClientId = true; | ||
| } | ||
| } catch { | ||
| console.warn("Unexpected value found for Glean clientId. Ignoring."); | ||
| needNewClientId = true; | ||
| } | ||
| } else { | ||
| needNewClientId = true; | ||
| } | ||
|
|
||
| if (needNewClientId) { | ||
| await this.clientId.generateAndSet(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Generates and sets the first_run_date if it is not set. | ||
| */ | ||
| private async initializeFirstRunDate(): Promise<void> { | ||
| const firstRunDate = await Glean.metricsDatabase.getMetric( | ||
| CLIENT_INFO_STORAGE, | ||
| this.firstRunDate | ||
| ); | ||
|
|
||
| if (!firstRunDate) { | ||
| await this.firstRunDate.set(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets and sets the os. | ||
| */ | ||
| async initializeOs(): Promise<void> { | ||
| await this.os.set(await PlatformInfo.os()); | ||
| } | ||
|
|
||
| /** | ||
| * Gets and sets the os. | ||
| */ | ||
| async initializeOsVersion(): Promise<void> { | ||
| await this.osVersion.set(await PlatformInfo.osVersion()); | ||
| } | ||
|
|
||
| /** | ||
| * Gets and sets the system architecture. | ||
| */ | ||
| async initializeArchitecture(): Promise<void> { | ||
| await this.architecture.set(await PlatformInfo.arch()); | ||
| } | ||
|
|
||
| /** | ||
| * Gets and sets the system / browser locale. | ||
| */ | ||
| async initializeLocale(): Promise<void> { | ||
| await this.locale.set(await PlatformInfo.locale()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * 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/. */ | ||
|
|
||
| // Must be up to date with https://github.com/mozilla/glean/blob/main/glean-core/src/system.rs | ||
| export const enum KnownOperatingSystems { | ||
| Android = "Android", | ||
| iOS = "iOS", | ||
| Linux = "Linux", | ||
| MacOS = "Darwin", | ||
| Windows = "Windows", | ||
| FreeBSD = "FreeBSD", | ||
| NetBSD = "NetBSD", | ||
| OpenBSD = "OpenBSD", | ||
| Solaris = "Solaris", | ||
| // ChromeOS is not listed in the Glean SDK because it is not a possibility there. | ||
| ChromeOS = "ChromeOS", | ||
| Unknown = "Unknown", | ||
| } | ||
|
|
||
| export interface PlatformInfo { | ||
| /** | ||
| * Gets and returns the current OS system. | ||
| * | ||
| * @returns The current OS. | ||
| */ | ||
| os(): Promise<KnownOperatingSystems>; | ||
|
|
||
| /** | ||
| * Gets and returns the current OS system version. | ||
| * | ||
| * @returns The current OS version. | ||
| */ | ||
| osVersion(): Promise<string>; | ||
|
|
||
| /** | ||
| * Gets and returnst the current system architecture. | ||
| * | ||
| * @returns The current system architecture. | ||
| */ | ||
| arch(): Promise<string>; | ||
|
|
||
| /** | ||
| * Gets and returnst the current system / browser locale. | ||
| * | ||
| * @returns The current system / browser locale. | ||
| */ | ||
| locale(): Promise<string>; | ||
| } | ||
|
|
||
| // Default export for tests sake. | ||
| const MockPlatformInfo: PlatformInfo = { | ||
| os(): Promise<KnownOperatingSystems> { | ||
| return Promise.resolve(KnownOperatingSystems.Unknown); | ||
| }, | ||
|
|
||
| osVersion(): Promise<string> { | ||
| return Promise.resolve("Unknown"); | ||
| }, | ||
|
|
||
| arch(): Promise<string> { | ||
| return Promise.resolve("Unknown"); | ||
| }, | ||
|
|
||
| locale(): Promise<string> { | ||
| return Promise.resolve("Unknown"); | ||
| }, | ||
| }; | ||
| export default MockPlatformInfo; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.