Skip to content

Commit

Permalink
Enable message logging in Sentry (#215)
Browse files Browse the repository at this point in the history
## Test plan
Check message availability in Sentry panel
<!-- REQUIRED; info at
https://docs-legacy.sourcegraph.com/dev/background-information/testing_principles
-->

Co-authored-by: Tomasz Gołębiowski <tgolebiowski@virtuslab.com>
  • Loading branch information
tomaszgolebiowski and Tomasz Gołębiowski authored Feb 4, 2025
1 parent 72a0926 commit 7023f6a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Cody.Core/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void Error(string message, [CallerMemberName] string callerName = "")
// TODO: _fileLogger.Error(customMessage);
DebugWrite(customMessage);
_outputWindowPane?.Error(message, callerName);
_sentryLog?.Error(customMessage, callerName);
_sentryLog?.Error(message, callerName);
_testLogger?.WriteLog(message, "ERROR", callerName);
}

Expand All @@ -87,7 +87,7 @@ public void Error(string message, Exception ex, [CallerMemberName] string caller
// TODO: _fileLogger.Error(originalException, customMessage);
DebugWrite(customMessage);
_outputWindowPane?.Error(outputMessage, callerName);
_sentryLog?.Error(outputMessage, originalException, callerName);
_sentryLog?.Error(message, originalException, callerName);
_testLogger?.WriteLog(message, "ERROR", callerName);
}

Expand Down
17 changes: 13 additions & 4 deletions src/Cody.Core/Logging/SentryLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,29 @@ namespace Cody.Core.Logging
public class SentryLog : ISentryLog
{
public const string CodyAssemblyPrefix = "Cody.";
public const string ErrorData = "ErrorData";

public void Error(string message, Exception ex, [CallerMemberName] string callerName = "")
{
SentrySdk.CaptureException(ex, scope =>
{
scope.SetExtra(nameof(callerName), callerName);
scope.SetExtra(nameof(message), message);
scope.Contexts[ErrorData] = new
{
Message = message,
CallerName = callerName,
};
});
}

public void Error(string message, [CallerMemberName] string callerName = "")
{
SentrySdk.CaptureMessage(message, scope =>
{
scope.SetExtra(nameof(callerName), callerName);
scope.Contexts[ErrorData] = new
{
Message = message,
CallerName = callerName,
};
});
}

Expand All @@ -49,9 +57,10 @@ public static void Initialize()
if (se.Exception?.Source?.StartsWith(CodyAssemblyPrefix) ?? false) return se;
if (se.Exception?.InnerException?.Source?.StartsWith(CodyAssemblyPrefix) ?? false) return se;
if (se.Message != null) return se;
if (se.Contexts.ContainsKey(ErrorData)) return se;
if (se.SentryExceptions == null) return se;

foreach(var ex in se.SentryExceptions)
foreach (var ex in se.SentryExceptions)
{
if (ex.Module?.StartsWith(CodyAssemblyPrefix) ?? false) return se;
if (ex.Stacktrace != null)
Expand Down
6 changes: 3 additions & 3 deletions src/Cody.VisualStudio/Client/AgentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public async Task<IAgentService> Initialize(ClientInfo clientInfo)

private void OnDisconnected(object sender, JsonRpcDisconnectedEventArgs e)
{
if(e.Exception != null)
if (e.Exception != null)
log.Error($"Agent disconnected due to {e.Description} (reason: {e.Reason})", e.Exception);
else
log.Info($"Agent disconnected due to {e.Description} (reason: {e.Reason})");
log.Error($"Agent disconnected due to {e.Description} (reason: {e.Reason})");
}

private void CreateAgentService()
Expand Down Expand Up @@ -139,7 +139,7 @@ public void Stop()

private void DisconnectInternal()
{
jsonRpc.Dispose();
if (!jsonRpc.IsDisposed) jsonRpc.Dispose();
connector.ErrorReceived -= OnErrorReceived;
connector.Disconnected -= OnAgentDisconnected;
connector.Disconnect();
Expand Down
1 change: 0 additions & 1 deletion src/Cody.VisualStudio/CodyPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ private void ReportSentryVsVersion()
{
scope.SetTag("vs", VsVersionService.DisplayVersion);
scope.SetTag("agent", VersionService.Agent);
SentrySdk.CaptureMessage("Initialized");
});
}

Expand Down

0 comments on commit 7023f6a

Please sign in to comment.