Skip to content

Commit 9384c30

Browse files
xnanodaxniieani
authored andcommitted
fix: update process performance entry after pairing
1 parent 3d996a2 commit 9384c30

File tree

4 files changed

+116
-131
lines changed

4 files changed

+116
-131
lines changed

src/v3/getSpanFromPerformanceEntry.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { getCommonUrlForTracing } from "../main";
2+
import { ensureTimestamp } from "./ensureTimestamp";
3+
import { Attributes, EntryType, InitiatorType, ScopeBase, Timestamp, TraceEntry } from "./types"
4+
5+
/**
6+
* Maps Performance Entry to Trace Entry
7+
* @param inputEntry - The performance entry event.
8+
* @returns {TraceEntry} The trace entry.
9+
*/
10+
export function getSpanFromPerformanceEntry<ScopeT extends ScopeBase>(
11+
inputEntry: PerformanceEntry,
12+
): TraceEntry<ScopeT> | undefined {
13+
14+
// react in dev mode generates hundreds of these marks, ignore them
15+
if (inputEntry.entryType === 'mark' && inputEntry.name.startsWith('--')) {
16+
return undefined
17+
}
18+
19+
const attributes =
20+
'details' in inputEntry && typeof inputEntry.detail === 'object' && inputEntry.details !== null
21+
? inputEntry.details as Attributes
22+
: {}
23+
24+
const type = inputEntry.entryType as EntryType;
25+
let { name } = inputEntry;
26+
27+
if (inputEntry.entryType === 'resource' || inputEntry.entryType === 'navigation') {
28+
const { commonUrl, query, hash } = getCommonUrlForTracing(inputEntry.name)
29+
name = commonUrl
30+
31+
// write a function in lotus to extract from datadog's SDK rather than hardcoding the implementation
32+
if (inputEntry.entryType === 'resource') {
33+
const resourceTiming = inputEntry as PerformanceResourceTiming
34+
35+
return {
36+
type,
37+
name,
38+
startTime: ensureTimestamp({ now: inputEntry.startTime }),
39+
attributes,
40+
duration: inputEntry.duration,
41+
// status,
42+
performanceEntry: inputEntry,
43+
resourceDetails: {
44+
initiatorType: resourceTiming.initiatorType as InitiatorType,
45+
query,
46+
hash
47+
}
48+
}
49+
}
50+
} else if (inputEntry.entryType !== 'mark' && inputEntry.entryType !== 'measure') {
51+
name = `${inputEntry.entryType}${inputEntry.name &&
52+
inputEntry.name !== 'unknown' &&
53+
inputEntry.name.length > 0 &&
54+
inputEntry.entryType !== inputEntry.name
55+
? `/${inputEntry.name}`
56+
: ''
57+
}`
58+
}
59+
60+
const timestamp: Partial<Timestamp> = {
61+
now: inputEntry.startTime
62+
}
63+
64+
const traceEntry: TraceEntry<ScopeT> = {
65+
type,
66+
name,
67+
startTime: ensureTimestamp(timestamp),
68+
attributes,
69+
duration: inputEntry.duration,
70+
// status,
71+
performanceEntry: inputEntry,
72+
}
73+
74+
return traceEntry
75+
}

src/v3/processPerformanceEntry.ts

Lines changed: 0 additions & 97 deletions
This file was deleted.

