Skip to content

Commit

Permalink
added plugin attestation (#248) (#253)
Browse files Browse the repository at this point in the history
fix annotations

transform labels to string for annotations

split annotations by lines

update CHANGELOG.md

cleanup

Co-authored-by: Andrii Chubatiuk <[email protected]>
  • Loading branch information
dmitryk-dk and AndrewChubatiuk authored Mar 11, 2025
1 parent 8c9744f commit 441ad57
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## tip

* BUGFIX: fix build annotation from the label field. All labels transforms to the string representation. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/188).

## v0.15.0

* FEATURE: add configuration screen for derived fields. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/202).
Expand Down
83 changes: 70 additions & 13 deletions src/backendResultTransformer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { DataQueryResponse, DataFrame, isDataFrame, FieldType, QueryResultMeta, DataQueryError } from '@grafana/data';
import {
CoreApp,
DataFrame,
DataQueryError,
DataQueryRequest,
DataQueryResponse,
FieldType,
isDataFrame,
QueryResultMeta,
} from '@grafana/data';

import { getDerivedFields } from './getDerivedFields';
import { makeTableFrames } from './makeTableFrames';
Expand All @@ -21,6 +30,19 @@ function setFrameMeta(frame: DataFrame, meta: QueryResultMeta): DataFrame {
};
}

function processStreamsFrames(
frames: DataFrame[],
queryMap: Map<string, Query>,
derivedFieldConfigs: DerivedFieldConfig[],
app: string
): DataFrame[] {
return frames.map((frame) => {
const query = frame.refId !== undefined ? queryMap.get(frame.refId) : undefined;
if (app === CoreApp.Dashboard) return processDashboardStreamFrame(frame, query, derivedFieldConfigs);
return processStreamFrame(frame, query, derivedFieldConfigs);
});
}

function processStreamFrame(
frame: DataFrame,
query: Query | undefined,
Expand Down Expand Up @@ -49,15 +71,48 @@ function processStreamFrame(
};
}

function processStreamsFrames(
frames: DataFrame[],
queryMap: Map<string, Query>,
function processDashboardStreamFrame(
frame: DataFrame,
query: Query | undefined,
derivedFieldConfigs: DerivedFieldConfig[]
): DataFrame[] {
return frames.map((frame) => {
const query = frame.refId !== undefined ? queryMap.get(frame.refId) : undefined;
return processStreamFrame(frame, query, derivedFieldConfigs);
});
): DataFrame {
const custom: Record<string, string> = {
...frame.meta?.custom, // keep the original meta.custom
};

if (dataFrameHasError(frame)) {
custom.error = 'Error when parsing some of the logs';
}

const meta: QueryResultMeta = {
preferredVisualisationType: 'logs',
limit: query?.maxLines,
searchWords: query !== undefined ? getHighlighterExpressionsFromQuery(query.expr) : undefined,
custom,
};

const newFrame = setFrameMeta(frame, meta);
const derivedFields = getDerivedFields(newFrame, derivedFieldConfigs);

return {
...newFrame,
fields: [
...newFrame.fields.map((field) => {
if (field.name === 'labels') {
return {
...field,
values: field.values.map((value) => {
return Object.entries(value).map(([key, value]) => {
return `${key}: ${JSON.stringify(value)}`;
});
}),
};
}
return field;
}),
...derivedFields,
],
};
}

function processMetricInstantFrames(frames: DataFrame[]): DataFrame[] {
Expand Down Expand Up @@ -126,10 +181,12 @@ function improveError(error: DataQueryError | undefined, queryMap: Map<string, Q

export function transformBackendResult(
response: DataQueryResponse,
queries: Query[],
derivedFieldConfigs: DerivedFieldConfig[],
request: DataQueryRequest,
derivedFieldConfigs: DerivedFieldConfig[]
): DataQueryResponse {
const { data, errors, ...rest } = response;
const queries = request.targets;
const { app } = request;

// in the typescript type, data is an array of basically anything.
// we do know that they have to be dataframes, so we make a quick check,
Expand All @@ -142,7 +199,7 @@ export function transformBackendResult(
return d;
});

const queryMap = new Map(queries.map((query) => [query.refId, query]));
const queryMap = new Map(queries.map((query) => [query.refId, query])) as Map<string, Query>;

const { streamsFrames, metricInstantFrames, metricRangeFrames } = groupFrames(dataFrames, queryMap);

Expand All @@ -154,7 +211,7 @@ export function transformBackendResult(
data: [
...processMetricRangeFrames(metricRangeFrames),
...processMetricInstantFrames(metricInstantFrames),
...processStreamsFrames(streamsFrames, queryMap, derivedFieldConfigs),
...processStreamsFrames(streamsFrames, queryMap, derivedFieldConfigs, app),
],
};
}
2 changes: 1 addition & 1 deletion src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class VictoriaLogsDatasource
.query(fixedRequest)
.pipe(
map((response) =>
transformBackendResult(response, fixedRequest.targets, this.derivedFields ?? [])
transformBackendResult(response, fixedRequest, this.derivedFields ?? [])
)
);
}
Expand Down

0 comments on commit 441ad57

Please sign in to comment.