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

[Rename] kbn-apm-config-loader to osd-apm-config-loader #78

Merged
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
13 changes: 0 additions & 13 deletions packages/kbn-apm-config-loader/README.md

This file was deleted.

This file was deleted.

13 changes: 13 additions & 0 deletions packages/osd-apm-config-loader/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# @osd/apm-config-loader

Configuration loader for the APM instrumentation script.

This module is only meant to be used by the APM instrumentation script (`src/apm.js`)
to load the required configuration options from the `opensearch_dashboards.yaml` configuration file with
default values.

### Why not just use @osd-config?

`@osd/config` is the recommended way to load and read the opensearchDashboards configuration file,
however in the specific case of APM, we want to only need the minimal dependencies
before loading `opensearch-apm-node` to avoid losing instrumentation on the already loaded modules.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pid:
enabled: true
file: '/var/run/kibana.pid'
file: '/var/run/opensearch_dashboards.pid'
obj: { val: 3 }
arr: [1]
empty_obj: {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pid.enabled: true
pid.file: '/var/run/kibana.pid'
pid.file: '/var/run/opensearch_dashboards.pid'
pid.obj: { val: 3 }
pid.arr: [1, 2]
pid.empty_obj: {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
foo: 1
bar: "pre-${OSD_ENV_VAR1}-mid-${OSD_ENV_VAR2}-post"

opensearch:
requestHeadersWhitelist: ["${OSD_ENV_VAR1}", "${OSD_ENV_VAR2}"]
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "@kbn/apm-config-loader",
"name": "@osd/apm-config-loader",
"main": "./target/index.js",
"types": "./target/index.d.ts",
"version": "1.0.0",
"license": "Apache-2.0",
"private": true,
"scripts": {
"build": "tsc",
"kbn:bootstrap": "yarn build",
"kbn:watch": "yarn build --watch"
"osd:bootstrap": "yarn build",
"osd:watch": "yarn build --watch"
},
"dependencies": {
"@elastic/safer-lodash-set": "0.0.0",
"@kbn/utils": "1.0.0",
"@osd/utils": "1.0.0",
"js-yaml": "^3.14.0",
"lodash": "^4.17.20"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,21 @@ describe('ApmConfiguration', () => {
expect(config.getConfig('serviceName').globalLabels.git_rev).toBe('distribution-sha');
});

it('reads the kibana uuid from the uuid file', () => {
it('reads the opensearchDashboards uuid from the uuid file', () => {
readUuidFileMock.mockReturnValue('instance-uuid');
const config = new ApmConfiguration(mockedRootDir, {}, false);
expect(config.getConfig('serviceName').globalLabels.kibana_uuid).toBe('instance-uuid');
expect(config.getConfig('serviceName').globalLabels.opensearch_dashboards_uuid).toBe('instance-uuid');
});

it('uses the uuid from the kibana config if present', () => {
it('uses the uuid from the opensearchDashboards config if present', () => {
readUuidFileMock.mockReturnValue('uuid-from-file');
const kibanaConfig = {
const opensearchDashboardsConfig = {
server: {
uuid: 'uuid-from-config',
},
};
const config = new ApmConfiguration(mockedRootDir, kibanaConfig, false);
expect(config.getConfig('serviceName').globalLabels.kibana_uuid).toBe('uuid-from-config');
const config = new ApmConfiguration(mockedRootDir, opensearchDashboardsConfig, false);
expect(config.getConfig('serviceName').globalLabels.opensearch_dashboards_uuid).toBe('uuid-from-config');
});

it('uses the correct default config depending on the `isDistributable` parameter', () => {
Expand All @@ -99,17 +99,17 @@ describe('ApmConfiguration', () => {
expect(Object.keys(config.getConfig('serviceName'))).not.toContain('serverUrl');
});

it('loads the configuration from the kibana config file', () => {
const kibanaConfig = {
elastic: {
it('loads the configuration from the opensearchDashboards config file', () => {
const opensearchDashboardsConfig = {
opensearch: {
apm: {
active: true,
serverUrl: 'https://url',
secretToken: 'secret',
},
},
};
const config = new ApmConfiguration(mockedRootDir, kibanaConfig, true);
const config = new ApmConfiguration(mockedRootDir, opensearchDashboardsConfig, true);
expect(config.getConfig('serviceName')).toEqual(
expect.objectContaining({
active: true,
Expand All @@ -134,8 +134,8 @@ describe('ApmConfiguration', () => {
});

it('respect the precedence of the dev config', () => {
const kibanaConfig = {
elastic: {
const opensearchDashboardsConfig = {
opensearch: {
apm: {
active: true,
serverUrl: 'https://url',
Expand All @@ -147,7 +147,7 @@ describe('ApmConfiguration', () => {
active: true,
serverUrl: 'https://dev-url.co',
};
const config = new ApmConfiguration(mockedRootDir, kibanaConfig, true);
const config = new ApmConfiguration(mockedRootDir, opensearchDashboardsConfig, true);
expect(config.getConfig('serviceName')).toEqual(
expect.objectContaining({
active: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { join } from 'path';
import { merge, get } from 'lodash';
import { execSync } from 'child_process';
// deep import to avoid loading the whole package
import { getDataPath } from '@kbn/utils/target/path';
import { getDataPath } from '@osd/utils/target/path';
import { readFileSync } from 'fs';
import { ApmAgentConfig } from './types';

Expand All @@ -42,6 +42,7 @@ const getDefaultConfig = (isDistributable: boolean): ApmAgentConfig => {
return {
active: false,
serverUrl: 'https://f1542b814f674090afd914960583265f.apm.us-central1.gcp.cloud.es.io:443',
// TODO: serverUrl
// The secretToken below is intended to be hardcoded in this file even though
// it makes it public. This is not a security/privacy issue. Normally we'd
// instead disable the need for a secretToken in the APM Server config where
Expand All @@ -57,17 +58,17 @@ const getDefaultConfig = (isDistributable: boolean): ApmAgentConfig => {

export class ApmConfiguration {
private baseConfig?: any;
private kibanaVersion: string;
private opensearchDashboardsVersion: string;
private pkgBuild: Record<string, any>;

constructor(
private readonly rootDir: string,
private readonly rawKibanaConfig: Record<string, any>,
private readonly rawOpenSearchDashboardsConfig: Record<string, any>,
private readonly isDistributable: boolean
) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { version, build } = require(join(this.rootDir, 'package.json'));
this.kibanaVersion = version;
this.opensearchDashboardsVersion = version;
this.pkgBuild = build;
}

Expand All @@ -82,7 +83,7 @@ export class ApmConfiguration {
if (!this.baseConfig) {
const apmConfig = merge(
getDefaultConfig(this.isDistributable),
this.getConfigFromKibanaConfig(),
this.getConfigFromOpenSearchDashboardsConfig(),
this.getDevConfig(),
this.getDistConfig()
);
Expand All @@ -92,32 +93,32 @@ export class ApmConfiguration {
apmConfig.globalLabels.git_rev = rev;
}

const uuid = this.getKibanaUuid();
const uuid = this.getOpenSearchDashboardsUuid();
if (uuid) {
apmConfig.globalLabels.kibana_uuid = uuid;
apmConfig.globalLabels.opensearch_dashboards_uuid = uuid;
}

apmConfig.serviceVersion = this.kibanaVersion;
apmConfig.serviceVersion = this.opensearchDashboardsVersion;
this.baseConfig = apmConfig;
}

return this.baseConfig;
}

private getConfigFromKibanaConfig(): ApmAgentConfig {
return get(this.rawKibanaConfig, 'elastic.apm', {});
private getConfigFromOpenSearchDashboardsConfig(): ApmAgentConfig {
return get(this.rawOpenSearchDashboardsConfig, 'opensearch.apm', {});
}

private getKibanaUuid() {
private getOpenSearchDashboardsUuid() {
// try to access the `server.uuid` value from the config file first.
// if not manually defined, we will then read the value from the `{DATA_FOLDER}/uuid` file.
// note that as the file is created by the platform AFTER apm init, the file
// will not be present at first startup, but there is nothing we can really do about that.
if (get(this.rawKibanaConfig, 'server.uuid')) {
return this.rawKibanaConfig.server.uuid;
if (get(this.rawOpenSearchDashboardsConfig, 'server.uuid')) {
return this.rawOpenSearchDashboardsConfig.server.uuid;
}

const dataPath: string = get(this.rawKibanaConfig, 'path.data') || getDataPath();
const dataPath: string = get(this.rawOpenSearchDashboardsConfig, 'path.data') || getDataPath();
try {
const filename = join(dataPath, 'uuid');
return readFileSync(filename, 'utf-8');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { ApmConfiguration } from './config';
* Load the APM configuration.
*
* @param argv the `process.argv` arguments
* @param rootDir The root directory of kibana (where the sources and the `package.json` file are)
* @param rootDir The root directory of opensearch-dashboards (where the sources and the `package.json` file are)
* @param production true for production builds, false otherwise
*/
export const loadConfiguration = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { resolve, join } from 'path';
import { getConfigPath } from '@kbn/utils';
import { getConfigPath } from '@osd/utils';
import { getConfigurationFilePaths } from './get_config_file_paths';

describe('getConfigurationFilePaths', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

import { resolve } from 'path';
// deep import to avoid loading the whole package
import { getConfigPath } from '@kbn/utils/target/path';
import { getConfigPath } from '@osd/utils/target/path';
import { getArgValues } from './read_argv';

/**
* Return the configuration files that needs to be loaded.
*
* This mimics the behavior of the `src/cli/serve/serve.js` cli script by reading
* `-c` and `--config` options from process.argv, and fallbacks to `@kbn/utils`'s `getConfigPath()`
* `-c` and `--config` options from process.argv, and fallbacks to `@osd/utils`'s `getConfigPath()`
*/
export const getConfigurationFilePaths = (argv: string[]): string[] => {
const rawPaths = getArgValues(argv, ['-c', '--config']);
Expand Down