src/v3/traceManager.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// import { Trace } from './Trace'
22
import { ActiveTrace } from './ActiveTrace'
33
import { ensureTimestamp } from './ensureTimestamp'
4-
import { processPerformanceEntry } from './processPerformanceEntry'
4+
import { getSpanFromPerformanceEntry } from './getSpanFromPerformanceEntry'
55
import type {
66
CompleteTraceDefinition,
77
ComputedSpanDefinition,
@@ -119,11 +119,10 @@ const observePerformanceWithTraceManager = <ScopeT extends ScopeBase>(traceManag
119119

120120
const observer = new PerformanceObserver((entryList) => {
121121
entryList.getEntries().forEach((entry) => {
122-
const traceEntry = processPerformanceEntry<ScopeT>(entry)
122+
const traceEntry = getSpanFromPerformanceEntry<ScopeT>(entry)
123123
if (traceEntry !== undefined) {
124124
traceManager.processEntry(traceEntry)
125125
}
126-
127126
})
128127
})
129128

src/v3/types.ts

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export type ComponentLifecycleEntryType =
2626
| 'component-render'
2727
| 'component-unmount'
2828

29-
export type EntryType = NativePerformanceEntryType | ComponentLifecycleEntryType | 'asset' | 'iframe'
29+
export type EntryType = NativePerformanceEntryType | ComponentLifecycleEntryType
3030

3131
export interface Timestamp {
3232
// absolute count of ms from epoch
@@ -61,34 +61,6 @@ export interface ActiveTraceConfig<ScopeT extends ScopeBase>
6161
export type TraceEntryStatus = 'ok' | 'error'
6262

6363
export interface Attributes {
64-
resource?: {
65-
type?:
66-
| 'document'
67-
| 'xhr'
68-
| 'beacon'
69-
| 'fetch'
70-
| 'css'
71-
| 'js'
72-
| 'image'
73-
| 'font'
74-
| 'media'
75-
| 'other'
76-
| 'native'
77-
method?:
78-
| 'POST'
79-
| 'GET'
80-
| 'HEAD'
81-
| 'PUT'
82-
| 'DELETE'
83-
| 'PATCH'
84-
| 'TRACE'
85-
| 'OPTIONS'
86-
| 'CONNECT'
87-
status?: number | undefined
88-
}
89-
resourceQuery?: Record<string, string | string[]>
90-
resourceHash?: string
91-
9264
[key: string]: unknown
9365
}
9466

@@ -103,7 +75,9 @@ export interface TraceEntryBase<ScopeT extends ScopeBase> {
10375

10476
startTime: Timestamp
10577

106-
scope: ScopeT
78+
// non performance entries, scope would be coming from outside like ticket id or user id
79+
// performance entries, there is no scope OR scope is in performance detail
80+
scope?: ScopeT
10781

10882
attributes: Attributes
10983

@@ -117,7 +91,8 @@ export interface TraceEntryBase<ScopeT extends ScopeBase> {
11791
/**
11892
* Status of the trace entry ('error' or 'ok').
11993
*/
120-
status: TraceEntryStatus
94+
// TODO: come back to this decision
95+
status?: TraceEntryStatus
12196

12297
/**
12398
* The original PerformanceEntry from which the TraceEntry
@@ -137,12 +112,45 @@ export interface ComponentRenderTraceEntry<ScopeT extends ScopeBase>
137112
errorInfo?: ErrorInfo
138113
}
139114

115+
export type InitiatorType =
116+
| 'audio'
117+
| 'beacon'
118+
| 'body'
119+
| 'css'
120+
| 'early-hint'
121+
| 'embed'
122+
| 'fetch'
123+
| 'frame'
124+
| 'iframe'
125+
| 'icon'
126+
| 'image'
127+
| 'img'
128+
| 'input'
129+
| 'link'
130+
| 'navigation'
131+
| 'object'
132+
| 'ping'
133+
| 'script'
134+
| 'track'
135+
| 'video'
136+
| 'xmlhttprequest'
137+
| 'other';
138+
139+
export interface ResourceSpan<ScopeT extends ScopeBase> extends TraceEntryBase<ScopeT> {
140+
resourceDetails: {
141+
initiatorType: InitiatorType
142+
query: Record<string, string | string[]>
143+
hash: string
144+
}
145+
}
146+
140147
/**
141148
* All possible trace entries
142149
*/
143150
export type TraceEntry<ScopeT extends ScopeBase> =
144151
| TraceEntryBase<ScopeT>
145152
| ComponentRenderTraceEntry<ScopeT>
153+
| ResourceSpan<ScopeT>
146154

147155
/**
148156
* Criteria for matching performance entries.

0 commit comments

Comments
 (0)