Skip to content

Commit

Permalink
fix: Add methodName and endpointName to base session. (serverpod#2483)
Browse files Browse the repository at this point in the history
  • Loading branch information
Isakdl authored Jul 29, 2024
1 parent 0f6be36 commit 68186d7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 114 deletions.
37 changes: 7 additions & 30 deletions packages/serverpod/lib/src/server/log_manager/log_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class SessionLogManager {
} catch (exception, stackTrace) {
stderr
.writeln('${DateTime.now().toUtc()} FAILED TO LOG STREAMING $type');
stderr.write('ENDPOINT: ${_endpointForSession(session)}');
stderr.write('ENDPOINT: ${session.endpointName}');
stderr.writeln('CALL error: $exception');
stderr.writeln('$stackTrace');
}
Expand Down Expand Up @@ -283,8 +283,8 @@ class SessionLogManager {
serverId: _serverId,
time: now,
touched: now,
endpoint: _endpointForSession(session),
method: _methodForSession(session),
endpoint: session.endpointName,
method: session.methodName,
isOpen: true,
);

Expand Down Expand Up @@ -343,8 +343,8 @@ class SessionLogManager {
serverId: _serverId,
time: now,
touched: now,
endpoint: _endpointForSession(session),
method: _methodForSession(session),
endpoint: session.endpointName,
method: session.methodName,
duration: duration.inMicroseconds / 1000000.0,
numQueries: cachedEntry.numQueries,
slow: isSlow,
Expand All @@ -365,9 +365,9 @@ class SessionLogManager {
}
} catch (e, logStackTrace) {
stderr.writeln('${DateTime.now().toUtc()} FAILED TO LOG SESSION');
if (_methodForSession(session) != null) {
if (session.methodName != null) {
stderr.writeln(
'CALL: ${_endpointForSession(session)}.${_methodForSession(session)} duration: ${duration.inMilliseconds}ms numQueries: ${cachedEntry.queries.length} authenticatedUser: $authenticatedUserId');
'CALL: ${session.endpointName}.${session.methodName} duration: ${duration.inMilliseconds}ms numQueries: ${cachedEntry.queries.length} authenticatedUser: $authenticatedUserId');
}
stderr.writeln('CALL error: $exception');
stderr.writeln('$logStackTrace');
Expand Down Expand Up @@ -419,26 +419,3 @@ class LogManager {
return logEntry;
}
}

String? _methodForSession(Session session) {
var localSession = session;

if (localSession is MethodCallSession) return localSession.methodName;
if (localSession is MethodStreamSession) return localSession.methodName;
if (localSession is FutureCallSession) return localSession.futureCallName;
if (localSession is InternalSession) return null;
if (localSession is StreamingSession) return null;

throw Exception('Unknown session type: $session');
}

String _endpointForSession(Session session) {
var localSession = session;
if (localSession is MethodCallSession) return localSession.endpointName;
if (localSession is MethodStreamSession) return localSession.endpointName;
if (localSession is FutureCallSession) return 'FutureCallSession';
if (localSession is InternalSession) return 'InternalSession';
if (localSession is StreamingSession) return 'StreamingSession';

throw Exception('Unknown session type: $session');
}
68 changes: 8 additions & 60 deletions packages/serverpod/lib/src/server/log_manager/log_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,71 +22,19 @@ class LogSettingsManager {
}
}

/// Gets the log settings for a [MethodCallSession].
LogSettings _getLogSettingsForMethodCallSession(
String endpoint,
String method,
) {
var settings = _methodOverrides['$endpoint.$method'];
if (settings != null) return settings;

settings = _endpointOverrides[endpoint];
if (settings != null) return settings;

return _runtimeSettings.logSettings;
}

/// Gets the log settings for a [InternalSession].
LogSettings _getLogSettingsForInternalSession() {
return _runtimeSettings.logSettings;
}

/// Gets the log settings for a [StreamingSession].
LogSettings _getLogSettingsForStreamingSession({required String endpoint}) {
var settings = _endpointOverrides[endpoint];
if (settings != null) return settings;

return _runtimeSettings.logSettings;
}

/// Gets the log settings for a [FutureCallSession].
LogSettings _getLogSettingsForFutureCallSession(String call) {
return _runtimeSettings.logSettings;
}

