Skip to content

Commit fdeba89

Browse files
vigneshshanmugamdevcorpio
authored andcommitted
fix(rum-react): respect active flag in react integration (elastic#392)
* fix(rum-react): respect active flag in react integration * remove logging when inactive * add check for same component * use apm.init instead of config init
1 parent ff5015e commit fdeba89

File tree

3 files changed

+59
-19
lines changed

3 files changed

+59
-19
lines changed

packages/rum-react/src/get-with-transaction.js

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ function isReactClassComponent(Component) {
4747
function getWithTransaction(apm) {
4848
return function withTransaction(name, type) {
4949
return function(Component) {
50+
const configService = apm.serviceFactory.getService('ConfigService')
51+
if (!configService.isActive()) {
52+
return Component
53+
}
54+
5055
if (!Component) {
5156
const loggingService = apm.serviceFactory.getService('LoggingService')
5257
loggingService.warn(

packages/rum-react/test/specs/get-apm-route.spec.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,29 @@ import { MemoryRouter as Router, Route } from 'react-router-dom'
3838
import { ApmBase } from '@elastic/apm-rum'
3939
import { createServiceFactory } from '@elastic/apm-rum-core'
4040
import { getApmRoute } from '../../src/get-apm-route'
41+
import { getGlobalConfig } from '../../../../dev-utils/test-config'
4142

4243
function Component(props) {
4344
return <h1>Testing, {props.name}</h1>
4445
}
4546

4647
describe('ApmRoute', function() {
48+
const { serverUrl, serviceName } = getGlobalConfig().agentConfig
49+
let serviceFactory, apmBase
50+
51+
beforeEach(() => {
52+
serviceFactory = createServiceFactory()
53+
apmBase = new ApmBase(serviceFactory, false)
54+
apmBase.init({
55+
active: true,
56+
serverUrl,
57+
serviceName,
58+
disableInstrumentations: ['page-load', 'error']
59+
})
60+
})
61+
4762
it('should work Route component', function() {
48-
const ApmRoute = getApmRoute(new ApmBase(createServiceFactory()))
63+
const ApmRoute = getApmRoute(apmBase)
4964

5065
const rendered = mount(
5166
<div>
@@ -67,8 +82,6 @@ describe('ApmRoute', function() {
6782
})
6883

6984
it('should work with Route render and log warning', function() {
70-
const serviceFactory = createServiceFactory()
71-
const apmBase = new ApmBase(serviceFactory)
7285
const loggingService = serviceFactory.getService('LoggingService')
7386
const ApmRoute = getApmRoute(apmBase)
7487

packages/rum-react/test/specs/get-with-transaction.spec.js

+38-16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Enzyme.configure({ adapter: new Adapter() })
3232
import { getWithTransaction } from '../../src/get-with-transaction'
3333
import { ApmBase } from '@elastic/apm-rum'
3434
import { createServiceFactory } from '@elastic/apm-rum-core'
35+
import { getGlobalConfig } from '../../../../dev-utils/test-config'
3536

3637
function TestComponent(apm) {
3738
const withTransaction = getWithTransaction(apm)
@@ -51,26 +52,30 @@ function TestComponent(apm) {
5152
}
5253

5354
describe('withTransaction', function() {
55+
const { serverUrl, serviceName } = getGlobalConfig().agentConfig
56+
let apmBase, serviceFactory
57+
58+
beforeEach(() => {
59+
serviceFactory = createServiceFactory()
60+
apmBase = new ApmBase(serviceFactory, false)
61+
apmBase.init({
62+
active: true,
63+
serverUrl,
64+
serviceName,
65+
disableInstrumentations: ['page-load', 'error']
66+
})
67+
})
68+
5469
it('should work if apm is disabled or not initialized', function() {
55-
TestComponent(new ApmBase(createServiceFactory(), true))
56-
TestComponent(new ApmBase(createServiceFactory(), false))
70+
TestComponent(new ApmBase(serviceFactory, true))
71+
TestComponent(apmBase)
5772
})
5873

5974
it('should start transaction for components', function() {
60-
const serviceFactory = createServiceFactory()
6175
const transactionService = serviceFactory.getService('TransactionService')
62-
63-
var apm = new ApmBase(serviceFactory, false)
64-
apm.init({
65-
debug: true,
66-
serverUrl: 'http://localhost:8200',
67-
serviceName: 'apm-agent-rum-react-integration-unit-test',
68-
sendPageLoadTransaction: false
69-
})
70-
7176
spyOn(transactionService, 'startTransaction')
7277

73-
TestComponent(apm)
78+
TestComponent(apmBase)
7479
expect(transactionService.startTransaction).toHaveBeenCalledWith(
7580
'test-transaction',
7681
'test-type',
@@ -79,16 +84,33 @@ describe('withTransaction', function() {
7984
})
8085

8186
it('should return WrappedComponent on falsy value and log warning', function() {
82-
const serviceFactory = createServiceFactory()
8387
const loggingService = serviceFactory.getService('LoggingService')
84-
8588
spyOn(loggingService, 'warn')
8689

87-
const withTransaction = getWithTransaction(new ApmBase(serviceFactory))
90+
const withTransaction = getWithTransaction(apmBase)
8891
const comp = withTransaction('test-name', 'test-type')(undefined)
8992
expect(comp).toBe(undefined)
9093
expect(loggingService.warn).toHaveBeenCalledWith(
9194
'test-name is not instrumented since component property is not provided'
9295
)
9396
})
97+
98+
it('should not instrument the route when rum is inactive', () => {
99+
const transactionService = serviceFactory.getService('TransactionService')
100+
spyOn(transactionService, 'startTransaction')
101+
102+
apmBase.config({ active: false })
103+
TestComponent(apmBase)
104+
105+
function Component() {
106+
return <h2>Component</h2>
107+
}
108+
const withTransaction = getWithTransaction(apmBase)
109+
110+
const WrappedComponent = withTransaction('test-transaction', 'test-type')(
111+
Component
112+
)
113+
expect(WrappedComponent).toEqual(Component)
114+
expect(transactionService.startTransaction).not.toHaveBeenCalled()
115+
})
94116
})

0 commit comments

Comments
 (0)