@outputai/http 0.8.2-next.42a0ddf.0 → 0.8.2-next.57bc8d6.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.
- package/dist/cost.js +4 -3
- package/dist/cost.spec.js +11 -13
- package/dist/fetch/logger.js +2 -2
- package/dist/fetch/logger.spec.js +10 -8
- package/package.json +3 -3
package/dist/cost.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Tracing,
|
|
1
|
+
import { Tracing, Event } from '@outputai/core/sdk/runtime';
|
|
2
2
|
import { requestIdSymbol } from './consts.js';
|
|
3
|
+
import { Logger } from '@outputai/core';
|
|
3
4
|
/**
|
|
4
5
|
* Attach cost information to the trace of an HTTP Request using the response
|
|
5
6
|
*
|
|
@@ -10,10 +11,10 @@ import { requestIdSymbol } from './consts.js';
|
|
|
10
11
|
export const addRequestCost = (response, value) => {
|
|
11
12
|
const eventId = Reflect.get(response, requestIdSymbol);
|
|
12
13
|
if (!eventId) {
|
|
13
|
-
|
|
14
|
+
Logger.warn('addRequestCost(): The "response" argument did not originate from @outputai/http, no costs were added.', { namespace: 'HTTP' });
|
|
14
15
|
return;
|
|
15
16
|
}
|
|
16
17
|
const attribute = new Tracing.Attribute.HTTPRequestCost(response.url, eventId, value);
|
|
17
18
|
Tracing.addEventAttribute({ eventId, attribute });
|
|
18
|
-
|
|
19
|
+
Event.emit('cost:http:request', attribute);
|
|
19
20
|
};
|
package/dist/cost.spec.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
2
|
import { Response } from 'undici';
|
|
3
3
|
import { requestIdSymbol } from './consts.js';
|
|
4
|
-
vi.mock('@outputai/core/
|
|
4
|
+
vi.mock('@outputai/core/sdk/runtime', () => {
|
|
5
5
|
class HTTPRequestCost {
|
|
6
6
|
static TYPE = 'http:request:cost';
|
|
7
7
|
type = HTTPRequestCost.TYPE;
|
|
@@ -21,19 +21,20 @@ vi.mock('@outputai/core/sdk_activity_integration', () => {
|
|
|
21
21
|
HTTPRequestCost
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
|
-
|
|
24
|
+
Event: {
|
|
25
|
+
emit: vi.fn()
|
|
26
|
+
}
|
|
25
27
|
};
|
|
26
28
|
});
|
|
27
|
-
import { Tracing,
|
|
29
|
+
import { Tracing, Event } from '@outputai/core/sdk/runtime';
|
|
28
30
|
import { addRequestCost } from './cost.js';
|
|
29
31
|
import { addRequestIdToResponse } from './fetch/utils.js';
|
|
30
32
|
const tracing = vi.mocked(Tracing, true);
|
|
31
|
-
const
|
|
33
|
+
const event = vi.mocked(Event, true);
|
|
32
34
|
describe('addRequestCost', () => {
|
|
33
35
|
beforeEach(() => {
|
|
34
36
|
tracing.addEventAttribute.mockClear();
|
|
35
|
-
emit.mockClear();
|
|
36
|
-
vi.spyOn(console, 'warn').mockImplementation(() => { });
|
|
37
|
+
event.emit.mockClear();
|
|
37
38
|
});
|
|
38
39
|
afterEach(() => {
|
|
39
40
|
vi.restoreAllMocks();
|
|
@@ -42,16 +43,14 @@ describe('addRequestCost', () => {
|
|
|
42
43
|
const response = new Response();
|
|
43
44
|
const cost = 1;
|
|
44
45
|
addRequestCost(response, cost);
|
|
45
|
-
expect(console.warn).toHaveBeenCalledWith('addRequestCost(): The "response" argument did not originate from @outputai/http, no costs were added.');
|
|
46
46
|
expect(tracing.addEventAttribute).not.toHaveBeenCalled();
|
|
47
|
-
expect(emit).not.toHaveBeenCalled();
|
|
47
|
+
expect(event.emit).not.toHaveBeenCalled();
|
|
48
48
|
});
|
|
49
49
|
it('records cost on the trace event when the response carries the request id', () => {
|
|
50
50
|
const response = new Response(undefined, { status: 200 });
|
|
51
51
|
Reflect.set(response, requestIdSymbol, 'evt-cost-1');
|
|
52
52
|
const cost = 2.5;
|
|
53
53
|
addRequestCost(response, cost);
|
|
54
|
-
expect(console.warn).not.toHaveBeenCalled();
|
|
55
54
|
expect(tracing.addEventAttribute).toHaveBeenCalledWith({
|
|
56
55
|
eventId: 'evt-cost-1',
|
|
57
56
|
attribute: expect.objectContaining({
|
|
@@ -62,7 +61,7 @@ describe('addRequestCost', () => {
|
|
|
62
61
|
})
|
|
63
62
|
});
|
|
64
63
|
const attribute = tracing.addEventAttribute.mock.calls[0][0].attribute;
|
|
65
|
-
expect(emit).toHaveBeenCalledWith('cost:http:request', attribute);
|
|
64
|
+
expect(event.emit).toHaveBeenCalledWith('cost:http:request', attribute);
|
|
66
65
|
});
|
|
67
66
|
it('records zero cost on the trace event', () => {
|
|
68
67
|
const response = new Response();
|
|
@@ -79,7 +78,7 @@ describe('addRequestCost', () => {
|
|
|
79
78
|
})
|
|
80
79
|
});
|
|
81
80
|
const attribute = tracing.addEventAttribute.mock.calls[0][0].attribute;
|
|
82
|
-
expect(emit).toHaveBeenCalledWith('cost:http:request', attribute);
|
|
81
|
+
expect(event.emit).toHaveBeenCalledWith('cost:http:request', attribute);
|
|
83
82
|
});
|
|
84
83
|
// ky clones the response before passing it to afterResponse hooks. Without
|
|
85
84
|
// the clone-propagation patch in addRequestIdToResponse, this path silently
|
|
@@ -90,7 +89,6 @@ describe('addRequestCost', () => {
|
|
|
90
89
|
addRequestIdToResponse(response, 'evt-clone-1');
|
|
91
90
|
const cloned = response.clone();
|
|
92
91
|
addRequestCost(cloned, 4.2);
|
|
93
|
-
expect(console.warn).not.toHaveBeenCalled();
|
|
94
92
|
expect(tracing.addEventAttribute).toHaveBeenCalledWith({
|
|
95
93
|
eventId: 'evt-clone-1',
|
|
96
94
|
attribute: expect.objectContaining({
|
|
@@ -100,6 +98,6 @@ describe('addRequestCost', () => {
|
|
|
100
98
|
})
|
|
101
99
|
});
|
|
102
100
|
const attribute = tracing.addEventAttribute.mock.calls[0][0].attribute;
|
|
103
|
-
expect(emit).toHaveBeenCalledWith('cost:http:request', attribute);
|
|
101
|
+
expect(event.emit).toHaveBeenCalledWith('cost:http:request', attribute);
|
|
104
102
|
});
|
|
105
103
|
});
|
package/dist/fetch/logger.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Tracing,
|
|
1
|
+
import { Tracing, Event } from '@outputai/core/sdk/runtime';
|
|
2
2
|
import { config } from '../config.js';
|
|
3
3
|
import { parseBody, redactHeaders, serializeError } from './utils.js';
|
|
4
4
|
/** Single source of truth for the `http:request` event shape. */
|
|
5
5
|
const emitHttpRequestEvent = (payload) => {
|
|
6
|
-
|
|
6
|
+
Event.emit('http:request', payload);
|
|
7
7
|
};
|
|
8
8
|
/**
|
|
9
9
|
* Sends the trace start event for an http request
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
2
|
import { Response, Request } from 'undici';
|
|
3
|
-
vi.mock('@outputai/core/
|
|
3
|
+
vi.mock('@outputai/core/sdk/runtime', () => {
|
|
4
4
|
class HTTPRequestCount {
|
|
5
5
|
static TYPE = 'http:request:count';
|
|
6
6
|
type = HTTPRequestCount.TYPE;
|
|
@@ -21,12 +21,14 @@ vi.mock('@outputai/core/sdk_activity_integration', () => {
|
|
|
21
21
|
HTTPRequestCount
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
|
-
|
|
24
|
+
Event: {
|
|
25
|
+
emit: vi.fn()
|
|
26
|
+
}
|
|
25
27
|
};
|
|
26
28
|
});
|
|
27
|
-
import { Tracing,
|
|
29
|
+
import { Tracing, Event } from '@outputai/core/sdk/runtime';
|
|
28
30
|
const tracing = vi.mocked(Tracing, true);
|
|
29
|
-
const
|
|
31
|
+
const event = vi.mocked(Event, true);
|
|
30
32
|
/** Loads logger with optional verbose tracing env so `config.js` is evaluated fresh. */
|
|
31
33
|
async function logLogger(verbose) {
|
|
32
34
|
vi.resetModules();
|
|
@@ -43,7 +45,7 @@ beforeEach(() => {
|
|
|
43
45
|
tracing.addEventEnd.mockClear();
|
|
44
46
|
tracing.addEventError.mockClear();
|
|
45
47
|
tracing.addEventAttribute.mockClear();
|
|
46
|
-
emit.mockClear();
|
|
48
|
+
event.emit.mockClear();
|
|
47
49
|
});
|
|
48
50
|
describe('fetch/logger', () => {
|
|
49
51
|
describe('logRequest', () => {
|
|
@@ -231,7 +233,7 @@ describe('fetch/logger', () => {
|
|
|
231
233
|
url: 'https://api.example.com/ok',
|
|
232
234
|
durationMs: 42
|
|
233
235
|
});
|
|
234
|
-
expect(emit).toHaveBeenCalledWith('http:request', {
|
|
236
|
+
expect(event.emit).toHaveBeenCalledWith('http:request', {
|
|
235
237
|
requestId: 'r-ok',
|
|
236
238
|
method: 'GET',
|
|
237
239
|
url: 'https://api.example.com/ok',
|
|
@@ -250,7 +252,7 @@ describe('fetch/logger', () => {
|
|
|
250
252
|
url: 'https://api.example.com/err',
|
|
251
253
|
durationMs: 15
|
|
252
254
|
});
|
|
253
|
-
expect(emit).toHaveBeenCalledWith('http:request', {
|
|
255
|
+
expect(event.emit).toHaveBeenCalledWith('http:request', {
|
|
254
256
|
requestId: 'r-err',
|
|
255
257
|
method: 'POST',
|
|
256
258
|
url: 'https://api.example.com/err',
|
|
@@ -269,7 +271,7 @@ describe('fetch/logger', () => {
|
|
|
269
271
|
url: 'https://api.example.com/net',
|
|
270
272
|
durationMs: 9
|
|
271
273
|
});
|
|
272
|
-
expect(emit).toHaveBeenCalledWith('http:request', {
|
|
274
|
+
expect(event.emit).toHaveBeenCalledWith('http:request', {
|
|
273
275
|
requestId: 'r-net',
|
|
274
276
|
method: 'GET',
|
|
275
277
|
url: 'https://api.example.com/net',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@outputai/http",
|
|
3
|
-
"version": "0.8.2-next.
|
|
3
|
+
"version": "0.8.2-next.57bc8d6.0",
|
|
4
4
|
"description": "Framework abstraction to make HTTP calls with tracing",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
],
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"ky": "1.14.3",
|
|
13
|
-
"undici": "8.
|
|
14
|
-
"@outputai/core": "0.8.2-next.
|
|
13
|
+
"undici": "8.5.0",
|
|
14
|
+
"@outputai/core": "0.8.2-next.57bc8d6.0"
|
|
15
15
|
},
|
|
16
16
|
"license": "Apache-2.0",
|
|
17
17
|
"publishConfig": {
|