22 * License, v. 2.0. If a copy of the MPL was not distributed with this
33 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
5- import type { CommonMetricData , MetricType } from "../index.js" ;
6- import Glean from "../../glean.js" ;
5+ import type { CommonMetricData } from "../index.js" ;
76import type CounterMetricType from "./counter.js" ;
87import type BooleanMetricType from "./boolean.js" ;
98import type StringMetricType from "./string.js" ;
10-
11- const MAX_LABELS = 16 ;
12- const OTHER_LABEL = "__other__" ;
13- const MAX_LABEL_LENGTH = 61 ;
14-
15- // ** IMPORTANT **
16- // When changing this documentation or the regex, be sure to change the same code
17- // in the Glean SDK repository as well.
18- //
19- // This regex is used for matching against labels and should allow for dots,
20- // underscores, and/or hyphens. Labels are also limited to starting with either
21- // a letter or an underscore character.
22- //
23- // Some examples of good and bad labels:
24- //
25- // Good:
26- // * `this.is.fine`
27- // * `this_is_fine_too`
28- // * `this.is_still_fine`
29- // * `thisisfine`
30- // * `_.is_fine`
31- // * `this.is-fine`
32- // * `this-is-fine`
33- // Bad:
34- // * `this.is.not_fine_due_tu_the_length_being_too_long_i_thing.i.guess`
35- // * `1.not_fine`
36- // * `this.$isnotfine`
37- // * `-.not_fine`
38- const LABEL_REGEX = / ^ [ a - z _ ] [ a - z 0 - 9 _ - ] { 0 , 29 } ( \. [ a - z _ ] [ a - z 0 - 9 _ - ] { 0 , 29 } ) * $ / ;
9+ import { combineIdentifierAndLabel , OTHER_LABEL } from "./labeled_utils.js" ;
3910
4011type SupportedLabeledTypes = CounterMetricType | BooleanMetricType | StringMetricType ;
4112
@@ -62,21 +33,6 @@ class LabeledMetricType<T extends SupportedLabeledTypes> {
6233 } ) ;
6334 }
6435
65- /**
66- * Combines a metric's base identifier and label.
67- *
68- * @param metricName the metric base identifier
69- * @param label the label
70- *
71- * @returns a string representing the complete metric id including the label.
72- */
73- private static combineIdentifierAndLabel (
74- metricName : string ,
75- label : string
76- ) : string {
77- return `${ metricName } /${ label } ` ;
78- }
79-
8036 /**
8137 * Create an instance of the submetric type for the provided static label.
8238 *
@@ -99,7 +55,7 @@ class LabeledMetricType<T extends SupportedLabeledTypes> {
9955 const adjustedLabel = allowedLabels . includes ( label ) ? label : OTHER_LABEL ;
10056 const newMeta : CommonMetricData = {
10157 ...meta ,
102- name : LabeledMetricType . combineIdentifierAndLabel ( meta . name , adjustedLabel )
58+ name : combineIdentifierAndLabel ( meta . name , adjustedLabel )
10359 } ;
10460 return new submetricClass ( newMeta ) ;
10561 }
@@ -125,58 +81,6 @@ class LabeledMetricType<T extends SupportedLabeledTypes> {
12581 } ;
12682 return new submetricClass ( newMeta ) ;
12783 }
128-
129- /**
130- * Checks if the dynamic label stored in the metric data is
131- * valid. If not, record an error and store data in the "__other__"
132- * label.
133- *
134- * @param metric the metric metadata.
135- *
136- * @returns a valid label that can be used to store data.
137- */
138- static async getValidDynamicLabel ( metric : MetricType ) : Promise < string > {
139- // Note that we assume `metric.dynamicLabel` to always be available within this function.
140- // This is a safe assumptions because we should only call `getValidDynamicLabel` if we have
141- // a dynamic label.
142- if ( metric . dynamicLabel === undefined ) {
143- throw new Error ( "This point should never be reached." ) ;
144- }
145-
146- const key = LabeledMetricType . combineIdentifierAndLabel ( metric . baseIdentifier ( ) , metric . dynamicLabel ) ;
147-
148- for ( const ping of metric . sendInPings ) {
149- if ( await Glean . metricsDatabase . hasMetric ( metric . lifetime , ping , metric . type , key ) ) {
150- return key ;
151- }
152- }
153-
154- let numUsedKeys = 0 ;
155- for ( const ping of metric . sendInPings ) {
156- numUsedKeys += await Glean . metricsDatabase . countByBaseIdentifier (
157- metric . lifetime ,
158- ping ,
159- metric . type ,
160- metric . baseIdentifier ( ) ) ;
161- }
162-
163- let hitError = false ;
164- if ( numUsedKeys >= MAX_LABELS ) {
165- hitError = true ;
166- } else if ( metric . dynamicLabel . length > MAX_LABEL_LENGTH ) {
167- console . error ( `label length ${ metric . dynamicLabel . length } exceeds maximum of ${ MAX_LABEL_LENGTH } ` ) ;
168- hitError = true ;
169- // TODO: record error in bug 1682574
170- } else if ( ! LABEL_REGEX . test ( metric . dynamicLabel ) ) {
171- console . error ( `label must be snake_case, got '${ metric . dynamicLabel } '` ) ;
172- hitError = true ;
173- // TODO: record error in bug 1682574
174- }
175-
176- return ( hitError )
177- ? LabeledMetricType . combineIdentifierAndLabel ( metric . baseIdentifier ( ) , OTHER_LABEL )
178- : key ;
179- }
18084}
18185
18286export default LabeledMetricType ;
0 commit comments