File size: 4,472 Bytes
a8fb296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Every API request gets one `api.request` client span and carries a `traceparent` header, so the API's request
// span joins the browser's trace (#260). The span keeps the path and never the query string: an address search
// sends the typed address as `q`.
import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'
import { SpanStatusCode } from '@opentelemetry/api'
import {
  InMemorySpanExporter,
  SimpleSpanProcessor,
  WebTracerProvider,
} from '@opentelemetry/sdk-trace-web'

// Registered before the client (and the app's own instrumentation) is imported, so this provider holds the global
// slot and its spans land in memory.
const exporter = new InMemorySpanExporter()
new WebTracerProvider({ spanProcessors: [new SimpleSpanProcessor(exporter)] }).register()

type Sent = { url: string; headers: Headers }
const sent: Sent[] = []
let api: typeof import('./api').default
let tracedFetch: typeof import('./api').tracedFetch

beforeAll(async () => {
  vi.stubGlobal('localStorage', { getItem: () => null, removeItem: () => undefined })
  vi.stubGlobal('location', { origin: 'https://navigator.example' })
  vi.stubGlobal(
    'fetch',
    vi.fn(async (input: string | URL, init: RequestInit = {}) => {
      const url = String(input)
      sent.push({ url, headers: new Headers(init.headers) })
      if (url.includes('/offline')) throw new TypeError('Failed to fetch')
      const status = url.includes('/missing') ? 404 : 200
      return new Response(JSON.stringify({ ok: status === 200 }), {
        status,
        headers: { 'content-type': 'application/json' },
      })
    }),
  )
  ;({ default: api, tracedFetch } = await import('./api'))
})

afterEach(() => {
  exporter.reset()
  sent.length = 0
})

function onlySpan() {
  const spans = exporter.getFinishedSpans()
  expect(spans).toHaveLength(1)
  return spans[0]
}

describe('api.request spans', () => {
  it('records the method, the path and the status, and never the query string', async () => {
    await api.get('/addresses/search', { params: { q: '12 Elm Street' } })
    const span = onlySpan()
    expect(span.name).toBe('api.request')
    expect(span.attributes).toEqual({
      'http.request.method': 'GET',
      'url.path': '/api/addresses/search',
      'http.response.status_code': 200,
    })
    expect(JSON.stringify(span.attributes)).not.toMatch(/Elm|\?|q=/)
    expect(sent[0].url).toBe('/api/addresses/search?q=12+Elm+Street') // the request itself is unchanged
  })

  it("sends traceparent naming the request's own span, so the API joins the trace", async () => {
    await api.get('/states')
    const { traceId, spanId } = onlySpan().spanContext()
    expect(sent[0].headers.get('traceparent')).toBe(`00-${traceId}-${spanId}-01`)
  })

  it('sends no traceparent to another origin', async () => {
    await api.get('https://tiles.example.org/states.json')
    await tracedFetch('https://tiles.example.org/counties.json')
    expect(sent.map((s) => s.headers.get('traceparent'))).toEqual([null, null])
  })

  it('marks an error status as an error', async () => {
    await expect(api.get('/missing')).rejects.toMatchObject({ response: { status: 404 } })
    const span = onlySpan()
    expect(span.status.code).toBe(SpanStatusCode.ERROR)
    expect(span.attributes['http.response.status_code']).toBe(404)
  })

  it('ends the span as an error when the request never gets a response', async () => {
    await expect(api.get('/offline')).rejects.toThrow('Failed to fetch')
    const span = onlySpan()
    expect(span.status.code).toBe(SpanStatusCode.ERROR)
    expect(span.attributes).toEqual({ 'http.request.method': 'GET', 'url.path': '/api/offline' })
  })
})

describe('tracedFetch, for code that calls fetch directly', () => {
  it('traces an API call the same way and returns the response unchanged', async () => {
    const response = await tracedFetch('/api/batch-jobs/launch?dry_run=1', {
      method: 'post',
      headers: { 'Content-Type': 'application/json' },
    })
    expect(response.status).toBe(200)
    const span = onlySpan()
    expect(span.attributes).toEqual({
      'http.request.method': 'POST',
      'url.path': '/api/batch-jobs/launch',
      'http.response.status_code': 200,
    })
    const { traceId, spanId } = span.spanContext()
    expect(sent[0].headers.get('traceparent')).toBe(`00-${traceId}-${spanId}-01`)
    expect(sent[0].headers.get('content-type')).toBe('application/json') // the caller's headers are kept
  })
})