Skip to content

Commit a7ee44f

Browse files
committed
fix: filter logs with ID field
1 parent 52986a9 commit a7ee44f

5 files changed

Lines changed: 35 additions & 14 deletions

File tree

docker/otel-collector/schema/seed/00002_otel_logs.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ CREATE TABLE IF NOT EXISTS ${DATABASE}.otel_logs
2525
`__hdx_materialized_k8s.pod.name` LowCardinality(String) MATERIALIZED ResourceAttributes['k8s.pod.name'] CODEC(ZSTD(1)),
2626
`__hdx_materialized_k8s.pod.uid` LowCardinality(String) MATERIALIZED ResourceAttributes['k8s.pod.uid'] CODEC(ZSTD(1)),
2727
`__hdx_materialized_deployment.environment.name` LowCardinality(String) MATERIALIZED ResourceAttributes['deployment.environment.name'] CODEC(ZSTD(1)),
28+
`__hdx_id` UInt16 MATERIALIZED toUInt16(rand()) CODEC(ZSTD(1)),
2829
INDEX idx_trace_id TraceId TYPE bloom_filter(0.001) GRANULARITY 1,
2930
INDEX idx_res_attr_key mapKeys(ResourceAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
3031
INDEX idx_res_attr_value mapValues(ResourceAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE ${DATABASE}.otel_logs DROP COLUMN IF EXISTS `__hdx_id`;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ALTER TABLE ${DATABASE}.otel_logs
2+
ADD COLUMN IF NOT EXISTS `__hdx_id` UInt16
3+
MATERIALIZED toUInt16(rand())
4+
CODEC(ZSTD(1));

packages/app/src/components/DBRowTable.tsx

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ import {
7676
useRenderedSqlChartConfig,
7777
} from '@/hooks/useChartConfig';
7878
import { useCsvExport } from '@/hooks/useCsvExport';
79-
import { useTableMetadata } from '@/hooks/useMetadata';
79+
import { useColumns, useTableMetadata } from '@/hooks/useMetadata';
8080
import useOffsetPaginatedQuery from '@/hooks/useOffsetPaginatedQuery';
8181
import { useGroupedPatterns } from '@/hooks/usePatterns';
8282
import useRowWhere, {
@@ -1374,14 +1374,15 @@ export const RawLogTable = memo(
13741374
},
13751375
);
13761376

1377-
export function appendSelectWithPrimaryAndPartitionKey(
1377+
export function appendSelectWithAdditionalKeys(
13781378
select: SelectList,
13791379
primaryKeys: string,
13801380
partitionKey: string,
1381+
extraKeys: string[],
13811382
): { select: SelectList; additionalKeysLength: number } {
13821383
const partitionKeyArr = extractColumnReferencesFromKey(partitionKey);
13831384
const primaryKeyArr = extractColumnReferencesFromKey(primaryKeys);
1384-
const allKeys = new Set([...partitionKeyArr, ...primaryKeyArr]);
1385+
const allKeys = new Set([...partitionKeyArr, ...primaryKeyArr, ...extraKeys]);
13851386
if (typeof select === 'string') {
13861387
const selectSplit = splitAndTrimWithBracket(select);
13871388
const selectColumns = new Set(selectSplit);
@@ -1407,7 +1408,12 @@ function getSelectLength(select: SelectList): number {
14071408
}
14081409
}
14091410

1410-
export function useConfigWithPrimaryAndPartitionKey(
1411+
// Column name used to disambiguate rows with identical visible column values.
1412+
// This is a MATERIALIZED column (random UInt16) added to otel_logs to ensure
1413+
// the WHERE clause generated for row detail queries uniquely identifies a row.
1414+
const HDX_ID_COLUMN = '__hdx_id';
1415+
1416+
export function useConfigWithAdditionalSelect(
14111417
config: BuilderChartConfigWithDateRange,
14121418
) {
14131419
const { data: tableMetadata } = useTableMetadata({
@@ -1416,6 +1422,12 @@ export function useConfigWithPrimaryAndPartitionKey(
14161422
connectionId: config.connection,
14171423
});
14181424

1425+
const { data: columns } = useColumns({
1426+
databaseName: config.from.databaseName,
1427+
tableName: config.from.tableName,
1428+
connectionId: config.connection,
1429+
});
1430+
14191431
const primaryKey = tableMetadata?.primary_key;
14201432
const partitionKey = tableMetadata?.partition_key;
14211433

@@ -1424,14 +1436,17 @@ export function useConfigWithPrimaryAndPartitionKey(
14241436
return undefined;
14251437
}
14261438

1427-
const { select, additionalKeysLength } =
1428-
appendSelectWithPrimaryAndPartitionKey(
1429-
config.select,
1430-
primaryKey,
1431-
partitionKey,
1432-
);
1439+
const hasHdxIdColumn =
1440+
columns?.some(c => c.name === HDX_ID_COLUMN) ?? false;
1441+
1442+
const { select, additionalKeysLength } = appendSelectWithAdditionalKeys(
1443+
config.select,
1444+
primaryKey,
1445+
partitionKey,
1446+
hasHdxIdColumn ? [HDX_ID_COLUMN] : [],
1447+
);
14331448
return { ...config, select, additionalKeysLength };
1434-
}, [primaryKey, partitionKey, config]);
1449+
}, [primaryKey, partitionKey, config, columns]);
14351450

14361451
return mergedConfig;
14371452
}
@@ -1564,7 +1579,7 @@ function DBSqlRowTableComponent({
15641579
return base;
15651580
}, [me, config, orderByArray]);
15661581

1567-
const mergedConfig = useConfigWithPrimaryAndPartitionKey(mergedConfigObj);
1582+
const mergedConfig = useConfigWithAdditionalSelect(mergedConfigObj);
15681583

15691584
const { data, fetchNextPage, hasNextPage, isFetching, isError, error } =
15701585
useOffsetPaginatedQuery(mergedConfig ?? config, {

packages/app/src/hooks/usePatterns.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { BuilderChartConfigWithDateRange } from '@hyperdx/common-utils/dist/type
55
import { useQuery } from '@tanstack/react-query';
66

77
import { timeBucketByGranularity, toStartOfInterval } from '@/ChartUtils';
8-
import { useConfigWithPrimaryAndPartitionKey } from '@/components/DBRowTable';
8+
import { useConfigWithAdditionalSelect } from '@/components/DBRowTable';
99
import { useQueriedChartConfig } from '@/hooks/useChartConfig';
1010
import { getFirstTimestampValueExpression } from '@/source';
1111

@@ -134,7 +134,7 @@ function usePatterns({
134134
statusCodeExpression?: string;
135135
enabled?: boolean;
136136
}) {
137-
const configWithPrimaryAndPartitionKey = useConfigWithPrimaryAndPartitionKey({
137+
const configWithPrimaryAndPartitionKey = useConfigWithAdditionalSelect({
138138
...config,
139139
// TODO: User-configurable pattern columns and non-pattern/group by columns
140140
select: [

0 commit comments

Comments
 (0)