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] Add index.fast_refresh to .apm-custom-link #159674

Merged
merged 10 commits into from
Jul 14, 2023
1 change: 1 addition & 0 deletions config/serverless.oblt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ xpack.apm.featureFlags.infraUiAvailable: false
xpack.apm.featureFlags.migrationToFleetAvailable: false
xpack.apm.featureFlags.sourcemapApiAvailable: false
xpack.apm.featureFlags.storageExplorerAvailable: false
xpack.apm.featureFlags.fastRefreshAvailable: true

# Specify in telemetry the project type
telemetry.labels.serverless: observability
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export default function ({ getService }: PluginFunctionalProviderContext) {
'xpack.apm.latestAgentVersionsUrl (string)',
'xpack.apm.featureFlags.agentConfigurationAvailable (any)',
'xpack.apm.featureFlags.configurableIndicesAvailable (any)',
'xpack.apm.featureFlags.fastRefreshAvailable (any)',
'xpack.apm.featureFlags.infrastructureTabAvailable (any)',
'xpack.apm.featureFlags.infraUiAvailable (any)',
'xpack.apm.featureFlags.migrationToFleetAvailable (any)',
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/apm/common/apm_feature_flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export enum ApmFeatureFlagName {
MigrationToFleetAvailable = 'migrationToFleetAvailable',
SourcemapApiAvailable = 'sourcemapApiAvailable',
StorageExplorerAvailable = 'storageExplorerAvailable',
FastRefreshAvailable = 'fastRefreshAvailable',
}

const apmFeatureFlagMap = {
Expand Down Expand Up @@ -47,6 +48,10 @@ const apmFeatureFlagMap = {
default: true,
type: t.boolean,
},
[ApmFeatureFlagName.FastRefreshAvailable]: {
default: false,
type: t.boolean,
},
};

type ApmFeatureFlagMap = typeof apmFeatureFlagMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const mockConfig: ConfigSchema = {
migrationToFleetAvailable: true,
sourcemapApiAvailable: true,
storageExplorerAvailable: true,
fastRefreshAvailable: false,
},
serverless: { enabled: false },
};
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/apm/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface ConfigSchema {
migrationToFleetAvailable: boolean;
sourcemapApiAvailable: boolean;
storageExplorerAvailable: boolean;
fastRefreshAvailable: boolean;
};
serverless: {
enabled: boolean;
Expand Down
66 changes: 0 additions & 66 deletions x-pack/plugins/apm/scripts/shared/create_or_update_index.ts

This file was deleted.

10 changes: 10 additions & 0 deletions x-pack/plugins/apm/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ const disabledOnServerless = schema.conditional(
schema.oneOf([schema.literal(true)], { defaultValue: true })
);

const enabledOnServerless = schema.conditional(
schema.contextRef('serverless'),
true,
schema.boolean({
defaultValue: true,
}),
schema.oneOf([schema.literal(false)], { defaultValue: false })
);

// All options should be documented in the APM configuration settings: https://github.com/elastic/kibana/blob/main/docs/settings/apm-settings.asciidoc
// and be included on cloud allow list unless there are specific reasons not to
const configSchema = schema.object({
Expand Down Expand Up @@ -88,6 +97,7 @@ const configSchema = schema.object({
migrationToFleetAvailable: disabledOnServerless,
sourcemapApiAvailable: disabledOnServerless,
storageExplorerAvailable: disabledOnServerless,
fastRefreshAvailable: enabledOnServerless,
}),
serverless: schema.object({
enabled: schema.conditional(
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/apm/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ export class APMPlugin

const logger = this.logger;
const client = core.elasticsearch.client.asInternalUser;
const { featureFlags } = this.currentConfig;

// create .apm-agent-configuration index without blocking start lifecycle
createApmAgentConfigurationIndex({ client, logger }).catch((e) => {
Expand All @@ -285,7 +286,7 @@ export class APMPlugin
});

// create .apm-custom-link index without blocking start lifecycle
createApmCustomLinkIndex({ client, logger }).catch((e) => {
createApmCustomLinkIndex({ client, logger, featureFlags }).catch((e) => {
logger.error('Failed to create .apm-custom-link index');
logger.error(e);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,29 @@ import {
Mappings,
} from '@kbn/observability-plugin/server';
import { APM_CUSTOM_LINK_INDEX } from '../apm_indices/get_apm_indices';
import { ApmFeatureFlags } from '../../../../common/apm_feature_flags';

export const createApmCustomLinkIndex = async ({
client,
logger,
featureFlags,
}: {
client: ElasticsearchClient;
logger: Logger;
featureFlags: ApmFeatureFlags;
}) => {
return createOrUpdateIndex({
index: APM_CUSTOM_LINK_INDEX,
client,
logger,
mappings,
settings: featureFlags.fastRefreshAvailable
? {
index: {
fast_refresh: true,
},
}
: {},
sorenlouv marked this conversation as resolved.
Show resolved Hide resolved
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ export function createAnnotationsClient(params: {
const initIndex = () =>
createOrUpdateIndex({
index,
mappings,
client: esClient,
logger,
mappings,
});

function ensureGoldLicense<T extends (...args: any[]) => any>(fn: T): T {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import pRetry from 'p-retry';
import { Logger, ElasticsearchClient } from '@kbn/core/server';
import { merge } from 'lodash';

export type Mappings = Required<estypes.IndicesCreateRequest>['body']['mappings'] &
Required<estypes.IndicesPutMappingRequest>['body'];

type IndexSettings = Required<estypes.IndicesPutSettingsRequest>['body']['settings'];

export async function createOrUpdateIndex({
index,
mappings,
settings,
client,
logger,
}: {
index: string;
mappings: Mappings;
settings?: IndexSettings;
client: ElasticsearchClient;
logger: Logger;
}) {
Expand All @@ -44,6 +49,7 @@ export async function createOrUpdateIndex({
index,
client,
mappings,
settings,
});

if (!result.acknowledged) {
Expand All @@ -64,26 +70,28 @@ export async function createOrUpdateIndex({
}
}

function createNewIndex({
async function createNewIndex({
index,
client,
mappings,
settings,
}: {
index: string;
client: ElasticsearchClient;
mappings: Required<estypes.IndicesCreateRequest>['body']['mappings'];
settings: Required<estypes.IndicesPutSettingsRequest>['body']['settings'];
}) {
return client.indices.create({
return await client.indices.create({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to await here instead of just passing the promise?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if that was the intention here, but awaiting a promise inside of a function can be useful for debugging, as it ensures the function shows up in the stacktrace. Here's some explanation: goldbergyoni/nodebestpractices#737

index,
body: {
// auto_expand_replicas: Allows cluster to not have replicas for this index
settings: { index: { auto_expand_replicas: '0-1' } },
settings: merge({ index: { auto_expand_replicas: '0-1' } }, settings),
mappings,
},
});
}

function updateExistingIndex({
async function updateExistingIndex({
index,
client,
mappings,
Expand All @@ -92,7 +100,7 @@ function updateExistingIndex({
client: ElasticsearchClient;
mappings: estypes.IndicesPutMappingRequest['body'];
}) {
return client.indices.putMapping({
return await client.indices.putMapping({
index,
body: mappings,
});
Expand Down