Skip to content

Commit fa0606b

Browse files
committed
Use internalClient for agent conf queries only
1 parent 67b652f commit fa0606b

File tree

16 files changed

+85
-32
lines changed

16 files changed

+85
-32
lines changed

x-pack/legacy/plugins/apm/public/utils/testHelpers.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ interface MockSetup {
9797
start: number;
9898
end: number;
9999
client: any;
100+
internalClient: any;
100101
config: {
101102
get: any;
102103
has: any;
@@ -122,12 +123,21 @@ export async function inspectSearchParams(
122123
}
123124
});
124125

126+
const internalClientSpy = jest.fn().mockReturnValueOnce({
127+
hits: {
128+
total: 0
129+
}
130+
});
131+
125132
const mockSetup = {
126133
start: 1528113600000,
127134
end: 1528977600000,
128135
client: {
129136
search: clientSpy
130137
} as any,
138+
internalClient: {
139+
search: internalClientSpy
140+
} as any,
131141
config: {
132142
get: () => 'myIndex' as any,
133143
has: () => true
@@ -153,8 +163,15 @@ export async function inspectSearchParams(
153163
// we're only extracting the search params
154164
}
155165

166+
let params;
167+
if (clientSpy.mock.calls.length) {
168+
params = clientSpy.mock.calls[0][0];
169+
} else {
170+
params = internalClientSpy.mock.calls[0][0];
171+
}
172+
156173
return {
157-
params: clientSpy.mock.calls[0][0],
174+
params,
158175
teardown: () => clientSpy.mockClear()
159176
};
160177
}

x-pack/legacy/plugins/apm/server/lib/errors/distribution/__tests__/get_buckets.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ describe('timeseriesFetcher', () => {
3131
client: {
3232
search: clientSpy
3333
} as any,
34+
internalClient: {
35+
search: clientSpy
36+
} as any,
3437
config: {
3538
get: () => 'myIndex' as any,
3639
has: () => true

x-pack/legacy/plugins/apm/server/lib/helpers/setup_request.test.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,25 @@ jest.mock('../settings/apm_indices/get_apm_indices', () => ({
2121

2222
function getMockRequest() {
2323
const callWithRequestSpy = jest.fn();
24+
const callWithInternalUserSpy = jest.fn();
2425
const mockRequest = ({
2526
params: {},
2627
query: {},
2728
server: {
2829
config: () => ({ get: () => 'apm-*' }),
2930
plugins: {
3031
elasticsearch: {
31-
getCluster: () => ({ callWithRequest: callWithRequestSpy })
32+
getCluster: () => ({
33+
callWithRequest: callWithRequestSpy,
34+
callWithInternalUser: callWithInternalUserSpy
35+
})
3236
}
3337
}
3438
},
3539
getUiSettingsService: () => ({ get: async () => false })
3640
} as any) as Legacy.Request;
3741

38-
return { callWithRequestSpy, mockRequest };
42+
return { callWithRequestSpy, callWithInternalUserSpy, mockRequest };
3943
}
4044

4145
describe('setupRequest', () => {
@@ -57,6 +61,27 @@ describe('setupRequest', () => {
5761
});
5862
});
5963

64+
it('should call callWithInternalUser with default args', async () => {
65+
const { mockRequest, callWithInternalUserSpy } = getMockRequest();
66+
const { internalClient } = await setupRequest(mockRequest);
67+
await internalClient.search({
68+
index: 'apm-*',
69+
body: { foo: 'bar' }
70+
} as any);
71+
expect(callWithInternalUserSpy).toHaveBeenCalledWith('search', {
72+
index: 'apm-*',
73+
body: {
74+
foo: 'bar',
75+
query: {
76+
bool: {
77+
filter: [{ range: { 'observer.version_major': { gte: 7 } } }]
78+
}
79+
}
80+
},
81+
ignore_throttled: true
82+
});
83+
});
84+
6085
describe('observer.version_major filter', () => {
6186
describe('if index is apm-*', () => {
6287
it('should merge `observer.version_major` filter with existing boolean filters', async () => {

x-pack/legacy/plugins/apm/server/lib/helpers/setup_request.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,8 @@ export interface APMRequestQuery {
2727
uiFilters?: string;
2828
}
2929

30-
interface SetupOptions {
31-
clientAsInternalUser?: boolean;
32-
}
33-
3430
export type Setup = PromiseReturnType<typeof setupRequest>;
35-
export async function setupRequest(
36-
req: Legacy.Request,
37-
{ clientAsInternalUser = false }: SetupOptions = {}
38-
) {
31+
export async function setupRequest(req: Legacy.Request) {
3932
const query = (req.query as unknown) as APMRequestQuery;
4033
const { server } = req;
4134
const config = server.config();
@@ -48,7 +41,8 @@ export async function setupRequest(
4841
start: moment.utc(query.start).valueOf(),
4942
end: moment.utc(query.end).valueOf(),
5043
uiFiltersES,
51-
client: getESClient(req, { clientAsInternalUser }),
44+
client: getESClient(req, { clientAsInternalUser: false }),
45+
internalClient: getESClient(req, { clientAsInternalUser: true }),
5246
config,
5347
indices
5448
};

x-pack/legacy/plugins/apm/server/lib/settings/agent_configuration/create_or_update_configuration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export async function createOrUpdateConfiguration({
2121
>;
2222
setup: Setup;
2323
}) {
24-
const { client, indices } = setup;
24+
const { internalClient, indices } = setup;
2525

2626
const params: APMIndexDocumentParams<AgentConfiguration> = {
2727
refresh: true,
@@ -44,5 +44,5 @@ export async function createOrUpdateConfiguration({
4444
params.id = configurationId;
4545
}
4646

47-
return client.index(params);
47+
return internalClient.index(params);
4848
}

x-pack/legacy/plugins/apm/server/lib/settings/agent_configuration/delete_configuration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ export async function deleteConfiguration({
1313
configurationId: string;
1414
setup: Setup;
1515
}) {
16-
const { client, indices } = setup;
16+
const { internalClient, indices } = setup;
1717

1818
const params = {
1919
refresh: 'wait_for',
2020
index: indices['apm_oss.apmAgentConfigurationIndex'],
2121
id: configurationId
2222
};
2323

24-
return client.delete(params);
24+
return internalClient.delete(params);
2525
}

x-pack/legacy/plugins/apm/server/lib/settings/agent_configuration/get_environments/get_existing_environments_for_service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function getExistingEnvironmentsForService({
1919
serviceName: string | undefined;
2020
setup: Setup;
2121
}) {
22-
const { client, indices } = setup;
22+
const { internalClient, indices } = setup;
2323

2424
const bool = serviceName
2525
? { filter: [{ term: { [SERVICE_NAME]: serviceName } }] }
@@ -42,7 +42,7 @@ export async function getExistingEnvironmentsForService({
4242
}
4343
};
4444

45-
const resp = await client.search(params);
45+
const resp = await internalClient.search(params);
4646
const buckets = idx(resp.aggregations, _ => _.environments.buckets) || [];
4747
return buckets.map(bucket => bucket.key as string);
4848
}

x-pack/legacy/plugins/apm/server/lib/settings/agent_configuration/list_configurations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ export type AgentConfigurationListAPIResponse = PromiseReturnType<
1212
typeof listConfigurations
1313
>;
1414
export async function listConfigurations({ setup }: { setup: Setup }) {
15-
const { client, indices } = setup;
15+
const { internalClient, indices } = setup;
1616

1717
const params = {
1818
index: indices['apm_oss.apmAgentConfigurationIndex']
1919
};
2020

21-
const resp = await client.search<AgentConfiguration>(params);
21+
const resp = await internalClient.search<AgentConfiguration>(params);
2222
return resp.hits.hits.map(item => ({
2323
id: item._id,
2424
...item._source

x-pack/legacy/plugins/apm/server/lib/settings/agent_configuration/mark_applied_by_agent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export async function markAppliedByAgent({
1616
body: AgentConfiguration;
1717
setup: Setup;
1818
}) {
19-
const { client, indices } = setup;
19+
const { internalClient, indices } = setup;
2020

2121
const params = {
2222
index: indices['apm_oss.apmAgentConfigurationIndex'],
@@ -27,5 +27,5 @@ export async function markAppliedByAgent({
2727
}
2828
};
2929

30-
return client.index<AgentConfiguration>(params);
30+
return internalClient.index<AgentConfiguration>(params);
3131
}

x-pack/legacy/plugins/apm/server/lib/settings/agent_configuration/search.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('search configurations', () => {
1616
setup: ({
1717
config: { get: () => '' },
1818
client: { search: async () => searchMocks },
19+
internalClient: { search: async () => searchMocks },
1920
indices: {
2021
apm_oss: {
2122
sourcemapIndices: 'myIndex',
@@ -41,6 +42,7 @@ describe('search configurations', () => {
4142
setup: ({
4243
config: { get: () => '' },
4344
client: { search: async () => searchMocks },
45+
internalClient: { search: async () => searchMocks },
4446
indices: {
4547
apm_oss: {
4648
sourcemapIndices: 'myIndex',

0 commit comments

Comments
 (0)