Skip to content

Commit

Permalink
Use hashed service as id
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed Feb 18, 2020
1 parent b35256d commit 9e943ea
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 232 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import * as t from 'io-ts';
import { transactionSampleRateRt } from '../transaction_sample_rate_rt';
import { transactionMaxSpansRt } from '../transaction_max_spans_rt';

export const serviceRt = t.intersection([
t.partial({ name: t.string }),
t.partial({ environment: t.string })
]);
export const serviceRt = t.partial({
name: t.string,
environment: t.string
});

export const agentConfigurationIntakeRt = t.intersection([
t.partial({ agent_name: t.string }),
t.type({ service: serviceRt }),
t.type({
settings: t.intersection([
t.partial({ transaction_sample_rate: transactionSampleRateRt }),
t.partial({ capture_body: t.string }),
t.partial({ transaction_max_spans: transactionMaxSpansRt })
])
service: serviceRt,
settings: t.partial({
transaction_sample_rate: transactionSampleRateRt,
capture_body: t.string,
transaction_max_spans: transactionMaxSpansRt
})
})
]);

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,27 @@ import {
import { APMIndexDocumentParams } from '../../helpers/es_client';

export async function createOrUpdateConfiguration({
configurationId,
configurationIntake,
setup
}: {
configurationId?: string;
configurationIntake: AgentConfigurationIntake;
setup: Setup;
}) {
const { internalClient, indices } = setup;

const params: APMIndexDocumentParams<AgentConfiguration> = {
refresh: true,
index: indices.apmAgentConfigurationIndex,
id: hash(configurationIntake.service),
refresh: 'wait_for',
body: {
agent_name: configurationIntake.agent_name,
service: {
name: configurationIntake.service.name,
environment: configurationIntake.service.environment
},
service: configurationIntake.service,
settings: configurationIntake.settings,
'@timestamp': Date.now(),
applied_by_agent: false,
etag: hash(configurationIntake)
}
};

// by specifying an id elasticsearch will delete the previous doc and insert the updated doc
if (configurationId) {
params.id = configurationId;
}

return internalClient.index(params);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
* you may not use this file except in compliance with the Elastic License.
*/

import hash from 'object-hash';
import { Setup } from '../../helpers/setup_request';
import { AgentConfiguration } from './configuration_types';

export async function deleteConfiguration({
configurationId,
service,
setup
}: {
configurationId: string;
service: AgentConfiguration['service'];
setup: Setup;
}) {
const { internalClient, indices } = setup;

const params = {
refresh: 'wait_for',
index: indices.apmAgentConfigurationIndex,
id: configurationId
id: hash(service),
refresh: 'wait_for'
};

return internalClient.delete(params);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@
* you may not use this file except in compliance with the Elastic License.
*/

import hash from 'object-hash';
import { Setup } from '../../helpers/setup_request';
import { AgentConfiguration } from './configuration_types';

export async function markAppliedByAgent({
id,
body,
setup
}: {
id: string;
body: AgentConfiguration;
setup: Setup;
}) {
const { internalClient, indices } = setup;

const params = {
index: indices.apmAgentConfigurationIndex,
id, // by specifying the `id` elasticsearch will do an "upsert"
id: hash(body.service),
body: {
...body,
applied_by_agent: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
SearchParamsMock,
inspectSearchParams
} from '../../../../public/utils/testHelpers';
import { findExactConfiguration } from './find_exact_configuration';

describe('agent configuration queries', () => {
let mock: SearchParamsMock;
Expand Down Expand Up @@ -100,39 +99,4 @@ describe('agent configuration queries', () => {
expect(mock.params).toMatchSnapshot();
});
});

describe('findExactConfiguration', () => {
it('find configuration by service.name', async () => {
mock = await inspectSearchParams(setup =>
findExactConfiguration({
service: { name: 'foo' },
setup
})
);

expect(mock.params).toMatchSnapshot();
});

it('find configuration by service.environment', async () => {
mock = await inspectSearchParams(setup =>
findExactConfiguration({
service: { environment: 'bar' },
setup
})
);

expect(mock.params).toMatchSnapshot();
});

it('find configuration by service.name and service.environment', async () => {
mock = await inspectSearchParams(setup =>
findExactConfiguration({
service: { name: 'foo', environment: 'bar' },
setup
})
);

expect(mock.params).toMatchSnapshot();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { setupRequest } from '../../lib/helpers/setup_request';
import { getServiceNames } from '../../lib/settings/agent_configuration/get_service_names';
import { createOrUpdateConfiguration } from '../../lib/settings/agent_configuration/create_or_update_configuration';
import { searchConfigurations } from '../../lib/settings/agent_configuration/search_configurations';
import { findExactConfiguration } from '../../lib/settings/agent_configuration/find_exact_configuration';
import { listConfigurations } from '../../lib/settings/agent_configuration/list_configurations';
import { getEnvironments } from '../../lib/settings/agent_configuration/get_environments';
import { deleteConfiguration } from '../../lib/settings/agent_configuration/delete_configuration';
Expand All @@ -21,7 +20,6 @@ import {
serviceRt,
agentConfigurationIntakeRt
} from '../../../common/runtime_types/agent_configuration_intake_rt';
import { jsonRt } from '../../../common/runtime_types/json_rt';

// get list of configurations
export const agentConfigurationRoute = createRoute(core => ({
Expand All @@ -48,21 +46,8 @@ export const deleteAgentConfigurationRoute = createRoute(() => ({
const setup = await setupRequest(context, request);
const { service } = context.params.body;

const config = await findExactConfiguration({ service, setup });
if (!config) {
context.logger.info(
`Config was not found for ${service.name}/${service.environment}`
);

throw Boom.notFound();
}

context.logger.info(
`Deleting config ${service.name}/${service.environment} (${config._id})`
);

return await deleteConfiguration({
configurationId: config._id,
service,
setup
});
}
Expand Down Expand Up @@ -114,35 +99,17 @@ export const createOrUpdateAgentConfigurationRoute = createRoute(() => ({
tags: ['access:apm', 'access:apm_write']
},
params: {
query: t.partial({ overwrite: jsonRt.pipe(t.boolean) }),
body: agentConfigurationIntakeRt
},
handler: async ({ context, request }) => {
const setup = await setupRequest(context, request);
const { body, query } = context.params;

// if the config already exists, it is fetched and updated
// this is to avoid creating two configs with identical service params
const config = await findExactConfiguration({
service: body.service,
setup
});

// if the config exists ?overwrite=true is required
if (config && !query.overwrite) {
throw Boom.badRequest(
`A configuration already exists for "${body.service.name}/${body.service.environment}. Use ?overwrite=true to overwrite the existing configuration.`
);
}
const { body } = context.params;

context.logger.info(
`${config ? 'Updating' : 'Creating'} config ${body.service.name}/${
body.service.environment
}`
`Upsert config ${body.service.name}/${body.service.environment}`
);

return await createOrUpdateConfiguration({
configurationId: config?._id,
configurationIntake: body,
setup
});
Expand Down Expand Up @@ -181,7 +148,7 @@ export const agentConfigurationSearchRoute = createRoute(core => ({

// update `applied_by_agent` field if etags match
if (etag === config._source.etag && !config._source.applied_by_agent) {
markAppliedByAgent({ id: config._id, body: config._source, setup });
markAppliedByAgent({ body: config._source, setup });
}

return config;
Expand Down

0 comments on commit 9e943ea

Please sign in to comment.