11import { context , ROOT_CONTEXT , trace , TraceFlags } from '@opentelemetry/api' ;
22import { SeverityNumber } from '@opentelemetry/api-logs' ;
3- import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http' ;
4- import { InMemoryLogRecordExporter } from '@opentelemetry/sdk-logs' ;
53import os from 'os' ;
64
75import { sendUsageStats } from '@/tasks/usageStats' ;
86
9- const mockExporter = new InMemoryLogRecordExporter ( ) ;
7+ const USAGE_STATS_URL = 'https://in-otel.hyperdx.io/v1/logs' ;
8+ const USAGE_STATS_API_KEY = '3f26ffad-14cf-4fb7-9dc9-e64fa0b84ee0' ;
109
11- jest . mock ( '@opentelemetry/exporter-logs-otlp-http' , ( ) => ( {
12- OTLPLogExporter : jest . fn ( ( ) => mockExporter ) ,
13- } ) ) ;
10+ // jest.setup.ts replaces the global fetch with a jest.fn.
11+ const mockFetch = jest . mocked ( fetch ) ;
12+
13+ const sentRequest = async ( ) => {
14+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 ) ;
15+ const [ url , init ] = mockFetch . mock . calls [ 0 ] ;
16+ return { url, init, body : await new Response ( init ?. body ) . json ( ) } ;
17+ } ;
1418
1519describe ( 'sendUsageStats' , ( ) => {
20+ const originalEnv = { ...process . env } ;
21+
22+ beforeAll ( ( ) => {
23+ // Headers meant for the operator's collector, which must not reach
24+ // HyperDX.
25+ process . env . OTEL_EXPORTER_OTLP_HEADERS =
26+ 'x-secret=1,Authorization=self-key' ;
27+ process . env . OTEL_EXPORTER_OTLP_LOGS_HEADERS = 'x-logs=2' ;
28+ } ) ;
29+
1630 beforeEach ( ( ) => {
17- mockExporter . reset ( ) ;
31+ mockFetch . mockReset ( ) ;
32+ mockFetch . mockResolvedValue ( new Response ( null , { status : 200 } ) ) ;
1833 } ) ;
1934
2035 afterEach ( ( ) => {
2136 jest . restoreAllMocks ( ) ;
2237 } ) ;
2338
24- it ( 'exports one record to the usage stats endpoint with its own key' , async ( ) => {
39+ afterAll ( ( ) => {
40+ process . env = originalEnv ;
41+ } ) ;
42+
43+ it ( 'posts one record to the usage stats endpoint with only its own key' , async ( ) => {
44+ const timeoutSpy = jest . spyOn ( AbortSignal , 'timeout' ) ;
2545 // A traced caller, so the record would pick up its span without an
2646 // explicit context.
2747 jest . spyOn ( context , 'active' ) . mockReturnValue (
@@ -35,47 +55,81 @@ describe('sendUsageStats', () => {
3555 await sendUsageStats ( {
3656 clusterId : 'cluster-1' ,
3757 userCounts : 2 ,
38- chStats : { tables : { otel_logs : { rows : 10 } } , rows : 10 } ,
58+ chStats : { rows : 10 } ,
3959 } ) ;
4060
41- expect ( OTLPLogExporter ) . toHaveBeenCalledWith (
42- expect . objectContaining ( {
43- url : 'https://in-otel.hyperdx.io/v1/logs' ,
44- headers : { authorization : expect . any ( String ) } ,
45- } ) ,
46- ) ;
61+ const { url, init, body } = await sentRequest ( ) ;
62+ expect ( url ) . toBe ( USAGE_STATS_URL ) ;
63+ expect ( init ?. method ) . toBe ( 'POST' ) ;
64+ expect ( init ?. headers ) . toEqual ( {
65+ authorization : USAGE_STATS_API_KEY ,
66+ 'content-type' : 'application/json' ,
67+ } ) ;
68+ expect ( timeoutSpy ) . toHaveBeenCalledWith ( 10_000 ) ;
4769
48- const records = mockExporter . getFinishedLogRecords ( ) ;
49- expect ( records ) . toHaveLength ( 1 ) ;
50- const [ record ] = records ;
51- expect ( record . body ) . toBe ( 'track-hyperdx-oss-usage-stats' ) ;
70+ expect ( body . resourceLogs ) . toHaveLength ( 1 ) ;
71+ const [ resourceLogs ] = body . resourceLogs ;
72+ expect ( resourceLogs . resource . attributes ) . toContainEqual ( {
73+ key : 'service.name' ,
74+ value : { stringValue : 'hyperdx-oss-usage-stats' } ,
75+ } ) ;
76+ expect ( resourceLogs . scopeLogs ) . toHaveLength ( 1 ) ;
77+ const [ scopeLogs ] = resourceLogs . scopeLogs ;
78+ expect ( scopeLogs . scope . name ) . toBe ( 'node-logger' ) ;
79+ expect ( scopeLogs . logRecords ) . toHaveLength ( 1 ) ;
80+ const [ record ] = scopeLogs . logRecords ;
81+ expect ( record . body ) . toEqual ( {
82+ stringValue : 'track-hyperdx-oss-usage-stats' ,
83+ } ) ;
5284 expect ( record . severityNumber ) . toBe ( SeverityNumber . INFO ) ;
5385 expect ( record . severityText ) . toBe ( 'info' ) ;
54- expect ( record . instrumentationScope . name ) . toBe ( 'node-logger' ) ;
55- expect ( record . spanContext ) . toBeUndefined ( ) ;
56- expect ( record . attributes ) . toEqual ( {
57- clusterId : 'cluster-1' ,
58- userCounts : 2 ,
59- chStats : { tables : { otel_logs : { rows : 10 } } , rows : 10 } ,
60- time : expect . any ( Number ) ,
61- pid : process . pid ,
62- hostname : os . hostname ( ) ,
63- } ) ;
64- expect ( record . resource . attributes [ 'service.name' ] ) . toBe (
65- 'hyperdx-oss-usage-stats' ,
66- ) ;
86+ expect ( record ) . not . toHaveProperty ( 'traceId' ) ;
87+ expect ( record ) . not . toHaveProperty ( 'spanId' ) ;
88+ expect ( record . attributes ) . toEqual ( [
89+ { key : 'clusterId' , value : { stringValue : 'cluster-1' } } ,
90+ { key : 'userCounts' , value : { intValue : 2 } } ,
91+ {
92+ key : 'chStats' ,
93+ value : {
94+ kvlistValue : { values : [ { key : 'rows' , value : { intValue : 10 } } ] } ,
95+ } ,
96+ } ,
97+ { key : 'time' , value : { intValue : expect . any ( Number ) } } ,
98+ { key : 'pid' , value : { intValue : process . pid } } ,
99+ { key : 'hostname' , value : { stringValue : os . hostname ( ) } } ,
100+ ] ) ;
67101 } ) ;
68102
69103 it ( 'sends NaN as null and drops undefined fields, as the pino records did' , async ( ) => {
70104 await sendUsageStats ( {
71105 clusterId : undefined ,
72- chStats : { tables : { otel_logs : { avgDaySize : NaN , rows : 10 } } } ,
106+ chStats : { avgDaySize : NaN , rows : 10 } ,
73107 } ) ;
74108
75- const [ record ] = mockExporter . getFinishedLogRecords ( ) ;
76- expect ( record . attributes ) . not . toHaveProperty ( 'clusterId' ) ;
77- expect ( record . attributes . chStats ) . toEqual ( {
78- tables : { otel_logs : { avgDaySize : null , rows : 10 } } ,
109+ const { body } = await sentRequest ( ) ;
110+ const [ record ] = body . resourceLogs [ 0 ] . scopeLogs [ 0 ] . logRecords ;
111+ expect ( record . attributes ) . not . toContainEqual (
112+ expect . objectContaining ( { key : 'clusterId' } ) ,
113+ ) ;
114+ expect ( record . attributes ) . toContainEqual ( {
115+ key : 'chStats' ,
116+ value : {
117+ kvlistValue : {
118+ values : [
119+ // An empty AnyValue, which is how OTLP JSON carries null
120+ { key : 'avgDaySize' , value : { } } ,
121+ { key : 'rows' , value : { intValue : 10 } } ,
122+ ] ,
123+ } ,
124+ } ,
79125 } ) ;
80126 } ) ;
127+
128+ it ( 'resolves when the request fails' , async ( ) => {
129+ mockFetch . mockRejectedValue ( new Error ( 'connect ECONNREFUSED' ) ) ;
130+ await expect ( sendUsageStats ( { userCounts : 1 } ) ) . resolves . toBeUndefined ( ) ;
131+
132+ mockFetch . mockResolvedValue ( new Response ( null , { status : 503 } ) ) ;
133+ await expect ( sendUsageStats ( { userCounts : 1 } ) ) . resolves . toBeUndefined ( ) ;
134+ } ) ;
81135} ) ;
0 commit comments