From 2a5a3233d040219a0d75f88b6efc677b95e9910f Mon Sep 17 00:00:00 2001 From: Bastien Caudan Date: Mon, 14 Dec 2020 16:49:25 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A5=20[RUMF-730]=20prefer=20object=20a?= =?UTF-8?q?nd=20type=20alias=20over=20enum=20in=20APIs=20(#630)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ♻️ switch parent context to v2 + missing tests * 🔥 remove v1 specific code * ♻️ remove v2 references * 🐛 fix loadEvent v2 * 💥 remove exported RUM event types * use v2 format for replica application overwrite * 💥 prefer constant union over enum in APIs reasoning: make APIs easier to use with TypeScript by using directly constants instead of importing our enums * 👌 propagate constant union for logs * 👌 propagate constant union for rum * 👌 alias constant union for logs * 👌 alias constant union for rum * 👌 replace exposed enum by variable + type alias --- packages/core/src/boot/init.ts | 10 +++--- packages/core/src/tools/error.ts | 18 +++++----- packages/logs/src/boot/logs.entry.ts | 2 -- packages/logs/src/domain/logger.ts | 34 ++++++++++-------- packages/logs/src/index.ts | 5 ++- packages/rum/src/boot/rum.entry.ts | 36 ++++++++----------- .../error/errorCollection.spec.ts | 3 +- .../error/errorCollection.ts | 7 ++-- packages/rum/src/index.ts | 1 - 9 files changed, 57 insertions(+), 59 deletions(-) diff --git a/packages/core/src/boot/init.ts b/packages/core/src/boot/init.ts index bc70d2d2e7..ad7aa3205f 100644 --- a/packages/core/src/boot/init.ts +++ b/packages/core/src/boot/init.ts @@ -34,10 +34,12 @@ export function defineGlobal(global: Global, } } -export enum Datacenter { - US = 'us', - EU = 'eu', -} +export const Datacenter = { + EU: 'eu', + US: 'us', +} as const + +export type Datacenter = (typeof Datacenter)[keyof typeof Datacenter] export const INTAKE_SITE = { [Datacenter.EU]: 'datadoghq.eu', diff --git a/packages/core/src/tools/error.ts b/packages/core/src/tools/error.ts index b0134a56e4..1b65b4bbc5 100644 --- a/packages/core/src/tools/error.ts +++ b/packages/core/src/tools/error.ts @@ -14,14 +14,16 @@ export interface RawError { } } -export enum ErrorSource { - AGENT = 'agent', - CONSOLE = 'console', - NETWORK = 'network', - SOURCE = 'source', - LOGGER = 'logger', - CUSTOM = 'custom', -} +export const ErrorSource = { + AGENT: 'agent', + CONSOLE: 'console', + CUSTOM: 'custom', + LOGGER: 'logger', + NETWORK: 'network', + SOURCE: 'source', +} as const + +export type ErrorSource = (typeof ErrorSource)[keyof typeof ErrorSource] export function formatUnknownError(stackTrace: StackTrace | undefined, errorObject: any, nonErrorPrefix: string) { if (!stackTrace || (stackTrace.message === undefined && !(errorObject instanceof Error))) { diff --git a/packages/logs/src/boot/logs.entry.ts b/packages/logs/src/boot/logs.entry.ts index 08965f79c2..76ce5d0694 100644 --- a/packages/logs/src/boot/logs.entry.ts +++ b/packages/logs/src/boot/logs.entry.ts @@ -24,8 +24,6 @@ export interface LoggerConfiguration { context?: Context } -export type Status = keyof typeof StatusType - export type LogsGlobal = ReturnType export const datadogLogs = makeLogsGlobal(startLogs) diff --git a/packages/logs/src/domain/logger.ts b/packages/logs/src/domain/logger.ts index 7f462e848b..a9984e77c3 100644 --- a/packages/logs/src/domain/logger.ts +++ b/packages/logs/src/domain/logger.ts @@ -1,13 +1,15 @@ import { combine, Context, ContextValue, createContextManager, ErrorSource, monitored } from '@datadog/browser-core' -export enum StatusType { - debug = 'debug', - info = 'info', - warn = 'warn', - error = 'error', -} +export const StatusType = { + debug: 'debug', + error: 'error', + info: 'info', + warn: 'warn', +} as const + +export type StatusType = (typeof StatusType)[keyof typeof StatusType] -export const STATUS_PRIORITIES: { [key in StatusType]: number } = { +const STATUS_PRIORITIES: { [key in StatusType]: number } = { [StatusType.debug]: 0, [StatusType.info]: 1, [StatusType.warn]: 2, @@ -22,26 +24,28 @@ export interface LogsMessage { [key: string]: ContextValue } -export enum HandlerType { - http = 'http', - console = 'console', - silent = 'silent', -} +export const HandlerType = { + console: 'console', + http: 'http', + silent: 'silent', +} as const + +export type HandlerType = (typeof HandlerType)[keyof typeof HandlerType] export class Logger { private contextManager = createContextManager() constructor( private sendLog: (message: LogsMessage) => void, - private handlerType = HandlerType.http, - private level = StatusType.debug, + private handlerType: HandlerType = HandlerType.http, + private level: StatusType = StatusType.debug, loggerContext: Context = {} ) { this.contextManager.set(loggerContext) } @monitored - log(message: string, messageContext?: Context, status = StatusType.info) { + log(message: string, messageContext?: Context, status: StatusType = StatusType.info) { if (STATUS_PRIORITIES[status] >= STATUS_PRIORITIES[this.level]) { switch (this.handlerType) { case HandlerType.http: diff --git a/packages/logs/src/index.ts b/packages/logs/src/index.ts index cbb07d9a0c..e4977bba23 100644 --- a/packages/logs/src/index.ts +++ b/packages/logs/src/index.ts @@ -1,3 +1,2 @@ -export { Datacenter } from '@datadog/browser-core' -export { StatusType, HandlerType, Logger, LogsMessage } from './domain/logger' -export { LogsUserConfiguration, Status, LoggerConfiguration, LogsGlobal, datadogLogs } from './boot/logs.entry' +export { Logger, LogsMessage } from './domain/logger' +export { LogsUserConfiguration, LoggerConfiguration, LogsGlobal, datadogLogs } from './boot/logs.entry' diff --git a/packages/rum/src/boot/rum.entry.ts b/packages/rum/src/boot/rum.entry.ts index 4480877900..59e6085b4f 100644 --- a/packages/rum/src/boot/rum.entry.ts +++ b/packages/rum/src/boot/rum.entry.ts @@ -15,7 +15,7 @@ import { UserConfiguration, } from '@datadog/browser-core' import { ActionType, CustomAction } from '../domain/rumEventsCollection/action/trackActions' -import { ProvidedError } from '../domain/rumEventsCollection/error/errorCollection' +import { ProvidedError, ProvidedSource } from '../domain/rumEventsCollection/error/errorCollection' import { startRum } from './rum' export interface RumUserConfiguration extends UserConfiguration { @@ -104,27 +104,21 @@ export function makeRumGlobal(startRumImpl: StartRum) { rumGlobal.addAction(name, context) }, - addError: monitor( - ( - error: unknown, - context?: Context, - source: ErrorSource.CUSTOM | ErrorSource.NETWORK | ErrorSource.SOURCE = ErrorSource.CUSTOM - ) => { - let checkedSource - if (source === ErrorSource.CUSTOM || source === ErrorSource.NETWORK || source === ErrorSource.SOURCE) { - checkedSource = source - } else { - console.error(`DD_RUM.addError: Invalid source '${source}'`) - checkedSource = ErrorSource.CUSTOM - } - addErrorStrategy({ - error, - context: deepClone(context), - source: checkedSource, - startTime: performance.now(), - }) + addError: monitor((error: unknown, context?: Context, source: ProvidedSource = ErrorSource.CUSTOM) => { + let checkedSource: ProvidedSource + if (source === ErrorSource.CUSTOM || source === ErrorSource.NETWORK || source === ErrorSource.SOURCE) { + checkedSource = source + } else { + console.error(`DD_RUM.addError: Invalid source '${source}'`) + checkedSource = ErrorSource.CUSTOM } - ), + addErrorStrategy({ + error, + context: deepClone(context), + source: checkedSource, + startTime: performance.now(), + }) + }), }) return rumGlobal diff --git a/packages/rum/src/domain/rumEventsCollection/error/errorCollection.spec.ts b/packages/rum/src/domain/rumEventsCollection/error/errorCollection.spec.ts index ac1a0a2dc7..b15c51317f 100644 --- a/packages/rum/src/domain/rumEventsCollection/error/errorCollection.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/error/errorCollection.spec.ts @@ -1,6 +1,5 @@ -import { Observable, RawError } from '@datadog/browser-core' +import { ErrorSource, Observable, RawError } from '@datadog/browser-core' import { setup, TestSetupBuilder } from '../../../../test/specHelper' -import { ErrorSource } from '../../../index' import { RumEventType } from '../../../types' import { doStartErrorCollection } from './errorCollection' diff --git a/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts b/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts index 48d8585b89..2b4ad474c9 100644 --- a/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts +++ b/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts @@ -2,7 +2,6 @@ import { computeStackTrace, Configuration, Context, - ErrorSource, formatUnknownError, getTimestamp, Observable, @@ -16,9 +15,11 @@ export interface ProvidedError { startTime: number error: unknown context?: Context - source: ErrorSource + source: ProvidedSource } +export type ProvidedSource = 'custom' | 'network' | 'source' + export function startErrorCollection(lifeCycle: LifeCycle, configuration: Configuration) { return doStartErrorCollection(lifeCycle, configuration, startAutomaticErrorCollection(configuration)) } @@ -42,7 +43,7 @@ export function doStartErrorCollection( } } -function computeRawError(error: unknown, startTime: number, source: ErrorSource): RawError { +function computeRawError(error: unknown, startTime: number, source: ProvidedSource): RawError { const stackTrace = error instanceof Error ? computeStackTrace(error) : undefined return { startTime, source, ...formatUnknownError(stackTrace, error, 'Provided') } } diff --git a/packages/rum/src/index.ts b/packages/rum/src/index.ts index 335f4b0d30..4cb9f1d7ac 100644 --- a/packages/rum/src/index.ts +++ b/packages/rum/src/index.ts @@ -1,2 +1 @@ -export { Datacenter, ErrorSource } from '@datadog/browser-core' export { RumUserConfiguration, RumGlobal, datadogRum } from './boot/rum.entry'