|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const assert = require('node:assert'); |
| 4 | +const { after, before, describe, it } = require('node:test'); |
| 5 | +// The registry of defined attribute names: the package's exports are the |
| 6 | +// attribute names the OpenTelemetry semantic conventions define. |
| 7 | +const semconv = require('@opentelemetry/semantic-conventions'); |
| 8 | +const { flush } = require('../lib/flush'); |
| 9 | +const { startTracing, startHTTPServer, httpGet } = require('./common/otel'); |
| 10 | + |
| 11 | +const registry = new Set( |
| 12 | + Object.values(semconv).filter((v) => typeof v === 'string'), |
| 13 | +); |
| 14 | + |
| 15 | +describe('otel semantic conventions', () => { |
| 16 | + let collector; |
| 17 | + |
| 18 | + before(async () => { |
| 19 | + collector = await startTracing(); |
| 20 | + }); |
| 21 | + |
| 22 | + after(() => collector.close()); |
| 23 | + |
| 24 | + it('emits only attribute names the conventions define', async () => { |
| 25 | + // One trace through every instrumentation shape: a server span with |
| 26 | + // its query attribute, http and fetch client spans, a failed request |
| 27 | + // for the error attributes, and a refused connection for the |
| 28 | + // exception event attributes. |
| 29 | + const ok = await startHTTPServer(); |
| 30 | + const failing = await startHTTPServer((req, res) => { |
| 31 | + res.writeHead(500); |
| 32 | + res.end(); |
| 33 | + }); |
| 34 | + |
| 35 | + await httpGet(ok.port, '/semconv?token=1'); |
| 36 | + await fetch(`http://127.0.0.1:${ok.port}/fetch`) |
| 37 | + .then((res) => res.text()) |
| 38 | + .catch(() => {}); |
| 39 | + await httpGet(failing.port, '/fail'); |
| 40 | + await httpGet(1, '/refused').catch(() => {}); |
| 41 | + |
| 42 | + flush(); |
| 43 | + const payload = await collector.next(); |
| 44 | + |
| 45 | + await ok.close(); |
| 46 | + await failing.close(); |
| 47 | + |
| 48 | + const spans = payload.resourceSpans[0].scopeSpans[0].spans; |
| 49 | + assert.ok( |
| 50 | + spans.some((s) => s.kind === 2), |
| 51 | + 'Expected a server span', |
| 52 | + ); |
| 53 | + assert.ok( |
| 54 | + spans.some((s) => s.kind === 3), |
| 55 | + 'Expected client spans', |
| 56 | + ); |
| 57 | + |
| 58 | + const assertDefined = (key, where) => |
| 59 | + assert.ok( |
| 60 | + registry.has(key), |
| 61 | + `${where} attribute "${key}" is not defined by the ` + |
| 62 | + 'semantic conventions', |
| 63 | + ); |
| 64 | + |
| 65 | + for (const span of spans) { |
| 66 | + for (const attr of span.attributes ?? []) { |
| 67 | + assertDefined(attr.key, 'span'); |
| 68 | + } |
| 69 | + for (const event of span.events ?? []) { |
| 70 | + for (const attr of event.attributes ?? []) { |
| 71 | + assertDefined(attr.key, 'event'); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + for (const attr of payload.resourceSpans[0].resource.attributes) { |
| 77 | + assertDefined(attr.key, 'resource'); |
| 78 | + } |
| 79 | + }); |
| 80 | +}); |
0 commit comments