|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import type { MetricType } from "../metrics"; |
| 6 | +import type { ErrorType } from "./error_type.js"; |
| 7 | +import CounterMetricType from "../metrics/types/counter.js"; |
| 8 | +import { combineIdentifierAndLabel, stripLabel } from "../metrics/types/labeled.js"; |
| 9 | + |
| 10 | +/** |
| 11 | + * For a given metric, get the metric in which to record errors. |
| 12 | + * |
| 13 | + * # Important |
| 14 | + * |
| 15 | + * Errors do not adhere to the usual "maximum label" restriction. |
| 16 | + * |
| 17 | + * @param metric The metric to record an error for. |
| 18 | + * @param error The error type to record. |
| 19 | + * @returns The metric to record to. |
| 20 | + */ |
| 21 | +function getErrorMetricForMetric(metric: MetricType, error: ErrorType): CounterMetricType { |
| 22 | + const identifier = metric.baseIdentifier(); |
| 23 | + const name = stripLabel(identifier); |
| 24 | + |
| 25 | + // We don't use the labeled metric type here, |
| 26 | + // because we want to bypass the max number of allowed labels. |
| 27 | + return new CounterMetricType({ |
| 28 | + name: combineIdentifierAndLabel(error, name), |
| 29 | + category: "glean.error", |
| 30 | + lifetime: "ping", |
| 31 | + // TODO: Also add the metric ping to the list. Depends on Bug 1710838. |
| 32 | + sendInPings: metric.sendInPings, |
| 33 | + disabled: false, |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +export default class ErrorManager { |
| 38 | + /** |
| 39 | + * Records an error into Glean. |
| 40 | + * |
| 41 | + * Errors are recorded as labeled counters in the `glean.error` category. |
| 42 | + * |
| 43 | + * @param metric The metric to record an error for. |
| 44 | + * @param error The error type to record. |
| 45 | + * @param message The message to log. This message is not sent with the ping. |
| 46 | + * It does not need to include the metric id, as that is automatically |
| 47 | + * prepended to the message. |
| 48 | + * @param numErrors The number of errors of the same type to report. |
| 49 | + */ |
| 50 | + async record ( |
| 51 | + metric: MetricType, |
| 52 | + error: ErrorType, |
| 53 | + message: string, |
| 54 | + numErrors = 1 |
| 55 | + ): Promise<void> { |
| 56 | + const errorMetric = getErrorMetricForMetric(metric, error); |
| 57 | + console.warn(`${metric.baseIdentifier()}: ${message}`); |
| 58 | + if (numErrors > 0) { |
| 59 | + await CounterMetricType._private_addUndispatched(errorMetric, numErrors); |
| 60 | + } else { |
| 61 | + // TODO: Throw error only when in test mode. Depends on Bug 1682771. |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Gets the number of recorded errors for the given metric and error type. |
| 67 | + * |
| 68 | + * @param metric The metric to get the number of errors for. |
| 69 | + * @param error The error type to get count of. |
| 70 | + * @param ping The ping from which we want to retrieve the number of recorded errors. |
| 71 | + * Defaults to the first value in `sendInPings`. |
| 72 | + * @returns The number of errors reported. |
| 73 | + */ |
| 74 | + async testGetNumRecordedErrors ( |
| 75 | + metric: MetricType, |
| 76 | + error: ErrorType, |
| 77 | + ping?: string |
| 78 | + ): Promise<number> { |
| 79 | + const errorMetric = getErrorMetricForMetric(metric, error); |
| 80 | + const numErrors = await errorMetric.testGetValue(ping); |
| 81 | + return numErrors || 0; |
| 82 | + } |
| 83 | +} |
| 84 | + |
0 commit comments