From cf8af8e516dc5df915b275c7f280848c315188c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Go=C5=82=C4=99biowski?= Date: Mon, 17 Feb 2025 12:55:41 +0100 Subject: [PATCH] Sentry Breadcrumb logging --- src/Cody.Core/Agent/NotificationHandlers.cs | 2 ++ src/Cody.Core/Logging/SentryLog.cs | 14 +++++--------- src/Cody.VisualStudio/Client/AgentClient.cs | 2 ++ src/Cody.VisualStudio/Client/TraceJsonRpc.cs | 12 ++++++++++++ src/Cody.VisualStudio/CodyPackage.cs | 2 +- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/Cody.Core/Agent/NotificationHandlers.cs b/src/Cody.Core/Agent/NotificationHandlers.cs index d7465786..8db344d2 100644 --- a/src/Cody.Core/Agent/NotificationHandlers.cs +++ b/src/Cody.Core/Agent/NotificationHandlers.cs @@ -5,6 +5,7 @@ using Cody.Core.Trace; using Cody.Core.Workspace; using Newtonsoft.Json.Linq; +using Sentry; using System; using System.Threading.Tasks; @@ -64,6 +65,7 @@ public void Debug(string channel, string message, string level) { //_logger.Debug($"[{channel} {message}]"); trace.TraceEvent("AgentDebug", message); + SentrySdk.AddBreadcrumb(message, "DebugFromAgent", "debug"); } [AgentCallback("webview/registerWebview")] diff --git a/src/Cody.Core/Logging/SentryLog.cs b/src/Cody.Core/Logging/SentryLog.cs index 177ec002..3e256739 100644 --- a/src/Cody.Core/Logging/SentryLog.cs +++ b/src/Cody.Core/Logging/SentryLog.cs @@ -1,6 +1,7 @@ using Cody.Core.Common; using Sentry; using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; @@ -29,14 +30,9 @@ public void Error(string message, Exception ex, [CallerMemberName] string caller public void Error(string message, [CallerMemberName] string callerName = "") { - SentrySdk.CaptureMessage(message, scope => - { - scope.Contexts[ErrorData] = new - { - Message = message, - CallerName = callerName, - }; - }); + SentrySdk.AddBreadcrumb(message, + data: new Dictionary { ["callerName"] = callerName }, + level: BreadcrumbLevel.Error); } private static DateTime GetLinkerBuildTime(Assembly assembly) @@ -83,7 +79,7 @@ public static void Initialize() { options.Dsn = "https://d129345ba8e1848a01435eb2596ca899@o19358.ingest.us.sentry.io/4508375896752129"; options.IsGlobalModeEnabled = true; - options.MaxBreadcrumbs = 10; + options.MaxBreadcrumbs = 50; options.Environment = env; options.Release = "cody-vs@" + version.ToString(); options.SetBeforeSend(se => diff --git a/src/Cody.VisualStudio/Client/AgentClient.cs b/src/Cody.VisualStudio/Client/AgentClient.cs index 6c8dbe11..974a67cf 100644 --- a/src/Cody.VisualStudio/Client/AgentClient.cs +++ b/src/Cody.VisualStudio/Client/AgentClient.cs @@ -4,6 +4,7 @@ using Cody.Core.Trace; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Serialization; +using Sentry; using StreamJsonRpc; using System; using System.Threading.Tasks; @@ -97,6 +98,7 @@ private void OnErrorReceived(object sender, string error) { //agentLog.Debug(error); trace.TraceEvent("AgentErrorOutput", error); + SentrySdk.AddBreadcrumb(error, "AgentErrorOutput"); } private IAgentConnector CreateConnector() diff --git a/src/Cody.VisualStudio/Client/TraceJsonRpc.cs b/src/Cody.VisualStudio/Client/TraceJsonRpc.cs index 3c74ec53..8634c884 100644 --- a/src/Cody.VisualStudio/Client/TraceJsonRpc.cs +++ b/src/Cody.VisualStudio/Client/TraceJsonRpc.cs @@ -1,4 +1,5 @@ using Cody.Core.Trace; +using Sentry; using StreamJsonRpc; using StreamJsonRpc.Protocol; using StreamJsonRpc.Reflection; @@ -14,11 +15,22 @@ public TraceJsonRpc(IJsonRpcMessageHandler messageHandler) : base(messageHandler void IJsonRpcTracingCallbacks.OnMessageSerialized(JsonRpcMessage message, object encodedMessage) { trace.TraceEvent("ToAgent", encodedMessage); + if (message is JsonRpcRequest request) + { + SentrySdk.AddBreadcrumb(request.Method, "ToAgent"); + } } void IJsonRpcTracingCallbacks.OnMessageDeserialized(JsonRpcMessage message, object encodedMessage) { trace.TraceEvent("FromAgent", encodedMessage); + if (message is JsonRpcRequest request) + { + if (request.Method == "debug/message") return; + SentrySdk.AddBreadcrumb(request.Method, "FromAgent"); + } } + + } } diff --git a/src/Cody.VisualStudio/CodyPackage.cs b/src/Cody.VisualStudio/CodyPackage.cs index 51d469e2..8eb87a3d 100644 --- a/src/Cody.VisualStudio/CodyPackage.cs +++ b/src/Cody.VisualStudio/CodyPackage.cs @@ -158,7 +158,7 @@ private void ReportSentryVsVersion() scope.SetTag("agent", VersionService.Agent); }); - VsShellUtilities.ShutdownToken.Register(() => SentrySdk.ConfigureScope(s => s.SetTag("isShuttingDown", "true"))); + VsShellUtilities.ShutdownToken.Register(() => SentrySdk.AddBreadcrumb("VS is shutting down")); } private static void InitializeTrace()