-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathvitalCollection.ts
More file actions
184 lines (162 loc) · 4.91 KB
/
Copy pathvitalCollection.ts
File metadata and controls
184 lines (162 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import type { ClocksState, Duration } from '@datadog/browser-core'
import { clocksNow, generateUUID, sanitize, toServerDuration } from '@datadog/browser-core'
import type { LifeCycle, RawRumEventCollectedData } from '../lifeCycle'
import { LifeCycleEventType } from '../lifeCycle'
import type { RawRumVitalEvent } from '../../rawRumEvent.types'
import { RumEventType, VitalType } from '../../rawRumEvent.types'
import type { PageStateHistory } from '../contexts/pageStateHistory'
import { PageState } from '../contexts/pageStateHistory'
import { startEventTracker } from '../eventTracker'
/**
* Vital options
*/
export interface VitalOptions {
/**
* Vital context
*/
context?: any
/**
* Vital description
*/
description?: string
}
/**
* Duration vital options
*/
export interface DurationVitalOptions extends VitalOptions {
/**
* Vital key, used to identify a specific vital instance when multiple vitals with the same name
* are tracked simultaneously
*/
vitalKey?: string
}
export interface FeatureOperationOptions extends VitalOptions {
operationKey?: string
}
export type FailureReason = 'error' | 'abandoned' | 'other'
/**
* Add duration vital options
*/
export interface AddDurationVitalOptions extends DurationVitalOptions {
/**
* Vital start time, expects a UNIX timestamp in milliseconds (the number of milliseconds since January 1, 1970)
*/
startTime: number
/**
* Vital duration, expects a duration in milliseconds
*/
duration: number
}
export interface DurationVitalStart extends DurationVitalOptions {
id: string
name: string
startClocks: ClocksState
handlingStack?: string
}
interface BaseVital extends VitalOptions {
name: string
startClocks: ClocksState
handlingStack?: string
}
export interface DurationVital extends BaseVital {
id: string
type: typeof VitalType.DURATION
duration: Duration
}
export interface OperationStepVital extends BaseVital {
type: typeof VitalType.OPERATION_STEP
stepType: 'start' | 'end'
operationKey?: string
failureReason?: string
}
interface DurationVitalData {
name: string
context?: any
description?: string
handlingStack?: string
}
export function startVitalCollection(lifeCycle: LifeCycle, pageStateHistory: PageStateHistory) {
const vitalTracker = startEventTracker<DurationVitalData>(lifeCycle)
function isValid(vital: DurationVital) {
return !pageStateHistory.wasInPageStateDuringPeriod(PageState.FROZEN, vital.startClocks.relative, vital.duration)
}
function addDurationVital(vital: DurationVital) {
if (isValid(vital)) {
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processVital(vital))
}
}
function addOperationStepVital(
name: string,
stepType: 'start' | 'end',
options?: FeatureOperationOptions & { handlingStack?: string },
failureReason?: FailureReason
) {
const { operationKey, context, description, handlingStack } = options || {}
const vital: OperationStepVital = {
name,
type: VitalType.OPERATION_STEP,
operationKey,
failureReason,
stepType,
startClocks: clocksNow(),
context: sanitize(context),
description,
handlingStack,
}
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processVital(vital))
}
function startDurationVital(
name: string,
options: DurationVitalOptions & { handlingStack?: string } = {},
startClocks = clocksNow()
) {
const { id } = vitalTracker.start(options.vitalKey ?? name, startClocks, {
name,
...options,
})
lifeCycle.notify(LifeCycleEventType.DURATION_VITAL_STARTED, { id, name, startClocks, ...options })
}
function stopDurationVital(name: string, options: DurationVitalOptions = {}, stopClocks = clocksNow()) {
const stopped = vitalTracker.stop(options.vitalKey ?? name, stopClocks, options)
if (stopped) {
addDurationVital({
type: VitalType.DURATION,
...stopped,
})
}
}
return {
addOperationStepVital,
addDurationVital,
startDurationVital,
stopDurationVital,
}
}
function processVital(vital: DurationVital | OperationStepVital): RawRumEventCollectedData<RawRumVitalEvent> {
const { startClocks, type, name, description, context, handlingStack } = vital
const vitalId = vital.type === VitalType.DURATION ? vital.id : undefined
const vitalData = {
id: vitalId ?? generateUUID(),
type,
name,
description,
...(type === VitalType.DURATION
? { duration: toServerDuration(vital.duration) }
: {
step_type: vital.stepType,
operation_key: vital.operationKey,
failure_reason: vital.failureReason,
}),
}
return {
rawRumEvent: {
date: startClocks.timeStamp,
vital: vitalData,
type: RumEventType.VITAL,
context,
},
startClocks,
duration: type === VitalType.DURATION ? vital.duration : undefined,
domainContext: handlingStack ? { handlingStack } : {},
}
}