|
1 | | -import type { SpanJSON, TransactionEvent } from '@sentry/core'; |
| 1 | +import type { SpanJSON, StreamedSpanJSON, TransactionEvent } from '@sentry/core'; |
2 | 2 | import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core'; |
3 | 3 | import { describe, expect, it } from 'vitest'; |
4 | | -import { _enhanceKitSpan, svelteKitSpansIntegration } from '../../../src/server-common/integrations/svelteKitSpans'; |
| 4 | +import { |
| 5 | + _enhanceKitSpan, |
| 6 | + _enhanceKitSpanStreamed, |
| 7 | + svelteKitSpansIntegration, |
| 8 | +} from '../../../src/server-common/integrations/svelteKitSpans'; |
5 | 9 |
|
6 | 10 | describe('svelteKitSpansIntegration', () => { |
7 | | - it('has a name and a preprocessEventHook', () => { |
| 11 | + it('has a name and a preprocessEvent and processSpan hook', () => { |
8 | 12 | const integration = svelteKitSpansIntegration(); |
9 | 13 |
|
10 | 14 | expect(integration.name).toBe('SvelteKitSpansEnhancement'); |
11 | 15 | expect(typeof integration.preprocessEvent).toBe('function'); |
| 16 | + expect(typeof integration.processSpan).toBe('function'); |
12 | 17 | }); |
13 | 18 |
|
14 | 19 | it('enhances spans from SvelteKit', () => { |
@@ -169,4 +174,106 @@ describe('svelteKitSpansIntegration', () => { |
169 | 174 | expect(span.data[SEMANTIC_ATTRIBUTE_SENTRY_OP]).toBe('custom.op'); |
170 | 175 | }); |
171 | 176 | }); |
| 177 | + |
| 178 | + describe('_enhanceKitSpanStreamed', () => { |
| 179 | + function makeStreamedSpan(overrides: Partial<StreamedSpanJSON> = {}): StreamedSpanJSON { |
| 180 | + return { |
| 181 | + name: 'unspecified', |
| 182 | + span_id: '123', |
| 183 | + trace_id: 'abc', |
| 184 | + start_timestamp: 0, |
| 185 | + end_timestamp: 1, |
| 186 | + status: 'ok', |
| 187 | + is_segment: false, |
| 188 | + attributes: {}, |
| 189 | + ...overrides, |
| 190 | + }; |
| 191 | + } |
| 192 | + |
| 193 | + it.each([ |
| 194 | + ['sveltekit.resolve', 'function.sveltekit.resolve', 'auto.http.sveltekit'], |
| 195 | + ['sveltekit.load', 'function.sveltekit.load', 'auto.function.sveltekit.load'], |
| 196 | + ['sveltekit.form_action', 'function.sveltekit.form_action', 'auto.function.sveltekit.action'], |
| 197 | + ['sveltekit.remote.call', 'function.sveltekit.remote', 'auto.rpc.sveltekit.remote'], |
| 198 | + ['sveltekit.handle.sequenced.0', 'function.sveltekit.handle', 'auto.function.sveltekit.handle'], |
| 199 | + ['sveltekit.handle.sequenced.myHandler', 'function.sveltekit.handle', 'auto.function.sveltekit.handle'], |
| 200 | + ])('enhances %s span with the correct op and origin', (spanName, op, origin) => { |
| 201 | + const span = makeStreamedSpan({ name: spanName, attributes: { someAttribute: 'someValue' } }); |
| 202 | + |
| 203 | + _enhanceKitSpanStreamed(span); |
| 204 | + |
| 205 | + expect(span.attributes?.[SEMANTIC_ATTRIBUTE_SENTRY_OP]).toBe(op); |
| 206 | + expect(span.attributes?.[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]).toBe(origin); |
| 207 | + }); |
| 208 | + |
| 209 | + it("doesn't change spans from other origins", () => { |
| 210 | + const span = makeStreamedSpan({ name: 'someOtherSpan' }); |
| 211 | + |
| 212 | + _enhanceKitSpanStreamed(span); |
| 213 | + |
| 214 | + expect(span.attributes?.[SEMANTIC_ATTRIBUTE_SENTRY_OP]).toBeUndefined(); |
| 215 | + expect(span.attributes?.[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]).toBeUndefined(); |
| 216 | + }); |
| 217 | + |
| 218 | + it("doesn't overwrite the sveltekit.handle.root span", () => { |
| 219 | + const rootHandleSpan = makeStreamedSpan({ |
| 220 | + name: 'sveltekit.handle.root', |
| 221 | + attributes: { |
| 222 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'http.server', |
| 223 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.sveltekit', |
| 224 | + }, |
| 225 | + }); |
| 226 | + |
| 227 | + _enhanceKitSpanStreamed(rootHandleSpan); |
| 228 | + |
| 229 | + expect(rootHandleSpan.attributes?.[SEMANTIC_ATTRIBUTE_SENTRY_OP]).toBe('http.server'); |
| 230 | + expect(rootHandleSpan.attributes?.[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]).toBe('auto.http.sveltekit'); |
| 231 | + }); |
| 232 | + |
| 233 | + it("doesn't enhance unrelated spans", () => { |
| 234 | + const span = makeStreamedSpan({ |
| 235 | + name: 'someOtherSpan', |
| 236 | + attributes: { |
| 237 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'db', |
| 238 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.db.pg', |
| 239 | + }, |
| 240 | + }); |
| 241 | + |
| 242 | + _enhanceKitSpanStreamed(span); |
| 243 | + |
| 244 | + expect(span.attributes?.[SEMANTIC_ATTRIBUTE_SENTRY_OP]).toBe('db'); |
| 245 | + expect(span.attributes?.[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]).toBe('auto.db.pg'); |
| 246 | + }); |
| 247 | + |
| 248 | + it("doesn't overwrite already set ops or origins on sveltekit spans", () => { |
| 249 | + // for example, if users manually set this (for whatever reason) |
| 250 | + const span = makeStreamedSpan({ |
| 251 | + name: 'sveltekit.resolve', |
| 252 | + attributes: { |
| 253 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'custom.op', |
| 254 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.custom.origin', |
| 255 | + }, |
| 256 | + }); |
| 257 | + |
| 258 | + _enhanceKitSpanStreamed(span); |
| 259 | + |
| 260 | + expect(span.attributes?.[SEMANTIC_ATTRIBUTE_SENTRY_OP]).toBe('custom.op'); |
| 261 | + expect(span.attributes?.[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]).toBe('auto.custom.origin'); |
| 262 | + }); |
| 263 | + |
| 264 | + it('overwrites previously set "manual" origins on sveltekit spans', () => { |
| 265 | + const span = makeStreamedSpan({ |
| 266 | + name: 'sveltekit.resolve', |
| 267 | + attributes: { |
| 268 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'custom.op', |
| 269 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'manual', |
| 270 | + }, |
| 271 | + }); |
| 272 | + |
| 273 | + _enhanceKitSpanStreamed(span); |
| 274 | + |
| 275 | + expect(span.attributes?.[SEMANTIC_ATTRIBUTE_SENTRY_OP]).toBe('custom.op'); |
| 276 | + expect(span.attributes?.[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]).toBe('auto.http.sveltekit'); |
| 277 | + }); |
| 278 | + }); |
172 | 279 | }); |
0 commit comments