Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/node/src/sdk/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ export class NodeClient extends ServerRuntimeClient<NodeClientOptions> {
private _logOnExitFlushListener: (() => void) | undefined;

public constructor(options: NodeClientOptions) {
const serverName = options.serverName || global.process.env.SENTRY_NAME || os.hostname();
const clientOptions: ServerRuntimeClientOptions = {
...options,
platform: 'node',
runtime: { name: 'node', version: global.process.version },
serverName: options.serverName || global.process.env.SENTRY_NAME || os.hostname(),
serverName,
};

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

if (serverName) {
this.on('beforeCaptureLog', log => {
log.attributes = {
...log.attributes,
'server.address': serverName,
};
});
}

process.on('beforeExit', this._logOnExitFlushListener);
}
}
Expand Down
30 changes: 29 additions & 1 deletion packages/node/test/sdk/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as os from 'os';
import { ProxyTracer } from '@opentelemetry/api';
import * as opentelemetryInstrumentationPackage from '@opentelemetry/instrumentation';
import type { Event, EventHint } from '@sentry/core';
import type { Event, EventHint, Log } from '@sentry/core';
import { SDK_VERSION, Scope, getCurrentScope, getGlobalScope, getIsolationScope } from '@sentry/core';
import { setOpenTelemetryContextAsyncContextStrategy } from '@sentry/opentelemetry';
import { afterEach, beforeEach, describe, expect, it, test, vi } from 'vitest';
Expand Down Expand Up @@ -281,4 +281,32 @@ describe('NodeClient', () => {
}),
);
});

describe('log capture', () => {
it('adds server name to log attributes', () => {
const options = getDefaultNodeClientOptions({ _experiments: { enableLogs: true } });
const client = new NodeClient(options);

const log: Log = { level: 'info', message: 'test message', attributes: {} };
client.emit('beforeCaptureLog', log);

expect(log.attributes).toEqual({
'server.address': expect.any(String),
});
});

it('preserves existing log attributes', () => {
const serverName = 'test-server';
const options = getDefaultNodeClientOptions({ serverName, _experiments: { enableLogs: true } });
const client = new NodeClient(options);

const log: Log = { level: 'info', message: 'test message', attributes: { 'existing.attr': 'value' } };
client.emit('beforeCaptureLog', log);

expect(log.attributes).toEqual({
'existing.attr': 'value',
'server.address': serverName,
});
});
});
});
Loading