|
1 | 1 | import type { Span } from '@opentelemetry/api'; |
| 2 | +import { SpanKind } from '@opentelemetry/api'; |
2 | 3 | import { TraceFlags, context, trace } from '@opentelemetry/api'; |
3 | 4 | import type { ReadableSpan } from '@opentelemetry/sdk-trace-base'; |
4 | 5 | import type { PropagationContext } from '@sentry/types'; |
5 | 6 |
|
6 | 7 | import { getCurrentHub } from '../src/custom/hub'; |
7 | 8 | import { InternalSentrySemanticAttributes } from '../src/semanticAttributes'; |
8 | | -import { startInactiveSpan, startSpan } from '../src/trace'; |
| 9 | +import { startInactiveSpan, startSpan, startSpanManual } from '../src/trace'; |
9 | 10 | import type { AbstractSpan } from '../src/types'; |
10 | 11 | import { setPropagationContextOnContext } from '../src/utils/contextData'; |
11 | 12 | import { getActiveSpan, getRootSpan } from '../src/utils/getActiveSpan'; |
| 13 | +import { getSpanKind } from '../src/utils/getSpanKind'; |
12 | 14 | import { getSpanMetadata } from '../src/utils/spanData'; |
13 | 15 | import { spanHasAttributes, spanHasName } from '../src/utils/spanTypes'; |
14 | 16 | import { cleanupOtel, mockSdkInit } from './helpers/mockSdkInit'; |
@@ -231,6 +233,33 @@ describe('trace', () => { |
231 | 233 | }, |
232 | 234 | ); |
233 | 235 | }); |
| 236 | + |
| 237 | + it('allows to pass base SpanOptions', () => { |
| 238 | + const date = Date.now() - 1000; |
| 239 | + |
| 240 | + startSpan( |
| 241 | + { |
| 242 | + name: 'outer', |
| 243 | + kind: SpanKind.CLIENT, |
| 244 | + attributes: { |
| 245 | + test1: 'test 1', |
| 246 | + test2: 2, |
| 247 | + }, |
| 248 | + |
| 249 | + startTime: date, |
| 250 | + }, |
| 251 | + span => { |
| 252 | + expect(span).toBeDefined(); |
| 253 | + expect(getSpanName(span)).toEqual('outer'); |
| 254 | + expect(getSpanAttributes(span)).toEqual({ |
| 255 | + [InternalSentrySemanticAttributes.SAMPLE_RATE]: 1, |
| 256 | + test1: 'test 1', |
| 257 | + test2: 2, |
| 258 | + }); |
| 259 | + expect(getSpanKind(span)).toEqual(SpanKind.CLIENT); |
| 260 | + }, |
| 261 | + ); |
| 262 | + }); |
234 | 263 | }); |
235 | 264 |
|
236 | 265 | describe('startInactiveSpan', () => { |
@@ -297,6 +326,98 @@ describe('trace', () => { |
297 | 326 |
|
298 | 327 | expect(getSpanMetadata(span2)).toEqual({ requestPath: 'test-path' }); |
299 | 328 | }); |
| 329 | + |
| 330 | + it('allows to pass base SpanOptions', () => { |
| 331 | + const date = Date.now() - 1000; |
| 332 | + |
| 333 | + const span = startInactiveSpan({ |
| 334 | + name: 'outer', |
| 335 | + kind: SpanKind.CLIENT, |
| 336 | + attributes: { |
| 337 | + test1: 'test 1', |
| 338 | + test2: 2, |
| 339 | + }, |
| 340 | + startTime: date, |
| 341 | + }); |
| 342 | + |
| 343 | + expect(span).toBeDefined(); |
| 344 | + expect(getSpanName(span)).toEqual('outer'); |
| 345 | + expect(getSpanAttributes(span)).toEqual({ |
| 346 | + [InternalSentrySemanticAttributes.SAMPLE_RATE]: 1, |
| 347 | + test1: 'test 1', |
| 348 | + test2: 2, |
| 349 | + }); |
| 350 | + expect(getSpanKind(span)).toEqual(SpanKind.CLIENT); |
| 351 | + }); |
| 352 | + }); |
| 353 | + |
| 354 | + describe('startSpanManual', () => { |
| 355 | + it('does not automatically finish the span', () => { |
| 356 | + expect(getActiveSpan()).toEqual(undefined); |
| 357 | + |
| 358 | + let _outerSpan: Span | undefined; |
| 359 | + let _innerSpan: Span | undefined; |
| 360 | + |
| 361 | + const res = startSpanManual({ name: 'outer' }, outerSpan => { |
| 362 | + expect(outerSpan).toBeDefined(); |
| 363 | + _outerSpan = outerSpan; |
| 364 | + |
| 365 | + expect(getSpanName(outerSpan)).toEqual('outer'); |
| 366 | + expect(getActiveSpan()).toEqual(outerSpan); |
| 367 | + |
| 368 | + startSpanManual({ name: 'inner' }, innerSpan => { |
| 369 | + expect(innerSpan).toBeDefined(); |
| 370 | + _innerSpan = innerSpan; |
| 371 | + |
| 372 | + expect(getSpanName(innerSpan)).toEqual('inner'); |
| 373 | + expect(getActiveSpan()).toEqual(innerSpan); |
| 374 | + }); |
| 375 | + |
| 376 | + expect(getSpanEndTime(_innerSpan!)).toEqual([0, 0]); |
| 377 | + |
| 378 | + _innerSpan!.end(); |
| 379 | + |
| 380 | + expect(getSpanEndTime(_innerSpan!)).not.toEqual([0, 0]); |
| 381 | + |
| 382 | + return 'test value'; |
| 383 | + }); |
| 384 | + |
| 385 | + expect(getSpanEndTime(_outerSpan!)).toEqual([0, 0]); |
| 386 | + |
| 387 | + _outerSpan!.end(); |
| 388 | + |
| 389 | + expect(getSpanEndTime(_outerSpan!)).not.toEqual([0, 0]); |
| 390 | + |
| 391 | + expect(res).toEqual('test value'); |
| 392 | + |
| 393 | + expect(getActiveSpan()).toEqual(undefined); |
| 394 | + }); |
| 395 | + |
| 396 | + it('allows to pass base SpanOptions', () => { |
| 397 | + const date = Date.now() - 1000; |
| 398 | + |
| 399 | + startSpanManual( |
| 400 | + { |
| 401 | + name: 'outer', |
| 402 | + kind: SpanKind.CLIENT, |
| 403 | + attributes: { |
| 404 | + test1: 'test 1', |
| 405 | + test2: 2, |
| 406 | + }, |
| 407 | + startTime: date, |
| 408 | + }, |
| 409 | + span => { |
| 410 | + expect(span).toBeDefined(); |
| 411 | + expect(getSpanName(span)).toEqual('outer'); |
| 412 | + expect(getSpanAttributes(span)).toEqual({ |
| 413 | + [InternalSentrySemanticAttributes.SAMPLE_RATE]: 1, |
| 414 | + test1: 'test 1', |
| 415 | + test2: 2, |
| 416 | + }); |
| 417 | + expect(getSpanKind(span)).toEqual(SpanKind.CLIENT); |
| 418 | + }, |
| 419 | + ); |
| 420 | + }); |
300 | 421 | }); |
301 | 422 | }); |
302 | 423 |
|
|
0 commit comments