Skip to content

Commit

Permalink
Merge pull request #4904 from unoplatform/dev/jela/telemetry-update
Browse files Browse the repository at this point in the history
fix: memory leak in telemetry
  • Loading branch information
jeromelaban authored Jan 15, 2021
2 parents 42399de + 619fe29 commit 10528e1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public class Telemetry
private TelemetryClient _client = null;
private Dictionary<string, string> _commonProperties = null;
private Dictionary<string, double> _commonMeasurements = null;
private TelemetryConfiguration _telemetryConfig;
private Task _trackEventTask = null;

private PersistenceChannel.PersistenceChannel _persistenceChannel;
private const string InstrumentationKey = "9a44058e-1913-4721-a979-9582ab8bedce";
private const string TelemetryOptout = "UNO_PLATFORM_TELEMETRY_OPTOUT";

Expand Down Expand Up @@ -99,6 +100,11 @@ public void Flush()
_trackEventTask.Wait();
}

public void Dispose()
{
_persistenceChannel.Dispose();
}

public void ThreadBlockingTrackEvent(string eventName, IDictionary<string, string> properties, IDictionary<string, double> measurements)
{
if (!Enabled)
Expand All @@ -112,18 +118,14 @@ private void InitializeTelemetry()
{
try
{
var persistenceChannel = new PersistenceChannel.PersistenceChannel(sendersCount: _senderCount);
persistenceChannel.SendingInterval = TimeSpan.FromMilliseconds(1);
#pragma warning disable CS0618 // Type or member is obsolete
TelemetryConfiguration.Active.TelemetryChannel = persistenceChannel;
#pragma warning restore CS0618 // Type or member is obsolete
_persistenceChannel = new PersistenceChannel.PersistenceChannel(sendersCount: _senderCount);
_persistenceChannel.SendingInterval = TimeSpan.FromMilliseconds(1);

_commonProperties = new TelemetryCommonProperties().GetTelemetryCommonProperties();
_commonMeasurements = new Dictionary<string, double>();

#pragma warning disable CS0618 // Type or member is obsolete
_client = new TelemetryClient();
#pragma warning disable CS0618 // Type or member is obsolete
_telemetryConfig = new TelemetryConfiguration { InstrumentationKey = InstrumentationKey };
_client = new TelemetryClient(_telemetryConfig);
_client.InstrumentationKey = InstrumentationKey;
_client.Context.User.Id = _commonProperties[TelemetryCommonProperties.MachineId];
_client.Context.Session.Id = CurrentSessionId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

<ItemGroup Condition="'$(TargetFramework)'=='net461'">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.12.0" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.16.0" />
<PackageReference Include="Microsoft.Build" Version="15.4.8" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Engine" Version="15.4.8" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="15.4.8" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="15.4.8" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.CodeAnalysis">
<Version>3.3.1</Version>
<ExcludeAssets>runtime</ExcludeAssets>
Expand Down Expand Up @@ -48,7 +49,7 @@
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions">
<Version>3.1.6</Version>
</PackageReference>
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.12.0" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.16.0" />
<PackageReference Include="Microsoft.CodeAnalysis">
<Version>3.8.0</Version>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ public KeyValuePair<string, string>[] Generate()
finally
{
_telemetry.Flush();
_telemetry.Dispose();
}
}

Expand Down

0 comments on commit 10528e1

Please sign in to comment.