Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sentry time bomb #229

Merged
merged 2 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/Cody.Core/Cody.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@
<PackageReference Include="Newtonsoft.Json">
<Version>13.0.1</Version>
</PackageReference>
<PackageReference Include="Sentry">
<Version>4.13.0</Version>
</PackageReference>
<PackageReference Include="Sentry" Version="4.13.0" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
36 changes: 35 additions & 1 deletion src/Cody.Core/Logging/SentryLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Sentry;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
Expand All @@ -12,6 +13,7 @@ public class SentryLog : ISentryLog
{
public const string CodyAssemblyPrefix = "Cody.";
public const string ErrorData = "ErrorData";
public const int DaysToLogToSentry = 180;

public void Error(string message, Exception ex, [CallerMemberName] string callerName = "")
{
Expand All @@ -37,11 +39,43 @@ public void Error(string message, [CallerMemberName] string callerName = "")
});
}

private static DateTime GetLinkerBuildTime(Assembly assembly)
{
try
{
var filePath = assembly.Location;
const int PeHeaderOffset = 60;
const int LinkerTimestampOffset = 8;

var buffer = new byte[2048];

using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
stream.Read(buffer, 0, buffer.Length);
tomaszgolebiowski marked this conversation as resolved.
Show resolved Hide resolved
}

var offset = BitConverter.ToInt32(buffer, PeHeaderOffset);
var secondsSince1970 = BitConverter.ToInt32(buffer, offset + LinkerTimestampOffset);
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

var linkTimeUtc = epoch.AddSeconds(secondsSince1970);
return linkTimeUtc;
}
catch
{
return DateTime.UtcNow;
}
}

public static void Initialize()
{
if (!Configuration.IsDebug && !Debugger.IsAttached)
{
var version = Assembly.GetExecutingAssembly().GetName().Version;
var assembly = Assembly.GetExecutingAssembly();
var buildDate = GetLinkerBuildTime(assembly);
if (Math.Abs((DateTime.UtcNow - buildDate).Days) > DaysToLogToSentry) return;

var version = assembly.GetName().Version;
string env = "dev";
if (version.Minor != 0) env = version.Minor % 2 != 0 ? "preview" : "production";

Expand Down