Skip to content

Commit 8562721

Browse files
committed
extract trace directory
1 parent 200a387 commit 8562721

File tree

7 files changed

+102
-99
lines changed

7 files changed

+102
-99
lines changed

packages/electron/src/domain/main/ipcMain.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ipcMain } from 'electron'
21
import type { IpcMain } from 'electron'
3-
import tracer from '../tracer'
4-
import type { DatadogCarrier } from '../trace'
2+
import { ipcMain } from 'electron'
3+
import tracer from '../trace/tracer'
4+
import type { DatadogCarrier } from '../trace/trace'
55

66
const SPAN_NAME_PREFIX = 'ipcMain'
77

packages/electron/src/domain/renderer/ipcRenderer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { IpcRenderer } from 'electron'
2-
import { createSpanIdentifier, createTraceIdentifier } from '@datadog/browser-rum-core/src/domain/tracing/identifier'
32
import { ipcRenderer } from 'electron'
4-
import type { DatadogCarrier } from '../trace'
3+
import { createSpanIdentifier, createTraceIdentifier } from '@datadog/browser-rum-core/src/domain/tracing/identifier'
4+
import type { DatadogCarrier } from '../trace/trace'
55

66
function createDatadogCarrier(): DatadogCarrier {
77
const spanId = createSpanIdentifier().toString()

packages/electron/src/domain/rum/convertSpans.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Observable } from '@datadog/browser-core'
22
import { generateUUID, ErrorHandling } from '@datadog/browser-core'
33
import type { RumErrorEvent } from '@datadog/browser-rum-core'
44
import { RumEventType } from '@datadog/browser-rum-core'
5-
import type { Trace } from '../trace'
5+
import type { Trace } from '../trace/trace'
66
import type { CollectedRumEvent } from './events'
77

