Submit
Path:
~
/
/
usr
/
share
/
grafana
/
public
/
app
/
plugins
/
datasource
/
loki
/
File Content:
getDerivedFields.ts
import { groupBy } from 'lodash'; import { FieldType, DataFrame, DataLink, Field } from '@grafana/data'; import { getDataSourceSrv } from '@grafana/runtime'; import { DerivedFieldConfig } from './types'; export function getDerivedFields(dataFrame: DataFrame, derivedFieldConfigs: DerivedFieldConfig[]): Field[] { if (!derivedFieldConfigs.length) { return []; } const derivedFieldsGrouped = groupBy(derivedFieldConfigs, 'name'); const newFields = Object.values(derivedFieldsGrouped).map(fieldFromDerivedFieldConfig); // line-field is the first string-field // NOTE: we should create some common log-frame-extra-string-field code somewhere const lineField = dataFrame.fields.find((f) => f.type === FieldType.string); if (lineField === undefined) { // if this is happening, something went wrong, let's raise an error throw new Error('invalid logs-dataframe, string-field missing'); } const labelFields = dataFrame.fields.find((f) => f.type === FieldType.other && f.name === 'labels'); for (let i = 0; i < lineField.values.length; i++) { for (const field of newFields) { // `matcherRegex` can be either a RegExp that is used to extract the value from the log line, or it can be a label key to derive the field from the labels if (derivedFieldsGrouped[field.name][0].matcherType === 'label' && labelFields) { const label = labelFields.values[i]; if (label) { // Find the key that matches both, the `matcherRegex` and the label key const intersectingKey = Object.keys(label).find( (key) => derivedFieldsGrouped[field.name][0].matcherRegex === key ); if (intersectingKey) { field.values.push(label[intersectingKey]); continue; } } field.values.push(null); } else if ( derivedFieldsGrouped[field.name][0].matcherType === 'regex' || derivedFieldsGrouped[field.name][0].matcherType === undefined ) { // `matcherRegex` will actually be used as a RegExp here const line = lineField.values[i]; const logMatch = line.match(derivedFieldsGrouped[field.name][0].matcherRegex); if (logMatch && logMatch[1]) { field.values.push(logMatch[1]); continue; } field.values.push(null); } else { field.values.push(null); } } } return newFields; } /** * Transform derivedField config into dataframe field with config that contains link. */ function fieldFromDerivedFieldConfig(derivedFieldConfigs: DerivedFieldConfig[]): Field { const dataSourceSrv = getDataSourceSrv(); const dataLinks = derivedFieldConfigs.reduce<DataLink[]>((acc, derivedFieldConfig) => { // Having field.datasourceUid means it is an internal link. if (derivedFieldConfig.datasourceUid) { const dsSettings = dataSourceSrv.getInstanceSettings(derivedFieldConfig.datasourceUid); const queryType = (type: string | undefined): string | undefined => { switch (type) { case 'tempo': return 'traceql'; case 'grafana-x-ray-datasource': return 'getTrace'; default: return undefined; } }; acc.push({ // Will be filled out later title: derivedFieldConfig.urlDisplayLabel || '', url: '', // This is hardcoded for Jaeger or Zipkin not way right now to specify datasource specific query object internal: { query: { query: derivedFieldConfig.url, queryType: queryType(dsSettings?.type) }, datasourceUid: derivedFieldConfig.datasourceUid, datasourceName: dsSettings?.name ?? 'Data source not found', }, }); } else if (derivedFieldConfig.url) { acc.push({ // We do not know what title to give here so we count on presentation layer to create a title from metadata. title: derivedFieldConfig.urlDisplayLabel || '', // This is hardcoded for Jaeger or Zipkin not way right now to specify datasource specific query object url: derivedFieldConfig.url, }); } return acc; }, []); return { name: derivedFieldConfigs[0].name, type: FieldType.string, config: { links: dataLinks, }, // We are adding values later on values: [], }; }
Edit
Rename
Chmod
Delete
FILE
FOLDER
INFO
Name
Size
Permission
Action
__mocks__
---
0755
components
---
0755
configuration
---
0755
docs
---
0755
img
---
0755
migrations
---
0755
querybuilder
---
0755
LanguageProvider.test.ts
23374 bytes
0644
LanguageProvider.ts
13424 bytes
0644
LiveStreams.test.ts
7021 bytes
0644
LiveStreams.ts
2641 bytes
0644
LogContextProvider.test.ts
21477 bytes
0644
LogContextProvider.ts
14192 bytes
0644
LokiVariableSupport.test.ts
3307 bytes
0644
LokiVariableSupport.ts
984 bytes
0644
README.md
127 bytes
0644
backendResultTransformer.test.ts
5537 bytes
0644
backendResultTransformer.ts
5248 bytes
0644
dataquery.cue
1633 bytes
0644
dataquery.gen.ts
1269 bytes
0644
datasource.test.ts
67600 bytes
0644
datasource.ts
42882 bytes
0644
getDerivedFields.test.ts
6291 bytes
0644
getDerivedFields.ts
4351 bytes
0644
languageUtils.test.ts
4461 bytes
0644
languageUtils.ts
5123 bytes
0644
language_utils.test.ts
1463 bytes
0644
lineParser.test.ts
1846 bytes
0644
lineParser.ts
912 bytes
0644
liveStreamsResultTransformer.test.ts
3396 bytes
0644
liveStreamsResultTransformer.ts
2624 bytes
0644
logsTimeSplitting.test.ts
1463 bytes
0644
logsTimeSplitting.ts
1661 bytes
0644
makeTableFrames.test.ts
3834 bytes
0644
makeTableFrames.ts
2415 bytes
0644
metricTimeSplitting.test.ts
2925 bytes
0644
metricTimeSplitting.ts
1586 bytes
0644
modifyQuery.test.ts
27450 bytes
0644
modifyQuery.ts
20058 bytes
0644
module.test.ts
2975 bytes
0644
module.ts
809 bytes
0644
plugin.json
756 bytes
0644
queryHints.test.ts
7526 bytes
0644
queryHints.ts
4453 bytes
0644
querySplitting.test.ts
20808 bytes
0644
querySplitting.ts
9911 bytes
0644
queryUtils.test.ts
20063 bytes
0644
queryUtils.ts
11174 bytes
0644
responseUtils.test.ts
5757 bytes
0644
responseUtils.ts
4637 bytes
0644
sortDataFrame.test.ts
5184 bytes
0644
sortDataFrame.ts
2708 bytes
0644
streaming.test.ts
1264 bytes
0644
streaming.ts
2939 bytes
0644
syntax.test.ts
3545 bytes
0644
syntax.ts
7646 bytes
0644
tracking.test.ts
6407 bytes
0644
tracking.ts
8353 bytes
0644
types.ts
2568 bytes
0644
N4ST4R_ID | Naxtarrr