Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[APM] Expose APM event client as part of plugin contract #82724

Merged
merged 2 commits into from
Nov 9, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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';
@@ -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,
@@ -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,
});
},
};
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
@@ -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 },
47 changes: 42 additions & 5 deletions x-pack/plugins/apm/server/plugin.ts
Original file line number Diff line number Diff line change
@@ -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';
@@ -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';
@@ -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> {
@@ -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(),
sorenlouv marked this conversation as resolved.
Show resolved Hide resolved
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,
},
});
},
};
}