Skip to content

Commit

Permalink
fix: tests reaching the web (#1161)
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-garcia committed Aug 25, 2021
1 parent bccf22b commit 58cb042
Show file tree
Hide file tree
Showing 75 changed files with 326 additions and 539 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- Improved SDK diagnostic logs ([#1161](https://github.com/getsentry/sentry-dotnet/pull/1161))
- Add Scope observer to SentryOptions ([#1153](https://github.com/getsentry/sentry-dotnet/pull/1153))

### Fixes
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/Sentry.Benchmarks/FakeHttpClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class FakeHttpClientFactory : ISentryHttpClientFactory
public HttpClient Create(SentryOptions options) => new(new FakeMessageHandler());
}

internal class FakeMessageHandler : HttpMessageHandler
internal class FakeMessageHandler : DelegatingHandler
{
private readonly Task<HttpResponseMessage> _result;

Expand Down
3 changes: 1 addition & 2 deletions samples/Sentry.Samples.AspNetCore.Basic/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public static IWebHost BuildWebHost(string[] args) =>
// will include the Log messages and any custom scope modifications
// as exemplified above.
throw new Exception(
exceptionMessage ?? "An exception thrown from the ASP.NET Core pipeline"
);
exceptionMessage ?? "An exception thrown from the ASP.NET Core pipeline");
});
});
})
Expand Down
3 changes: 1 addition & 2 deletions src/Sentry.AspNet/Internal/SystemWebRequestEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ public SystemWebRequestEventProcessor(IRequestPayloadExtractor payloadExtractor,
{
@event.Sdk.AddPackage(
$"nuget:{SdkVersion.Name}",
SdkVersion.Version
);
SdkVersion.Version);
}

return @event;
Expand Down
3 changes: 1 addition & 2 deletions src/Sentry.AspNet/SentryAspNetOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public static void AddAspNet(this SentryOptions options, RequestSize maxRequestB
var payloadExtractor = new RequestBodyExtractionDispatcher(
new IRequestPayloadExtractor[] {new FormRequestPayloadExtractor(), new DefaultRequestPayloadExtractor()},
options,
() => maxRequestBodySize
);
() => maxRequestBodySize);

var eventProcessor = new SystemWebRequestEventProcessor(payloadExtractor, options);

Expand Down
6 changes: 2 additions & 4 deletions src/Sentry.AspNetCore/SentryTracingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ public SentryTracingMiddleware(
_options.DiagnosticLogger?.LogInfo(
"Started transaction with span ID '{0}' and trace ID '{1}'.",
transaction.SpanId,
transaction.TraceId
);
transaction.TraceId);

return transaction;
}
Expand Down Expand Up @@ -145,8 +144,7 @@ public async Task InvokeAsync(HttpContext context)
_options.DiagnosticLogger?.LogDebug(
"Changed transaction name from '{0}' to '{1}' after request pipeline executed.",
transaction.Name,
transactionName
);
transactionName);
}

transaction.Name = transactionName;
Expand Down
3 changes: 1 addition & 2 deletions src/Sentry/Breadcrumb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ public void WriteTo(Utf8JsonWriter writer)

writer.WriteString(
"timestamp",
Timestamp.ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffZ", DateTimeFormatInfo.InvariantInfo)
);
Timestamp.ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffZ", DateTimeFormatInfo.InvariantInfo));

writer.WriteStringIfNotWhiteSpace("message", Message);
writer.WriteStringIfNotWhiteSpace("type", Type);
Expand Down
3 changes: 1 addition & 2 deletions src/Sentry/Dsn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ public static Dsn Parse(string dsn)
path,
secretKey,
publicKey,
apiBaseUri
);
apiBaseUri);
}

public static Dsn? TryParse(string? dsn)
Expand Down
94 changes: 30 additions & 64 deletions src/Sentry/GlobalSessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,13 @@ public GlobalSessionManager(
?? Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Sentry",
_options.Dsn!.GetHashString()
);
_options.Dsn!.GetHashString());

Directory.CreateDirectory(directoryPath);

_options.DiagnosticLogger?.LogDebug(
"Created directory for installation ID file ({0}).",
directoryPath
);
directoryPath);

var filePath = Path.Combine(directoryPath, ".installation");

