|
1 | 1 | import { describe, expect, it, vitest as jest } from 'vitest'
|
2 | 2 | import { getSpanSummaryAttributes } from './convertToRum'
|
3 |
| -import { |
4 |
| - fromDefinition, |
5 |
| - type SpanMatcherFn, |
6 |
| - withMatchingRelations, |
7 |
| -} from './matchSpan' |
| 3 | +import { fromDefinition } from './matchSpan' |
8 | 4 | import {
|
9 | 5 | createTraceRecording,
|
10 | 6 | getComputedSpans,
|
11 | 7 | getComputedValues,
|
12 | 8 | } from './recordingComputeUtils'
|
13 |
| -import type { SpanAndAnnotation } from './spanAnnotationTypes' |
14 |
| -import type { Span } from './spanTypes' |
15 | 9 | import {
|
16 | 10 | createMockSpanAndAnnotation,
|
17 | 11 | createTimestamp,
|
@@ -752,4 +746,181 @@ describe('recordingComputeUtils', () => {
|
752 | 746 | expect(recording.variant).toBe('origin')
|
753 | 747 | })
|
754 | 748 | })
|
| 749 | + |
| 750 | + describe('promoteSpanAttributesForTrace and attribute promotion', () => { |
| 751 | + const promotedAttributesTraceDefinition = { |
| 752 | + ...baseDefinitionFixture, |
| 753 | + promoteSpanAttributes: [ |
| 754 | + { |
| 755 | + span: { name: 'foo-span' }, |
| 756 | + attributes: ['foo', 'bar'], |
| 757 | + }, |
| 758 | + { |
| 759 | + span: { name: 'baz-span' }, |
| 760 | + attributes: ['baz'], |
| 761 | + }, |
| 762 | + ], |
| 763 | + } |
| 764 | + |
| 765 | + it('should promote specified attributes from last matching spans to trace', () => { |
| 766 | + const recording = createTraceRecording( |
| 767 | + { |
| 768 | + definition: promotedAttributesTraceDefinition, |
| 769 | + recordedItems: new Set([ |
| 770 | + createMockSpanAndAnnotation(100, { |
| 771 | + name: 'foo-span', |
| 772 | + attributes: { foo: 1, bar: 2, unused: 42 }, |
| 773 | + }), |
| 774 | + createMockSpanAndAnnotation(200, { |
| 775 | + name: 'foo-span', |
| 776 | + attributes: { foo: 7, bar: 8 }, |
| 777 | + }), |
| 778 | + createMockSpanAndAnnotation(300, { |
| 779 | + name: 'baz-span', |
| 780 | + attributes: { baz: 'hello' }, |
| 781 | + }), |
| 782 | + ]), |
| 783 | + input: { |
| 784 | + id: 'test', |
| 785 | + startTime: createTimestamp(0), |
| 786 | + relatedTo: {}, |
| 787 | + variant: 'origin', |
| 788 | + }, |
| 789 | + recordedItemsByLabel: {}, |
| 790 | + }, |
| 791 | + { |
| 792 | + transitionFromState: 'active', |
| 793 | + transitionToState: 'complete', |
| 794 | + lastRelevantSpanAndAnnotation: undefined, |
| 795 | + completeSpanAndAnnotation: undefined, |
| 796 | + cpuIdleSpanAndAnnotation: undefined, |
| 797 | + lastRequiredSpanAndAnnotation: undefined, |
| 798 | + }, |
| 799 | + ) |
| 800 | + // Should select last foo-span (timestamp 200) for foo/bar, and baz-span for baz |
| 801 | + expect(recording.attributes.foo).toBe(7) |
| 802 | + expect(recording.attributes.bar).toBe(8) |
| 803 | + expect(recording.attributes.baz).toBe('hello') |
| 804 | + expect('unused' in recording.attributes).toBe(false) |
| 805 | + }) |
| 806 | + |
| 807 | + it('should not set unset promoted attributes if not found', () => { |
| 808 | + const partialAttrDefinition = { |
| 809 | + ...baseDefinitionFixture, |
| 810 | + promoteSpanAttributes: [ |
| 811 | + { |
| 812 | + span: { name: 'foo-span' }, |
| 813 | + attributes: ['foo', 'bar'], |
| 814 | + }, |
| 815 | + { |
| 816 | + span: { name: 'no-match' }, |
| 817 | + attributes: ['baz', 'shouldNotBeSet'], |
| 818 | + }, |
| 819 | + ], |
| 820 | + } |
| 821 | + const recording = createTraceRecording( |
| 822 | + { |
| 823 | + definition: partialAttrDefinition, |
| 824 | + recordedItems: new Set([ |
| 825 | + createMockSpanAndAnnotation(111, { |
| 826 | + name: 'foo-span', |
| 827 | + attributes: { foo: 99 }, |
| 828 | + }), |
| 829 | + ]), |
| 830 | + input: { |
| 831 | + id: 'test', |
| 832 | + startTime: createTimestamp(0), |
| 833 | + relatedTo: {}, |
| 834 | + variant: 'origin', |
| 835 | + }, |
| 836 | + recordedItemsByLabel: {}, |
| 837 | + }, |
| 838 | + { |
| 839 | + transitionFromState: 'active', |
| 840 | + transitionToState: 'complete', |
| 841 | + lastRelevantSpanAndAnnotation: undefined, |
| 842 | + completeSpanAndAnnotation: undefined, |
| 843 | + cpuIdleSpanAndAnnotation: undefined, |
| 844 | + lastRequiredSpanAndAnnotation: undefined, |
| 845 | + }, |
| 846 | + ) |
| 847 | + expect(recording.attributes.foo).toBe(99) |
| 848 | + expect('bar' in recording.attributes).toBe(false) |
| 849 | + expect('baz' in recording.attributes).toBe(false) |
| 850 | + expect('shouldNotBeSet' in recording.attributes).toBe(false) |
| 851 | + }) |
| 852 | + |
| 853 | + it('should allow attribute promotion on interruption', () => { |
| 854 | + const recording = createTraceRecording( |
| 855 | + { |
| 856 | + definition: promotedAttributesTraceDefinition, |
| 857 | + recordedItems: new Set([ |
| 858 | + createMockSpanAndAnnotation(100, { |
| 859 | + name: 'foo-span', |
| 860 | + attributes: { foo: 'z' }, |
| 861 | + }), |
| 862 | + createMockSpanAndAnnotation(200, { |
| 863 | + name: 'baz-span', |
| 864 | + attributes: { baz: 10 }, |
| 865 | + }), |
| 866 | + ]), |
| 867 | + input: { |
| 868 | + id: 'test', |
| 869 | + startTime: createTimestamp(0), |
| 870 | + relatedTo: {}, |
| 871 | + variant: 'origin', |
| 872 | + }, |
| 873 | + recordedItemsByLabel: {}, |
| 874 | + }, |
| 875 | + { |
| 876 | + transitionFromState: 'active', |
| 877 | + transitionToState: 'interrupted', |
| 878 | + interruptionReason: 'timeout', |
| 879 | + lastRelevantSpanAndAnnotation: undefined, |
| 880 | + }, |
| 881 | + ) |
| 882 | + expect(recording.attributes.foo).toBe('z') |
| 883 | + expect(recording.attributes.baz).toBe(10) |
| 884 | + }) |
| 885 | + |
| 886 | + it('should allow original trace attributes to take precedence over promoted', () => { |
| 887 | + const definition = { |
| 888 | + ...promotedAttributesTraceDefinition, |
| 889 | + } |
| 890 | + |
| 891 | + const recording = createTraceRecording( |
| 892 | + { |
| 893 | + definition, |
| 894 | + recordedItems: new Set([ |
| 895 | + createMockSpanAndAnnotation(100, { |
| 896 | + name: 'foo-span', |
| 897 | + attributes: { foo: 'notUsed' }, |
| 898 | + }), |
| 899 | + createMockSpanAndAnnotation(200, { |
| 900 | + name: 'baz-span', |
| 901 | + attributes: { baz: 111 }, |
| 902 | + }), |
| 903 | + ]), |
| 904 | + input: { |
| 905 | + id: 'test', |
| 906 | + startTime: createTimestamp(0), |
| 907 | + relatedTo: {}, |
| 908 | + variant: 'origin', |
| 909 | + attributes: { foo: 'owned', baz: 'shouldWin' }, |
| 910 | + }, |
| 911 | + recordedItemsByLabel: {}, |
| 912 | + }, |
| 913 | + { |
| 914 | + transitionFromState: 'active', |
| 915 | + transitionToState: 'complete', |
| 916 | + lastRelevantSpanAndAnnotation: undefined, |
| 917 | + completeSpanAndAnnotation: undefined, |
| 918 | + cpuIdleSpanAndAnnotation: undefined, |
| 919 | + lastRequiredSpanAndAnnotation: undefined, |
| 920 | + }, |
| 921 | + ) |
| 922 | + expect(recording.attributes.foo).toBe('owned') |
| 923 | + expect(recording.attributes.baz).toBe('shouldWin') |
| 924 | + }) |
| 925 | + }) |
755 | 926 | })
|
0 commit comments