Skip to content

Commit 77f0fda

Browse files
afgomezAlejandro Fernández Gómez
authored andcommitted
Tweak log item API
The values will be returned as arrays of strings to match the response of the log entries API. The UI is now responsible to decide how render the fields.
1 parent bb893c2 commit 77f0fda

File tree

6 files changed

+64
-79
lines changed

6 files changed

+64
-79
lines changed

x-pack/plugins/infra/common/http_api/log_entries/item.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const logEntriesItemRequestRT = rt.type({
1616

1717
export type LogEntriesItemRequest = rt.TypeOf<typeof logEntriesItemRequestRT>;
1818

19-
const logEntriesItemFieldRT = rt.type({ field: rt.string, value: rt.string });
19+
const logEntriesItemFieldRT = rt.type({ field: rt.string, value: rt.array(rt.string) });
2020
const logEntriesItemRT = rt.type({
2121
id: rt.string,
2222
index: rt.string,

x-pack/plugins/infra/public/components/logging/log_entry_flyout/log_entry_flyout.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export const LogEntryFlyout = ({
9494
onClick={createFilterHandler(item)}
9595
/>
9696
</EuiToolTip>
97-
{item.value}
97+
{formatValue(item.value)}
9898
</span>
9999
),
100100
},
@@ -147,3 +147,7 @@ export const InfraFlyoutLoadingPanel = euiStyled.div`
147147
bottom: 0;
148148
left: 0;
149149
`;
150+
151+
function formatValue(value: string[]) {
152+
return value.length > 1 ? value.join(', ') : value[0];
153+
}

x-pack/plugins/infra/server/lib/domains/log_entries_domain/convert_document_source_to_log_item_fields.test.ts

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,62 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { convertDocumentSourceToLogItemFields } from './convert_document_source_to_log_item_fields';
7+
import { convertESFieldsToLogItemFields } from './convert_document_source_to_log_item_fields';
88

9-
describe('convertDocumentSourceToLogItemFields', () => {
10-
test('should convert document', () => {
11-
const doc = {
12-
agent: {
13-
hostname: 'demo-stack-client-01',
14-
id: '7adef8b6-2ab7-45cd-a0d5-b3baad735f1b',
15-
type: 'filebeat',
16-
ephemeral_id: 'a0c8164b-3564-4e32-b0bf-f4db5a7ae566',
17-
version: '7.0.0',
18-
},
9+
describe('convertESFieldsToLogItemFields', () => {
10+
test('Converts the fields collection to LogItemFields', () => {
11+
const esFields = {
12+
'agent.hostname': ['demo-stack-client-01'],
13+
'agent.id': ['7adef8b6-2ab7-45cd-a0d5-b3baad735f1b'],
14+
'agent.type': ['filebeat'],
15+
'agent.ephemeral_id': ['a0c8164b-3564-4e32-b0bf-f4db5a7ae566'],
16+
'agent.version': ['7.0.0'],
1917
tags: ['prod', 'web'],
2018
metadata: [
2119
{ key: 'env', value: 'prod' },
2220
{ key: 'stack', value: 'web' },
2321
],
24-
host: {
25-
hostname: 'packer-virtualbox-iso-1546820004',
26-
name: 'demo-stack-client-01',
27-
},
22+
'host.hostname': ['packer-virtualbox-iso-1546820004'],
23+
'host.name': ['demo-stack-client-01'],
2824
};
2925

30-
const fields = convertDocumentSourceToLogItemFields(doc);
26+
const fields = convertESFieldsToLogItemFields(esFields);
3127
expect(fields).toEqual([
3228
{
3329
field: 'agent.hostname',
34-
value: 'demo-stack-client-01',
30+
value: ['demo-stack-client-01'],
3531
},
3632
{
3733
field: 'agent.id',
38-
value: '7adef8b6-2ab7-45cd-a0d5-b3baad735f1b',
34+
value: ['7adef8b6-2ab7-45cd-a0d5-b3baad735f1b'],
3935
},
4036
{
4137
field: 'agent.type',
42-
value: 'filebeat',
38+
value: ['filebeat'],
4339
},
4440
{
4541
field: 'agent.ephemeral_id',
46-
value: 'a0c8164b-3564-4e32-b0bf-f4db5a7ae566',
42+
value: ['a0c8164b-3564-4e32-b0bf-f4db5a7ae566'],
4743
},
4844
{
4945
field: 'agent.version',
50-
value: '7.0.0',
46+
value: ['7.0.0'],
5147
},
5248
{
5349
field: 'tags',
54-
value: '["prod","web"]',
50+
value: ['prod', 'web'],
5551
},
5652
{
5753
field: 'metadata',
58-
value: '[{"key":"env","value":"prod"},{"key":"stack","value":"web"}]',
54+
value: ['{"key":"env","value":"prod"}', '{"key":"stack","value":"web"}'],
5955
},
6056
{
6157
field: 'host.hostname',
62-
value: 'packer-virtualbox-iso-1546820004',
58+
value: ['packer-virtualbox-iso-1546820004'],
6359
},
6460
{
6561
field: 'host.name',
66-
value: 'demo-stack-client-01',
62+
value: ['demo-stack-client-01'],
6763
},
6864
]);
6965
});

x-pack/plugins/infra/server/lib/domains/log_entries_domain/convert_document_source_to_log_item_fields.ts

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,20 @@
66

77
import stringify from 'json-stable-stringify';
88
import { LogEntriesItemField } from '../../../../common/http_api';
9-
import { JsonArray, JsonObject, jsonObjectRT, JsonValue } from '../../../../common/typed_json';
9+
import { JsonArray } from '../../../../common/typed_json';
1010

11-
const serializeValue = (value: JsonValue): string => {
12-
if (typeof value === 'object' && value != null) {
13-
return stringify(value);
14-
} else {
15-
return `${value}`;
16-
}
11+
const serializeValue = (value: JsonArray): string[] => {
12+
return value.map((v) => {
13+
if (typeof v === 'object' && v != null) {
14+
return stringify(v);
15+
} else {
16+
return `${v}`;
17+
}
18+
});
1719
};
1820

19-
// TODO: move rendering to browser
2021
export const convertESFieldsToLogItemFields = (fields: {
2122
[field: string]: JsonArray;
2223
}): LogEntriesItemField[] => {
2324
return Object.keys(fields).map((field) => ({ field, value: serializeValue(fields[field]) }));
2425
};
25-
26-
export const convertDocumentSourceToLogItemFields = (
27-
source: JsonObject,
28-
path: string[] = [],
29-
fields: LogEntriesItemField[] = []
30-
): LogEntriesItemField[] => {
31-
return Object.keys(source).reduce((acc, key) => {
32-
const value = source[key];
33-
const nextPath = [...path, key];
34-
if (jsonObjectRT.is(value)) {
35-
return convertDocumentSourceToLogItemFields(value, nextPath, acc);
36-
}
37-
const field = { field: nextPath.join('.'), value: serializeValue(value) };
38-
return [...acc, field];
39-
}, fields);
40-
};

x-pack/plugins/infra/server/lib/domains/log_entries_domain/log_entries_domain.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ export class InfraLogEntriesDomain {
252252
): Promise<LogEntriesItem> {
253253
const document = await this.adapter.getLogItem(requestContext, id, sourceConfiguration);
254254
const defaultFields = [
255-
{ field: '_index', value: document._index },
256-
{ field: '_id', value: document._id },
255+
{ field: '_index', value: [document._index] },
256+
{ field: '_id', value: [document._id] },
257257
];
258258

259259
return {

x-pack/test/api_integration/apis/metrics_ui/log_item.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,107 +44,107 @@ export default function ({ getService }: FtrProviderContext) {
4444
expect(logItem.fields).to.eql([
4545
{
4646
field: '@timestamp',
47-
value: '2018-10-17T19:42:22.000Z',
47+
value: ['2018-10-17T19:42:22.000Z'],
4848
},
4949
{
5050
field: '_id',
51-
value: 'yT2Mg2YBh-opCxJv8Vqj',
51+
value: ['yT2Mg2YBh-opCxJv8Vqj'],
5252
},
5353
{
5454
field: '_index',
55-
value: 'filebeat-7.0.0-alpha1-2018.10.17',
55+
value: ['filebeat-7.0.0-alpha1-2018.10.17'],
5656
},
5757
{
5858
field: 'apache2.access.body_sent.bytes',
59-
value: '1336',
59+
value: ['1336'],
6060
},
6161
{
6262
field: 'apache2.access.http_version',
63-
value: '1.1',
63+
value: ['1.1'],
6464
},
6565
{
6666
field: 'apache2.access.method',
67-
value: 'GET',
67+
value: ['GET'],
6868
},
6969
{
7070
field: 'apache2.access.referrer',
71-
value: '-',
71+
value: ['-'],
7272
},
7373
{
7474
field: 'apache2.access.remote_ip',
75-
value: '10.128.0.11',
75+
value: ['10.128.0.11'],
7676
},
7777
{
7878
field: 'apache2.access.response_code',
79-
value: '200',
79+
value: ['200'],
8080
},
8181
{
8282
field: 'apache2.access.url',
83-
value: '/a-fresh-start-will-put-you-on-your-way',
83+
value: ['/a-fresh-start-will-put-you-on-your-way'],
8484
},
8585
{
8686
field: 'apache2.access.user_agent.device',
87-
value: 'Other',
87+
value: ['Other'],
8888
},
8989
{
9090
field: 'apache2.access.user_agent.name',
91-
value: 'Other',
91+
value: ['Other'],
9292
},
9393
{
9494
field: 'apache2.access.user_agent.os',
95-
value: 'Other',
95+
value: ['Other'],
9696
},
9797
{
9898
field: 'apache2.access.user_agent.os_name',
99-
value: 'Other',
99+
value: ['Other'],
100100
},
101101
{
102102
field: 'apache2.access.user_name',
103-
value: '-',
103+
value: ['-'],
104104
},
105105
{
106106
field: 'beat.hostname',
107-
value: 'demo-stack-apache-01',
107+
value: ['demo-stack-apache-01'],
108108
},
109109
{
110110
field: 'beat.name',
111-
value: 'demo-stack-apache-01',
111+
value: ['demo-stack-apache-01'],
112112
},
113113
{
114114
field: 'beat.version',
115-
value: '7.0.0-alpha1',
115+
value: ['7.0.0-alpha1'],
116116
},
117117
{
118118
field: 'fileset.module',
119-
value: 'apache2',
119+
value: ['apache2'],
120120
},
121121
{
122122
field: 'fileset.name',
123-
value: 'access',
123+
value: ['access'],
124124
},
125125
{
126126
field: 'host.name',
127-
value: 'demo-stack-apache-01',
127+
value: ['demo-stack-apache-01'],
128128
},
129129
{
130130
field: 'input.type',
131-
value: 'log',
131+
value: ['log'],
132132
},
133133
{
134134
field: 'offset',
135-
value: '5497614',
135+
value: ['5497614'],
136136
},
137137
{
138138
field: 'prospector.type',
139-
value: 'log',
139+
value: ['log'],
140140
},
141141
{
142142
field: 'read_timestamp',
143-
value: '2018-10-17T19:42:23.160Z',
143+
value: ['2018-10-17T19:42:23.160Z'],
144144
},
145145
{
146146
field: 'source',
147-
value: '/var/log/apache2/access.log',
147+
value: ['/var/log/apache2/access.log'],
148148
},
149149
]);
150150
});

0 commit comments

Comments
 (0)