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

moved DELETE agent_config body to query params #200281

Closed
wants to merge 4 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,9 @@ async function deleteConfig(config: Config, toasts: NotificationsStart['toasts']
await callApmApi('DELETE /api/apm/settings/agent-configuration 2023-10-31', {
signal: null,
params: {
body: {
service: {
name: config.service.name,
environment: config.service.environment,
},
query: {
Copy link
Contributor

Choose a reason for hiding this comment

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

@bryce-b this is a public API, I'm not sure if we can just change its signature. I think the APM agents team are the ones using it, have you checked with them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would it be better if I create a new API instead? If they do indeed use it, there won't be a clean way to make this change and continue supporting old agents.

Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest keeping the same API but implementing both query and body parameters. With query params overwriting body. That way, we stay backward compatible and can provide a working API for public (we might document only query parameters for simplicity). What do you think? @bryce-b @cauemarcondes

Copy link
Contributor

Choose a reason for hiding this comment

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

We have found the root cause of this problem. Lets close this PR, please.

serviceName: config.service.name,
serviceEnvironment: config.service.environment,
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const getSingleAgentConfigurationRoute = createApmServerRoute({
});

// delete configuration
const deleteAgentConfigurationRoute = createApmServerRoute({
const deleteAgentConfigurationBodyRoute = createApmServerRoute({
endpoint: 'DELETE /api/apm/settings/agent-configuration 2023-10-31',
options: {
tags: ['access:apm', 'access:apm_settings_write'],
Expand All @@ -110,8 +110,73 @@ const deleteAgentConfigurationRoute = createApmServerRoute({
handler: async (resources): Promise<{ result: string }> => {
throwNotFoundIfAgentConfigNotAvailable(resources.featureFlags);

const { params, logger, core, telemetryUsageCounter } = resources;
const { service } = params.body;

const apmIndices = await resources.getApmIndices();
const [internalESClient, apmEventClient] = await Promise.all([
createInternalESClientWithResources(resources),
getApmEventClient(resources),
]);
const exactConfig = await findExactConfiguration({
service,
internalESClient,
apmEventClient,
});
if (!exactConfig) {
logger.info(`Config was not found for ${service.name}/${service.environment}`);

throw Boom.notFound();
}

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

const deleteConfigurationResult = await deleteConfiguration({
configurationId: exactConfig.id!,
internalESClient,
});

if (resources.plugins.fleet) {
await syncAgentConfigsToApmPackagePolicies({
coreStartPromise: core.start(),
fleetPluginStart: await resources.plugins.fleet.start(),
internalESClient,
apmIndices,
telemetryUsageCounter,
});
logger.info(
`Updated Fleet integration policy for APM to remove the deleted agent configuration.`
);
}

return deleteConfigurationResult;
},
});

// delete configuration
const deleteAgentConfigurationQueryRoute = createApmServerRoute({
endpoint: 'DELETE /api/apm/settings/agent-configuration 2023-10-31',
options: {
tags: ['access:apm', 'access:apm_settings_write'],
access: 'public',
},
params: t.partial({
query: t.intersection([
t.partial({ serviceName: t.string }),
t.partial({ serviceEnvironment: t.string }),
]),
}),
handler: async (resources): Promise<{ result: string }> => {
throwNotFoundIfAgentConfigNotAvailable(resources.featureFlags);

const { params, logger, core, telemetryUsageCounter } = resources;
const { serviceName, serviceEnvironment } = params.query;
const name = serviceName ? decodeURIComponent(serviceName) : undefined;
const environment = serviceEnvironment ? decodeURIComponent(serviceEnvironment) : undefined;
const service = {
name,
environment,
};

const apmIndices = await resources.getApmIndices();
const [internalESClient, apmEventClient] = await Promise.all([
createInternalESClientWithResources(resources),
Expand Down Expand Up @@ -347,7 +412,8 @@ const agentConfigurationAgentNameRoute = createApmServerRoute({
export const agentConfigurationRouteRepository = {
...agentConfigurationRoute,
...getSingleAgentConfigurationRoute,
...deleteAgentConfigurationRoute,
...deleteAgentConfigurationBodyRoute,
...deleteAgentConfigurationQueryRoute,
...createOrUpdateAgentConfigurationRoute,
...agentConfigurationSearchRoute,
...listAgentConfigurationEnvironmentsRoute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default function featureControlsTests({ getService }: FtrProviderContext)
}

function deleteAgent(
body: APIClientRequestParamsOf<'DELETE /api/apm/settings/agent-configuration 2023-10-31'>['params']['body']
query: APIClientRequestParamsOf<'DELETE /api/apm/settings/agent-configuration 2023-10-31'>['params']['body']
) {
return apmApiClient.writeUser({
endpoint: 'DELETE /api/apm/settings/agent-configuration 2023-10-31',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,24 @@ export default function ApiTest(ftrProviderContext: FtrProviderContext) {
});
}

async function deleteConfigurationQuery(configuration: any) {
return apmApiClient.writeUser({
endpoint: 'DELETE /api/apm/settings/agent-configuration 2023-10-31',
params: {
query: {
name: configuration.service.name,
environment: configuration.service.environment,
},
},
});
}

async function deleteConfiguration(configuration: any) {
return apmApiClient.writeUser({
endpoint: 'DELETE /api/apm/settings/agent-configuration 2023-10-31',
params: { body: { service: configuration.service } },
params: {
body: { service: configuration.service },
},
});
}

Expand Down Expand Up @@ -196,6 +210,29 @@ export default function ApiTest(ftrProviderContext: FtrProviderContext) {
});
});

describe('Agent config with query DELETE', () => {
let packagePolicyWithAgentConfig: PackagePolicy;
const testConfiguration = {
service: {},
settings: { transaction_sample_rate: '0.55' },
};
before(async () => {
await createConfiguration(testConfiguration);
packagePolicyWithAgentConfig = await getPackagePolicy(bettertest, packagePolicyId);
});

after(async () => {
await deleteConfigurationQuery(testConfiguration);
});

it('sets the expected agent configs on the new package policy object', async () => {
const agentConfigs = get(packagePolicyWithAgentConfig, AGENT_CONFIG_PATH)!;
const { service, config } = agentConfigs[0];
expect(service).to.eql({});
expect(config).to.eql({ transaction_sample_rate: '0.55' });
});
});

describe('Source maps', () => {
let resp: APIReturnType<'POST /api/apm/sourcemaps 2023-10-31'>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default function agentConfigurationTests({ getService }: FtrProviderConte
return apmApiClient.readUser({
endpoint: 'GET /api/apm/settings/agent-configuration/view 2023-10-31',
params: {
query: {
body: {
name,
environment,
},
Expand Down