Skip to content

Commit ef519b6

Browse files
committed
fix(rum-core): add extra check in applyFilters
1 parent 3fffde1 commit ef519b6

File tree

5 files changed

+79
-14
lines changed

5 files changed

+79
-14
lines changed

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20
1+
20.19.0

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@
195195
"@elastic/apm-rum-vue": "file:packages/rum-vue"
196196
},
197197
"engines": {
198-
"node": ">=12 <=16",
199-
"npm": "6"
198+
"node": "^20.19.0 || ^22.12.0 || >=24.0.0"
200199
}
201200
}

packages/rum-core/src/common/config-service.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import { getCurrentScript, setLabel, merge, extend, isUndefined } from './utils'
2727
import EventHandler from './event-handler'
2828
import { CONFIG_CHANGE, LOCAL_CONFIG_KEY } from './constants'
29+
import { __DEV__ } from '../state'
2930

3031
function getConfigFromScript() {
3132
var script = getCurrentScript()
@@ -124,11 +125,23 @@ class Config {
124125
}
125126

126127
applyFilters(data) {
127-
for (var i = 0; i < this.filters.length; i++) {
128-
data = this.filters[i](data)
129-
if (!data) {
128+
try {
129+
for (var i = 0; i < this.filters.length; i++) {
130+
data = this.filters[i](data)
131+
if (!data) {
132+
return
133+
}
134+
}
135+
data.transactions = data.transactions.filter(t => t)
136+
data.errors = data.errors.filter(e => e)
137+
if (data.transactions.length === 0 && data.errors.length === 0) {
130138
return
131139
}
140+
} catch (e) {
141+
if (__DEV__) {
142+
console.log('[Elastic APM] Error applying filters for RUM payload', e)
143+
}
144+
return
132145
}
133146
return data
134147
}

packages/rum-core/test/common/config-service.spec.js

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,54 @@ describe('ConfigService', function () {
6868
})
6969

7070
it('should addFilter correctly', function () {
71+
var rawPayload = { transactions: [{}], errors: [{}] }
72+
var filterPayload1 = { transactions: [{}, {}], errors: [{}, {}] }
73+
var filterPayload2 = { transactions: [{}, {}, {}], errors: [{}, {}, {}] }
7174
expect(function () {
7275
configService.addFilter('test')
7376
}).toThrow()
7477

7578
configService.addFilter(function (testArg) {
76-
expect(testArg).toBe('hamid-test')
77-
return 'hamid-test-1'
79+
expect(testArg).toBe(rawPayload)
80+
return filterPayload1
7881
})
7982

8083
configService.addFilter(function (testArg) {
81-
expect(testArg).toBe('hamid-test-1')
82-
return 'hamid-test-2'
84+
expect(testArg).toBe(filterPayload1)
85+
return filterPayload2
8386
})
8487

85-
var result = configService.applyFilters('hamid-test')
86-
expect(result).toBe('hamid-test-2')
88+
var result = configService.applyFilters(rawPayload)
89+
expect(result).toBe(filterPayload2)
8790

8891
configService.addFilter(function () {})
8992
configService.addFilter(function () {
9093
throw new Error('Out of reach!')
9194
})
9295

93-
result = configService.applyFilters('hamid-test')
96+
result = configService.applyFilters(payload)
97+
expect(result).toBeUndefined()
98+
})
99+
100+
it('should applyFilters without throwing', function () {
101+
var rawPayload = { transactions: [{}], errors: [{}] }
102+
103+
configService.addFilter(function () {
104+
throw new Error('Should now throw!')
105+
})
106+
107+
var result = configService.applyFilters(rawPayload)
108+
expect(result).toBeUndefined()
109+
})
110+
111+
it('should applyFilters and filter empty payloads', function () {
112+
var rawPayload = { transactions: [{}], errors: [{}] }
113+
114+
configService.addFilter(function () {
115+
return { transactions: [], errors: [] }
116+
})
117+
118+
var result = configService.applyFilters(rawPayload)
94119
expect(result).toBeUndefined()
95120
})
96121

packages/rum/src/index.d.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,35 @@ interface ServiceFactory {
187187
}
188188

189189
type FilterFn = (payload: Payload) => Payload | boolean | void
190-
type Payload = { [key: string]: any }
190+
// type Payload = { [key: string]: any }
191+
type SpanPayload = {
192+
trace_id: string
193+
transaction_id: string
194+
parent_id: string
195+
id: string
196+
type: string
197+
subtype: string
198+
name: string
199+
}
200+
type TransactionPayload = {
201+
trace_id: string
202+
id: string
203+
type: string
204+
name: string
205+
spans: SpanPayload[]
206+
}
207+
type ErrorPayload = {
208+
id: string
209+
exception: {
210+
type: string
211+
message: string
212+
}
213+
}
214+
type Payload = {
215+
transactions: TransactionPayload[]
216+
errors: ErrorPayload[]
217+
[key: string]: any
218+
}
191219

192220
type TaskId = string | number
193221
type LabelValue = string | number | boolean

0 commit comments

Comments
 (0)