/// Returns the [LogSettings] for a specific session.
LogSettings getLogSettingsForSession(Session session) {
var localSession = session;
if (localSession is MethodCallSession) {
return _getLogSettingsForMethodCallSession(
localSession.endpointName,
localSession.methodName,
);
}
var endpoint = session.endpointName;
var method = session.methodName;

if (localSession is StreamingSession) {
return _getLogSettingsForStreamingSession(
endpoint: localSession.endpointName,
);
}
LogSettings? settings;

if (localSession is InternalSession) {
return _getLogSettingsForInternalSession();
}

if (localSession is FutureCallSession) {
return _getLogSettingsForFutureCallSession(
localSession.futureCallName,
);
}
if (method != null) settings = _methodOverrides['$endpoint.$method'];
if (settings != null) return settings;

if (localSession is MethodStreamSession) {
return _getLogSettingsForMethodCallSession(
localSession.endpointName,
localSession.methodName,
);
}
settings = _endpointOverrides[endpoint];
if (settings != null) return settings;

throw Exception('Unknown session type: $session');
return _runtimeSettings.logSettings;
}
}
67 changes: 43 additions & 24 deletions packages/serverpod/lib/src/server/session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ abstract class Session {

late final SessionLogManager? _logManager;

/// Endpoint that triggered this session.
final String endpointName;

/// Method that triggered this session, if any.
final String? methodName;

/// Creates a new session. This is typically done internally by the [Server].
Session({
UuidValue? sessionId,
Expand All @@ -106,6 +112,8 @@ abstract class Session {
HttpRequest? httpRequest,
WebSocket? webSocket,
required this.enableLogging,
required this.endpointName,
this.methodName,
}) : _authenticationKey = authenticationKey,
sessionId = sessionId ?? const Uuid().v4obj() {
_startTime = DateTime.now();
Expand Down Expand Up @@ -232,7 +240,7 @@ class InternalSession extends Session {
InternalSession({
required super.server,
super.enableLogging = true,
});
}) : super(endpointName: 'InternalSession');
}

/// When a call is made to the [Server] a [MethodCallSession] object is created.
Expand All @@ -249,10 +257,16 @@ class MethodCallSession extends Session {
late final Map<String, dynamic> queryParameters;

/// The name of the called [Endpoint].
late final String endpointName;
late final String _endpointName;

@override
String get endpointName => _endpointName;

/// The name of the method that is being called.
late final String methodName;
late final String _methodName;

@override
String get methodName => _methodName;

/// The [HttpRequest] associated with the call.
final HttpRequest httpRequest;
Expand All @@ -266,7 +280,7 @@ class MethodCallSession extends Session {
required this.httpRequest,
String? authenticationKey,
super.enableLogging = true,
}) {
}) : super(endpointName: 'MethodCall') {
// Read query parameters
var queryParameters = <String, dynamic>{};
if (body != '' && body != 'null') {
Expand All @@ -280,16 +294,16 @@ class MethodCallSession extends Session {
if (path.contains('/')) {
// Using the new path format (for OpenAPI)
var pathComponents = path.split('/');
endpointName = pathComponents[0];
methodName = pathComponents[1];
_endpointName = pathComponents[0];
_methodName = pathComponents[1];
} else {
// Using the standard format with query parameters
endpointName = path;
_endpointName = path;
var methodName = queryParameters['method'];
if (methodName == null && path == 'webserver') {
this.methodName = '';
_methodName = '';
} else if (methodName != null) {
this.methodName = methodName;
_methodName = methodName;
} else {
throw FormatException(
'No method name specified in call to $endpointName',
Expand All @@ -307,24 +321,24 @@ class MethodCallSession extends Session {
/// associated with the current connection and provides easy access to the
/// database.
class MethodStreamSession extends Session {
/// The name of the method that is being called.
final String methodName;

/// The name of the endpoint that is being called.
final String endpointName;

/// The connection id that uniquely identifies the stream.
final UuidValue connectionId;

final String _methodName;

@override
String get methodName => _methodName;

/// Creates a new [MethodStreamSession].
MethodStreamSession({
required super.server,
required super.enableLogging,
required super.authenticationKey,
required this.endpointName,
required this.methodName,
required super.endpointName,
required String methodName,
required this.connectionId,
});
}) : _methodName = methodName,
super(methodName: methodName);
}

/// When a web socket connection is opened to the [Server] a [StreamingSession]
Expand All @@ -346,22 +360,27 @@ class StreamingSession extends Session {
/// Set if there is an open session log.
int? sessionLogId;

/// The endpoint that is currently being processed.
String endpointName;

/// The id of the current incoming message being processed. Increments by 1
/// for each message passed to an endpoint for processing.
int currentMessageId = 0;

String _endpointName;

/// The name of the endpoint that is being called.
set endpointName(String endpointName) => _endpointName = endpointName;

@override
String get endpointName => _endpointName;

/// Creates a new [Session] for the web socket stream.
StreamingSession({
required super.server,
required this.uri,
required this.httpRequest,
required this.webSocket,
this.endpointName = 'StreamingSession',
super.endpointName = 'StreamingSession',
super.enableLogging = true,
}) {
}) : _endpointName = endpointName {
// Read query parameters
var queryParameters = <String, String>{};
queryParameters.addAll(uri.queryParameters);
Expand Down Expand Up @@ -390,7 +409,7 @@ class FutureCallSession extends Session {
required super.server,
required this.futureCallName,
super.enableLogging = true,
});
}) : super(endpointName: 'FutureCall', methodName: futureCallName);
}

/// Collects methods for accessing cloud storage.
Expand Down

0 comments on commit 68186d7

Please sign in to comment.