Skip to content

Support logging network tasks that don't use URLSession - #382

Draft
ffittschen wants to merge 2 commits into
kean:mainfrom
ffittschen:feat/network-logger-manual-tasks
Draft

ffittschen wants to merge 2 commits into
kean:mainfrom
ffittschen:feat/network-logger-manual-tasks

Conversation

@ffittschen

Copy link
Copy Markdown

This PR lets integrations that don't go through URLSession, such as gRPC calls over SwiftNIO, log their tasks through NetworkLogger. Today they can only call LoggerStore.storeRequest, which stores the task once it has finished and bypasses the logger's filters and redaction. The two commits can be reviewed separately.

1. NetworkLogger methods for tasks without a URLSessionTask

logTaskCreated and logTaskCompleted send the same networkTaskCreated and networkTaskCompleted events as the URLSession path, through send(_:). So the task shows as pending until it completes, and the filters, redaction and willHandleEvent all apply.

2. Descriptions of custom-domain errors in the console

Failures outside NSURLErrorDomain and the decoding domain all show as "Error" in the console, and that's how every non-URLSession failure looks. The console now shows the first line of the stored NSError description, cut to 40 characters, e.g. NOT_FOUND (5): user not found. There's no schema change, and URL, decoding and status-code failures look as before. This also goes partway towards #341.

PulseGRPC uses both to log grpc-swift-2 calls. Part of #378.

How to test

PulseGRPC's example app takes Pulse from a branch that includes this PR. Its gRPC calls go to an in-process server, so they need no network.

  1. Clone PulseGRPC, open Examples/PulseGRPCExample.swiftpm in Xcode and run it on an iOS simulator.
  2. Tap "Unary OK", then "Show Console". Assert that POST /echo.Echo/Get shows 200 OK, and that its inspector shows the request and response bodies as JSON.
  3. Close the console, tap "Unary NOT_FOUND" and "Deadline exceeded", and open the console again. Assert that the two new entries show NOT_FOUND (5): No echo named 'ghost' and DEADLINE_EXCEEDED (4): RPC timed out… in red instead of "Error", and that each inspector shows the full description.
  4. Tap "GET api.github.com". Assert that the REST entry looks the same as on main.

Limitations

  • The description is parsed from NSError's debug description, because the store only keeps errorDebugDescription. A Swift error that doesn't bridge to an NSError with a description still shows "Error".

Add NetworkLogger.logTaskCreated(taskId:taskType:request:label:taskDescription:)
and NetworkLogger.logTaskCompleted(taskId:taskType:request:response:error:
requestBody:responseBody:metrics:label:taskDescription:) for tasks that
don't go through URLSession, such as gRPC calls made over SwiftNIO.

The caller owns the task ID, timing and bodies; NetworkLogger keeps no
new state. Both methods build the same networkTaskCreated and
networkTaskCompleted events as the URLSessionTask-based methods and go
through send(_:), so include/exclude filters, header, query item and
data field redaction, and willHandleEvent all apply. The task shows as
pending until it completes. The label defaults to Configuration.label.

Document the methods in the network logging article.
The console showed "Error" for every error outside of NSURLErrorDomain
and the decoding error domain, so failures from other stacks, such as
gRPC, all looked the same in the list.

For these errors, show the error's localized description, taken from
the stored NSError debug description (Error Domain=… Code=…
"<description>" UserInfo=…). Only the first line is shown, truncated
to 40 characters. Errors without a localized description, and Swift
errors whose debug description isn't an NSError one, still show
"Error".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant