|
1 |
| -import type { DsnComponents, DynamicSamplingContext, Event } from '@sentry/types'; |
| 1 | +import type { Client, DsnComponents, DynamicSamplingContext, Event } from '@sentry/types'; |
2 | 2 |
|
3 |
| -import { createEventEnvelope } from '../../src/envelope'; |
| 3 | +import { |
| 4 | + SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, |
| 5 | + SentrySpan, |
| 6 | + getCurrentScope, |
| 7 | + getIsolationScope, |
| 8 | + setAsyncContextStrategy, |
| 9 | + setCurrentClient, |
| 10 | +} from '../../src'; |
| 11 | +import { createEventEnvelope, createSpanEnvelope } from '../../src/envelope'; |
| 12 | +import { TestClient, getDefaultTestClientOptions } from '../mocks/client'; |
4 | 13 |
|
5 | 14 | const testDsn: DsnComponents = { protocol: 'https', projectId: 'abc', host: 'testry.io', publicKey: 'pubKey123' };
|
6 | 15 |
|
@@ -75,3 +84,77 @@ describe('createEventEnvelope', () => {
|
75 | 84 | });
|
76 | 85 | });
|
77 | 86 | });
|
| 87 | + |
| 88 | +describe('createSpanEnvelope', () => { |
| 89 | + let client: Client | undefined; |
| 90 | + beforeEach(() => { |
| 91 | + getCurrentScope().clear(); |
| 92 | + getIsolationScope().clear(); |
| 93 | + setAsyncContextStrategy(undefined); |
| 94 | + const options = getDefaultTestClientOptions({ tracesSampleRate: 1, dsn: 'https://username@domain/123' }); |
| 95 | + client = new TestClient(options); |
| 96 | + setCurrentClient(client); |
| 97 | + client.init(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('creates a span envelope', () => { |
| 101 | + const span = new SentrySpan({ |
| 102 | + name: 'test', |
| 103 | + isStandalone: true, |
| 104 | + startTimestamp: 1, |
| 105 | + endTimestamp: 2, |
| 106 | + sampled: true, |
| 107 | + attributes: { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom' }, |
| 108 | + }); |
| 109 | + |
| 110 | + const spanEnvelope = createSpanEnvelope([span]); |
| 111 | + |
| 112 | + const spanItem = spanEnvelope[1][0][1]; |
| 113 | + expect(spanItem).toEqual({ |
| 114 | + data: { |
| 115 | + 'sentry.origin': 'manual', |
| 116 | + 'sentry.source': 'custom', |
| 117 | + }, |
| 118 | + description: 'test', |
| 119 | + is_segment: true, |
| 120 | + origin: 'manual', |
| 121 | + span_id: expect.stringMatching(/^[0-9a-f]{16}$/), |
| 122 | + segment_id: spanItem.segment_id, |
| 123 | + start_timestamp: 1, |
| 124 | + timestamp: 2, |
| 125 | + trace_id: expect.stringMatching(/^[0-9a-f]{32}$/), |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + it('adds `trace` and `sent_at` envelope headers', () => { |
| 130 | + const spanEnvelope = createSpanEnvelope([ |
| 131 | + new SentrySpan({ name: 'test', attributes: { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom' } }), |
| 132 | + ]); |
| 133 | + |
| 134 | + const spanEnvelopeHeaders = spanEnvelope[0]; |
| 135 | + expect(spanEnvelopeHeaders).toEqual({ |
| 136 | + sent_at: expect.any(String), |
| 137 | + trace: { |
| 138 | + environment: 'production', |
| 139 | + public_key: 'username', |
| 140 | + sampled: 'false', |
| 141 | + trace_id: expect.stringMatching(/^[0-9a-f]{32}$/), |
| 142 | + transaction: 'test', |
| 143 | + }, |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + it("doesn't add a `trace` envelope header if there's no public key", () => { |
| 148 | + const options = getDefaultTestClientOptions({ tracesSampleRate: 1, dsn: 'https://domain/123' }); |
| 149 | + client = new TestClient(options); |
| 150 | + setCurrentClient(client); |
| 151 | + client.init(); |
| 152 | + |
| 153 | + const spanEnvelope = createSpanEnvelope([new SentrySpan()]); |
| 154 | + |
| 155 | + const spanEnvelopeHeaders = spanEnvelope[0]; |
| 156 | + expect(spanEnvelopeHeaders).toEqual({ |
| 157 | + sent_at: expect.any(String), |
| 158 | + }); |
| 159 | + }); |
| 160 | +}); |
0 commit comments