Skip to content

Commit 5d78946

Browse files
committed
fix(Core): force Sentry to fully use our http client
1 parent 5c0fabd commit 5d78946

2 files changed

Lines changed: 202 additions & 176 deletions

File tree

SnapX.Core/SnapX.cs

Lines changed: 189 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using SnapX.Core.Upload;
1616
using SnapX.Core.Utils;
1717
using SnapX.Core.Utils.Extensions;
18+
using SnapX.Core.Utils.Miscellaneous;
1819
#if WINDOWS
1920
using SnapX.Core.Utils.Native;
2021
#endif
@@ -464,191 +465,35 @@ private static void Run()
464465

465466
#if DEBUG
466467
options.Environment = "development";
467-
options.CreateHttpMessageHandler = () => new LoggingHttpMessageHandler(new SentryHttpMessageHandler(), DebugHelper.Logger);
468+
options.CreateHttpMessageHandler = () => new LoggingHttpMessageHandler(new SentryHttpMessageHandler(HttpClientFactory.Handler), DebugHelper.Logger);
468469
options.DisableSentryHttpMessageHandler = true;
469470
#else
470471
options.Environment = "production";
472+
options.CreateHttpMessageHandler = () => HttpClientFactory.Handler;
471473
#endif
474+
options.ConfigureClient = client =>
475+
{
476+
var snapXHttpClient = HttpClientFactory.Get();
477+
478+
foreach (var header in snapXHttpClient.DefaultRequestHeaders)
479+
{
480+
client.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value);
481+
}
482+
483+
client.DefaultRequestVersion = snapXHttpClient.DefaultRequestVersion;
484+
client.DefaultVersionPolicy = snapXHttpClient.DefaultVersionPolicy;
485+
};
472486
// VLCException includes multiple paths with username
473487
// For full transparency, I discovered this issue on my computer.
474488
// No other users are effected to my knowledge.
475-
options.SetBeforeSend((sentryEvent, hint) =>
489+
options.SetBeforeSend((sentryEvent, _) =>
476490
{
477491
if (sentryEvent.Exception != null
478492
&& !string.IsNullOrEmpty(sentryEvent.Exception.Message))
479493
{
480494
if (sentryEvent.Exception.Message.Contains(Environment.UserName)) return null;
481495
}
482-
483-
var json = new JsonObject
484-
{
485-
["event_id"] = sentryEvent.EventId.ToString(),
486-
["timestamp"] = sentryEvent.Timestamp.ToString("o") // ISO 8601 format
487-
};
488-
489-
if (sentryEvent.Message != null)
490-
{
491-
json["logentry"] = new JsonObject
492-
{
493-
["message"] = sentryEvent.Message.Formatted
494-
};
495-
}
496-
497-
if (!string.IsNullOrWhiteSpace(sentryEvent.Logger))
498-
{
499-
json["logger"] = sentryEvent.Logger;
500-
}
501-
502-
if (!string.IsNullOrWhiteSpace(sentryEvent.Platform))
503-
{
504-
json["platform"] = sentryEvent.Platform;
505-
}
506-
507-
if (!string.IsNullOrWhiteSpace(sentryEvent.ServerName))
508-
{
509-
json["server_name"] = sentryEvent.ServerName;
510-
}
511-
512-
if (!string.IsNullOrWhiteSpace(sentryEvent.Release))
513-
{
514-
json["release"] = sentryEvent.Release;
515-
}
516-
517-
if (!string.IsNullOrWhiteSpace(sentryEvent.Distribution))
518-
{
519-
json["dist"] = sentryEvent.Distribution;
520-
}
521-
522-
if (sentryEvent.SentryExceptions != null)
523-
{
524-
var exceptionArray = new JsonArray();
525-
foreach (var sentryException in sentryEvent.SentryExceptions)
526-
{
527-
var exceptionJson = new JsonObject
528-
{
529-
["type"] = sentryException.Type,
530-
["value"] = sentryException.Value
531-
};
532-
exceptionArray.Add(exceptionJson);
533-
}
534-
json["exception"] = new JsonObject
535-
{
536-
["values"] = exceptionArray
537-
};
538-
}
539-
540-
if (sentryEvent.SentryThreads != null)
541-
{
542-
var threadArray = new JsonArray();
543-
foreach (var sentryThread in sentryEvent.SentryThreads)
544-
{
545-
var threadJson = new JsonObject
546-
{
547-
["id"] = sentryThread.Id,
548-
["name"] = sentryThread.Name
549-
};
550-
threadArray.Add(threadJson);
551-
}
552-
json["threads"] = new JsonObject
553-
{
554-
["values"] = threadArray
555-
};
556-
}
557-
558-
if (sentryEvent.Level.HasValue)
559-
{
560-
json["level"] = sentryEvent.Level.ToString().ToLowerInvariant();
561-
}
562-
563-
if (!string.IsNullOrWhiteSpace(sentryEvent.TransactionName))
564-
{
565-
json["transaction"] = sentryEvent.TransactionName;
566-
}
567-
568-
if (sentryEvent.Request != null)
569-
{
570-
var requestJson = new JsonObject
571-
{
572-
["url"] = sentryEvent.Request.Url,
573-
["method"] = sentryEvent.Request.Method
574-
};
575-
json["request"] = requestJson;
576-
}
577-
578-
if (sentryEvent.Contexts != null)
579-
{
580-
var contextsJson = new JsonObject();
581-
foreach (var context in sentryEvent.Contexts)
582-
{
583-
// SentryContexts continues to get harder to serialize, so instead enjoy the STRING of what the object is called!
584-
contextsJson[context.Key] = context.Value.ToString();
585-
}
586-
json["contexts"] = contextsJson;
587-
}
588-
589-
if (sentryEvent.User != null)
590-
{
591-
var userJson = new JsonObject
592-
{
593-
["id"] = sentryEvent.User.Id,
594-
["email"] = sentryEvent.User.Email,
595-
["username"] = sentryEvent.User.Username
596-
};
597-
json["user"] = userJson;
598-
}
599-
600-
if (!string.IsNullOrWhiteSpace(sentryEvent.Environment))
601-
{
602-
json["environment"] = sentryEvent.Environment;
603-
}
604-
605-
if (sentryEvent.Sdk != null)
606-
{
607-
var sdkJson = new JsonObject
608-
{
609-
["name"] = sentryEvent.Sdk.Name,
610-
["version"] = sentryEvent.Sdk.Version
611-
};
612-
json["sdk"] = sdkJson;
613-
}
614-
615-
if (sentryEvent.Breadcrumbs.Any())
616-
{
617-
var breadcrumbsJson = new JsonArray();
618-
foreach (var breadcrumb in sentryEvent.Breadcrumbs)
619-
{
620-
var breadcrumbJson = new JsonObject
621-
{
622-
["message"] = breadcrumb.Message,
623-
["timestamp"] = breadcrumb.Timestamp.ToString("o")
624-
};
625-
breadcrumbsJson.Add(breadcrumbJson);
626-
}
627-
json["breadcrumbs"] = breadcrumbsJson;
628-
}
629-
630-
if (sentryEvent.Extra.Any())
631-
{
632-
var extraJson = new JsonObject();
633-
foreach (var kvp in sentryEvent.Extra)
634-
{
635-
extraJson[kvp.Key] = JsonSerializer.SerializeToNode(kvp.Value);
636-
}
637-
json["extra"] = extraJson;
638-
}
639-
640-
if (sentryEvent.Tags.Any())
641-
{
642-
var tagsJson = new JsonObject();
643-
foreach (var tag in sentryEvent.Tags)
644-
{
645-
tagsJson[tag.Key] = tag.Value;
646-
}
647-
json["tags"] = tagsJson;
648-
}
649-
650-
telemetry.LogTelemetry("Sentry", sentryEvent.TransactionName ?? sentryEvent.Exception?.Message ?? "SentryEvent", json.ToJsonString());
651-
496+
Task.Run(() => LogTelemetry(sentryEvent, telemetry));
652497
return sentryEvent;
653498
});
654499

@@ -934,5 +779,177 @@ void AddFlagIfTrue(bool condition, string flagName)
934779
var output = string.Join(", ", Flags);
935780
DebugHelper.WriteLine("Flags: " + output);
936781
}
782+
783+
private static void LogTelemetry(SentryEvent sentryEvent, Telemetry telemetry)
784+
{
785+
var json = new JsonObject
786+
{
787+
["event_id"] = sentryEvent.EventId.ToString(),
788+
["timestamp"] = sentryEvent.Timestamp.ToString("o") // ISO 8601 format
789+
};
790+
791+
if (sentryEvent.Message != null)
792+
{
793+
json["logentry"] = new JsonObject
794+
{
795+
["message"] = sentryEvent.Message.Formatted
796+
};
797+
}
798+
799+
if (!string.IsNullOrWhiteSpace(sentryEvent.Logger))
800+
{
801+
json["logger"] = sentryEvent.Logger;
802+
}
803+
804+
if (!string.IsNullOrWhiteSpace(sentryEvent.Platform))
805+
{
806+
json["platform"] = sentryEvent.Platform;
807+
}
808+
809+
if (!string.IsNullOrWhiteSpace(sentryEvent.ServerName))
810+
{
811+
json["server_name"] = sentryEvent.ServerName;
812+
}
813+
814+
if (!string.IsNullOrWhiteSpace(sentryEvent.Release))
815+
{
816+
json["release"] = sentryEvent.Release;
817+
}
818+
819+
if (!string.IsNullOrWhiteSpace(sentryEvent.Distribution))
820+
{
821+
json["dist"] = sentryEvent.Distribution;
822+
}
823+
824+
if (sentryEvent.SentryExceptions != null)
825+
{
826+
var exceptionArray = new JsonArray();
827+
foreach (var sentryException in sentryEvent.SentryExceptions)
828+
{
829+
var exceptionJson = new JsonObject
830+
{
831+
["type"] = sentryException.Type,
832+
["value"] = sentryException.Value
833+
};
834+
exceptionArray.Add(exceptionJson);
835+
}
836+
json["exception"] = new JsonObject
837+
{
838+
["values"] = exceptionArray
839+
};
840+
}
841+
842+
if (sentryEvent.SentryThreads != null)
843+
{
844+
var threadArray = new JsonArray();
845+
foreach (var sentryThread in sentryEvent.SentryThreads)
846+
{
847+
var threadJson = new JsonObject
848+
{
849+
["id"] = sentryThread.Id,
850+
["name"] = sentryThread.Name
851+
};
852+
threadArray.Add(threadJson);
853+
}
854+
json["threads"] = new JsonObject
855+
{
856+
["values"] = threadArray
857+
};
858+
}
859+
860+
if (sentryEvent.Level.HasValue)
861+
{
862+
json["level"] = sentryEvent.Level.ToString().ToLowerInvariant();
863+
}
864+
865+
if (!string.IsNullOrWhiteSpace(sentryEvent.TransactionName))
866+
{
867+
json["transaction"] = sentryEvent.TransactionName;
868+
}
869+
870+
if (sentryEvent.Request != null)
871+
{
872+
var requestJson = new JsonObject
873+
{
874+
["url"] = sentryEvent.Request.Url,
875+
["method"] = sentryEvent.Request.Method
876+
};
877+
json["request"] = requestJson;
878+
}
879+
880+
if (sentryEvent.Contexts != null)
881+
{
882+
var contextsJson = new JsonObject();
883+
foreach (var context in sentryEvent.Contexts)
884+
{
885+
// SentryContexts continues to get harder to serialize, so instead enjoy the STRING of what the object is called!
886+
contextsJson[context.Key] = context.Value.ToString();
887+
}
888+
json["contexts"] = contextsJson;
889+
}
890+
891+
if (sentryEvent.User != null)
892+
{
893+
var userJson = new JsonObject
894+
{
895+
["id"] = sentryEvent.User.Id,
896+
["email"] = sentryEvent.User.Email,
897+
["username"] = sentryEvent.User.Username
898+
};
899+
json["user"] = userJson;
900+
}
901+
902+
if (!string.IsNullOrWhiteSpace(sentryEvent.Environment))
903+
{
904+
json["environment"] = sentryEvent.Environment;
905+
}
906+
907+
if (sentryEvent.Sdk != null)
908+
{
909+
var sdkJson = new JsonObject
910+
{
911+
["name"] = sentryEvent.Sdk.Name,
912+
["version"] = sentryEvent.Sdk.Version
913+
};
914+
json["sdk"] = sdkJson;
915+
}
916+
917+
if (sentryEvent.Breadcrumbs.Any())
918+
{
919+
var breadcrumbsJson = new JsonArray();
920+
foreach (var breadcrumb in sentryEvent.Breadcrumbs)
921+
{
922+
var breadcrumbJson = new JsonObject
923+
{
924+
["message"] = breadcrumb.Message,
925+
["timestamp"] = breadcrumb.Timestamp.ToString("o")
926+
};
927+
breadcrumbsJson.Add(breadcrumbJson);
928+
}
929+
json["breadcrumbs"] = breadcrumbsJson;
930+
}
931+
932+
if (sentryEvent.Extra.Any())
933+
{
934+
var extraJson = new JsonObject();
935+
foreach (var kvp in sentryEvent.Extra)
936+
{
937+
extraJson[kvp.Key] = JsonSerializer.SerializeToNode(kvp.Value);
938+
}
939+
json["extra"] = extraJson;
940+
}
941+
942+
if (sentryEvent.Tags.Any())
943+
{
944+
var tagsJson = new JsonObject();
945+
foreach (var tag in sentryEvent.Tags)
946+
{
947+
tagsJson[tag.Key] = tag.Value;
948+
}
949+
json["tags"] = tagsJson;
950+
}
951+
952+
telemetry.LogTelemetry("Sentry", sentryEvent.TransactionName ?? sentryEvent.Exception?.Message ?? "SentryEvent", json.ToJsonString());
953+
}
937954
}
938955

0 commit comments

Comments
 (0)