Skip to content

Commit

Permalink
✨ add telemetry event types
Browse files Browse the repository at this point in the history
  • Loading branch information
bcaudan committed Feb 23, 2022
1 parent c716d91 commit e57a036
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 23 deletions.
120 changes: 120 additions & 0 deletions packages/core/src/domain/internalMonitoring/telemetryEvent.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/* eslint-disable */
/**
* DO NOT MODIFY IT BY HAND. Run `yarn rum-events-format:sync` instead.
*/

/**
* Schema of all properties of a telemetry event
*/
export type TelemetryEvent = TelemetryErrorEvent | TelemetryDebugEvent
/**
* Schema of all properties of a telemetry error event
*/
export type TelemetryErrorEvent = CommonTelemetryProperties & {
/**
* Level/severity of the log
*/
status: 'error'
/**
* Body of the log
*/
message: string
/**
* Error properties
*/
error?: {
/**
* The stack trace or the complementary information about the error
*/
stack?: string
/**
* The error type or kind (or code in some cases)
*/
kind?: string
[k: string]: unknown
}
[k: string]: unknown
}
/**
* Schema of all properties of a telemetry debug event
*/
export type TelemetryDebugEvent = CommonTelemetryProperties & {
/**
* Level/severity of the log
*/
status: 'debug'
/**
* Body of the log
*/
message: string
[k: string]: unknown
}

/**
* Schema of common properties of Telemetry events
*/
export interface CommonTelemetryProperties {
/**
* Internal properties
*/
_dd: {
/**
* Event type
*/
event_type: 'internal_telemetry'
[k: string]: unknown
}
/**
* Start of the event in ms from epoch
*/
date: number
/**
* The SDK generating the telemetry event
*/
service: string
/**
* The version of the SDK generating the telemetry event
*/
version: string
/**
* Application properties
*/
readonly application?: {
/**
* UUID of the application
*/
id: string
[k: string]: unknown
}
/**
* Session properties
*/
session?: {
/**
* UUID of the session
*/
id: string
[k: string]: unknown
}
/**
* View properties
*/
view?: {
/**
* UUID of the view
*/
id: string
[k: string]: unknown
}
/**
* Action properties
*/
action?: {
/**
* UUID of the action
*/
id: string
[k: string]: unknown
}
[k: string]: unknown
}
32 changes: 16 additions & 16 deletions packages/rum-core/test/formatValidation.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import type { Context } from '@datadog/browser-core'
import ajv from 'ajv'
import rumEventsFormatJson from '../../../rum-events-format/rum-events-format.json'
import _commonSchemaJson from '../../../rum-events-format/schemas/_common-schema.json'
import actionSchemaJson from '../../../rum-events-format/schemas/action-schema.json'
import errorSchemaJson from '../../../rum-events-format/schemas/error-schema.json'
import longTaskSchemaJson from '../../../rum-events-format/schemas/long_task-schema.json'
import resourceSchemaJson from '../../../rum-events-format/schemas/resource-schema.json'
import viewSchemaJson from '../../../rum-events-format/schemas/view-schema.json'
import rumEventsSchemaJson from '../../../rum-events-format/schemas/rum-events-schema.json'
import _commonSchemaJson from '../../../rum-events-format/schemas/rum/_common-schema.json'
import actionSchemaJson from '../../../rum-events-format/schemas/rum/action-schema.json'
import errorSchemaJson from '../../../rum-events-format/schemas/rum/error-schema.json'
import longTaskSchemaJson from '../../../rum-events-format/schemas/rum/long_task-schema.json'
import resourceSchemaJson from '../../../rum-events-format/schemas/rum/resource-schema.json'
import viewSchemaJson from '../../../rum-events-format/schemas/rum/view-schema.json'

export function validateFormat(rumEvent: Context) {
export function validateRumFormat(rumEvent: Context) {
const instance = new ajv({
allErrors: true,
})
void instance
.addSchema(_commonSchemaJson, 'schemas/_common-schema.json')
.addSchema(viewSchemaJson, 'schemas/view-schema.json')
.addSchema(actionSchemaJson, 'schemas/action-schema.json')
.addSchema(resourceSchemaJson, 'schemas/resource-schema.json')
.addSchema(longTaskSchemaJson, 'schemas/long_task-schema.json')
.addSchema(errorSchemaJson, 'schemas/error-schema.json')
.addSchema(rumEventsFormatJson, 'rum-events-format.json')
.validate('rum-events-format.json', rumEvent)
.addSchema(_commonSchemaJson, 'rum/_common-schema.json')
.addSchema(viewSchemaJson, 'rum/view-schema.json')
.addSchema(actionSchemaJson, 'rum/action-schema.json')
.addSchema(resourceSchemaJson, 'rum/resource-schema.json')
.addSchema(longTaskSchemaJson, 'rum/long_task-schema.json')
.addSchema(errorSchemaJson, 'rum/error-schema.json')
.addSchema(rumEventsSchemaJson, 'rum-events-schema.json')
.validate('rum-events-schema.json', rumEvent)

if (instance.errors) {
instance.errors.map((error) => fail(`${error.dataPath || 'event'} ${error.message!}`))
Expand Down
4 changes: 2 additions & 2 deletions packages/rum-core/test/specHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import type { CiTestWindow } from '../src/domain/ciTestContext'
import type { RumConfiguration } from '../src/domain/configuration'
import { validateAndBuildRumConfiguration } from '../src/domain/configuration'
import { validateFormat } from './formatValidation'
import { validateRumFormat } from './formatValidation'
import { createRumSessionManagerMock } from './mockRumSessionManager'

export interface TestSetupBuilder {
Expand Down Expand Up @@ -205,7 +205,7 @@ function validateRumEventFormat(rawRumEvent: RawRumEvent) {
url: 'fake url',
},
}
validateFormat(combine(fakeContext, rawRumEvent))
validateRumFormat(combine(fakeContext, rawRumEvent))
}

export type ViewTest = ReturnType<typeof setupViewTest>
Expand Down
16 changes: 12 additions & 4 deletions scripts/generate-schema-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ const { compileFromFile } = require('json-schema-to-typescript')
const prettier = require('prettier')
const { printLog, logAndExit } = require('./utils')

const workingDirectory = path.join(__dirname, '../rum-events-format')
const schemaPath = path.join(workingDirectory, 'rum-events-format.json')
const compiledTypesPath = path.join(__dirname, '../packages/rum-core/src/rumEvent.types.ts')
const schemasDirectoryPath = path.join(__dirname, '../rum-events-format/schemas')
const prettierConfigPath = path.join(__dirname, '../.prettierrc.yml')

async function main() {
await generateTypesFromSchema('../packages/rum-core/src/rumEvent.types.ts', 'rum-events-schema.json')
await generateTypesFromSchema(
'../packages/core/src/domain/internalMonitoring/telemetryEvent.types.ts',
'telemetry-events-schema.json'
)
}

async function generateTypesFromSchema(typesPath, schema) {
const schemaPath = path.join(schemasDirectoryPath, schema)
const compiledTypesPath = path.join(__dirname, typesPath)
const prettierConfig = await prettier.resolveConfig(prettierConfigPath)
printLog(`Compiling ${schemaPath}...`)
const compiledTypes = await compileFromFile(schemaPath, {
cwd: workingDirectory,
cwd: schemasDirectoryPath,
bannerComment:
'/* eslint-disable */\n/**\n * DO NOT MODIFY IT BY HAND. Run `yarn rum-events-format:sync` instead.\n*/',
style: prettierConfig,
Expand Down

0 comments on commit e57a036

Please sign in to comment.