|
| 1 | +import { buildResourceAttributes } from '../logs/logs-utils' |
| 2 | +import type { ResolvedPostHogLogsConfig } from '../logs/types' |
| 3 | +import { buildMetricsResourceAttributes } from '../metrics/metrics-utils' |
| 4 | +import type { ResolvedPostHogMetricsConfig } from '../metrics/types' |
| 5 | +import { buildTracesResourceAttributes } from '../traces/otlp' |
| 6 | +import type { ResolvedTracesConfig } from '../traces/types' |
| 7 | + |
| 8 | +const shared = { |
| 9 | + serviceName: 'checkout', |
| 10 | + serviceVersion: '2.1.0', |
| 11 | + environment: 'production', |
| 12 | + resourceAttributes: { 'host.name': 'web-01' }, |
| 13 | +} |
| 14 | + |
| 15 | +const conflicting = { |
| 16 | + serviceName: 'checkout', |
| 17 | + serviceVersion: '2.1.0', |
| 18 | + environment: 'production', |
| 19 | + resourceAttributes: { |
| 20 | + 'service.name': 'hijacked', |
| 21 | + 'service.version': '0.0.0', |
| 22 | + 'deployment.environment': 'hijacked-env', |
| 23 | + 'telemetry.sdk.name': 'hijacked-sdk', |
| 24 | + 'telemetry.sdk.version': '0.0.0', |
| 25 | + 'host.name': 'web-01', |
| 26 | + }, |
| 27 | +} |
| 28 | + |
| 29 | +const allThree = (partial: object): Record<string, unknown>[] => [ |
| 30 | + buildResourceAttributes(partial as ResolvedPostHogLogsConfig, 'posthog-node', '1.0.0'), |
| 31 | + buildMetricsResourceAttributes(partial as ResolvedPostHogMetricsConfig, 'posthog-node', '1.0.0'), |
| 32 | + buildTracesResourceAttributes(partial as ResolvedTracesConfig, 'posthog-node', '1.0.0'), |
| 33 | +] |
| 34 | + |
| 35 | +describe('shared OTLP resource attributes', () => { |
| 36 | + it.each([ |
| 37 | + ['a fully populated config', shared], |
| 38 | + ['a config with conflicting user attributes', conflicting], |
| 39 | + ['an empty config', {}], |
| 40 | + ])('produces the same attributes for logs, metrics and traces given %s', (_label, config) => { |
| 41 | + const [logs, metrics, traces] = allThree(config) |
| 42 | + expect(metrics).toEqual(logs) |
| 43 | + expect(traces).toEqual(logs) |
| 44 | + expect(Object.keys(metrics)).toEqual(Object.keys(logs)) |
| 45 | + expect(Object.keys(traces)).toEqual(Object.keys(logs)) |
| 46 | + }) |
| 47 | + |
| 48 | + it('layers the identity keys over user resource attributes', () => { |
| 49 | + for (const attributes of allThree(conflicting)) { |
| 50 | + expect(attributes).toEqual({ |
| 51 | + 'service.name': 'checkout', |
| 52 | + 'service.version': '2.1.0', |
| 53 | + 'deployment.environment': 'production', |
| 54 | + 'telemetry.sdk.name': 'posthog-node', |
| 55 | + 'telemetry.sdk.version': '1.0.0', |
| 56 | + 'host.name': 'web-01', |
| 57 | + }) |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + it('keeps user resource attributes that do not collide', () => { |
| 62 | + for (const attributes of allThree(shared)) { |
| 63 | + expect(attributes).toEqual({ |
| 64 | + 'host.name': 'web-01', |
| 65 | + 'service.name': 'checkout', |
| 66 | + 'deployment.environment': 'production', |
| 67 | + 'service.version': '2.1.0', |
| 68 | + 'telemetry.sdk.name': 'posthog-node', |
| 69 | + 'telemetry.sdk.version': '1.0.0', |
| 70 | + }) |
| 71 | + } |
| 72 | + }) |
| 73 | + |
| 74 | + it('falls back to unknown_service and omits unset optional keys', () => { |
| 75 | + for (const attributes of allThree({})) { |
| 76 | + expect(attributes).toEqual({ |
| 77 | + 'service.name': 'unknown_service', |
| 78 | + 'telemetry.sdk.name': 'posthog-node', |
| 79 | + 'telemetry.sdk.version': '1.0.0', |
| 80 | + }) |
| 81 | + } |
| 82 | + }) |
| 83 | +}) |
0 commit comments