Skip to content

Commit ef88356

Browse files
authored
Skip telemetry work when it cannot be sent (#5071)
1 parent 646f0e4 commit ef88356

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

‎src/UniGetUI.Interface.Telemetry/TelemetryHandler.cs‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ public static async Task InitializeAsync()
104104
{
105105
try
106106
{
107-
if (Settings.Get(Settings.K.DisableTelemetry))
107+
// Bail out before waiting for internet or gathering any data when telemetry
108+
// can't be sent — opted out, or this build has no OpenSearch credentials
109+
// (every from-source/community build). Only the official build proceeds.
110+
if (Settings.Get(Settings.K.DisableTelemetry) || !CredentialsConfigured())
108111
return;
109112

110113
await CoreTools.WaitForInternetConnection();
@@ -216,7 +219,9 @@ private static void EnqueuePackageEvent(
216219
{
217220
if (result is null && eventSource is null)
218221
throw new ArgumentException("result and eventSource cannot both be null");
219-
if (Settings.Get(Settings.K.DisableTelemetry))
222+
// Skip enqueuing entirely when telemetry can't be sent, so events don't
223+
// accumulate only to be drained-and-discarded at flush time.
224+
if (Settings.Get(Settings.K.DisableTelemetry) || !CredentialsConfigured())
220225
return;
221226

222227
_pendingPackageEvents.Enqueue(new UniGetUIPackageEvent
@@ -240,6 +245,9 @@ private static void EnqueuePackageEvent(
240245
/// </summary>
241246
public static async Task FlushPackageEventsAsync()
242247
{
248+
if (Settings.Get(Settings.K.DisableTelemetry) || !CredentialsConfigured())
249+
return;
250+
243251
if (_pendingPackageEvents.IsEmpty)
244252
return;
245253

@@ -311,7 +319,7 @@ private static async Task TrackBundleEventAsync(string operation, string bundleT
311319
{
312320
try
313321
{
314-
if (Settings.Get(Settings.K.DisableTelemetry))
322+
if (Settings.Get(Settings.K.DisableTelemetry) || !CredentialsConfigured())
315323
return;
316324

317325
await CoreTools.WaitForInternetConnection();
@@ -397,8 +405,14 @@ private static TelemetryApplicationInfo BuildApplicationInfo() =>
397405
ArchitectureType = RuntimeInformation.ProcessArchitecture.ToString(),
398406
};
399407

408+
// Every field here is constant for the process lifetime, so build it once and
409+
// reuse the instance across all events (including each doc in a bulk flush).
410+
// The value is only ever read (serialized), never mutated, so sharing is safe;
411+
// a benign race just builds it twice with identical results.
412+
private static TelemetryPlatformInfo? _platformInfo;
413+
400414
private static TelemetryPlatformInfo BuildPlatformInfo() =>
401-
new()
415+
_platformInfo ??= new()
402416
{
403417
Name = GetPlatformName(),
404418
Version = Environment.OSVersion.VersionString,

0 commit comments

Comments
 (0)