Closed Bug 1582133 Opened 6 years ago Closed 6 years ago

iOS: User metrics should be in a namespace/prefixed with a namespace

Categories

(Data Platform and Tools :: Glean: SDK, defect, P3)

defect

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: janerik, Assigned: janerik)

Details

(Whiteboard: [telemetry:glean-rs:m9])

Attachments

(1 file)

In Kotlin we put all user-generated metrics into a specified namespace, so they won't ever clash with the user's code.

Swift unfortunately does not have namespaces.
For the category of metrics we use an otherwise empty enum (that can't be instantiated) and put metrics into statics inside.
However that might clash with user code when the category is already used.

One option would be to prepend GleanMetrics to all names.

Whiteboard: [telemetry:glean-rs:m?] → [telemetry:glean-rs:m9]

It should be possible to add the generated metrics to the base Glean class, using it like a namespace, by using extensions. According to the docs for extensions, we can add new nested types, which would mean we could generate something like:

extension Glean {
    internal enum GleanInternalMetrics {
        /// The name of the operating system.
        static let os = StringMetricType(
            category: "",
            name: "os",
            sendInPings: ["glean_client_info"],
            lifetime: .application,
            disabled: false
        )
        ...
    }
}

Or we could create a new base class like a struct/enum, but it would have to be named something other than Glean to prevent clashing with the Glean internal API class.

Hm, that seems very sensible indeed.
If we generate the above, users would then call this code:

Glean.GleanInternalMetrics.os.set("foobar")

(obviously they wouldn't get the "internal" metrics, they should use proper naming)

Users can use a local type alias if they want:

typealias GleanInternalMetrics = Glean.GleanInternalMetrics

:dexter, heads up for what this would look like.

Flags: needinfo?(alessio.placitelli)

(In reply to Jan-Erik Rediger [:janerik] from comment #2)

Hm, that seems very sensible indeed.
If we generate the above, users would then call this code:

Glean.GleanInternalMetrics.os.set("foobar")

(obviously they wouldn't get the "internal" metrics, they should use proper naming)

Users can use a local type alias if they want:

typealias GleanInternalMetrics = Glean.GleanInternalMetrics

:dexter, heads up for what this would look like.

This looks good to me, with a nit: in the Kotlin code we don't add metrics to the Glean class, but to the GleanMetrics namespace. So we'd need to extend a GleanMetrics object, for consistency and for matching the other platform. Ideally, we'd do the same on Desktop. See here.

Flags: needinfo?(alessio.placitelli)

Fair enough, easy to do. We can just add an empty GleanMetrics object to Glean itself, which then gets extended by users.

Assignee: nobody → jrediger
Attached file GitHub Pull Request
Status: NEW → RESOLVED
Closed: 6 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: