|
1 | | -import { SourceKind, TTraceSource } from '@hyperdx/common-utils/dist/types'; |
| 1 | +import { |
| 2 | + SourceKind, |
| 3 | + TLogSource, |
| 4 | + TTraceSource, |
| 5 | +} from '@hyperdx/common-utils/dist/types'; |
2 | 6 |
|
3 | | -import { getEventBody } from '../source'; |
| 7 | +import { getEventBody, getTraceDurationNumberFormat } from '../source'; |
| 8 | + |
| 9 | +const TRACE_SOURCE: TTraceSource = { |
| 10 | + kind: SourceKind.Trace, |
| 11 | + from: { |
| 12 | + databaseName: 'default', |
| 13 | + tableName: 'otel_traces', |
| 14 | + }, |
| 15 | + timestampValueExpression: 'Timestamp', |
| 16 | + connection: 'test-connection', |
| 17 | + name: 'Traces', |
| 18 | + id: 'test-source-id', |
| 19 | + spanNameExpression: 'SpanName', |
| 20 | + durationExpression: 'Duration', |
| 21 | + durationPrecision: 9, |
| 22 | + traceIdExpression: 'TraceId', |
| 23 | + spanIdExpression: 'SpanId', |
| 24 | + parentSpanIdExpression: 'ParentSpanId', |
| 25 | + spanKindExpression: 'SpanKind', |
| 26 | + defaultTableSelectExpression: 'Timestamp, ServiceName', |
| 27 | +} as TTraceSource; |
4 | 28 |
|
5 | 29 | describe('getEventBody', () => { |
6 | | - // Added to prevent regression back to HDX-3361 |
7 | 30 | it('returns spanNameExpression for trace kind source when both bodyExpression and spanNameExpression are present', () => { |
8 | | - const source = { |
9 | | - kind: SourceKind.Trace, |
10 | | - from: { |
11 | | - databaseName: 'default', |
12 | | - tableName: 'otel_traces', |
13 | | - }, |
14 | | - timestampValueExpression: 'Timestamp', |
15 | | - connection: 'test-connection', |
16 | | - name: 'Traces', |
17 | | - id: 'test-source-id', |
18 | | - spanNameExpression: 'SpanName', |
19 | | - durationExpression: 'Duration', |
20 | | - durationPrecision: 9, |
21 | | - traceIdExpression: 'TraceId', |
22 | | - spanIdExpression: 'SpanId', |
23 | | - parentSpanIdExpression: 'ParentSpanId', |
24 | | - spanKindExpression: 'SpanKind', |
25 | | - } as TTraceSource; |
26 | | - |
27 | | - const result = getEventBody(source); |
28 | | - |
| 31 | + const result = getEventBody(TRACE_SOURCE); |
29 | 32 | expect(result).toBe('SpanName'); |
30 | 33 | }); |
31 | 34 | }); |
| 35 | + |
| 36 | +describe('getTraceDurationNumberFormat', () => { |
| 37 | + it('returns undefined for non-trace sources', () => { |
| 38 | + const logSource = { |
| 39 | + kind: SourceKind.Log, |
| 40 | + id: 'log-source', |
| 41 | + } as TLogSource; |
| 42 | + const result = getTraceDurationNumberFormat(logSource, [ |
| 43 | + { valueExpression: 'count()' }, |
| 44 | + ]); |
| 45 | + expect(result).toBeUndefined(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('returns undefined when source is undefined', () => { |
| 49 | + const result = getTraceDurationNumberFormat(undefined, [ |
| 50 | + { valueExpression: 'count()' }, |
| 51 | + ]); |
| 52 | + expect(result).toBeUndefined(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('returns undefined when select expressions do not reference duration', () => { |
| 56 | + const result = getTraceDurationNumberFormat(TRACE_SOURCE, [ |
| 57 | + { valueExpression: 'count()' }, |
| 58 | + ]); |
| 59 | + expect(result).toBeUndefined(); |
| 60 | + }); |
| 61 | + |
| 62 | + // --- exact match --- |
| 63 | + |
| 64 | + it('matches when valueExpression exactly equals durationExpression', () => { |
| 65 | + expect( |
| 66 | + getTraceDurationNumberFormat(TRACE_SOURCE, [ |
| 67 | + { valueExpression: 'Duration', aggFn: 'avg' }, |
| 68 | + ]), |
| 69 | + ).toEqual({ output: 'duration', factor: 1e-9 }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('matches without aggFn (raw expression passed through)', () => { |
| 73 | + expect( |
| 74 | + getTraceDurationNumberFormat(TRACE_SOURCE, [ |
| 75 | + { valueExpression: 'Duration' }, |
| 76 | + ]), |
| 77 | + ).toEqual({ output: 'duration', factor: 1e-9 }); |
| 78 | + }); |
| 79 | + |
| 80 | + // --- non-matching expressions --- |
| 81 | + |
| 82 | + it('does not match expressions that only contain the duration name', () => { |
| 83 | + expect( |
| 84 | + getTraceDurationNumberFormat(TRACE_SOURCE, [ |
| 85 | + { valueExpression: 'avg(Duration)' }, |
| 86 | + ]), |
| 87 | + ).toBeUndefined(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('does not match division expressions', () => { |
| 91 | + expect( |
| 92 | + getTraceDurationNumberFormat(TRACE_SOURCE, [ |
| 93 | + { valueExpression: 'Duration/1e6' }, |
| 94 | + ]), |
| 95 | + ).toBeUndefined(); |
| 96 | + expect( |
| 97 | + getTraceDurationNumberFormat(TRACE_SOURCE, [ |
| 98 | + { valueExpression: '(Duration)/1e6' }, |
| 99 | + ]), |
| 100 | + ).toBeUndefined(); |
| 101 | + expect( |
| 102 | + getTraceDurationNumberFormat(TRACE_SOURCE, [ |
| 103 | + { valueExpression: 'Duration / 1e9' }, |
| 104 | + ]), |
| 105 | + ).toBeUndefined(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('does not match modified or similar-named expressions', () => { |
| 109 | + expect( |
| 110 | + getTraceDurationNumberFormat(TRACE_SOURCE, [ |
| 111 | + { valueExpression: 'Duration * 2' }, |
| 112 | + ]), |
| 113 | + ).toBeUndefined(); |
| 114 | + expect( |
| 115 | + getTraceDurationNumberFormat(TRACE_SOURCE, [ |
| 116 | + { valueExpression: 'LongerDuration' }, |
| 117 | + ]), |
| 118 | + ).toBeUndefined(); |
| 119 | + expect( |
| 120 | + getTraceDurationNumberFormat(TRACE_SOURCE, [ |
| 121 | + { valueExpression: 'round(Duration / 1e6, 2)' }, |
| 122 | + ]), |
| 123 | + ).toBeUndefined(); |
| 124 | + }); |
| 125 | + |
| 126 | + // --- aggFn filtering --- |
| 127 | + |
| 128 | + it('returns undefined for count aggFn', () => { |
| 129 | + expect( |
| 130 | + getTraceDurationNumberFormat(TRACE_SOURCE, [ |
| 131 | + { valueExpression: 'Duration', aggFn: 'count' }, |
| 132 | + ]), |
| 133 | + ).toBeUndefined(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('returns undefined for count_distinct aggFn', () => { |
| 137 | + expect( |
| 138 | + getTraceDurationNumberFormat(TRACE_SOURCE, [ |
| 139 | + { valueExpression: 'Duration', aggFn: 'count_distinct' }, |
| 140 | + ]), |
| 141 | + ).toBeUndefined(); |
| 142 | + }); |
| 143 | + |
| 144 | + it.each(['sum', 'min', 'max', 'quantile', 'avg', 'any', 'last_value'])( |
| 145 | + 'detects duration with %s aggFn', |
| 146 | + aggFn => { |
| 147 | + expect( |
| 148 | + getTraceDurationNumberFormat(TRACE_SOURCE, [ |
| 149 | + { valueExpression: 'Duration', aggFn }, |
| 150 | + ]), |
| 151 | + ).toEqual({ output: 'duration', factor: 1e-9 }); |
| 152 | + }, |
| 153 | + ); |
| 154 | + |
| 155 | + it('detects duration with combinator aggFn like avgIf', () => { |
| 156 | + expect( |
| 157 | + getTraceDurationNumberFormat(TRACE_SOURCE, [ |
| 158 | + { valueExpression: 'Duration', aggFn: 'avgIf' }, |
| 159 | + ]), |
| 160 | + ).toEqual({ output: 'duration', factor: 1e-9 }); |
| 161 | + }); |
| 162 | + |
| 163 | + it('skips non-preserving aggFn and detects preserving one in mixed selects', () => { |
| 164 | + expect( |
| 165 | + getTraceDurationNumberFormat(TRACE_SOURCE, [ |
| 166 | + { valueExpression: 'Duration', aggFn: 'count' }, |
| 167 | + { valueExpression: 'Duration', aggFn: 'avg' }, |
| 168 | + ]), |
| 169 | + ).toEqual({ output: 'duration', factor: 1e-9 }); |
| 170 | + }); |
| 171 | + |
| 172 | + it('returns undefined when only non-preserving aggFns reference duration', () => { |
| 173 | + expect( |
| 174 | + getTraceDurationNumberFormat(TRACE_SOURCE, [ |
| 175 | + { valueExpression: 'Duration', aggFn: 'count' }, |
| 176 | + { valueExpression: 'Duration', aggFn: 'count_distinct' }, |
| 177 | + ]), |
| 178 | + ).toBeUndefined(); |
| 179 | + }); |
| 180 | + |
| 181 | + it('returns undefined when select is empty', () => { |
| 182 | + expect(getTraceDurationNumberFormat(TRACE_SOURCE, [])).toBeUndefined(); |
| 183 | + }); |
| 184 | +}); |
0 commit comments