Skip to content

Commit

Permalink
refactor: Move query counter to the log manager. (serverpod#2553)
Browse files Browse the repository at this point in the history
  • Loading branch information
Isakdl authored Jul 31, 2024
1 parent 7ffe397 commit 2142c8b
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 13 deletions.
12 changes: 8 additions & 4 deletions packages/serverpod/lib/src/server/log_manager/log_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class SessionLogManager {

final LogSettings Function(Session) _settingsForSession;

int _numberOfQueries;

int _logOrderId;

int get _nextLogOrderId => ++_logOrderId;
Expand All @@ -32,6 +34,7 @@ class SessionLogManager {
required LogSettings Function(Session) settingsForSession,
required String serverId,
}) : _logOrderId = 0,
_numberOfQueries = 0,
_logWriter = logWriter,
_settingsForSession = settingsForSession,
_serverId = serverId;
Expand Down Expand Up @@ -144,6 +147,7 @@ class SessionLogManager {
required StackTrace stackTrace,
}) async {
var executionTime = duration.inMicroseconds / _microNormalizer;
_numberOfQueries++;

var logSettings = _settingsForSession(session);

Expand Down Expand Up @@ -314,10 +318,10 @@ class SessionLogManager {
if (session.serverpod.runMode == ServerpodRunMode.development) {
if (session is MethodCallSession) {
stdout.writeln(
'METHOD CALL: ${session.endpointName}.${session.methodName} duration: ${duration.inMilliseconds}ms numQueries: ${cachedEntry.queries.length} authenticatedUser: $authenticatedUserId');
'METHOD CALL: ${session.endpointName}.${session.methodName} duration: ${duration.inMilliseconds}ms numQueries: $_numberOfQueries authenticatedUser: $authenticatedUserId');
} else if (session is FutureCallSession) {
stdout.writeln(
'FUTURE CALL: ${session.futureCallName} duration: ${duration.inMilliseconds}ms numQueries: ${cachedEntry.queries.length}');
'FUTURE CALL: ${session.futureCallName} duration: ${duration.inMilliseconds}ms numQueries: $_numberOfQueries');
}
if (exception != null) {
stdout.writeln(exception);
Expand Down Expand Up @@ -347,7 +351,7 @@ class SessionLogManager {
endpoint: session.endpointName,
method: session.methodName,
duration: duration.inMicroseconds / 1000000.0,
numQueries: cachedEntry.numQueries,
numQueries: _numberOfQueries,
slow: isSlow,
error: exception,
stackTrace: stackTrace?.toString(),
Expand All @@ -368,7 +372,7 @@ class SessionLogManager {
stderr.writeln('${DateTime.now().toUtc()} FAILED TO LOG SESSION');
if (session.methodName != null) {
stderr.writeln(
'CALL: ${session.endpointName}.${session.methodName} duration: ${duration.inMilliseconds}ms numQueries: ${cachedEntry.queries.length} authenticatedUser: $authenticatedUserId');
'CALL: ${session.endpointName}.${session.methodName} duration: ${duration.inMilliseconds}ms numQueries: $_numberOfQueries authenticatedUser: $authenticatedUserId');
}
stderr.writeln('CALL error: $exception');
stderr.writeln('$logStackTrace');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ class SessionLogEntryCache {
/// Queries made during the session.
final List<QueryLogEntry> queries = [];

/// Number of queries made during this session.
int get numQueries => queries.length;

/// Log entries made during the session.
final List<LogEntry> logEntries = [];

Expand Down
6 changes: 6 additions & 0 deletions tests/serverpod_test_client/lib/src/protocol/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,12 @@ class EndpointLogging extends _i1.EndpointRef {
{'seconds': seconds},
);

_i2.Future<void> queryMethod(int queries) => caller.callServerEndpoint<void>(
'logging',
'queryMethod',
{'queries': queries},
);

_i2.Future<void> failedQueryMethod() => caller.callServerEndpoint<void>(
'logging',
'failedQueryMethod',
Expand Down
16 changes: 10 additions & 6 deletions tests/serverpod_test_server/lib/src/endpoints/logging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ class LoggingEndpoint extends Endpoint {
Future<void> slowQueryMethod(Session session, int seconds) async {
try {
await session.db.unsafeQuery('SELECT pg_sleep($seconds);');
} catch (e) {
print(e);
}
} catch (e) {}
}

Future<void> queryMethod(Session session, int queries) async {
try {
for (var i = 0; i < queries; i++) {
await Types.db.findFirstRow(session);
}
} catch (e) {}
}

Future<void> failedQueryMethod(Session session) async {
try {
await session.db.unsafeQuery('SELECT * FROM table_does_not_exist;');
} catch (e) {
print(e);
}
} catch (e) {}
}

Future<void> slowMethod(Session session, int delayMillis) async {
Expand Down
18 changes: 18 additions & 0 deletions tests/serverpod_test_server/lib/src/generated/endpoints.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2237,6 +2237,24 @@ class Endpoints extends _i1.EndpointDispatch {
params['seconds'],
),
),
'queryMethod': _i1.MethodConnector(
name: 'queryMethod',
params: {
'queries': _i1.ParameterDescription(
name: 'queries',
type: _i1.getType<int>(),
nullable: false,
)
},
call: (
_i1.Session session,
Map<String, dynamic> params,
) async =>
(endpoints['logging'] as _i17.LoggingEndpoint).queryMethod(
session,
params['queries'],
),
),
'failedQueryMethod': _i1.MethodConnector(
name: 'failedQueryMethod',
params: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ listParameters:
- returnDurationListNullableDurations:
logging:
- slowQueryMethod:
- queryMethod:
- failedQueryMethod:
- slowMethod:
- failingMethod:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,72 @@ void main() async {
expect(logs.first.sessionLogEntry.method, 'slowQueryMethod');
});

test(
'Given a log setting that enables all logs when calling a method executing a query then a query log is created',
() async {
var settings = RuntimeSettingsBuilder()
.withLogSettings(
LogSettingsBuilder().build(),
)
.build();

await server.updateRuntimeSettings(settings);

await client.logging.queryMethod(1);

var logs = await LoggingUtil.findAllLogs(session);

expect(logs, hasLength(1));

expect(logs.first.queries, hasLength(1));
expect(logs.first.sessionLogEntry.numQueries, 1);
});

test(
'Given a log setting that enables all logs when calling a method executing several queries then a log for each query is created',
() async {
var settings = RuntimeSettingsBuilder()
.withLogSettings(
LogSettingsBuilder().build(),
)
.build();

await server.updateRuntimeSettings(settings);

await client.logging.queryMethod(4);

var logs = await LoggingUtil.findAllLogs(session);

expect(logs, hasLength(1));

expect(logs.first.queries, hasLength(4));
expect(logs.first.sessionLogEntry.numQueries, 4);
});

test(
'Given a log setting that turns off all database query logging when calling a method executing several queries then the number of queries are still counted.',
() async {
var settings = RuntimeSettingsBuilder()
.withLogSettings(
LogSettingsBuilder()
.withLoggingTurnedDown()
.withLogAllSessions(true)
.build(),
)
.build();

await server.updateRuntimeSettings(settings);

await client.logging.queryMethod(4);

var logs = await LoggingUtil.findAllLogs(session);

expect(logs, hasLength(1));

expect(logs.first.queries, isEmpty);
expect(logs.first.sessionLogEntry.numQueries, 4);
});

test(
'Given a log setting that enables failed query logging when calling a method executing an invalid query then a single log entry is created.',
() async {
Expand Down

0 comments on commit 2142c8b

Please sign in to comment.