Skip to content

Commit

Permalink
[APM] Expose APM event client as part of plugin contract (#82724)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
dgieselaar and kibanamachine authored Nov 9, 2020
1 parent 6110ef8 commit 858befe
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
import { ValuesType } from 'utility-types';
import { APMBaseDoc } from '../../../../../typings/es_schemas/raw/apm_base_doc';
import { APMError } from '../../../../../typings/es_schemas/ui/apm_error';
import { KibanaRequest } from '../../../../../../../../src/core/server';
import {
KibanaRequest,
LegacyScopedClusterClient,
} from '../../../../../../../../src/core/server';
import { ProcessorEvent } from '../../../../../common/processor_event';
import {
ESSearchRequest,
ESSearchResponse,
} from '../../../../../typings/elasticsearch';
import { ApmIndicesConfig } from '../../../settings/apm_indices/get_apm_indices';
import { APMRequestHandlerContext } from '../../../../routes/typings';
import { addFilterToExcludeLegacyData } from './add_filter_to_exclude_legacy_data';
import { callClientWithDebug } from '../call_client_with_debug';
import { Transaction } from '../../../../../typings/es_schemas/ui/transaction';
Expand Down Expand Up @@ -51,20 +53,23 @@ type TypedSearchResponse<
export type APMEventClient = ReturnType<typeof createApmEventClient>;

export function createApmEventClient({
context,
esClient,
debug,
request,
indices,
options: { includeFrozen } = { includeFrozen: false },
}: {
context: APMRequestHandlerContext;
esClient: Pick<
LegacyScopedClusterClient,
'callAsInternalUser' | 'callAsCurrentUser'
>;
debug: boolean;
request: KibanaRequest;
indices: ApmIndicesConfig;
options: {
includeFrozen: boolean;
};
}) {
const client = context.core.elasticsearch.legacy.client;

return {
search<TParams extends APMEventESSearchRequest>(
params: TParams,
Expand All @@ -77,14 +82,14 @@ export function createApmEventClient({
: withProcessorEventFilter;

return callClientWithDebug({
apiCaller: client.callAsCurrentUser,
apiCaller: esClient.callAsCurrentUser,
operationName: 'search',
params: {
...withPossibleLegacyDataFilter,
ignore_throttled: !includeFrozen,
},
request,
debug: context.params.query._debug,
debug,
});
},
};
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/apm/server/lib/helpers/setup_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export async function setupRequest<TParams extends SetupRequestParams>(
const coreSetupRequest = {
indices,
apmEventClient: createApmEventClient({
context,
esClient: context.core.elasticsearch.legacy.client,
debug: context.params.query._debug,
request,
indices,
options: { includeFrozen },
Expand Down
47 changes: 42 additions & 5 deletions x-pack/plugins/apm/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import { map, take } from 'rxjs/operators';
import {
CoreSetup,
CoreStart,
KibanaRequest,
Logger,
Plugin,
PluginInitializerContext,
RequestHandlerContext,
} from 'src/core/server';
import { APMConfig, APMXPackConfig, mergeConfigs } from '.';
import { APMOSSPluginSetup } from '../../../../src/plugins/apm_oss/server';
import { HomeServerPluginSetup } from '../../../../src/plugins/home/server';
import { UsageCollectionSetup } from '../../../../src/plugins/usage_collection/server';
import { UI_SETTINGS } from '../../../../src/plugins/data/common';
import { ActionsPlugin } from '../../actions/server';
import { AlertingPlugin } from '../../alerts/server';
import { CloudSetup } from '../../cloud/server';
Expand All @@ -30,6 +33,7 @@ import { TaskManagerSetupContract } from '../../task_manager/server';
import { APM_FEATURE, registerFeaturesUsage } from './feature';
import { registerApmAlerts } from './lib/alerts/register_apm_alerts';
import { createApmTelemetry } from './lib/apm_telemetry';
import { createApmEventClient } from './lib/helpers/create_es_client/create_apm_event_client';
import { getInternalSavedObjectsClient } from './lib/helpers/get_internal_saved_objects_client';
import { createApmAgentConfigurationIndex } from './lib/settings/agent_configuration/create_agent_config_index';
import { getApmIndices } from './lib/settings/apm_indices/get_apm_indices';
Expand All @@ -42,6 +46,11 @@ import { uiSettings } from './ui_settings';
export interface APMPluginSetup {
config$: Observable<APMConfig>;
getApmIndices: () => ReturnType<typeof getApmIndices>;
createApmEventClient: (params: {
debug?: boolean;
request: KibanaRequest;
context: RequestHandlerContext;
}) => Promise<ReturnType<typeof createApmEventClient>>;
}

export class APMPlugin implements Plugin<APMPluginSetup> {
Expand Down Expand Up @@ -141,13 +150,41 @@ export class APMPlugin implements Plugin<APMPluginSetup> {
},
});

const boundGetApmIndices = async () =>
getApmIndices({
savedObjectsClient: await getInternalSavedObjectsClient(core),
config: await mergedConfig$.pipe(take(1)).toPromise(),
});

return {
config$: mergedConfig$,
getApmIndices: async () =>
getApmIndices({
savedObjectsClient: await getInternalSavedObjectsClient(core),
config: await mergedConfig$.pipe(take(1)).toPromise(),
}),
getApmIndices: boundGetApmIndices,
createApmEventClient: async ({
request,
context,
debug,
}: {
debug?: boolean;
request: KibanaRequest;
context: RequestHandlerContext;
}) => {
const [indices, includeFrozen] = await Promise.all([
boundGetApmIndices(),
context.core.uiSettings.client.get(UI_SETTINGS.SEARCH_INCLUDE_FROZEN),
]);

const esClient = context.core.elasticsearch.legacy.client;

return createApmEventClient({
debug: debug ?? false,
esClient,
request,
indices,
options: {
includeFrozen,
},
});
},
};
}

Expand Down

0 comments on commit 858befe

Please sign in to comment.