-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathmain.ts
More file actions
196 lines (173 loc) · 5.31 KB
/
Copy pathmain.ts
File metadata and controls
196 lines (173 loc) · 5.31 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
185
186
187
188
189
190
191
192
193
194
195
196
/**
* Datadog Browser Live Debugger SDK
* Provides live debugger capabilities for browser applications.
*
* @packageDocumentation
* @see [Live Debugger Documentation](https://docs.datadoghq.com/tracing/live_debugger/)
*/
import { defineGlobal, display, getGlobalObject, makePublicApi, mockable } from '@datadog/browser-core'
import type { PublicApi, Site } from '@datadog/browser-core'
import { initDebuggerTransport, onEntry, onReturn, onThrow } from '../domain/api'
import { startDeliveryApiPolling } from '../domain/deliveryApi'
import { getProbes } from '../domain/probes'
import { startDebuggerBatch } from '../transport/startDebuggerBatch'
export interface DebuggerBuildMetadata {
version?: string
}
/**
* Configuration options for initializing the Live Debugger SDK
*/
export interface DebuggerInitConfiguration {
/**
* The client token for Datadog. Required for authenticating your application with Datadog.
*
* @category Authentication
*/
clientToken: string
/**
* The Datadog site to send data to
*
* @category Transport
* @defaultValue 'datadoghq.com'
*/
site?: Site
/**
* The service name for your application
*
* @category Data Collection
*/
service: string
/**
* The application's environment (e.g., prod, staging)
*
* @category Data Collection
*/
env?: string
/**
* The application's version
*
* @category Data Collection
*/
version?: string
/**
* Polling interval in milliseconds for fetching probe updates
*
* @category Delivery API
* @defaultValue 60000
*/
pollInterval?: number
/**
* Maximum number of snapshot events allowed globally per second
*
* @category Data Collection
* @defaultValue 25
*/
maxSnapshotsPerSecondGlobally?: number
/**
* Default maximum number of snapshot events allowed per probe per second
*
* @category Data Collection
* @defaultValue 1
*/
maxSnapshotsPerSecondPerProbe?: number
/**
* Default maximum number of non-snapshot events allowed per probe per second
*
* @category Data Collection
* @defaultValue 5000
*/
maxNonSnapshotsPerSecondPerProbe?: number
/**
* A proxy URL for routing SDK requests. When set, delivery API requests are
* sent to `{proxy}/api/unstable/debugger/frontend/probes` instead of the
* default Datadog API host derived from `site`.
*
* @category Transport
*/
proxy?: string
}
/**
* Public API for the Live Debugger browser SDK.
*
* @category Main
*/
export interface DatadogDebugger extends PublicApi {
/**
* Initialize the Live Debugger SDK
*
* @category Init
* @param initConfiguration - Configuration options
* @example
* ```ts
* datadogDebugger.init({
* clientToken: '<DATADOG_CLIENT_TOKEN>',
* service: 'my-app',
* site: 'datadoghq.com',
* env: 'production'
* version: 'my-deployed-build-version',
* })
* ```
*/
init: (initConfiguration: DebuggerInitConfiguration) => void
}
export interface BrowserWindow extends Window {
DD_DEBUGGER?: DatadogDebugger
__DD_LIVE_DEBUGGER_BUILD__?: DebuggerBuildMetadata
$dd_entry?: typeof onEntry
$dd_return?: typeof onReturn
$dd_throw?: typeof onThrow
$dd_probes?: typeof getProbes
}
function resolveDebuggerVersion(initConfiguration: DebuggerInitConfiguration): string | undefined {
const buildVersion = getGlobalObject<BrowserWindow>().__DD_LIVE_DEBUGGER_BUILD__?.version
if (
initConfiguration.version !== undefined &&
buildVersion !== undefined &&
initConfiguration.version !== buildVersion
) {
display.warn(
`Debugger: init version "${initConfiguration.version}" does not match the build-plugin version "${buildVersion}". Using the init version.`
)
}
return initConfiguration.version ?? buildVersion
}
/**
* Create the public API for the Live Debugger
*/
function makeDebuggerPublicApi(): DatadogDebugger {
return makePublicApi<DatadogDebugger>({
init: (initConfiguration: DebuggerInitConfiguration) => {
const resolvedConfiguration = {
...initConfiguration,
version: resolveDebuggerVersion(initConfiguration),
}
// Initialize debugger's own transport
const batch = mockable(startDebuggerBatch)(resolvedConfiguration)
mockable(initDebuggerTransport)(resolvedConfiguration, batch)
// Expose internal hooks on globalThis for instrumented code
const debuggerGlobal = getGlobalObject<BrowserWindow>()
debuggerGlobal.$dd_entry = onEntry
debuggerGlobal.$dd_return = onReturn
debuggerGlobal.$dd_throw = onThrow
debuggerGlobal.$dd_probes = getProbes
mockable(startDeliveryApiPolling)({
service: resolvedConfiguration.service,
clientToken: resolvedConfiguration.clientToken,
site: resolvedConfiguration.site,
proxy: resolvedConfiguration.proxy,
env: resolvedConfiguration.env,
version: resolvedConfiguration.version,
pollInterval: resolvedConfiguration.pollInterval,
})
},
})
}
/**
* The global Live Debugger instance. Use this to call Live Debugger methods.
*
* @category Main
* @see {@link DatadogDebugger}
* @see [Live Debugger Documentation](https://docs.datadoghq.com/tracing/live_debugger/)
*/
export const datadogDebugger = makeDebuggerPublicApi()
defineGlobal(getGlobalObject<BrowserWindow>(), 'DD_DEBUGGER', datadogDebugger)