Expand All @@ -78,16 +76,14 @@ public GlobalSessionManager(
{
_options.DiagnosticLogger?.LogDebug(
"File containing installation ID does not exist ({0}).",
filePath
);
filePath);
}
catch (DirectoryNotFoundException)
{
// on PS4 we're seeing CreateDirectory work but ReadAllText throw DirectoryNotFoundException
_options.DiagnosticLogger?.LogDebug(
"Directory containing installation ID does not exist ({0}).",
filePath
);
filePath);
}

// Generate new installation ID and store it in a file
Expand All @@ -96,8 +92,7 @@ public GlobalSessionManager(

_options.DiagnosticLogger?.LogDebug(
"Saved installation ID '{0}' to file '{1}'.",
id, filePath
);
id, filePath);

return id;
}
Expand All @@ -107,8 +102,7 @@ public GlobalSessionManager(
{
_options.DiagnosticLogger?.LogError(
"Failed to resolve persistent installation ID.",
ex
);
ex);

return null;
}
Expand All @@ -130,8 +124,7 @@ public GlobalSessionManager(
if (string.IsNullOrWhiteSpace(installationId))
{
_options.DiagnosticLogger?.LogError(
"Failed to find an appropriate network interface for installation ID."
);
"Failed to find an appropriate network interface for installation ID.");

return null;
}
Expand All @@ -142,8 +135,7 @@ public GlobalSessionManager(
{
_options.DiagnosticLogger?.LogError(
"Failed to resolve hardware installation ID.",
ex
);
ex);

return null;
}
Expand Down Expand Up @@ -182,14 +174,12 @@ internal string GetMachineNameInstallationId() =>
{
_options.DiagnosticLogger?.LogDebug(
"Resolved installation ID '{0}'.",
id
);
id);
}
else
{
_options.DiagnosticLogger?.LogDebug(
"Failed to resolve installation ID."
);
"Failed to resolve installation ID.");
}

return _resolvedInstallationId = id;
Expand All @@ -214,25 +204,18 @@ private void PersistSession(SessionUpdate update, DateTimeOffset? pauseTimestamp

_options.DiagnosticLogger?.LogDebug(
"Created persistence directory for session file '{0}'.",
_persistenceDirectoryPath
);
_persistenceDirectoryPath);

var filePath = Path.Combine(_persistenceDirectoryPath, PersistedSessionFileName);

var persistedSessionUpdate = new PersistedSessionUpdate(update, pauseTimestamp);
persistedSessionUpdate.WriteToFile(filePath);

_options.DiagnosticLogger?.LogInfo(
"Persisted session to a file '{0}'.",
filePath
);
_options.DiagnosticLogger?.LogDebug("Persisted session to a file '{0}'.", filePath);
}
catch (Exception ex)
{
_options.DiagnosticLogger?.LogError(
"Failed to persist session on the file system.",
ex
);
_options.DiagnosticLogger?.LogError("Failed to persist session on the file system.", ex);
}
}

Expand All @@ -256,33 +239,29 @@ private void DeletePersistedSession()
{
_options.DiagnosticLogger?.LogDebug(
"Deleting persisted session file with contents: {0}",
File.ReadAllText(filePath)
);
File.ReadAllText(filePath));
}
catch (Exception ex)
{
_options.DiagnosticLogger?.LogError(
"Failed to read the contents of persisted session file '{0}'.",
ex,
filePath
);
filePath);
}
}

File.Delete(filePath);

_options.DiagnosticLogger?.LogInfo(
"Deleted persisted session file '{0}'.",
filePath
);
filePath);
}
catch (Exception ex)
{
_options.DiagnosticLogger?.LogError(
"Failed to delete persisted session from the file system: '{0}'",
ex,
filePath
);
filePath);
}
}

Expand Down Expand Up @@ -316,16 +295,14 @@ private void DeletePersistedSession()
_ when recoveredUpdate.PauseTimestamp is not null => SessionEndStatus.Exited,
{ } crashedLastRun => crashedLastRun() ? SessionEndStatus.Crashed : SessionEndStatus.Abnormal,
_ => SessionEndStatus.Abnormal
}
);
});
}
catch (IOException ioEx) when (ioEx is FileNotFoundException or DirectoryNotFoundException)
{
// Not a notable error
_options.DiagnosticLogger?.LogDebug(
"A persisted session does not exist at {0}.",
filePath
);
filePath);

