Skip to content

Commit 341ba86

Browse files
committed
feat: expose x-trace-id in debug logs
1 parent 04294ec commit 341ba86

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88
### Added
99
<!-- * add to here... -->
10+
* Include `x-trace-id` response headers in debug logs
1011
### Changed
1112
<!-- * add to here... -->
1213

src/client.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,22 @@ export class HttpClient {
227227
): Promise<{ statusCode: number; content: TContent }> {
228228
try {
229229
const response = await axiosInstance.request(axiosRequestConfig);
230+
if (response.headers !== undefined) {
231+
logDebug('Trace details:', {
232+
xTraceId: response.headers['x-trace-id'],
233+
});
234+
}
230235

231236
if (axiosRequestConfig.responseType === 'text') {
232237
// Workaround for axios-bug: https://github.com/axios/axios/issues/907
233238
if (typeof response.data === 'object') {
234239
response.data = JSON.stringify(response.data);
235240
}
236241
}
237-
return { statusCode: response.status, content: response.data };
242+
return {
243+
statusCode: response.status,
244+
content: response.data,
245+
};
238246
} catch (axios_error_raw) {
239247
const axiosError = axios_error_raw as AxiosError;
240248
const message: string = axiosError.message || '';

tests/client.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2025 DeepL SE (https://www.deepl.com)
2+
// Use of this source code is governed by an MIT
3+
// license that can be found in the LICENSE file.
4+
5+
import * as deepl from 'deepl-node';
6+
7+
import { exampleText, makeDeeplClient, makeTranslator } from './core';
8+
import log from 'loglevel';
9+
10+
jest.mock('loglevel', () => ({
11+
debug: jest.fn(),
12+
getLogger: jest.fn().mockReturnValue({
13+
setLevel: jest.fn(),
14+
debug: jest.fn(),
15+
info: jest.fn(),
16+
}),
17+
}));
18+
19+
describe('client tests', () => {
20+
describe('log debug', () => {
21+
beforeEach(() => {
22+
jest.clearAllMocks();
23+
log.getLogger('deepl').setLevel('debug');
24+
});
25+
26+
it('with translate text', async () => {
27+
const translator = makeTranslator({});
28+
const result = await translator.translateText(exampleText.en, null, 'de');
29+
30+
expect(log.getLogger('deepl').debug).toHaveBeenCalledTimes(3);
31+
expect(log.getLogger('deepl').debug).toHaveBeenNthCalledWith(
32+
1,
33+
expect.stringContaining('Request details:'),
34+
);
35+
expect(log.getLogger('deepl').debug).toHaveBeenNthCalledWith(
36+
2,
37+
expect.stringContaining('Trace details:, xTraceId = '),
38+
);
39+
expect(log.getLogger('deepl').debug).toHaveBeenNthCalledWith(
40+
3,
41+
expect.stringContaining('Response details:, content = '),
42+
);
43+
});
44+
45+
it('with rephrase text', async () => {
46+
const deeplClient = makeDeeplClient({});
47+
const result = await deeplClient.rephraseText(exampleText.de, 'de');
48+
49+
expect(log.getLogger('deepl').debug).toHaveBeenCalledTimes(3);
50+
expect(log.getLogger('deepl').debug).toHaveBeenNthCalledWith(
51+
1,
52+
expect.stringContaining('Request details:'),
53+
);
54+
expect(log.getLogger('deepl').debug).toHaveBeenNthCalledWith(
55+
2,
56+
expect.stringContaining('Trace details:, xTraceId = '),
57+
);
58+
expect(log.getLogger('deepl').debug).toHaveBeenNthCalledWith(
59+
3,
60+
expect.stringContaining('Response details:, content = '),
61+
);
62+
});
63+
});
64+
});

0 commit comments

Comments
 (0)