Skip to content

Commit

Permalink
[Profiling] Configurable ES client
Browse files Browse the repository at this point in the history
  • Loading branch information
dgieselaar committed Oct 5, 2022
1 parent dba72d5 commit 3469b1b
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 75 deletions.
9 changes: 8 additions & 1 deletion x-pack/plugins/profiling/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = {
Expand Down
27 changes: 24 additions & 3 deletions x-pack/plugins/profiling/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,6 +16,7 @@ import {
ProfilingPluginStartDeps,
ProfilingRequestHandlerContext,
} from './types';
import { createProfilingEsClient } from './utils/create_profiling_es_client';

export class ProfilingPlugin
implements
Expand All @@ -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();
}

Expand All @@ -38,14 +40,33 @@ 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!,
dependencies: {
start: depsStart,
setup: deps,
},
services: {
createProfilingEsClient: ({ request, esClient: defaultEsClient }) => {
const esClient = profilingSpecificEsClient
? profilingSpecificEsClient.asScoped(request).asInternalUser
: defaultEsClient;

return createProfilingEsClient({ request, esClient });
},
},
});
});

Expand Down
7 changes: 5 additions & 2 deletions x-pack/plugins/profiling/server/routes/flamechart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import { RouteRegisterParameters } from '.';
import { getRoutePaths } from '../../common';
import { createCalleeTree } from '../../common/callee';
import { createFlameGraph } 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(
{
Expand Down
8 changes: 6 additions & 2 deletions x-pack/plugins/profiling/server/routes/frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
StackFrame,
StackFrameMetadata,
} from '../../common/profiling';
import { createProfilingEsClient, ProfilingESClient } from '../utils/create_profiling_es_client';
import { ProfilingESClient } from '../utils/create_profiling_es_client';
import { mgetStackFrames, mgetExecutables } from './stacktrace';

async function getFrameInformation({
Expand Down Expand Up @@ -57,7 +57,11 @@ async function getFrameInformation({
}

export function registerFrameInformationRoute(params: RouteRegisterParameters) {
const { logger, router } = params;
const {
logger,
router,
services: { createProfilingEsClient },
} = params;

const routePaths = getRoutePaths();

Expand Down
7 changes: 5 additions & 2 deletions x-pack/plugins/profiling/server/routes/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { schema, TypeOf } from '@kbn/config-schema';
import { RouteRegisterParameters } from '.';
import { getRoutePaths } from '../../common';
import { createTopNFunctions } from '../../common/functions';
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';
Expand All @@ -25,7 +24,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(
{
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/profiling/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { registerFlameChartSearchRoute } from './flamechart';
import { registerFrameInformationRoute } from './frames';
Expand All @@ -31,6 +34,12 @@ export interface RouteRegisterParameters {
start: ProfilingPluginStartDeps;
setup: ProfilingPluginSetupDeps;
};
services: {
createProfilingEsClient: (params: {
request: KibanaRequest;
esClient: ElasticsearchClient;
}) => ProfilingESClient;
};
}

export function registerRoutes(params: RouteRegisterParameters) {
Expand Down
114 changes: 49 additions & 65 deletions x-pack/plugins/profiling/server/routes/topn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
*/

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';
import { computeBucketWidthFromTimeRangeAndBucketCount } from '../../common/histogram';
import { groupStackFrameMetadataByStackTrace, StackTraceID } from '../../common/profiling';
import { getFieldNameForTopNType, TopNType } from '../../common/stack_traces';
import { createTopNSamples, getTopNAggregationRequest, TopNResponse } from '../../common/topn';
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';
Expand Down Expand Up @@ -155,13 +154,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,
Expand Down Expand Up @@ -203,72 +207,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,
});
}

0 comments on commit 3469b1b

Please sign in to comment.