Skip to content

Commit

Permalink
[PoC] Cloud reads deployment metadata from config
Browse files Browse the repository at this point in the history
  • Loading branch information
afharo committed Sep 13, 2022
1 parent 886d61a commit 762996d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
*/

import type { AnalyticsClient } from '@kbn/analytics-client';
import { of } from 'rxjs';
import { map, type Observable, of } from 'rxjs';

export function registerCloudDeploymentIdAnalyticsContext(
analytics: Pick<AnalyticsClient, 'registerContextProvider'>,
metadata$: Observable<Record<string, unknown>>,
cloudId?: string
) {
if (!cloudId) {
Expand All @@ -25,4 +26,15 @@ export function registerCloudDeploymentIdAnalyticsContext(
},
},
});

analytics.registerContextProvider({
name: 'Cloud Deployment Metadata',
context$: metadata$.pipe(map((cloudMetadata) => ({ cloudMetadata }))),
schema: {
cloudMetadata: {
type: 'pass_through',
_meta: { description: 'The Cloud metadata' },
},
},
});
}
4 changes: 3 additions & 1 deletion x-pack/plugins/cloud/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export interface CloudConfigType {
/** The URL to the remotely-hosted chat application. */
chatURL: string;
};
/** The Cloud Metadata provided via config */
metadata: Record<string, unknown>;
}

interface CloudSetupDependencies {
Expand Down Expand Up @@ -257,7 +259,7 @@ export class CloudPlugin implements Plugin<CloudSetup> {
security?: Pick<SecurityPluginSetup, 'authc'>,
cloudId?: string
) {
registerCloudDeploymentIdAnalyticsContext(analytics, cloudId);
registerCloudDeploymentIdAnalyticsContext(analytics, of(this.config.metadata), cloudId);

if (security) {
analytics.registerContextProvider({
Expand Down
18 changes: 14 additions & 4 deletions x-pack/plugins/cloud/server/collectors/cloud_usage_collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,37 @@
* 2.0.
*/

import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server';
import { firstValueFrom, type Observable } from 'rxjs';
import { type UsageCollectionSetup } from '@kbn/usage-collection-plugin/server';

interface Config {
isCloudEnabled: boolean;
metadata$: Observable<Record<string, unknown>>;
}

interface CloudUsage {
isCloudEnabled: boolean;
metadata: Record<string, string>; // Using string instead of unknown because `telemetry-tools` will fail the validation otherwise
}

export function createCloudUsageCollector(usageCollection: UsageCollectionSetup, config: Config) {
const { isCloudEnabled } = config;
const { isCloudEnabled, metadata$ } = config;
return usageCollection.makeUsageCollector<CloudUsage>({
type: 'cloud',
isReady: () => true,
schema: {
isCloudEnabled: { type: 'boolean' },
isCloudEnabled: {
type: 'boolean',
_meta: { description: '`true` when the deployment is running on ESS' },
},
metadata: {
DYNAMIC_KEY: { type: 'keyword', _meta: { description: 'Cloud Deployment Metadata' } },
},
},
fetch: () => {
fetch: async () => {
return {
isCloudEnabled,
metadata: (await firstValueFrom(metadata$)) as Record<string, string>,
};
},
});
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/cloud/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const configSchema = schema.object({
id: schema.maybe(schema.string()),
organization_url: schema.maybe(schema.string()),
profile_url: schema.maybe(schema.string()),
metadata: schema.recordOf(schema.string(), schema.any(), { defaultValue: {} }),
});

export type CloudConfigType = TypeOf<typeof configSchema>;
Expand All @@ -61,6 +62,7 @@ export const config: PluginConfigDescriptor<CloudConfigType> = {
id: true,
organization_url: true,
profile_url: true,
metadata: true,
},
schema: configSchema,
};
9 changes: 7 additions & 2 deletions x-pack/plugins/cloud/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { map, type Observable } from 'rxjs';
import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server';
import { CoreSetup, Logger, Plugin, PluginInitializerContext } from '@kbn/core/server';
import type { SecurityPluginSetup } from '@kbn/security-plugin/server';
Expand Down Expand Up @@ -36,19 +37,23 @@ export interface CloudSetup {
export class CloudPlugin implements Plugin<CloudSetup> {
private readonly logger: Logger;
private readonly config: CloudConfigType;
private readonly config$: Observable<CloudConfigType>;
private readonly isDev: boolean;

constructor(private readonly context: PluginInitializerContext) {
this.logger = this.context.logger.get();
this.config = this.context.config.get<CloudConfigType>();
this.config$ = this.context.config.create<CloudConfigType>();
this.isDev = this.context.env.mode.dev;
}

public setup(core: CoreSetup, { usageCollection, security }: PluginsSetup): CloudSetup {
this.logger.debug('Setting up Cloud plugin');
const isCloudEnabled = getIsCloudEnabled(this.config.id);
registerCloudDeploymentIdAnalyticsContext(core.analytics, this.config.id);
registerCloudUsageCollector(usageCollection, { isCloudEnabled });
// We want to react to hot reloading updates to make sure we always use the latest metadata
const metadata$ = this.config$.pipe(map(({ metadata }) => metadata));
registerCloudDeploymentIdAnalyticsContext(core.analytics, metadata$, this.config.id);
registerCloudUsageCollector(usageCollection, { isCloudEnabled, metadata$ });

if (isCloudEnabled) {
security?.setIsElasticCloudDeployment();
Expand Down

0 comments on commit 762996d

Please sign in to comment.