Skip to content
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

💥 [RUMF-730] prefer object and type alias over enum in APIs #630

Merged
merged 14 commits into from
Dec 14, 2020
Merged
10 changes: 6 additions & 4 deletions packages/core/src/boot/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ export function defineGlobal<Global, Name extends keyof Global>(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',
Expand Down
18 changes: 10 additions & 8 deletions packages/core/src/tools/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down
2 changes: 0 additions & 2 deletions packages/logs/src/boot/logs.entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ export interface LoggerConfiguration {
context?: Context
}

export type Status = keyof typeof StatusType

export type LogsGlobal = ReturnType<typeof makeLogsGlobal>

export const datadogLogs = makeLogsGlobal(startLogs)
Expand Down
34 changes: 19 additions & 15 deletions packages/logs/src/domain/logger.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions packages/logs/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
36 changes: 15 additions & 21 deletions packages/rum/src/boot/rum.entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
computeStackTrace,
Configuration,
Context,
ErrorSource,
formatUnknownError,
getTimestamp,
Observable,
Expand All @@ -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))
}
Expand All @@ -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') }
}
Expand Down
1 change: 0 additions & 1 deletion packages/rum/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { Datacenter, ErrorSource } from '@datadog/browser-core'
export { RumUserConfiguration, RumGlobal, datadogRum } from './boot/rum.entry'