Skip to content

Commit

Permalink
[UII] Allow to reset log level for agents >= 8.15.0 (#183434)
Browse files Browse the repository at this point in the history
‼️ Should be reverted if
elastic/elastic-agent#4747 does not make
8.15.0.

## Summary

Resolves #180778 

This PR allows agent log level to be reset back to the level set on its
policy (or if not set, simply the default agent level, see
elastic/elastic-agent#3090).

To achieve this, this PR:
- Allows `null` to be passed for the log level settings action, i.e.:

```
POST kbn:/api/fleet/agents/<AGENT_ID>/actions
{"action":{"type":"SETTINGS","data":{"log_level": null}}}
```
- Enables the agent policy log level setting implemented in
#180607
- Always show `Apply changes` on the agent details > Logs tab
- For agents >= 8.15.0, always show `Reset to policy` on the agent
details > Logs tab
- Ensures both buttons are disabled if user does not have access to
write to agents

<img width="1254" alt="image"
src="https://github.com/elastic/kibana/assets/1965714/bcdf763e-2053-4071-9aa8-8bcb57b8fee1">

<img width="1267" alt="image"
src="https://github.com/elastic/kibana/assets/1965714/182ac54d-d5ad-435f-9376-70bb24f288f9">

### Caveats
1. The reported agent log level is not accurate if agent is using the
level from its policy and does not have a log level set on its own level
(elastic/elastic-agent#4747), so the initial
selection on the agent log level could be wrong
2. We have no way to tell where the log level came from
(elastic/elastic-agent#4748), so that's why
`Apply changes` and `Reset to policy` are always shown

### Testing
Use the latest `8.15.0-SNAPSHOT` for agents or fleet server to test this
change

### Checklist

Delete any items that are not applicable to this PR.

- [x] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
jen-huang authored May 16, 2024
1 parent b211842 commit 4c80f26
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 120 deletions.
17 changes: 5 additions & 12 deletions x-pack/plugins/fleet/common/constants/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,10 @@ export const DEFAULT_MAX_AGENT_POLICIES_WITH_INACTIVITY_TIMEOUT = 750;
export const AGENTLESS_POLICY_ID = 'agentless'; // the policy id defined here: https://github.com/elastic/project-controller/blob/main/internal/project/security/security_kibana_config.go#L86

export const AGENT_LOG_LEVELS = {
ERROR: 'error',
WARNING: 'warning',
INFO: 'info',
DEBUG: 'debug',
info: 'info',
debug: 'debug',
warning: 'warning',
error: 'error',
};

export const DEFAULT_LOG_LEVEL = AGENT_LOG_LEVELS.INFO;

export const agentLoggingLevels = {
Info: 'info',
Debug: 'debug',
Warning: 'warning',
Error: 'error',
} as const;
export const DEFAULT_LOG_LEVEL = AGENT_LOG_LEVELS.info;
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/common/openapi/bundled.json
Original file line number Diff line number Diff line change
Expand Up @@ -7294,6 +7294,7 @@
"properties": {
"log_level": {
"type": "string",
"nullable": true,
"enum": [
"debug",
"info",
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/common/openapi/bundled.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4674,6 +4674,7 @@ components:
properties:
log_level:
type: string
nullable: true
enum:
- debug
- info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ oneOf:
properties:
log_level:
type: string
nullable: true
enum:
- debug
- info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import { EuiCode } from '@elastic/eui';

import { z } from 'zod';

import { agentLoggingLevels } from '../constants';
import { AGENT_LOG_LEVELS, DEFAULT_LOG_LEVEL } from '../constants';

import type { SettingsConfig } from './types';

Expand Down Expand Up @@ -130,20 +133,19 @@ export const AGENT_POLICY_ADVANCED_SETTINGS: SettingsConfig[] = [
},
{
name: 'agent.logging.level',
hidden: true,
title: i18n.translate('xpack.fleet.settings.agentPolicyAdvanced.agentLoggingLevelTitle', {
defaultMessage: 'Agent logging level',
}),
description: i18n.translate(
'xpack.fleet.settings.agentPolicyAdvanced.agentLoggingLevelDescription',
{
defaultMessage:
'Sets the log level for all the agents on the policy. The default log level is "info".',
}
description: (
<FormattedMessage
id="xpack.fleet.settings.agentPolicyAdvanced.agentLoggingLevelDescription"
defaultMessage="Sets the log level for all the agents on the policy. The default log level is {level}."
values={{ level: <EuiCode>{DEFAULT_LOG_LEVEL}</EuiCode> }}
/>
),
api_field: {
name: 'agent_logging_level',
},
schema: z.nativeEnum(agentLoggingLevels).default(agentLoggingLevels.Info),
schema: z.nativeEnum(AGENT_LOG_LEVELS).default(DEFAULT_LOG_LEVEL),
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,15 @@ describe('AgentLogsUI', () => {
},
} as any);
});
const renderComponent = () => {
const renderComponent = (
opts = {
agentVersion: '8.11.0',
}
) => {
const renderer = createFleetTestRendererMock();
const agent = {
id: 'agent1',
local_metadata: { elastic: { agent: { version: '8.11' } } },
local_metadata: { elastic: { agent: { version: opts.agentVersion, log_level: 'debug' } } },
} as any;
const state = {
datasets: ['elastic_agent'],
Expand Down Expand Up @@ -125,4 +129,35 @@ describe('AgentLogsUI', () => {
`http://localhost:5620/app/discover#/?_g=(filters:!(),refreshInterval:(pause:!t,value:60000),time:(from:'2023-20-04T14:00:00.340Z',to:'2023-20-04T14:20:00.340Z'))&_a=(columns:!(event.dataset,message),index:'logs-*',query:(language:kuery,query:'elastic_agent.id:agent1 and (data_stream.dataset:elastic_agent) and (log.level:info or log.level:error)'))`
);
});

it('should show log level dropdown with correct value', () => {
mockStartServices();
const result = renderComponent();
const logLevelDropdown = result.getByTestId('selectAgentLogLevel');
expect(logLevelDropdown.getElementsByTagName('option').length).toBe(4);
expect(logLevelDropdown).toHaveDisplayValue('debug');
});

it('should always show apply log level changes button', () => {
mockStartServices();
const result = renderComponent();
const applyLogLevelBtn = result.getByTestId('applyLogLevelBtn');
expect(applyLogLevelBtn).toBeInTheDocument();
expect(applyLogLevelBtn).not.toHaveAttribute('disabled');
});

it('should hide reset log level button for agents version < 8.15.0', () => {
mockStartServices();
const result = renderComponent();
const resetLogLevelBtn = result.queryByTestId('resetLogLevelBtn');
expect(resetLogLevelBtn).not.toBeInTheDocument();
});

it('should show reset log level button for agents version >= 8.15.0', () => {
mockStartServices();
const result = renderComponent({ agentVersion: '8.15.0' });
const resetLogLevelBtn = result.getByTestId('resetLogLevelBtn');
expect(resetLogLevelBtn).toBeInTheDocument();
expect(resetLogLevelBtn).not.toHaveAttribute('disabled');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,10 @@ export const AgentLogsUI: React.FunctionComponent<AgentLogsProps> = memo(
</EuiPanel>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<SelectLogLevel agent={agent} />
<SelectLogLevel
agent={agent}
agentPolicyLogLevel={agentPolicy?.advanced_settings?.agent_logging_level}
/>
</EuiFlexItem>
</WrapperFlexGroup>
);
Expand Down
Loading

0 comments on commit 4c80f26

Please sign in to comment.