[Profiling] Configurable ES client (#144533)

This commit is contained in:
Dario Gieselaar 2022-12-07 16:36:13 +01:00 committed by GitHub
parent f8fea0fb93
commit 313537c178
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 100 additions and 73 deletions

View file

@ -11,9 +11,16 @@ import { ProfilingPlugin } from './plugin';
const configSchema = schema.object({
enabled: schema.boolean({ defaultValue: false }),
elasticsearch: schema.maybe(
schema.object({
hosts: schema.string(),
username: schema.string(),
password: schema.string(),
})
),
});
type ProfilingConfig = TypeOf<typeof configSchema>;
export type ProfilingConfig = TypeOf<typeof configSchema>;
// plugin config
export const config: PluginConfigDescriptor<ProfilingConfig> = {

View file

@ -6,7 +6,7 @@
*/
import { CoreSetup, CoreStart, Logger, Plugin, PluginInitializerContext } from '@kbn/core/server';
import { ProfilingConfig } from '.';
import { PROFILING_FEATURE } from './feature';
import { registerRoutes } from './routes';
import {
@ -16,6 +16,7 @@ import {
ProfilingPluginStartDeps,
ProfilingRequestHandlerContext,
} from './types';
import { createProfilingEsClient } from './utils/create_profiling_es_client';
export class ProfilingPlugin
implements
@ -28,7 +29,8 @@ export class ProfilingPlugin
{
private readonly logger: Logger;
constructor(initializerContext: PluginInitializerContext) {
constructor(private readonly initializerContext: PluginInitializerContext<ProfilingConfig>) {
this.initializerContext = initializerContext;
this.logger = initializerContext.logger.get();
}
@ -38,7 +40,17 @@ export class ProfilingPlugin
deps.features.registerKibanaFeature(PROFILING_FEATURE);
core.getStartServices().then(([_, depsStart]) => {
const config = this.initializerContext.config.get();
core.getStartServices().then(([coreStart, depsStart]) => {
const profilingSpecificEsClient = config.elasticsearch
? coreStart.elasticsearch.createClient('profiling', {
hosts: [config.elasticsearch.hosts],
username: config.elasticsearch.username,
password: config.elasticsearch.password,
})
: undefined;
registerRoutes({
router,
logger: this.logger!,
@ -46,6 +58,15 @@ export class ProfilingPlugin
start: depsStart,
setup: deps,
},
services: {
createProfilingEsClient: ({ request, esClient: defaultEsClient }) => {
const esClient = profilingSpecificEsClient
? profilingSpecificEsClient.asScoped(request).asInternalUser
: defaultEsClient;
return createProfilingEsClient({ request, esClient });
},
},
});
});

View file

@ -11,13 +11,16 @@ import { getRoutePaths } from '../../common';
import { createCalleeTree } from '../../common/callee';
import { handleRouteHandlerError } from '../utils/handle_route_error_handler';
import { createBaseFlameGraph } from '../../common/flamegraph';
import { createProfilingEsClient } from '../utils/create_profiling_es_client';
import { withProfilingSpan } from '../utils/with_profiling_span';
import { getClient } from './compat';
import { getExecutablesAndStackTraces } from './get_executables_and_stacktraces';
import { createCommonFilter } from './query';
export function registerFlameChartSearchRoute({ router, logger }: RouteRegisterParameters) {
export function registerFlameChartSearchRoute({
router,
logger,
services: { createProfilingEsClient },
}: RouteRegisterParameters) {
const paths = getRoutePaths();
router.get(
{

View file

@ -10,7 +10,6 @@ import { RouteRegisterParameters } from '.';
import { getRoutePaths } from '../../common';
import { createTopNFunctions } from '../../common/functions';
import { handleRouteHandlerError } from '../utils/handle_route_error_handler';
import { createProfilingEsClient } from '../utils/create_profiling_es_client';
import { withProfilingSpan } from '../utils/with_profiling_span';
import { getClient } from './compat';
import { getExecutablesAndStackTraces } from './get_executables_and_stacktraces';
@ -26,7 +25,11 @@ const querySchema = schema.object({
type QuerySchemaType = TypeOf<typeof querySchema>;
export function registerTopNFunctionsSearchRoute({ router, logger }: RouteRegisterParameters) {
export function registerTopNFunctionsSearchRoute({
router,
logger,
services: { createProfilingEsClient },
}: RouteRegisterParameters) {
const paths = getRoutePaths();
router.get(
{

View file

@ -6,11 +6,14 @@
*/
import type { IRouter, Logger } from '@kbn/core/server';
import type { KibanaRequest } from '@kbn/core-http-server';
import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server';
import {
ProfilingPluginSetupDeps,
ProfilingPluginStartDeps,
ProfilingRequestHandlerContext,
} from '../types';
import { ProfilingESClient } from '../utils/create_profiling_es_client';
import { registerCacheExecutablesRoute, registerCacheStackFramesRoute } from './cache';
@ -32,6 +35,12 @@ export interface RouteRegisterParameters {
start: ProfilingPluginStartDeps;
setup: ProfilingPluginSetupDeps;
};
services: {
createProfilingEsClient: (params: {
request: KibanaRequest;
esClient: ElasticsearchClient;
}) => ProfilingESClient;
};
}
export function registerRoutes(params: RouteRegisterParameters) {

View file

@ -6,7 +6,7 @@
*/
import { schema } from '@kbn/config-schema';
import type { IRouter, Logger } from '@kbn/core/server';
import type { Logger } from '@kbn/core/server';
import { RouteRegisterParameters } from '.';
import { getRoutePaths, INDEX_EVENTS } from '../../common';
import { ProfilingESField } from '../../common/elasticsearch';
@ -15,8 +15,7 @@ import { groupStackFrameMetadataByStackTrace, StackTraceID } from '../../common/
import { getFieldNameForTopNType, TopNType } from '../../common/stack_traces';
import { createTopNSamples, getTopNAggregationRequest, TopNResponse } from '../../common/topn';
import { handleRouteHandlerError } from '../utils/handle_route_error_handler';
import { ProfilingRequestHandlerContext } from '../types';
import { createProfilingEsClient, ProfilingESClient } from '../utils/create_profiling_es_client';
import { ProfilingESClient } from '../utils/create_profiling_es_client';
import { withProfilingSpan } from '../utils/with_profiling_span';
import { getClient } from './compat';
import { findDownsampledIndex } from './downsampling';
@ -156,13 +155,18 @@ export async function topNElasticSearchQuery({
};
}
export function queryTopNCommon(
router: IRouter<ProfilingRequestHandlerContext>,
logger: Logger,
pathName: string,
searchField: string,
highCardinality: boolean
) {
export function queryTopNCommon({
logger,
router,
services: { createProfilingEsClient },
pathName,
searchField,
highCardinality,
}: RouteRegisterParameters & {
pathName: string;
searchField: string;
highCardinality: boolean;
}) {
router.get(
{
path: pathName,
@ -197,72 +201,52 @@ export function queryTopNCommon(
);
}
export function registerTraceEventsTopNContainersSearchRoute({
router,
logger,
}: RouteRegisterParameters) {
export function registerTraceEventsTopNContainersSearchRoute(parameters: RouteRegisterParameters) {
const paths = getRoutePaths();
return queryTopNCommon(
router,
logger,
paths.TopNContainers,
getFieldNameForTopNType(TopNType.Containers),
false
);
return queryTopNCommon({
...parameters,
pathName: paths.TopNContainers,
searchField: getFieldNameForTopNType(TopNType.Containers),
highCardinality: false,
});
}
export function registerTraceEventsTopNDeploymentsSearchRoute({
router,
logger,
}: RouteRegisterParameters) {
export function registerTraceEventsTopNDeploymentsSearchRoute(parameters: RouteRegisterParameters) {
const paths = getRoutePaths();
return queryTopNCommon(
router,
logger,
paths.TopNDeployments,
getFieldNameForTopNType(TopNType.Deployments),
false
);
return queryTopNCommon({
...parameters,
pathName: paths.TopNDeployments,
searchField: getFieldNameForTopNType(TopNType.Deployments),
highCardinality: false,
});
}
export function registerTraceEventsTopNHostsSearchRoute({
router,
logger,
}: RouteRegisterParameters) {
export function registerTraceEventsTopNHostsSearchRoute(parameters: RouteRegisterParameters) {
const paths = getRoutePaths();
return queryTopNCommon(
router,
logger,
paths.TopNHosts,
getFieldNameForTopNType(TopNType.Hosts),
false
);
return queryTopNCommon({
...parameters,
pathName: paths.TopNHosts,
searchField: getFieldNameForTopNType(TopNType.Hosts),
highCardinality: false,
});
}
export function registerTraceEventsTopNStackTracesSearchRoute({
router,
logger,
}: RouteRegisterParameters) {
export function registerTraceEventsTopNStackTracesSearchRoute(parameters: RouteRegisterParameters) {
const paths = getRoutePaths();
return queryTopNCommon(
router,
logger,
paths.TopNTraces,
getFieldNameForTopNType(TopNType.Traces),
false
);
return queryTopNCommon({
...parameters,
pathName: paths.TopNTraces,
searchField: getFieldNameForTopNType(TopNType.Traces),
highCardinality: false,
});
}
export function registerTraceEventsTopNThreadsSearchRoute({
router,
logger,
}: RouteRegisterParameters) {
export function registerTraceEventsTopNThreadsSearchRoute(parameters: RouteRegisterParameters) {
const paths = getRoutePaths();
return queryTopNCommon(
router,
logger,
paths.TopNThreads,
getFieldNameForTopNType(TopNType.Threads),
true
);
return queryTopNCommon({
...parameters,
pathName: paths.TopNThreads,
searchField: getFieldNameForTopNType(TopNType.Threads),
highCardinality: true,
});
}