Skip to content

Commit 6c40f4b

Browse files
authored
feat(node): Add server.address to nodejs logs (#16006)
`server.address` is the equivalent to `server_name` we attach to errors and transactions. See the spec for the attribute here: https://getsentry.github.io/sentry-conventions/generated/attributes/server.html#serveraddress resolves https://linear.app/getsentry/issue/LOGS-32
1 parent 2e3d6e3 commit 6c40f4b

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

packages/node/src/sdk/client.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
2121
private _logOnExitFlushListener: (() => void) | undefined;
2222

2323
public constructor(options: NodeClientOptions) {
24+
const serverName = options.serverName || global.process.env.SENTRY_NAME || os.hostname();
2425
const clientOptions: ServerRuntimeClientOptions = {
2526
...options,
2627
platform: 'node',
2728
runtime: { name: 'node', version: global.process.version },
28-
serverName: options.serverName || global.process.env.SENTRY_NAME || os.hostname(),
29+
serverName,
2930
};
3031

3132
if (options.openTelemetryInstrumentations) {
@@ -47,6 +48,15 @@ export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
4748
_INTERNAL_flushLogsBuffer(this);
4849
};
4950

51+
if (serverName) {
52+
this.on('beforeCaptureLog', log => {
53+
log.attributes = {
54+
...log.attributes,
55+
'server.address': serverName,
56+
};
57+
});
58+
}
59+
5060
process.on('beforeExit', this._logOnExitFlushListener);
5161
}
5262
}

packages/node/test/sdk/client.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as os from 'os';
22
import { ProxyTracer } from '@opentelemetry/api';
33
import * as opentelemetryInstrumentationPackage from '@opentelemetry/instrumentation';
4-
import type { Event, EventHint } from '@sentry/core';
4+
import type { Event, EventHint, Log } from '@sentry/core';
55
import { SDK_VERSION, Scope, getCurrentScope, getGlobalScope, getIsolationScope } from '@sentry/core';
66
import { setOpenTelemetryContextAsyncContextStrategy } from '@sentry/opentelemetry';
77
import { afterEach, beforeEach, describe, expect, it, test, vi } from 'vitest';
@@ -281,4 +281,32 @@ describe('NodeClient', () => {
281281
}),
282282
);
283283
});
284+
285+
describe('log capture', () => {
286+
it('adds server name to log attributes', () => {
287+
const options = getDefaultNodeClientOptions({ _experiments: { enableLogs: true } });
288+
const client = new NodeClient(options);
289+
290+
const log: Log = { level: 'info', message: 'test message', attributes: {} };
291+
client.emit('beforeCaptureLog', log);
292+
293+
expect(log.attributes).toEqual({
294+
'server.address': expect.any(String),
295+
});
296+
});
297+
298+
it('preserves existing log attributes', () => {
299+
const serverName = 'test-server';
300+
const options = getDefaultNodeClientOptions({ serverName, _experiments: { enableLogs: true } });
301+
const client = new NodeClient(options);
302+
303+
const log: Log = { level: 'info', message: 'test message', attributes: { 'existing.attr': 'value' } };
304+
client.emit('beforeCaptureLog', log);
305+
306+
expect(log.attributes).toEqual({
307+
'existing.attr': 'value',
308+
'server.address': serverName,
309+
});
310+
});
311+
});
284312
});

0 commit comments

Comments
 (0)