Skip to content

Commit

Permalink
Updating APM labels from kibana.yml (#144036)
Browse files Browse the repository at this point in the history
* register apm config
listen to config file changes and update apm labels

* lint

* code review

* fixes
  • Loading branch information
Liza Katz authored Oct 27, 2022
1 parent 7fe7327 commit 0f95ae4
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/kbn-apm-config-loader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export { getConfiguration } from './src/config_loader';
export { initApm } from './src/init_apm';
export { shouldInstrumentClient } from './src/rum_agent_configuration';
export type { ApmConfiguration } from './src/config';
export { apmConfigSchema } from './src/apm_config';
21 changes: 21 additions & 0 deletions packages/kbn-apm-config-loader/src/apm_config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { schema } from '@kbn/config-schema';

export const apmConfigSchema = schema.object({
apm: schema.object(
{
active: schema.maybe(schema.boolean()),
serverUrl: schema.maybe(schema.uri()),
secretToken: schema.maybe(schema.string()),
globalLabels: schema.object({}, { unknowns: 'allow' }),
},
{ unknowns: 'allow' }
),
});
22 changes: 22 additions & 0 deletions src/core/server/root/elastic_config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { schema, TypeOf } from '@kbn/config-schema';
import { apmConfigSchema } from '@kbn/apm-config-loader';
import type { ServiceConfigDescriptor } from '@kbn/core-base-server-internal';

const elasticConfig = schema.object({
apm: apmConfigSchema,
});

export type ElasticConfigType = TypeOf<typeof elasticConfig>;

export const elasticApmConfig: ServiceConfigDescriptor<ElasticConfigType> = {
path: 'elastic',
schema: elasticConfig,
};
36 changes: 35 additions & 1 deletion src/core/server/root/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@
*/

import { ConnectableObservable, Subscription } from 'rxjs';
import { first, publishReplay, switchMap, concatMap, tap } from 'rxjs/operators';
import {
first,
publishReplay,
switchMap,
concatMap,
tap,
distinctUntilChanged,
} from 'rxjs/operators';
import type { Logger, LoggerFactory } from '@kbn/logging';
import { Env, RawConfigurationProvider } from '@kbn/config';
import { LoggingConfigType, LoggingSystem } from '@kbn/core-logging-server-internal';
import apm from 'elastic-apm-node';
import { isEqual } from 'lodash';
import type { ElasticConfigType } from './elastic_config';
import { Server } from '../server';

/**
Expand All @@ -22,6 +32,7 @@ export class Root {
private readonly loggingSystem: LoggingSystem;
private readonly server: Server;
private loggingConfigSubscription?: Subscription;
private apmConfigSubscription?: Subscription;

constructor(
rawConfigProvider: RawConfigurationProvider,
Expand All @@ -37,7 +48,9 @@ export class Root {
public async preboot() {
try {
this.server.setupCoreConfig();
this.setupApmLabelSync();
await this.setupLogging();

this.log.debug('prebooting root');
return await this.server.preboot();
} catch (e) {
Expand Down Expand Up @@ -85,13 +98,34 @@ export class Root {
this.loggingConfigSubscription.unsubscribe();
this.loggingConfigSubscription = undefined;
}
if (this.apmConfigSubscription !== undefined) {
this.apmConfigSubscription.unsubscribe();
this.apmConfigSubscription = undefined;
}
await this.loggingSystem.stop();

if (this.onShutdown !== undefined) {
this.onShutdown(reason);
}
}

private setupApmLabelSync() {
const { configService } = this.server;

// Update APM labels on config change
this.apmConfigSubscription = configService
.getConfig$()
.pipe(
switchMap(() => configService.atPath<ElasticConfigType>('elastic')),
distinctUntilChanged(isEqual),
tap((elasticConfig) => {
const labels = elasticConfig.apm?.globalLabels || {};
apm.addLabels(labels);
})
)
.subscribe();
}

private async setupLogging() {
const { configService } = this.server;
// Stream that maps config updates to logger updates, including update failures.
Expand Down
2 changes: 2 additions & 0 deletions src/core/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import {
config as pluginsConfig,
} from '@kbn/core-plugins-server-internal';
import { CoreApp } from './core_app';
import { elasticApmConfig } from './root/elastic_config';

const coreId = Symbol('core');
const rootConfigPath = '';
Expand Down Expand Up @@ -457,6 +458,7 @@ export class Server {
cspConfig,
deprecationConfig,
elasticsearchConfig,
elasticApmConfig,
executionContextConfig,
externalUrlConfig,
httpConfig,
Expand Down

0 comments on commit 0f95ae4

Please sign in to comment.