return null;
}
Expand All @@ -334,8 +311,7 @@ private void DeletePersistedSession()
_options.DiagnosticLogger?.LogError(
"Failed to recover persisted session from the file system '{0}'.",
ex,
filePath
);
filePath);

return null;
}
Expand All @@ -349,8 +325,7 @@ private void DeletePersistedSession()
{
// Release health without release is just health (useless)
_options.DiagnosticLogger?.LogError(
"Failed to start a session because there is no release information."
);
"Failed to start a session because there is no release information.");

return null;
}
Expand All @@ -367,17 +342,15 @@ private void DeletePersistedSession()
if (previousSession is not null)
{
_options.DiagnosticLogger?.LogWarning(
"Starting a new session while an existing one is still active."
);
"Starting a new session while an existing one is still active.");

// End previous session
EndSession(previousSession, _clock.GetUtcNow(), SessionEndStatus.Exited);
}

_options.DiagnosticLogger?.LogInfo(
"Started new session (SID: {0}; DID: {1}).",
session.Id, session.DistinctId
);
session.Id, session.DistinctId);

var update = session.CreateUpdate(true, _clock.GetUtcNow());

Expand All @@ -390,8 +363,7 @@ private SessionUpdate EndSession(Session session, DateTimeOffset timestamp, Sess
{
_options.DiagnosticLogger?.LogInfo(
"Ended session (SID: {0}; DID: {1}) with status '{2}'.",
session.Id, session.DistinctId, status
);
session.Id, session.DistinctId, status);

var update = session.CreateUpdate(false, timestamp, status);

Expand All @@ -406,8 +378,7 @@ private SessionUpdate EndSession(Session session, DateTimeOffset timestamp, Sess
if (session is null)
{
_options.DiagnosticLogger?.LogDebug(
"Failed to end session because there is none active."
);
"Failed to end session because there is none active.");

return null;
}
Expand All @@ -433,8 +404,7 @@ public IReadOnlyList<SessionUpdate> ResumeSession()
if (_lastPauseTimestamp is not { } sessionPauseTimestamp)
{
_options.DiagnosticLogger?.LogDebug(
"Attempted to resume a session, but the current session hasn't been paused."
);
"Attempted to resume a session, but the current session hasn't been paused.");

return Array.Empty<SessionUpdate>();
}
Expand All @@ -450,8 +420,7 @@ public IReadOnlyList<SessionUpdate> ResumeSession()
_options.DiagnosticLogger?.LogDebug(
"Paused session has been paused for {0}, which is longer than the configured timeout. " +
"Starting a new session instead of resuming this one.",
pauseDuration
);
pauseDuration);

var updates = new List<SessionUpdate>(2);

Expand All @@ -472,8 +441,7 @@ public IReadOnlyList<SessionUpdate> ResumeSession()

_options.DiagnosticLogger?.LogDebug(
"Paused session has been paused for {0}, which is shorter than the configured timeout.",
pauseDuration
);
pauseDuration);

return Array.Empty<SessionUpdate>();
}
Expand All @@ -489,8 +457,7 @@ public IReadOnlyList<SessionUpdate> ResumeSession()
if (session.ErrorCount > 1)
{
_options.DiagnosticLogger?.LogDebug(
"Reported an error on a session that already contains errors. Not creating an update."
);
"Reported an error on a session that already contains errors. Not creating an update.");

return null;
}
Expand All @@ -499,8 +466,7 @@ public IReadOnlyList<SessionUpdate> ResumeSession()
}

_options.DiagnosticLogger?.LogDebug(
"Failed to report an error on a session because there is none active."
);
"Failed to report an error on a session because there is none active.");

return null;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Sentry/IHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public interface IHub :
/// </summary>
ITransaction StartTransaction(
ITransactionContext context,
IReadOnlyDictionary<string, object?> customSamplingContext
);
IReadOnlyDictionary<string, object?> customSamplingContext);

/// <summary>
/// Binds specified exception the specified span.
Expand Down
Loading

0 comments on commit 58cb042

Please sign in to comment.