Skip to content
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

Update trace queries to better handle time fields #890

Merged
merged 7 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/data/sqlGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ describe('SQL Generator', () => {
};
const expectedSqlParts = [
'SELECT "TraceId" as traceID, "SpanId" as spanID, "ParentSpanId" as parentSpanID,',
'"ServiceName" as serviceName, "SpanName" as operationName, "Timestamp" as startTime,',
'"ServiceName" as serviceName, "SpanName" as operationName, multiply(toUnixTimestamp64Nano("Timestamp"), 0.000001) as startTime,',
'multiply("Duration", 0.000001) as duration,',
`arrayMap(key -> map('key', key, 'value',"SpanAttributes"[key]),`,
`mapKeys("SpanAttributes")) as tags,`,
Expand Down Expand Up @@ -256,7 +256,7 @@ describe('SQL Generator', () => {
`WITH 'abcdefg' as trace_id, (SELECT min(Start) FROM "default"."otel_traces_trace_id_ts" WHERE TraceId = trace_id) as trace_start,`,
`(SELECT max(End) + 1 FROM "default"."otel_traces_trace_id_ts" WHERE TraceId = trace_id) as trace_end`,
'SELECT "TraceId" as traceID, "SpanId" as spanID, "ParentSpanId" as parentSpanID,',
'"ServiceName" as serviceName, "SpanName" as operationName, "Timestamp" as startTime,',
'"ServiceName" as serviceName, "SpanName" as operationName, multiply(toUnixTimestamp64Nano("Timestamp"), 0.000001) as startTime,',
'multiply("Duration", 0.000001) as duration,',
`arrayMap(key -> map('key', key, 'value',"SpanAttributes"[key]),`,
`mapKeys("SpanAttributes")) as tags,`,
Expand Down Expand Up @@ -335,7 +335,7 @@ describe('SQL Generator', () => {
};
const expectedSqlParts = [
'SELECT "TraceId" as traceID, "ServiceName" as serviceName, "SpanName" as operationName,',
'"Timestamp" as startTime, multiply("Duration", 0.000001) as duration',
'multiply(toUnixTimestamp64Nano("Timestamp"), 0.000001) as startTime, multiply("Duration", 0.000001) as duration',
'FROM "default"."otel_traces" WHERE ( Timestamp >= $__fromTime AND Timestamp <= $__toTime )',
'AND ( ParentSpanId = \'\' ) AND ( Duration > 0 ) ORDER BY Timestamp DESC, Duration DESC LIMIT 1000'
];
Expand Down
12 changes: 8 additions & 4 deletions src/data/sqlGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import otel from 'otel';
* Generates a SQL string for the given QueryBuilderOptions
*/
export const generateSql = (options: QueryBuilderOptions): string => {
const hasTraceIdFilter = options.meta?.isTraceIdMode && options.meta?.traceId
const hasTraceIdFilter = options.meta?.isTraceIdMode && options.meta?.traceId;
if (options.queryType === QueryType.Traces && hasTraceIdFilter) {
return generateTraceIdQuery(options);
} else if (options.queryType === QueryType.Traces) {
Expand Down Expand Up @@ -50,7 +50,7 @@ const generateTraceSearchQuery = (options: QueryBuilderOptions): string => {

const traceStartTime = getColumnByHint(options, ColumnHint.Time);
if (traceStartTime !== undefined) {
selectParts.push(`${escapeIdentifier(traceStartTime.name)} as startTime`);
selectParts.push(`${convertTimeFieldToMilliseconds(escapeIdentifier(traceStartTime.name))} as startTime`);
}

const traceDurationTime = getColumnByHint(options, ColumnHint.TraceDurationTime);
Expand Down Expand Up @@ -125,7 +125,7 @@ const generateTraceIdQuery = (options: QueryBuilderOptions): string => {

const traceStartTime = getColumnByHint(options, ColumnHint.Time);
if (traceStartTime !== undefined) {
selectParts.push(`${escapeIdentifier(traceStartTime.name)} as startTime`);
selectParts.push(`${convertTimeFieldToMilliseconds(escapeIdentifier(traceStartTime.name))} as startTime`);
}

const traceDurationTime = getColumnByHint(options, ColumnHint.TraceDurationTime);
Expand Down Expand Up @@ -555,7 +555,7 @@ const escapeValue = (value: string): string => {
}

/**
* Returns the a SELECT column for trace duration.
* Returns the SELECT column for trace duration.
* Time unit is used to convert the value to milliseconds, as is required by Grafana's Trace panel.
*/
const getTraceDurationSelectSql = (columnIdentifier: string, timeUnit?: TimeUnit): string => {
Expand All @@ -574,6 +574,10 @@ const getTraceDurationSelectSql = (columnIdentifier: string, timeUnit?: TimeUnit
}
}

/** Returns the input time field converted to a Unix timestamp in nanoseconds and then adjusted to milliseconds. */
const convertTimeFieldToMilliseconds = (columnIdentifier: string) =>
`multiply(toUnixTimestamp64Nano(${columnIdentifier}), 0.000001)`;

/**
* Concatenates query parts with no empty spaces.
*/
Expand Down
Loading