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

[8.x] Push the total request-body-bytes to usage-api (#194429) #198614

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion x-pack/plugins/actions/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"spaces",
"security",
"monitoringCollection",
"serverless"
"serverless",
"cloud"
],
"extraPublicDirs": [
"common"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { OAuthParams } from '../routes/get_oauth_access_token';
import { eventLogClientMock } from '@kbn/event-log-plugin/server/event_log_client.mock';
import { GetGlobalExecutionKPIParams, GetGlobalExecutionLogParams } from '../../common';
import { estypes } from '@elastic/elasticsearch';
import { DEFAULT_USAGE_API_URL } from '../config';

jest.mock('@kbn/core-saved-objects-utils-server', () => {
const actual = jest.requireActual('@kbn/core-saved-objects-utils-server');
Expand Down Expand Up @@ -613,6 +614,9 @@ describe('create()', () => {
microsoftGraphApiUrl: DEFAULT_MICROSOFT_GRAPH_API_URL,
microsoftGraphApiScope: DEFAULT_MICROSOFT_GRAPH_API_SCOPE,
microsoftExchangeUrl: DEFAULT_MICROSOFT_EXCHANGE_URL,
usage: {
url: DEFAULT_USAGE_API_URL,
},
});

const localActionTypeRegistryParams = {
Expand Down
5 changes: 4 additions & 1 deletion x-pack/plugins/actions/server/actions_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { ByteSizeValue } from '@kbn/config-schema';
import { ActionsConfig } from './config';
import { ActionsConfig, DEFAULT_USAGE_API_URL } from './config';
import {
DEFAULT_MICROSOFT_EXCHANGE_URL,
DEFAULT_MICROSOFT_GRAPH_API_SCOPE,
Expand Down Expand Up @@ -42,6 +42,9 @@ const defaultActionsConfig: ActionsConfig = {
microsoftGraphApiUrl: DEFAULT_MICROSOFT_GRAPH_API_URL,
microsoftGraphApiScope: DEFAULT_MICROSOFT_GRAPH_API_SCOPE,
microsoftExchangeUrl: DEFAULT_MICROSOFT_EXCHANGE_URL,
usage: {
url: DEFAULT_USAGE_API_URL,
},
};

describe('ensureUriAllowed', () => {
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/actions/server/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ describe('config validation', () => {
"proxyRejectUnauthorizedCertificates": true,
"rejectUnauthorized": true,
"responseTimeout": "PT1M",
"usage": Object {
"url": "https://usage-api.usage-api/api/v1/usage",
},
}
`);
});
Expand Down Expand Up @@ -85,6 +88,9 @@ describe('config validation', () => {
"proxyRejectUnauthorizedCertificates": false,
"rejectUnauthorized": false,
"responseTimeout": "PT1M",
"usage": Object {
"url": "https://usage-api.usage-api/api/v1/usage",
},
}
`);
});
Expand Down Expand Up @@ -225,6 +231,9 @@ describe('config validation', () => {
"proxyVerificationMode": "none",
"verificationMode": "none",
},
"usage": Object {
"url": "https://usage-api.usage-api/api/v1/usage",
},
}
`);
});
Expand Down
19 changes: 10 additions & 9 deletions x-pack/plugins/actions/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const connectorTypeSchema = schema.object({
maxAttempts: schema.maybe(schema.number({ min: MIN_MAX_ATTEMPTS, max: MAX_MAX_ATTEMPTS })),
});

export const DEFAULT_USAGE_API_URL = 'https://usage-api.usage-api/api/v1/usage';

// We leverage enabledActionTypes list by allowing the other plugins to overwrite it by using "setEnabledConnectorTypes" in the plugin setup.
// The list can be overwritten only if it's not already been set in the config.
const enabledConnectorTypesSchema = schema.arrayOf(
Expand Down Expand Up @@ -145,15 +147,14 @@ export const configSchema = schema.object({
max: schema.maybe(schema.number({ min: MIN_QUEUED_MAX, defaultValue: DEFAULT_QUEUED_MAX })),
})
),
usage: schema.maybe(
schema.object({
ca: schema.maybe(
schema.object({
path: schema.string(),
})
),
})
),
usage: schema.object({
url: schema.string({ defaultValue: DEFAULT_USAGE_API_URL }),
ca: schema.maybe(
schema.object({
path: schema.string(),
})
),
}),
});

export type ActionsConfig = TypeOf<typeof configSchema>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { ByteSizeValue } from '@kbn/config-schema';
import { Logger } from '@kbn/core/server';
import { loggingSystemMock } from '@kbn/core/server/mocks';
import { createReadySignal } from '@kbn/event-log-plugin/server/lib/ready_signal';
import { ActionsConfig } from '../config';
import { ActionsConfig, DEFAULT_USAGE_API_URL } from '../config';
import { ActionsConfigurationUtilities, getActionsConfigurationUtilities } from '../actions_config';
import { resolveCustomHosts } from '../lib/custom_host_settings';
import {
Expand Down Expand Up @@ -691,6 +691,9 @@ const BaseActionsConfig: ActionsConfig = {
microsoftGraphApiUrl: DEFAULT_MICROSOFT_GRAPH_API_URL,
microsoftGraphApiScope: DEFAULT_MICROSOFT_GRAPH_API_SCOPE,
microsoftExchangeUrl: DEFAULT_MICROSOFT_EXCHANGE_URL,
usage: {
url: DEFAULT_USAGE_API_URL,
},
};

function getACUfromConfig(config: Partial<ActionsConfig> = {}): ActionsConfigurationUtilities {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { ByteSizeValue } from '@kbn/config-schema';
import { Logger } from '@kbn/core/server';
import { loggingSystemMock } from '@kbn/core/server/mocks';
import { createReadySignal } from '@kbn/event-log-plugin/server/lib/ready_signal';
import { ActionsConfig } from '../config';
import { ActionsConfig, DEFAULT_USAGE_API_URL } from '../config';
import { ActionsConfigurationUtilities, getActionsConfigurationUtilities } from '../actions_config';
import { resolveCustomHosts } from '../lib/custom_host_settings';
import {
Expand Down Expand Up @@ -597,6 +597,9 @@ const BaseActionsConfig: ActionsConfig = {
microsoftGraphApiUrl: DEFAULT_MICROSOFT_GRAPH_API_URL,
microsoftGraphApiScope: DEFAULT_MICROSOFT_GRAPH_API_SCOPE,
microsoftExchangeUrl: DEFAULT_MICROSOFT_EXCHANGE_URL,
usage: {
url: DEFAULT_USAGE_API_URL,
},
};

function getACUfromConfig(config: Partial<ActionsConfig> = {}): ActionsConfigurationUtilities {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { resolve as pathResolve, join as pathJoin } from 'path';
import { ByteSizeValue } from '@kbn/config-schema';
import moment from 'moment';

import { ActionsConfig } from '../config';
import { ActionsConfig, DEFAULT_USAGE_API_URL } from '../config';
import { Logger } from '@kbn/core/server';
import { loggingSystemMock } from '@kbn/core/server/mocks';

Expand Down Expand Up @@ -82,6 +82,9 @@ describe('custom_host_settings', () => {
microsoftGraphApiUrl: DEFAULT_MICROSOFT_GRAPH_API_URL,
microsoftGraphApiScope: DEFAULT_MICROSOFT_GRAPH_API_SCOPE,
microsoftExchangeUrl: DEFAULT_MICROSOFT_EXCHANGE_URL,
usage: {
url: DEFAULT_USAGE_API_URL,
},
};

test('ensure it copies over the config parts that it does not touch', () => {
Expand Down
15 changes: 15 additions & 0 deletions x-pack/plugins/actions/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
DEFAULT_MICROSOFT_GRAPH_API_SCOPE,
DEFAULT_MICROSOFT_GRAPH_API_URL,
} from '../common';
import { cloudMock } from '@kbn/cloud-plugin/server/mocks';

const executor: ExecutorType<{}, {}, {}, void> = async (options) => {
return { status: 'ok', actionId: options.actionId };
Expand Down Expand Up @@ -59,6 +60,9 @@ function getConfig(overrides = {}) {
microsoftGraphApiUrl: DEFAULT_MICROSOFT_GRAPH_API_URL,
microsoftGraphApiScope: DEFAULT_MICROSOFT_GRAPH_API_SCOPE,
microsoftExchangeUrl: DEFAULT_MICROSOFT_EXCHANGE_URL,
usage: {
url: 'ca.path',
},
...overrides,
};
}
Expand All @@ -84,6 +88,9 @@ describe('Actions Plugin', () => {
microsoftGraphApiUrl: DEFAULT_MICROSOFT_GRAPH_API_URL,
microsoftGraphApiScope: DEFAULT_MICROSOFT_GRAPH_API_SCOPE,
microsoftExchangeUrl: DEFAULT_MICROSOFT_EXCHANGE_URL,
usage: {
url: 'ca.path',
},
});
plugin = new ActionsPlugin(context);
coreSetup = coreMock.createSetup();
Expand All @@ -95,6 +102,7 @@ describe('Actions Plugin', () => {
eventLog: eventLogMock.createSetup(),
usageCollection: usageCollectionPluginMock.createSetupContract(),
features: featuresPluginMock.createSetup(),
cloud: cloudMock.createSetup(),
};
coreSetup.getStartServices.mockResolvedValue([
coreMock.createStart(),
Expand Down Expand Up @@ -347,6 +355,7 @@ describe('Actions Plugin', () => {
eventLog: eventLogMock.createSetup(),
usageCollection: usageCollectionPluginMock.createSetupContract(),
features: featuresPluginMock.createSetup(),
cloud: cloudMock.createSetup(),
};
}

Expand Down Expand Up @@ -374,6 +383,7 @@ describe('Actions Plugin', () => {
usageCollection: usageCollectionPluginMock.createSetupContract(),
features: featuresPluginMock.createSetup(),
serverless: serverlessPluginMock.createSetupContract(),
cloud: cloudMock.createSetup(),
};
}

Expand Down Expand Up @@ -585,6 +595,9 @@ describe('Actions Plugin', () => {
microsoftGraphApiUrl: DEFAULT_MICROSOFT_GRAPH_API_URL,
microsoftGraphApiScope: DEFAULT_MICROSOFT_GRAPH_API_SCOPE,
microsoftExchangeUrl: DEFAULT_MICROSOFT_EXCHANGE_URL,
usage: {
url: 'ca.path',
},
});
plugin = new ActionsPlugin(context);
coreSetup = coreMock.createSetup();
Expand All @@ -596,6 +609,7 @@ describe('Actions Plugin', () => {
eventLog: eventLogMock.createSetup(),
usageCollection: usageCollectionPluginMock.createSetupContract(),
features: featuresPluginMock.createSetup(),
cloud: cloudMock.createSetup(),
};
pluginsStart = {
licensing: licensingMock.createStart(),
Expand Down Expand Up @@ -680,6 +694,7 @@ describe('Actions Plugin', () => {
eventLog: eventLogMock.createSetup(),
usageCollection: usageCollectionPluginMock.createSetupContract(),
features: featuresPluginMock.createSetup(),
cloud: cloudMock.createSetup(),
};
pluginsStart = {
licensing: licensingMock.createStart(),
Expand Down
15 changes: 15 additions & 0 deletions x-pack/plugins/actions/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
import { MonitoringCollectionSetup } from '@kbn/monitoring-collection-plugin/server';

import { ServerlessPluginSetup, ServerlessPluginStart } from '@kbn/serverless/server';
import type { CloudSetup } from '@kbn/cloud-plugin/server';
import { ActionsConfig, AllowedHosts, EnabledConnectorTypes, getValidatedConfig } from './config';
import { resolveCustomHosts } from './lib/custom_host_settings';
import { events } from './lib/event_based_telemetry';
Expand Down Expand Up @@ -112,6 +113,7 @@ import type { IUnsecuredActionsClient } from './unsecured_actions_client/unsecur
import { UnsecuredActionsClient } from './unsecured_actions_client/unsecured_actions_client';
import { createBulkUnsecuredExecutionEnqueuerFunction } from './create_unsecured_execute_function';
import { createSystemConnectors } from './create_system_actions';
import { ConnectorUsageReportingTask } from './usage/connector_usage_reporting_task';

export interface PluginSetupContract {
registerType<
Expand Down Expand Up @@ -184,6 +186,7 @@ export interface ActionsPluginsSetup {
spaces?: SpacesPluginSetup;
monitoringCollection?: MonitoringCollectionSetup;
serverless?: ServerlessPluginSetup;
cloud: CloudSetup;
}

export interface ActionsPluginsStart {
Expand Down Expand Up @@ -218,6 +221,7 @@ export class ActionsPlugin implements Plugin<PluginSetupContract, PluginStartCon
private readonly telemetryLogger: Logger;
private inMemoryConnectors: InMemoryConnector[];
private inMemoryMetrics: InMemoryMetrics;
private connectorUsageReportingTask: ConnectorUsageReportingTask | undefined;

constructor(initContext: PluginInitializerContext) {
this.logger = initContext.logger.get();
Expand Down Expand Up @@ -327,6 +331,15 @@ export class ActionsPlugin implements Plugin<PluginSetupContract, PluginStartCon
this.getInMemoryConnectors,
eventLogIndex
);

this.connectorUsageReportingTask = new ConnectorUsageReportingTask({
logger: this.logger,
eventLogIndex,
core,
taskManager: plugins.taskManager,
projectId: plugins.cloud.serverless.projectId,
config: this.actionsConfig.usage,
});
}

// Usage counter for telemetry
Expand Down Expand Up @@ -603,6 +616,8 @@ export class ActionsPlugin implements Plugin<PluginSetupContract, PluginStartCon

this.validateEnabledConnectorTypes(plugins);

this.connectorUsageReportingTask?.start(plugins.taskManager).catch(() => {});

return {
isActionTypeEnabled: (id, options = { notifyUsage: false }) => {
return this.actionTypeRegistry!.isActionTypeEnabled(id, options);
Expand Down
Loading