Support logging network tasks that don't use URLSession - #382
Draft
ffittschen wants to merge 2 commits into
Draft
ffittschen wants to merge 2 commits into
ffittschen wants to merge 2 commits into
Conversation
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".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR lets integrations that don't go through
URLSession, such as gRPC calls over SwiftNIO, log their tasks throughNetworkLogger. Today they can only callLoggerStore.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.
NetworkLoggermethods for tasks without aURLSessionTasklogTaskCreatedandlogTaskCompletedsend the samenetworkTaskCreatedandnetworkTaskCompletedevents as the URLSession path, throughsend(_:). So the task shows as pending until it completes, and the filters, redaction andwillHandleEventall apply.2. Descriptions of custom-domain errors in the console
Failures outside
NSURLErrorDomainand 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 storedNSErrordescription, 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.
Examples/PulseGRPCExample.swiftpmin Xcode and run it on an iOS simulator.POST /echo.Echo/Getshows200 OK, and that its inspector shows the request and response bodies as JSON.NOT_FOUND (5): No echo named 'ghost'andDEADLINE_EXCEEDED (4): RPC timed out…in red instead of "Error", and that each inspector shows the full description.main.Limitations
NSError's debug description, because the store only keepserrorDebugDescription. A Swift error that doesn't bridge to anNSErrorwith a description still shows "Error".