88
export function startConvertSpanToRumEvent(
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { createServer } from 'node:http'
2+
import type { Observable } from '@datadog/browser-core'
3+
import { HookNames, DISCARDED } from '@datadog/browser-core'
4+
import { decode } from '@msgpack/msgpack'
5+
import type { Hooks } from '../../hooks'
6+
import type { Trace } from './trace'
7+
import tracer from './tracer'
8+
9+
export function createDdTraceAgent(onTraceObservable: Observable<Trace>, hooks: Hooks) {
10+
const server = createServer()
11+
12+
server.on('request', (req, res) => {
13+
// Collect binary data chunks
14+
const chunks: Buffer[] = []
15+
req.on('data', (chunk: Buffer) => {
16+
chunks.push(chunk)
17+
})
18+
req.on('end', () => {
19+
const buffer = Buffer.concat(chunks)
20+
21+
const decoded = decode(buffer) as Array<
22+
Array<{ name: string; type: string; meta: { [key: string]: unknown }; [key: string]: unknown }>
23+
>
24+
25+
const defaultRumEventAttributes = hooks.triggerHook(HookNames.Assemble, {
26+
eventType: 'span' as any,
27+
})!
28+
29+
if (defaultRumEventAttributes === DISCARDED) {
30+
return
31+
}
32+
33+
for (const trace of decoded) {
34+
const filteredTrace = trace
35+
.filter((span) => !isSdkRequest(span))
36+
.map((span) => ({
37+
// rewrite id
38+
...span,
39+
trace_id: Number(span.trace_id)?.toString(16),
40+
span_id: Number(span.span_id)?.toString(16),
41+
parent_id: Number(span.parent_id)?.toString(16),
42+
meta: {
43+
...span.meta,
44+
'_dd.application.id': defaultRumEventAttributes.application!.id,
45+
'_dd.session.id': defaultRumEventAttributes.session!.id,
46+
'_dd.view.id': defaultRumEventAttributes.view!.id,
47+
},
48+
}))
49+
50+
if (filteredTrace.length > 0) {
51+
onTraceObservable.notify(filteredTrace)
52+
}
53+
}
54+
})
55+
56+
// Respond with the agent API format that dd-trace expects
57+
res.writeHead(200, { 'Content-Type': 'application/json' })
58+
res.end(
59+
JSON.stringify({
60+
rate_by_service: {
61+
'service:dd-trace,env:prod': 1,
62+
},
63+
})
64+
)
65+
})
66+
67+
server.listen(0, () => {
68+
const addressInfo = server.address()
69+
if (!addressInfo) {
70+
throw new Error('Failed to get server address')
71+
}
72+
73+
if (typeof addressInfo === 'string') {
74+
throw new Error(`Address is a string: ${addressInfo}`)
75+
}
76+
77+
const { port } = addressInfo
78+
const url = `http://127.0.0.1:${port}`
79+
80+
// console.log('agents url', url)
81+
tracer.setUrl(url)
82+
})
83+
}
84+
85+
function isSdkRequest(span: any) {
86+
const spanRequestUrl = span.meta['http.url'] as string | undefined
87+
return (
88+
(spanRequestUrl &&
89+
(spanRequestUrl.startsWith('http://127.0.0.1') ||
90+
spanRequestUrl.startsWith('https://browser-intake-datadoghq.com/'))) ||
91+
(span.resource as string).startsWith('browser-intake-datadoghq.com')
92+
)
93+
}

packages/electron/src/entries/main.ts

Lines changed: 3 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
* - [x] setup bridge client with ipc from webviews (renderer processes)
1111
* - [x] use `exposeInMainWorld` to setup the bridge function that will setup the ipc to the main process
1212
*/
13-
import { createServer } from 'node:http'
1413
import type { RawError, PageMayExitEvent, Encoder, InitConfiguration } from '@datadog/browser-core'
1514
import {
1615
Observable,
@@ -19,25 +18,22 @@ import {
1918
createHttpRequest,
2019
createFlushController,
2120
createIdentityEncoder,
22-
HookNames,
23-
DISCARDED,
2421
} from '@datadog/browser-core'
2522
import type { RumConfiguration, RumInitConfiguration } from '@datadog/browser-rum-core'
2623
import { createHooks } from '@datadog/browser-rum-core'
2724
import { validateAndBuildRumConfiguration } from '@datadog/browser-rum-core/cjs/domain/configuration'
28-
import { decode } from '@msgpack/msgpack'
2925
import type { TrackType } from '@datadog/browser-core/cjs/domain/configuration'
3026
import { createEndpointBuilder } from '@datadog/browser-core/cjs/domain/configuration'
31-
import tracer from '../domain/tracer'
32-
import type { Hooks } from '../hooks'
27+
import tracer from '../domain/trace/tracer'
3328
import { createIpcMain } from '../domain/main/ipcMain'
3429
import type { CollectedRumEvent } from '../domain/rum/events'
3530
import { setupMainBridge } from '../domain/main/bridge'
3631
import { startActivityTracking } from '../domain/rum/activity'
3732
import { startRumEventAssembleAndSend } from '../domain/rum/assembly'
3833
import { startMainProcessTracking } from '../domain/rum/mainProcessTracking'
3934
import { startConvertSpanToRumEvent } from '../domain/rum/convertSpans'
40-
import type { Trace } from '../domain/trace'
35+
import type { Trace } from '../domain/trace/trace'
36+
import { createDdTraceAgent } from '../domain/trace/traceAgent'
4137

4238
function makeDatadogElectron() {
4339
return {
@@ -148,89 +144,3 @@ export function startElectronSpanBatch(
148144

149145
return batch
150146
}
151-
152-
function createDdTraceAgent(onTraceObservable: Observable<Trace>, hooks: Hooks) {
153-
const server = createServer()
154-
155-
server.on('request', (req, res) => {
156-
// Collect binary data chunks
157-
const chunks: Buffer[] = []
158-
req.on('data', (chunk: Buffer) => {
159-
chunks.push(chunk)
160-
})
161-
req.on('end', () => {
162-
const buffer = Buffer.concat(chunks)
163-
164-
const decoded = decode(buffer) as Array<
165-
Array<{ name: string; type: string; meta: { [key: string]: unknown }; [key: string]: unknown }>
166-
>
167-
168-
const defaultRumEventAttributes = hooks.triggerHook(HookNames.Assemble, {
169-
eventType: 'span' as any,
170-
})!
171-
172-
if (defaultRumEventAttributes === DISCARDED) {
173-
return
174-
}
175-
176-
for (const trace of decoded) {
177-
const filteredTrace = trace
178-
.filter((span) => !isSdkRequest(span))
179-
.map((span) => ({
180-
// rewrite id
181-
...span,
182-
trace_id: Number(span.trace_id)?.toString(16),
183-
span_id: Number(span.span_id)?.toString(16),
184-
parent_id: Number(span.parent_id)?.toString(16),
185-
meta: {
186-
...span.meta,
187-
'_dd.application.id': defaultRumEventAttributes.application!.id,
188-
'_dd.session.id': defaultRumEventAttributes.session!.id,
189-
'_dd.view.id': defaultRumEventAttributes.view!.id,
190-
},
191-
}))
192-
193-
if (filteredTrace.length > 0) {
194-
onTraceObservable.notify(filteredTrace)
195-
}
196-
}
197-
})
198-
199-
// Respond with the agent API format that dd-trace expects
200-
res.writeHead(200, { 'Content-Type': 'application/json' })
201-
res.end(
202-
JSON.stringify({
203-
rate_by_service: {
204-
'service:dd-trace,env:prod': 1,
205-
},
206-
})
207-
)
208-
})
209-
210-
server.listen(0, () => {
211-
const addressInfo = server.address()
212-
if (!addressInfo) {
213-
throw new Error('Failed to get server address')
214-
}
215-
216-
if (typeof addressInfo === 'string') {
217-
throw new Error(`Address is a string: ${addressInfo}`)
218-
}
219-
220-
const { port } = addressInfo
221-
const url = `http://127.0.0.1:${port}`
222-
223-
// console.log('agents url', url)
224-
tracer.setUrl(url)
225-
})
226-
}
227-
228-
function isSdkRequest(span: any) {
229-
const spanRequestUrl = span.meta['http.url'] as string | undefined
230-
return (
231-
(spanRequestUrl &&
232-
(spanRequestUrl.startsWith('http://127.0.0.1') ||
233-
spanRequestUrl.startsWith('https://browser-intake-datadoghq.com/'))) ||
234-
(span.resource as string).startsWith('browser-intake-datadoghq.com')
235-
)
236-
}

0 commit comments

Comments
 (0)