|
1 | | -import { trace } from '@opentelemetry/api' |
| 1 | +import { SpanStatusCode, trace } from '@opentelemetry/api' |
2 | 2 | import { beforeEach, describe, expect, test, vi } from 'vitest' |
3 | 3 |
|
4 | 4 | import { |
| 5 | + ATTRIBUTES_EXCEPTION_FINGERPRINT_KEY, |
5 | 6 | ATTRIBUTES_LEVEL_KEY, |
6 | 7 | ATTRIBUTES_MESSAGE_KEY, |
7 | 8 | ATTRIBUTES_MESSAGE_TEMPLATE_KEY, |
8 | 9 | ATTRIBUTES_SPAN_TYPE_KEY, |
9 | 10 | ATTRIBUTES_TAGS_KEY, |
10 | 11 | } from './constants' |
11 | | -import { info } from './index' |
| 12 | +import { info, span } from './index' |
12 | 13 |
|
13 | | -vi.mock('@opentelemetry/api', () => { |
| 14 | +const { spanMock } = vi.hoisted(() => { |
14 | 15 | const spanMock = { |
15 | 16 | end: vi.fn(), |
| 17 | + recordException: vi.fn(), |
16 | 18 | setAttribute: vi.fn(), |
17 | 19 | setStatus: vi.fn(), |
18 | 20 | } |
| 21 | + return { spanMock } |
| 22 | +}) |
19 | 23 |
|
| 24 | +vi.mock('@opentelemetry/api', () => { |
20 | 25 | const tracerMock = { |
| 26 | + startActiveSpan: vi.fn((_name: string, _options: unknown, _context: unknown, fn: (s: typeof spanMock) => unknown) => fn(spanMock)), |
21 | 27 | startSpan: vi.fn(() => spanMock), |
22 | 28 | } |
23 | 29 |
|
24 | 30 | return { |
25 | 31 | context: { |
26 | 32 | active: vi.fn(), |
27 | 33 | }, |
| 34 | + SpanStatusCode: { ERROR: 2 }, |
28 | 35 | trace: { |
29 | 36 | getTracer: vi.fn(() => tracerMock), |
| 37 | + setSpan: vi.fn(), |
30 | 38 | }, |
31 | 39 | } |
32 | 40 | }) |
@@ -80,3 +88,105 @@ describe('info', () => { |
80 | 88 | ) |
81 | 89 | }) |
82 | 90 | }) |
| 91 | + |
| 92 | +describe('span', () => { |
| 93 | + beforeEach(() => { |
| 94 | + vi.clearAllMocks() |
| 95 | + }) |
| 96 | + |
| 97 | + test('sync callback succeeds - span ends normally', () => { |
| 98 | + const result = span('test {x}', { attributes: { x: 1 }, callback: () => 'ok' }) |
| 99 | + |
| 100 | + expect(result).toBe('ok') |
| 101 | + expect(spanMock.end).toHaveBeenCalledOnce() |
| 102 | + expect(spanMock.recordException).not.toHaveBeenCalled() |
| 103 | + expect(spanMock.setStatus).not.toHaveBeenCalled() |
| 104 | + }) |
| 105 | + |
| 106 | + test('sync callback throws Error - records exception and re-throws', () => { |
| 107 | + const error = new Error('boom') |
| 108 | + |
| 109 | + expect(() => |
| 110 | + span('test {x}', { |
| 111 | + attributes: { x: 1 }, |
| 112 | + callback: () => { |
| 113 | + throw error |
| 114 | + }, |
| 115 | + }) |
| 116 | + ).toThrow(error) |
| 117 | + |
| 118 | + expect(spanMock.recordException).toHaveBeenCalledWith(error) |
| 119 | + expect(spanMock.setStatus).toHaveBeenCalledWith({ code: SpanStatusCode.ERROR, message: 'Error: boom' }) |
| 120 | + expect(spanMock.setAttribute).toHaveBeenCalledWith(ATTRIBUTES_LEVEL_KEY, 17) |
| 121 | + expect(spanMock.setAttribute).toHaveBeenCalledWith(ATTRIBUTES_EXCEPTION_FINGERPRINT_KEY, expect.any(String)) |
| 122 | + expect(spanMock.end).toHaveBeenCalledOnce() |
| 123 | + }) |
| 124 | + |
| 125 | + test('sync callback throws string - records exception without fingerprint', () => { |
| 126 | + expect(() => |
| 127 | + span('test', { |
| 128 | + callback: () => { |
| 129 | + // eslint-disable-next-line no-throw-literal, @typescript-eslint/only-throw-error |
| 130 | + throw 'oops' |
| 131 | + }, |
| 132 | + }) |
| 133 | + ).toThrow('oops') |
| 134 | + |
| 135 | + expect(spanMock.recordException).toHaveBeenCalledWith('oops') |
| 136 | + expect(spanMock.setStatus).toHaveBeenCalledWith({ code: SpanStatusCode.ERROR, message: 'Error: oops' }) |
| 137 | + expect(spanMock.setAttribute).toHaveBeenCalledWith(ATTRIBUTES_LEVEL_KEY, 17) |
| 138 | + expect(spanMock.setAttribute).not.toHaveBeenCalledWith(ATTRIBUTES_EXCEPTION_FINGERPRINT_KEY, expect.anything()) |
| 139 | + expect(spanMock.end).toHaveBeenCalledOnce() |
| 140 | + }) |
| 141 | + |
| 142 | + test('async callback resolves - span ends normally', async () => { |
| 143 | + const result = span('test', { callback: () => Promise.resolve('async-ok') }) |
| 144 | + await expect(result).resolves.toBe('async-ok') |
| 145 | + |
| 146 | + expect(spanMock.end).toHaveBeenCalledOnce() |
| 147 | + expect(spanMock.recordException).not.toHaveBeenCalled() |
| 148 | + expect(spanMock.setStatus).not.toHaveBeenCalled() |
| 149 | + }) |
| 150 | + |
| 151 | + test('async callback rejects with Error - records exception', async () => { |
| 152 | + const error = new Error('async-boom') |
| 153 | + const result = span('test', { callback: () => Promise.reject(error) }) |
| 154 | + |
| 155 | + await expect(result).rejects.toThrow(error) |
| 156 | + |
| 157 | + // Allow microtask for the .then() handler to run |
| 158 | + await new Promise((resolve) => setTimeout(resolve, 0)) |
| 159 | + |
| 160 | + expect(spanMock.recordException).toHaveBeenCalledWith(error) |
| 161 | + expect(spanMock.setStatus).toHaveBeenCalledWith({ code: SpanStatusCode.ERROR, message: 'Error: async-boom' }) |
| 162 | + expect(spanMock.setAttribute).toHaveBeenCalledWith(ATTRIBUTES_LEVEL_KEY, 17) |
| 163 | + expect(spanMock.setAttribute).toHaveBeenCalledWith(ATTRIBUTES_EXCEPTION_FINGERPRINT_KEY, expect.any(String)) |
| 164 | + expect(spanMock.end).toHaveBeenCalledOnce() |
| 165 | + }) |
| 166 | + |
| 167 | + test('async callback rejects with string - records exception without fingerprint', async () => { |
| 168 | + // eslint-disable-next-line prefer-promise-reject-errors, @typescript-eslint/prefer-promise-reject-errors |
| 169 | + const result = span('test', { callback: () => Promise.reject('async-oops') }) |
| 170 | + |
| 171 | + await expect(result).rejects.toBe('async-oops') |
| 172 | + |
| 173 | + await new Promise((resolve) => setTimeout(resolve, 0)) |
| 174 | + |
| 175 | + expect(spanMock.recordException).toHaveBeenCalledWith('async-oops') |
| 176 | + expect(spanMock.setStatus).toHaveBeenCalledWith({ code: SpanStatusCode.ERROR, message: 'Error: async-oops' }) |
| 177 | + expect(spanMock.setAttribute).toHaveBeenCalledWith(ATTRIBUTES_LEVEL_KEY, 17) |
| 178 | + expect(spanMock.setAttribute).not.toHaveBeenCalledWith(ATTRIBUTES_EXCEPTION_FINGERPRINT_KEY, expect.anything()) |
| 179 | + expect(spanMock.end).toHaveBeenCalledOnce() |
| 180 | + }) |
| 181 | + |
| 182 | + test('thenable callback result is returned untouched', () => { |
| 183 | + const then = vi.fn() |
| 184 | + const lazyThenable = { then } |
| 185 | + |
| 186 | + const result = span('test', { callback: () => lazyThenable }) |
| 187 | + |
| 188 | + expect(result).toBe(lazyThenable) |
| 189 | + expect(then).not.toHaveBeenCalled() |
| 190 | + expect(spanMock.end).toHaveBeenCalledOnce() |
| 191 | + }) |
| 192 | +}) |
0 commit comments