Skip to content

Commit

Permalink
Sentry time bomb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Gołębiowski committed Feb 11, 2025
1 parent 311addf commit 6393888
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
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
34 changes: 33 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,41 @@ 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);

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.Now;
}
}

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

0 comments on commit 6393888

Please sign in to comment.