-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Added Enhancements to Log Ai Insight #247291
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
Changes from all commits
c9aab67
44b44e2
b64015b
a87c1dc
e5d48e5
e02947c
769fee0
e50dd81
52ee4df
ac070e8
b8c86ee
b0685ce
55affb9
0b45ec5
3124e3a
e1309f1
2112716
a57c8be
384bced
2523a21
35b7291
0e6ac58
2ad03d3
ab652c6
94135cc
35ec4bd
d9b647f
07ce96a
f4382fe
c5d3a6f
ae8568f
6fd1ce8
33b4b6b
6130807
f009254
5ce23e8
2423a38
e56b156
d70c2fd
c907227
5dc626c
73ccf32
bd60b60
cf98e3a
5c124c5
e9fd179
8a6da49
7bbe53d
b220f84
7719f35
15fa737
c389435
efb1d96
1dea11f
1973cbd
775b93c
ab0c124
01b7d1b
ca63002
53f8492
e95c9c2
0b5b0cd
58c53df
e6bee31
841558f
8d98f5c
3b2bda0
95c5a02
a4af6d0
f752200
5d835e2
306143a
90e3885
6c67d8d
b47a1e6
4ea551b
8f6049e
51a1ba8
f2faa93
cac6cfc
430b890
a3a965b
6500993
999af77
bbce724
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,14 +7,28 @@ | |||||||||||||||
|
|
||||||||||||||||
| import type { ElasticsearchClient } from '@kbn/core/server'; | ||||||||||||||||
|
|
||||||||||||||||
| export interface LogDocument { | ||||||||||||||||
| const LOG_DOCUMENT_FIELDS = [ | ||||||||||||||||
| '@timestamp', | ||||||||||||||||
| 'message', | ||||||||||||||||
| 'log.level', | ||||||||||||||||
| 'service.name', | ||||||||||||||||
| 'trace.id', | ||||||||||||||||
| 'span.id', | ||||||||||||||||
| 'http.response.status_code', | ||||||||||||||||
| 'error.exception.message', | ||||||||||||||||
| ] as const; | ||||||||||||||||
|
|
||||||||||||||||
| type FieldKeys = (typeof LOG_DOCUMENT_FIELDS)[number]; | ||||||||||||||||
|
|
||||||||||||||||
| export type LogDocument = { | ||||||||||||||||
| 'log.level'?: string; | ||||||||||||||||
| '@timestamp'?: string; | ||||||||||||||||
| service?: { | ||||||||||||||||
| name?: string; | ||||||||||||||||
| environment?: string; | ||||||||||||||||
| }; | ||||||||||||||||
| [key: string]: unknown; | ||||||||||||||||
| } | ||||||||||||||||
| message?: string; | ||||||||||||||||
| 'http.response.status_code'?: number; | ||||||||||||||||
| 'error.exception.message'?: string; | ||||||||||||||||
| } & { | ||||||||||||||||
| [K in FieldKeys]?: unknown; | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| export const getLogDocumentById = async ({ | ||||||||||||||||
| esClient, | ||||||||||||||||
|
|
@@ -25,10 +39,26 @@ export const getLogDocumentById = async ({ | |||||||||||||||
| index: string; | ||||||||||||||||
| id: string; | ||||||||||||||||
| }): Promise<LogDocument | undefined> => { | ||||||||||||||||
| const result = await esClient.get<LogDocument>({ | ||||||||||||||||
| const result = await esClient.search({ | ||||||||||||||||
|
sorenlouv marked this conversation as resolved.
|
||||||||||||||||
| index, | ||||||||||||||||
| id, | ||||||||||||||||
| size: 1, | ||||||||||||||||
| _source: false, | ||||||||||||||||
| fields: [...LOG_DOCUMENT_FIELDS], | ||||||||||||||||
| query: { | ||||||||||||||||
| ids: { values: [id] }, | ||||||||||||||||
| }, | ||||||||||||||||
| }); | ||||||||||||||||
|
|
||||||||||||||||
| return result._source; | ||||||||||||||||
| const hit = result.hits.hits[0]; | ||||||||||||||||
|
|
||||||||||||||||
| if (!hit?.fields) { | ||||||||||||||||
| return undefined; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| return Object.fromEntries( | ||||||||||||||||
| Object.entries(hit.fields).map(([key, value]) => [ | ||||||||||||||||
| key, | ||||||||||||||||
| Array.isArray(value) && value.length === 1 ? value[0] : value, | ||||||||||||||||
| ]) | ||||||||||||||||
| ) as LogDocument; | ||||||||||||||||
|
Comment on lines
+58
to
+63
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I'm adding this util to /**
* Elasticsearch `fields` response returns all values as arrays.
* This utility unwraps single-element arrays to their first value,
* while preserving multi-element arrays.
*/
export function unwrapEsFields<T = Record<string, unknown>>(
fields: Record<string, unknown[] | undefined> | undefined
): T {
return Object.fromEntries(
Object.entries(fields ?? {}).map(([key, value]) => [key, unwrapEsFieldValue(value)])
) as T;
}
/**
* Get a single field value from ES fields response, unwrapping single-element arrays.
* Multi-element arrays are preserved.
*/
export function getEsField<T = unknown>(
fields: Record<string, unknown[] | undefined> | undefined,
key: string
): T | undefined {
return unwrapEsFieldValue(fields?.[key]) as T | undefined;
}
function unwrapEsFieldValue(value: unknown): unknown {
return Array.isArray(value) && value.length === 1 ? value[0] : value;
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sorenlouv , was this the utility function you were planning to create, or would you like me to add it? I just can’t find it in main
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The util is in #250331. Not in main yet
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||
| }; | ||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you consider adding
contextto system prompt instead of user prompt? Pros/cons?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question! For me the context is the user's input data to analyze, so userPromt is a right point to add it. Also this part will be available for the chat with agent later as a part of attachments