@outputai/http 0.4.1-dev.c0b98d8.0 → 0.4.1-next.6bc541c.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,7 +13,6 @@ RequestInit };
13
13
  * Behaves the same as any fetch function except:
14
14
  * - Sets a request header called `x-request--trace-id` with a random UUID;
15
15
  * - Sends the request, response, error and/or failure to the Trace system;
16
- * - Emits a `http:request` event on every call (success, http_error, network_error).
17
16
  *
18
17
  * @see {@link https://fetch.spec.whatwg.org/}
19
18
  * @param input - URL string, URL object or Request object (undici's or Node's)
@@ -15,7 +15,6 @@ export * as undici from 'undici';
15
15
  * Behaves the same as any fetch function except:
16
16
  * - Sets a request header called `x-request--trace-id` with a random UUID;
17
17
  * - Sends the request, response, error and/or failure to the Trace system;
18
- * - Emits a `http:request` event on every call (success, http_error, network_error).
19
18
  *
20
19
  * @see {@link https://fetch.spec.whatwg.org/}
21
20
  * @param input - URL string, URL object or Request object (undici's or Node's)
@@ -30,25 +29,20 @@ export const fetch = async (input, init) => {
30
29
  const requestId = randomUUID();
31
30
  headers.set('x-request-trace-id', requestId);
32
31
  const request = new undici.Request(base, { headers });
33
- const method = request.method;
34
- const url = request.url;
35
- const startedAt = performance.now();
36
32
  await logRequest({ requestId, request });
37
33
  try {
38
34
  const response = await undici.fetch(request);
39
- const durationMs = Math.round(performance.now() - startedAt);
40
35
  // This enriches the response of the request id, so it is identifiable later.
41
36
  addRequestIdToResponse(response, requestId);
42
37
  if (response.status > 399) {
43
- await logError({ requestId, response, method, url, durationMs });
38
+ await logError({ requestId, response });
44
39
  return response;
45
40
  }
46
- await logResponse({ requestId, response, method, url, durationMs });
41
+ await logResponse({ requestId, response });
47
42
  return response;
48
43
  }
49
44
  catch (error) {
50
- const durationMs = Math.round(performance.now() - startedAt);
51
- logFailure({ requestId, error: error, method, url, durationMs });
45
+ logFailure({ requestId, error: error });
52
46
  throw error;
53
47
  }
54
48
  };
@@ -62,13 +62,8 @@ describe('fetch/index', () => {
62
62
  expect(loggerMock.logRequest.mock.calls[0][0].request.method).toBe('GET');
63
63
  expect(loggerMock.logRequest.mock.calls[0][0].request.url).toBe(`${MOCK_ORIGIN}/ok`);
64
64
  expect(loggerMock.logResponse).toHaveBeenCalledTimes(1);
65
- const responseCall = loggerMock.logResponse.mock.calls[0][0];
66
- expect(responseCall.requestId).toBe(FIXED_REQUEST_ID);
67
- expect(responseCall.response).toBe(response);
68
- expect(responseCall.method).toBe('GET');
69
- expect(responseCall.url).toBe(`${MOCK_ORIGIN}/ok`);
70
- expect(typeof responseCall.durationMs).toBe('number');
71
- expect(responseCall.durationMs).toBeGreaterThanOrEqual(0);
65
+ expect(loggerMock.logResponse.mock.calls[0][0].requestId).toBe(FIXED_REQUEST_ID);
66
+ expect(loggerMock.logResponse.mock.calls[0][0].response).toBe(response);
72
67
  expect(utilsMock.addRequestIdToResponse).toHaveBeenCalledTimes(1);
73
68
  expect(utilsMock.addRequestIdToResponse).toHaveBeenCalledWith(response, FIXED_REQUEST_ID);
74
69
  expect(loggerMock.logError).not.toHaveBeenCalled();
@@ -12,55 +12,34 @@ export declare const logRequest: ({ requestId, request }: {
12
12
  }) => Promise<void>;
13
13
  /**
14
14
  * Sends the trace error event for an http response with error status
15
- * and emits a `http:request` event with `outcome: 'http_error'`.
16
15
  *
17
16
  * @param options
18
17
  * @param options.requestId - id of the request
19
18
  * @param options.response - The HTTP Response object
20
- * @param options.method - HTTP method of the request
21
- * @param options.url - URL of the request
22
- * @param options.durationMs - elapsed time from request issuance to response, in milliseconds
23
19
  */
24
- export declare const logError: ({ requestId, response, method, url, durationMs }: {
20
+ export declare const logError: ({ requestId, response }: {
25
21
  requestId: string;
26
22
  response: Response;
27
- method: string;
28
- url: string;
29
- durationMs: number;
30
23
  }) => Promise<void>;
31
24
  /**
32
25
  * Sends the trace end event for an http response
33
- * and emits a `http:request` event with `outcome: 'success'`.
34
26
  *
35
27
  * @param {object} options
36
28
  * @param options.requestId - id of the request
37
29
  * @param {Response} options.response - The HTTP Response object
38
- * @param options.method - HTTP method of the request
39
- * @param options.url - URL of the request
40
- * @param options.durationMs - elapsed time from request issuance to response, in milliseconds
41
30
  */
42
- export declare const logResponse: ({ requestId, response, method, url, durationMs }: {
31
+ export declare const logResponse: ({ requestId, response }: {
43
32
  requestId: string;
44
33
  response: Response;
45
- method: string;
46
- url: string;
47
- durationMs: number;
48
34
  }) => Promise<void>;
49
35
  /**
50
36
  * Creates the trace error event for a network/connection failure
51
- * and emits a `http:request` event with `outcome: 'network_error'`.
52
37
  *
53
38
  * @param options
54
39
  * @param options.requestId - id of the request
55
40
  * @param options.error - The error thrown
56
- * @param options.method - HTTP method of the request
57
- * @param options.url - URL of the request
58
- * @param options.durationMs - elapsed time from request issuance to failure, in milliseconds
59
41
  */
60
- export declare const logFailure: ({ requestId, error, method, url, durationMs }: {
42
+ export declare const logFailure: ({ requestId, error }: {
61
43
  requestId: string;
62
44
  error: Error;
63
- method: string;
64
- url: string;
65
- durationMs: number;
66
45
  }) => void;
@@ -1,4 +1,4 @@
1
- import { Tracing, emitEvent } from '@outputai/core/sdk_activity_integration';
1
+ import { Tracing } from '@outputai/core/sdk_activity_integration';
2
2
  import { config } from '../config.js';
3
3
  import { parseBody, redactHeaders, serializeError } from './utils.js';
4
4
  /**
@@ -20,80 +20,38 @@ export const logRequest = async ({ requestId, request }) => {
20
20
  };
21
21
  /**
22
22
  * Sends the trace error event for an http response with error status
23
- * and emits a `http:request` event with `outcome: 'http_error'`.
24
23
  *
25
24
  * @param options
26
25
  * @param options.requestId - id of the request
27
26
  * @param options.response - The HTTP Response object
28
- * @param options.method - HTTP method of the request
29
- * @param options.url - URL of the request
30
- * @param options.durationMs - elapsed time from request issuance to response, in milliseconds
31
27
  */
32
- export const logError = async ({ requestId, response, method, url, durationMs }) => {
33
- await Tracing.addEventError({
34
- id: requestId, details: {
35
- status: response.status,
36
- statusText: response.statusText,
37
- headers: redactHeaders(response.headers),
38
- body: await parseBody(response)
39
- }
40
- });
41
- emitEvent('http:request', {
42
- requestId,
43
- method,
44
- url,
28
+ export const logError = async ({ requestId, response }) => Tracing.addEventError({
29
+ id: requestId, details: {
45
30
  status: response.status,
46
- durationMs,
47
- outcome: 'http_error'
48
- });
49
- };
31
+ statusText: response.statusText,
32
+ headers: redactHeaders(response.headers),
33
+ body: await parseBody(response)
34
+ }
35
+ });
50
36
  /**
51
37
  * Sends the trace end event for an http response
52
- * and emits a `http:request` event with `outcome: 'success'`.
53
38
  *
54
39
  * @param {object} options
55
40
  * @param options.requestId - id of the request
56
41
  * @param {Response} options.response - The HTTP Response object
57
- * @param options.method - HTTP method of the request
58
- * @param options.url - URL of the request
59
- * @param options.durationMs - elapsed time from request issuance to response, in milliseconds
60
42
  */
61
- export const logResponse = async ({ requestId, response, method, url, durationMs }) => {
62
- await Tracing.addEventEnd({
63
- id: requestId, details: {
64
- status: response.status,
65
- statusText: response.statusText,
66
- ...(config.logVerbose && { headers: redactHeaders(response.headers), body: await parseBody(response) })
67
- }
68
- });
69
- emitEvent('http:request', {
70
- requestId,
71
- method,
72
- url,
43
+ export const logResponse = async ({ requestId, response }) => Tracing.addEventEnd({
44
+ id: requestId, details: {
73
45
  status: response.status,
74
- durationMs,
75
- outcome: 'success'
76
- });
77
- };
46
+ statusText: response.statusText,
47
+ ...(config.logVerbose && { headers: redactHeaders(response.headers), body: await parseBody(response) })
48
+ }
49
+ });
78
50
  /**
79
51
  * Creates the trace error event for a network/connection failure
80
- * and emits a `http:request` event with `outcome: 'network_error'`.
81
52
  *
82
53
  * @param options
83
54
  * @param options.requestId - id of the request
84
55
  * @param options.error - The error thrown
85
- * @param options.method - HTTP method of the request
86
- * @param options.url - URL of the request
87
- * @param options.durationMs - elapsed time from request issuance to failure, in milliseconds
88
56
  */
89
- export const logFailure = ({ requestId, error, method, url, durationMs }) => {
90
- Tracing.addEventError({ id: requestId, details: serializeError(error) });
91
- emitEvent('http:request', {
92
- requestId,
93
- method,
94
- url,
95
- status: undefined,
96
- durationMs,
97
- outcome: 'network_error'
98
- });
99
- };
57
+ export const logFailure = ({ requestId, error }) => Tracing.addEventError({ id: requestId, details: serializeError(error) });
@@ -6,12 +6,10 @@ vi.mock('@outputai/core/sdk_activity_integration', () => ({
6
6
  addEventEnd: vi.fn(),
7
7
  addEventError: vi.fn(),
8
8
  addEventAttribute: vi.fn()
9
- },
10
- emitEvent: vi.fn()
9
+ }
11
10
  }));
12
- import { Tracing, emitEvent } from '@outputai/core/sdk_activity_integration';
11
+ import { Tracing } from '@outputai/core/sdk_activity_integration';
13
12
  const tracing = vi.mocked(Tracing, true);
14
- const emit = vi.mocked(emitEvent, true);
15
13
  /** Loads logger with optional verbose tracing env so `config.js` is evaluated fresh. */
16
14
  async function logLogger(verbose) {
17
15
  vi.resetModules();
@@ -28,7 +26,6 @@ beforeEach(() => {
28
26
  tracing.addEventEnd.mockClear();
29
27
  tracing.addEventError.mockClear();
30
28
  tracing.addEventAttribute.mockClear();
31
- emit.mockClear();
32
29
  });
33
30
  describe('fetch/logger', () => {
34
31
  describe('logRequest', () => {
@@ -100,9 +97,7 @@ describe('fetch/logger', () => {
100
97
  'content-type': 'application/json'
101
98
  }
102
99
  });
103
- await logError({
104
- requestId: 'e1', response, method: 'GET', url: 'https://upstream.test/x', durationMs: 1
105
- });
100
+ await logError({ requestId: 'e1', response });
106
101
  expect(tracing.addEventError).toHaveBeenCalledWith({
107
102
  id: 'e1',
108
103
  details: {
@@ -125,9 +120,7 @@ describe('fetch/logger', () => {
125
120
  statusText: 'Bad Gateway',
126
121
  headers: { 'content-type': 'text/plain' }
127
122
  });
128
- await logError({
129
- requestId: 'e2', response, method: 'GET', url: 'https://upstream.test/y', durationMs: 1
130
- });
123
+ await logError({ requestId: 'e2', response });
131
124
  expect(tracing.addEventError).toHaveBeenCalledWith({
132
125
  id: 'e2',
133
126
  details: {
@@ -147,9 +140,7 @@ describe('fetch/logger', () => {
147
140
  statusText: 'OK',
148
141
  headers: { 'content-type': 'application/json', Authorization: 'x' }
149
142
  });
150
- await logResponse({
151
- requestId: 'lr1', response, method: 'GET', url: 'https://x.test/a', durationMs: 1
152
- });
143
+ await logResponse({ requestId: 'lr1', response });
153
144
  expect(tracing.addEventEnd).toHaveBeenCalledWith({
154
145
  id: 'lr1',
155
146
  details: {
@@ -168,9 +159,7 @@ describe('fetch/logger', () => {
168
159
  'Set-Cookie': 'a=b'
169
160
  }
170
161
  });
171
- await logResponse({
172
- requestId: 'lr-v', response, method: 'POST', url: 'https://x.test/b', durationMs: 1
173
- });
162
+ await logResponse({ requestId: 'lr-v', response });
174
163
  expect(tracing.addEventEnd).toHaveBeenCalledWith({
175
164
  id: 'lr-v',
176
165
  details: {
@@ -189,7 +178,7 @@ describe('fetch/logger', () => {
189
178
  it('forwards serialized error details (including stack) to Tracing.addEventError', async () => {
190
179
  const { logFailure } = await logLogger(false);
191
180
  const err = new TypeError('network');
192
- logFailure({ requestId: 'f1', error: err, method: 'GET', url: 'https://example.test/x', durationMs: 12 });
181
+ logFailure({ requestId: 'f1', error: err });
193
182
  expect(tracing.addEventError).toHaveBeenCalledWith({
194
183
  id: 'f1',
195
184
  details: {
@@ -202,63 +191,4 @@ describe('fetch/logger', () => {
202
191
  });
203
192
  });
204
193
  });
205
- describe('http:request event emission', () => {
206
- it('emits http:request with outcome=success on logResponse', async () => {
207
- const { logResponse } = await logLogger(false);
208
- const response = new Response('', { status: 200 });
209
- await logResponse({
210
- requestId: 'r-ok',
211
- response,
212
- method: 'GET',
213
- url: 'https://api.example.com/ok',
214
- durationMs: 42
215
- });
216
- expect(emit).toHaveBeenCalledWith('http:request', {
217
- requestId: 'r-ok',
218
- method: 'GET',
219
- url: 'https://api.example.com/ok',
220
- status: 200,
221
- durationMs: 42,
222
- outcome: 'success'
223
- });
224
- });
225
- it('emits http:request with outcome=http_error on logError', async () => {
226
- const { logError } = await logLogger(false);
227
- const response = new Response('boom', { status: 500 });
228
- await logError({
229
- requestId: 'r-err',
230
- response,
231
- method: 'POST',
232
- url: 'https://api.example.com/err',
233
- durationMs: 15
234
- });
235
- expect(emit).toHaveBeenCalledWith('http:request', {
236
- requestId: 'r-err',
237
- method: 'POST',
238
- url: 'https://api.example.com/err',
239
- status: 500,
240
- durationMs: 15,
241
- outcome: 'http_error'
242
- });
243
- });
244
- it('emits http:request with outcome=network_error on logFailure (status undefined)', async () => {
245
- const { logFailure } = await logLogger(false);
246
- const err = new TypeError('network');
247
- logFailure({
248
- requestId: 'r-net',
249
- error: err,
250
- method: 'GET',
251
- url: 'https://api.example.com/net',
252
- durationMs: 9
253
- });
254
- expect(emit).toHaveBeenCalledWith('http:request', {
255
- requestId: 'r-net',
256
- method: 'GET',
257
- url: 'https://api.example.com/net',
258
- status: undefined,
259
- durationMs: 9,
260
- outcome: 'network_error'
261
- });
262
- });
263
- });
264
194
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@outputai/http",
3
- "version": "0.4.1-dev.c0b98d8.0",
3
+ "version": "0.4.1-next.6bc541c.0",
4
4
  "description": "Framework abstraction to make HTTP calls with tracing",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -11,7 +11,7 @@
11
11
  "dependencies": {
12
12
  "ky": "1.14.3",
13
13
  "undici": "8.1.0",
14
- "@outputai/core": "0.4.1-dev.c0b98d8.0"
14
+ "@outputai/core": "0.4.1-next.6bc541c.0"
15
15
  },
16
16
  "license": "Apache-2.0",
17
17
  "publishConfig": {