Skip to content

Commit

Permalink
removing settings from clients
Browse files Browse the repository at this point in the history
  • Loading branch information
cauemarcondes committed Dec 1, 2023
1 parent 466943b commit 47091dc
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 213 deletions.
53 changes: 2 additions & 51 deletions x-pack/plugins/apm/server/routes/profiling/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@
import { toNumberRt } from '@kbn/io-ts-utils';
import type { BaseFlameGraph, TopNFunctions } from '@kbn/profiling-utils';
import * as t from 'io-ts';
import {
profilingAWSCostDiscountRate,
profilingCo2PerKWH,
profilingCostPervCPUPerHour,
profilingDatacenterPUE,
profilingPervCPUWattArm64,
profilingPervCPUWattX86,
} from '@kbn/observability-plugin/common';
import { HOST_NAME } from '../../../common/es_fields/apm';
import {
mergeKueries,
Expand Down Expand Up @@ -57,21 +49,6 @@ const profilingFlamegraphRoute = createApmServerRoute({
await plugins.profilingDataAccess?.start(),
]);
if (profilingDataAccessStart) {
const [
co2PerKWH,
datacenterPUE,
pervCPUWattX86,
pervCPUWattArm64,
awsCostDiscountRate,
costPervCPUPerHour,
] = await Promise.all([
core.uiSettings.client.get<number>(profilingCo2PerKWH),
core.uiSettings.client.get<number>(profilingDatacenterPUE),
core.uiSettings.client.get<number>(profilingPervCPUWattX86),
core.uiSettings.client.get<number>(profilingPervCPUWattArm64),
core.uiSettings.client.get<number>(profilingAWSCostDiscountRate),
core.uiSettings.client.get<number>(profilingCostPervCPUPerHour),
]);
const { start, end, environment, documentType, rollupInterval, kuery } =
params.query;
const { serviceName } = params.path;
Expand All @@ -92,19 +69,14 @@ const profilingFlamegraphRoute = createApmServerRoute({

const flamegraph =
await profilingDataAccessStart?.services.fetchFlamechartData({
core,
esClient: esClient.asCurrentUser,
rangeFromMs: start,
rangeToMs: end,
kuery: mergeKueries([
`(${toKueryFilterFormat(HOST_NAME, serviceHostNames)})`,
kuery,
]),
co2PerKWH,
datacenterPUE,
pervCPUWattX86,
pervCPUWattArm64,
awsCostDiscountRate,
costPervCPUPerHour,
});

return { flamegraph, hostNames: serviceHostNames };
Expand Down Expand Up @@ -151,22 +123,6 @@ const profilingFunctionsRoute = createApmServerRoute({
} = params.query;
const { serviceName } = params.path;

const [
co2PerKWH,
datacenterPUE,
pervCPUWattX86,
pervCPUWattArm64,
awsCostDiscountRate,
costPervCPUPerHour,
] = await Promise.all([
core.uiSettings.client.get<number>(profilingCo2PerKWH),
core.uiSettings.client.get<number>(profilingDatacenterPUE),
core.uiSettings.client.get<number>(profilingPervCPUWattX86),
core.uiSettings.client.get<number>(profilingPervCPUWattArm64),
core.uiSettings.client.get<number>(profilingAWSCostDiscountRate),
core.uiSettings.client.get<number>(profilingCostPervCPUPerHour),
]);

const serviceHostNames = await getServiceHostNames({
apmEventClient,
start,
Expand All @@ -182,6 +138,7 @@ const profilingFunctionsRoute = createApmServerRoute({
}

const functions = await profilingDataAccessStart?.services.fetchFunction({
core,
esClient: esClient.asCurrentUser,
rangeFromMs: start,
rangeToMs: end,
Expand All @@ -191,12 +148,6 @@ const profilingFunctionsRoute = createApmServerRoute({
]),
startIndex,
endIndex,
co2PerKWH,
datacenterPUE,
pervCPUWattX86,
pervCPUWattArm64,
awsCostDiscountRate,
costPervCPUPerHour,
});
return { functions, hostNames: serviceHostNames };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@
*/

import type { CoreRequestHandlerContext } from '@kbn/core-http-request-handler-context-server';
import {
profilingAWSCostDiscountRate,
profilingCo2PerKWH,
profilingCostPervCPUPerHour,
profilingDatacenterPUE,
profilingPervCPUWattArm64,
profilingPervCPUWattX86,
} from '@kbn/observability-plugin/common';
import type { ProfilingDataAccessPluginStart } from '@kbn/profiling-data-access-plugin/server';
import type { BaseFlameGraph } from '@kbn/profiling-utils';
import { HOST_FIELD } from '../../../../common/constants';
Expand All @@ -24,32 +16,11 @@ export async function fetchProfilingFlamegraph(
profilingDataAccess: ProfilingDataAccessPluginStart,
coreRequestContext: CoreRequestHandlerContext
): Promise<BaseFlameGraph> {
const [
co2PerKWH,
datacenterPUE,
pervCPUWattX86,
pervCPUWattArm64,
awsCostDiscountRate,
costPervCPUPerHour,
] = await Promise.all([
coreRequestContext.uiSettings.client.get<number>(profilingCo2PerKWH),
coreRequestContext.uiSettings.client.get<number>(profilingDatacenterPUE),
coreRequestContext.uiSettings.client.get<number>(profilingPervCPUWattX86),
coreRequestContext.uiSettings.client.get<number>(profilingPervCPUWattArm64),
coreRequestContext.uiSettings.client.get<number>(profilingAWSCostDiscountRate),
coreRequestContext.uiSettings.client.get<number>(profilingCostPervCPUPerHour),
]);

return await profilingDataAccess.services.fetchFlamechartData({
core: coreRequestContext,
esClient: coreRequestContext.elasticsearch.client.asCurrentUser,
rangeFromMs: from,
rangeToMs: to,
kuery: `${HOST_FIELD} : "${hostname}"`,
co2PerKWH,
datacenterPUE,
pervCPUWattX86,
pervCPUWattArm64,
awsCostDiscountRate,
costPervCPUPerHour,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@
import type { CoreRequestHandlerContext } from '@kbn/core-http-request-handler-context-server';
import type { ProfilingDataAccessPluginStart } from '@kbn/profiling-data-access-plugin/server';
import type { TopNFunctions } from '@kbn/profiling-utils';
import {
profilingAWSCostDiscountRate,
profilingCo2PerKWH,
profilingCostPervCPUPerHour,
profilingDatacenterPUE,
profilingPervCPUWattArm64,
profilingPervCPUWattX86,
} from '@kbn/observability-plugin/common';
import { HOST_FIELD } from '../../../../common/constants';
import type { InfraProfilingFunctionsRequestParams } from '../../../../common/http_api/profiling_api';

Expand All @@ -25,34 +17,14 @@ export async function fetchProfilingFunctions(
coreRequestContext: CoreRequestHandlerContext
): Promise<TopNFunctions> {
const { hostname, from, to, startIndex, endIndex } = params;
const [
co2PerKWH,
datacenterPUE,
pervCPUWattX86,
pervCPUWattArm64,
awsCostDiscountRate,
costPervCPUPerHour,
] = await Promise.all([
coreRequestContext.uiSettings.client.get<number>(profilingCo2PerKWH),
coreRequestContext.uiSettings.client.get<number>(profilingDatacenterPUE),
coreRequestContext.uiSettings.client.get<number>(profilingPervCPUWattX86),
coreRequestContext.uiSettings.client.get<number>(profilingPervCPUWattArm64),
coreRequestContext.uiSettings.client.get<number>(profilingAWSCostDiscountRate),
coreRequestContext.uiSettings.client.get<number>(profilingCostPervCPUPerHour),
]);

return await profilingDataAccess.services.fetchFunction({
core: coreRequestContext,
esClient: coreRequestContext.elasticsearch.client.asCurrentUser,
rangeFromMs: from,
rangeToMs: to,
kuery: `${HOST_FIELD} : "${hostname}"`,
startIndex,
endIndex,
co2PerKWH,
datacenterPUE,
pervCPUWattX86,
pervCPUWattArm64,
awsCostDiscountRate,
costPervCPUPerHour,
});
}
30 changes: 1 addition & 29 deletions x-pack/plugins/profiling/server/routes/flamechart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@
*/

import { schema } from '@kbn/config-schema';
import {
profilingAWSCostDiscountRate,
profilingCo2PerKWH,
profilingCostPervCPUPerHour,
profilingDatacenterPUE,
profilingPervCPUWattArm64,
profilingPervCPUWattX86,
} from '@kbn/observability-plugin/common';
import { RouteRegisterParameters } from '.';
import { getRoutePaths } from '../../common';
import { handleRouteHandlerError } from '../utils/handle_route_error_handler';
Expand Down Expand Up @@ -43,35 +35,15 @@ export function registerFlameChartSearchRoute({
const { timeFrom, timeTo, kuery } = request.query;

const core = await context.core;
const [
co2PerKWH,
datacenterPUE,
pervCPUWattX86,
pervCPUWattArm64,
awsCostDiscountRate,
costPervCPUPerHour,
] = await Promise.all([
core.uiSettings.client.get<number>(profilingCo2PerKWH),
core.uiSettings.client.get<number>(profilingDatacenterPUE),
core.uiSettings.client.get<number>(profilingPervCPUWattX86),
core.uiSettings.client.get<number>(profilingPervCPUWattArm64),
core.uiSettings.client.get<number>(profilingAWSCostDiscountRate),
core.uiSettings.client.get<number>(profilingCostPervCPUPerHour),
]);

try {
const esClient = await getClient(context);
const flamegraph = await profilingDataAccess.services.fetchFlamechartData({
core,
esClient,
rangeFromMs: timeFrom,
rangeToMs: timeTo,
kuery,
co2PerKWH,
datacenterPUE,
pervCPUWattX86,
pervCPUWattArm64,
awsCostDiscountRate,
costPervCPUPerHour,
});

return response.ok({ body: flamegraph });
Expand Down
30 changes: 1 addition & 29 deletions x-pack/plugins/profiling/server/routes/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@
*/

import { schema, TypeOf } from '@kbn/config-schema';
import {
profilingAWSCostDiscountRate,
profilingCo2PerKWH,
profilingCostPervCPUPerHour,
profilingDatacenterPUE,
profilingPervCPUWattArm64,
profilingPervCPUWattX86,
} from '@kbn/observability-plugin/common';
import { RouteRegisterParameters } from '.';
import { getRoutePaths } from '../../common';
import { handleRouteHandlerError } from '../utils/handle_route_error_handler';
Expand Down Expand Up @@ -46,37 +38,17 @@ export function registerTopNFunctionsSearchRoute({
async (context, request, response) => {
try {
const core = await context.core;
const [
co2PerKWH,
datacenterPUE,
pervCPUWattX86,
pervCPUWattArm64,
awsCostDiscountRate,
costPervCPUPerHour,
] = await Promise.all([
core.uiSettings.client.get<number>(profilingCo2PerKWH),
core.uiSettings.client.get<number>(profilingDatacenterPUE),
core.uiSettings.client.get<number>(profilingPervCPUWattX86),
core.uiSettings.client.get<number>(profilingPervCPUWattArm64),
core.uiSettings.client.get<number>(profilingAWSCostDiscountRate),
core.uiSettings.client.get<number>(profilingCostPervCPUPerHour),
]);

const { timeFrom, timeTo, startIndex, endIndex, kuery }: QuerySchemaType = request.query;
const esClient = await getClient(context);
const topNFunctions = await profilingDataAccess.services.fetchFunction({
core,
esClient,
rangeFromMs: timeFrom,
rangeToMs: timeTo,
kuery,
startIndex,
endIndex,
co2PerKWH,
datacenterPUE,
pervCPUWattX86,
pervCPUWattArm64,
awsCostDiscountRate,
costPervCPUPerHour,
});

return response.ok({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,50 @@
* 2.0.
*/

import { ElasticsearchClient } from '@kbn/core/server';
import { CoreRequestHandlerContext, ElasticsearchClient } from '@kbn/core/server';
import {
profilingAWSCostDiscountRate,
profilingCo2PerKWH,
profilingCostPervCPUPerHour,
profilingDatacenterPUE,
profilingPervCPUWattArm64,
profilingPervCPUWattX86,
} from '@kbn/observability-plugin/common';
import { percentToFactor } from '../../utils/percent_to_factor';
import { kqlQuery } from '../../utils/query';
import { RegisterServicesParams } from '../register_services';

export interface FetchFlamechartParams {
esClient: ElasticsearchClient;
core: CoreRequestHandlerContext;
rangeFromMs: number;
rangeToMs: number;
kuery: string;
co2PerKWH: number;
datacenterPUE: number;
pervCPUWattX86: number;
pervCPUWattArm64: number;
awsCostDiscountRate: number;
costPervCPUPerHour: number;
}

const targetSampleSize = 20000; // minimum number of samples to get statistically sound results

export function createFetchFlamechart({ createProfilingEsClient }: RegisterServicesParams) {
return async ({
esClient,
rangeFromMs,
rangeToMs,
kuery,
co2PerKWH,
datacenterPUE,
pervCPUWattX86,
pervCPUWattArm64,
awsCostDiscountRate,
costPervCPUPerHour,
}: FetchFlamechartParams) => {
return async ({ core, esClient, rangeFromMs, rangeToMs, kuery }: FetchFlamechartParams) => {
const rangeFromSecs = rangeFromMs / 1000;
const rangeToSecs = rangeToMs / 1000;

const [
co2PerKWH,
datacenterPUE,
pervCPUWattX86,
pervCPUWattArm64,
awsCostDiscountRate,
costPervCPUPerHour,
] = await Promise.all([
core.uiSettings.client.get<number>(profilingCo2PerKWH),
core.uiSettings.client.get<number>(profilingDatacenterPUE),
core.uiSettings.client.get<number>(profilingPervCPUWattX86),
core.uiSettings.client.get<number>(profilingPervCPUWattArm64),
core.uiSettings.client.get<number>(profilingAWSCostDiscountRate),
core.uiSettings.client.get<number>(profilingCostPervCPUPerHour),
]);

const profilingEsClient = createProfilingEsClient({ esClient });
const totalSeconds = rangeToSecs - rangeFromSecs;
const flamegraph = await profilingEsClient.profilingFlamegraph({
Expand Down
Loading

0 comments on commit 47091dc

Please sign in to comment.