Skip to content

Commit

Permalink
Obsolete TelemetryConfiguration.Active singleton usage on .NET Core (#…
Browse files Browse the repository at this point in the history
…1148)

* Obsolete TelemetryConfiguration.Active singleton usage on .NET Core in favor of DI

* changelog

* fxcop

* tests fxcop

* Obsolete TelemetryClient() as well

* Update CHANGELOG.md

* better comment on TelemetryClient()

* Update comment on Active
  • Loading branch information
Liudmila Molkova authored Jun 13, 2019
1 parent 0ff97fc commit 6532148
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 33 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

This changelog will be used to generate documentation on [release notes page](http://azure.microsoft.com/documentation/articles/app-insights-release-notes-dotnet/).

## Version 2.11.0-beta1
- [Deprecate TelemetryConfiguration.Active on .NET Core in favor of dependency injection pattern](https://github.com/microsoft/ApplicationInsights-dotnet/pull/1152)

## Version 2.10.0
- [SDKVersion modified to be dotnetc for NetCore. This helps identify the source of code path, as implementations are slightly different for NetCore.](https://github.com/microsoft/ApplicationInsights-dotnet/pull/1125)
- [Fix telemetry timestamp precision on .NET Framework](https://github.com/microsoft/ApplicationInsights-dotnet-server/issues/1175)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using KellermanSoftware.CompareNetObjects;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.ApplicationInsights.TestFramework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -277,7 +278,7 @@ public void DependencyTelemetrySetGetOperationDetail()
Assert.AreEqual(detail, retrievedValue.ToString());

// Clear and verify the detail is no longer present
new TelemetryClient().TrackDependency(telemetry);
new TelemetryClient(TelemetryConfiguration.CreateDefault()).TrackDependency(telemetry);
Assert.IsFalse(telemetry.TryGetOperationDetail(key, out retrievedValue));
}

Expand All @@ -291,7 +292,7 @@ public void DependencyTelemetryGetUnsetOperationDetail()
Assert.IsNull(retrievedValue);

// should not throw
new TelemetryClient().TrackDependency(telemetry);
new TelemetryClient(TelemetryConfiguration.CreateDefault()).TrackDependency(telemetry);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void CreatingOperationItemWithNullTelemetryClientThrowsArgumentNullExcept
[ExpectedException(typeof(ArgumentNullException))]
public void CreatingOperationItemWithNullTelemetryThrowsArgumentNullException()
{
var operationItem = new OperationHolder<DependencyTelemetry>(new TelemetryClient(), null);
var operationItem = new OperationHolder<DependencyTelemetry>(new TelemetryClient(TelemetryConfiguration.CreateDefault()), null);
}

/// <summary>
Expand All @@ -36,7 +36,7 @@ public void CreatingOperationItemWithNullTelemetryThrowsArgumentNullException()
[TestMethod]
public void CreatingOperationItemDoesNotThrowOnPassingValidArguments()
{
var operationItem = new OperationHolder<DependencyTelemetry>(new TelemetryClient(), new DependencyTelemetry());
var operationItem = new OperationHolder<DependencyTelemetry>(new TelemetryClient(TelemetryConfiguration.CreateDefault()), new DependencyTelemetry());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing
#pragma warning disable 612, 618 // obsolete TelemetryConfigration.Active
namespace Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing
{
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -134,3 +135,4 @@ private class TestEventSource : EventSource
}
}
}
#pragma warning restore 612, 618 // obsolete TelemetryConfigration.Active
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public void CanExtendHeartbeatPayload()
[TestMethod]
public void InitializationOfTelemetryClientDoesntResetHeartbeat()
{
TelemetryClient client = new TelemetryClient();
TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
TelemetryClient client = new TelemetryClient(configuration);

bool origIsEnabled = true;
string origExcludedHbProvider = "Nonsense-Test";
Expand All @@ -128,7 +129,7 @@ public void InitializationOfTelemetryClientDoesntResetHeartbeat()
}
}

TelemetryClient client2 = new TelemetryClient();
TelemetryClient client2 = new TelemetryClient(configuration);

foreach (var module in TelemetryModules.Instance.Modules)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void ActiveSetterIsInternalAndNotMeantToBeUsedByOurCustomers()
{
Assert.IsFalse(typeof(TelemetryConfiguration).GetTypeInfo().GetDeclaredProperty("Active").GetSetMethod(true).IsPublic);
}

#pragma warning disable 612, 618
[TestMethod]
public void ActiveIsLazilyInitializedToDelayCostOfLoadingConfigurationFromFile()
{
Expand Down Expand Up @@ -186,7 +186,7 @@ public void ActiveInitializesSingleInstanceWhenConfigurationComponentsAccessActi
TelemetryConfigurationFactory.Instance = null;
}
}

#pragma warning restore 612, 618
#endregion

#region CreateDefault
Expand Down
6 changes: 3 additions & 3 deletions Test/Microsoft.ApplicationInsights.Test/Shared/MetricTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
#pragma warning disable 612, 618 // obsolete TelemetryConfigration.Active
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.ExceptionServices;

using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Metrics;
Expand Down Expand Up @@ -1870,3 +1869,4 @@ private static Metric InvokeMetricCtor(
}
}
}
#pragma warning restore 612, 618 // obsolete TelemetryConfigration.Active
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void Ctor()
Assert.ThrowsException<ArgumentNullException>( () => new ApplicationInsightsTelemetryPipeline((TelemetryConfiguration) null) );
}
{
TelemetryConfiguration defaultPipeline = TelemetryConfiguration.Active;
TelemetryConfiguration defaultPipeline = TelemetryConfiguration.CreateDefault();
//using (defaultPipeline)
{
var pipelineAdapter = new ApplicationInsightsTelemetryPipeline(defaultPipeline);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class MetricEmissionTestsVirtualTime
[TestMethod]
public void RecordNormalMetric()
{
TelemetryConfiguration telemetryPipeline = TelemetryConfiguration.Active;
TelemetryConfiguration telemetryPipeline = TelemetryConfiguration.CreateDefault();
//using (telemetryPipeline)
{
RecordNormalMetric(telemetryPipeline);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma warning disable CA1716 // Namespace naming
#pragma warning disable CA1716, 612, 618 // Namespace naming, obsolete TelemetryConfigration.Active

namespace User.Namespace.Example01
{
Expand Down Expand Up @@ -1199,4 +1199,4 @@ public void Example06()
}
}

#pragma warning restore CA1716 // Namespace naming
#pragma warning restore CA1716, 612, 618 // Namespace naming, obsolete TelemetryConfigration.Active
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
using System;
using System.Threading;
#pragma warning disable 612, 618 // obsolete TelemetryConfigration.Active

using System;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Metrics;
using Microsoft.ApplicationInsights.Metrics.TestUtility;
using Microsoft.ApplicationInsights.Metrics.Extensibility;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights;
using System.Collections.Generic;
using Microsoft.ApplicationInsights.Channel;

namespace SomeCustomerNamespace
{
Expand Down Expand Up @@ -139,3 +136,4 @@ private static void Metrics_SpecifiedPipeline(TelemetryConfiguration telemetryPi
//}
}
}
#pragma warning restore 612, 618 // obsolete TelemetryConfigration.Active
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void InitializeSetsDateTime()
{
EventTelemetry telemetry = new EventTelemetry("TestEvent");

new TelemetryClient().Initialize(telemetry);
new TelemetryClient(TelemetryConfiguration.CreateDefault()).Initialize(telemetry);

Assert.IsTrue(telemetry.Timestamp != default(DateTimeOffset));
}
Expand All @@ -118,7 +118,7 @@ public void TimestampIsPrecise()
for (int i = 0; i < timeStampDiff.Length; i++)
{
var telemetry = new DependencyTelemetry();
new TelemetryClient().Initialize(telemetry);
new TelemetryClient(TelemetryConfiguration.CreateDefault()).Initialize(telemetry);

if (i > 0)
{
Expand All @@ -143,7 +143,7 @@ public void InitializeSetsRoleInstance()
PlatformSingleton.Current = new StubPlatform { OnGetMachineName = () => "TestMachine" };

EventTelemetry telemetry = new EventTelemetry("TestEvent");
new TelemetryClient().Initialize(telemetry);
new TelemetryClient(TelemetryConfiguration.CreateDefault()).Initialize(telemetry);

Assert.AreEqual("TestMachine", telemetry.Context.Cloud.RoleInstance);
Assert.IsNull(telemetry.Context.Internal.NodeName);
Expand All @@ -159,7 +159,7 @@ public void InitializeDoesNotOverrideRoleInstance()
EventTelemetry telemetry = new EventTelemetry("TestEvent");
telemetry.Context.Cloud.RoleInstance = "MyMachineImplementation";

new TelemetryClient().Initialize(telemetry);
new TelemetryClient(TelemetryConfiguration.CreateDefault()).Initialize(telemetry);

Assert.AreEqual("MyMachineImplementation", telemetry.Context.Cloud.RoleInstance);
Assert.AreEqual("TestMachine", telemetry.Context.Internal.NodeName);
Expand All @@ -175,7 +175,7 @@ public void InitializeDoesNotOverrideNodeName()
EventTelemetry telemetry = new EventTelemetry("TestEvent");
telemetry.Context.Internal.NodeName = "MyMachineImplementation";

new TelemetryClient().Initialize(telemetry);
new TelemetryClient(TelemetryConfiguration.CreateDefault()).Initialize(telemetry);

Assert.AreEqual("TestMachine", telemetry.Context.Cloud.RoleInstance);
Assert.AreEqual("MyMachineImplementation", telemetry.Context.Internal.NodeName);
Expand All @@ -192,7 +192,7 @@ public void InitializeIKeySetsIkeyFromContext()
{
EventTelemetry telemetry = new EventTelemetry("TestEvent");

var tc = new TelemetryClient();
var tc = new TelemetryClient(TelemetryConfiguration.CreateDefault());
// Set ikey on Context
tc.InstrumentationKey = "mykey";
tc.InitializeInstrumentationKey(telemetry);
Expand Down Expand Up @@ -235,7 +235,7 @@ public void InitializeIKeyDoesNotOverrideIKey()
EventTelemetry telemetry = new EventTelemetry("TestEvent");
telemetry.Context.InstrumentationKey = "expectedIKey";

var tc = new TelemetryClient();
var tc = new TelemetryClient(TelemetryConfiguration.CreateDefault());
tc.InstrumentationKey = "mykey";
tc.InitializeInstrumentationKey(telemetry);

Expand Down Expand Up @@ -2129,13 +2129,15 @@ private TelemetryClient InitializeTelemetryClient(ICollection<ITelemetry> sentTe
return client;
}

#pragma warning disable 612, 618 // obsolete TelemetryConfigration.Active
/// <summary>
/// Resets the TelemetryConfiguration.Active default instance to null so that the iKey auto population paths can be followed for testing.
/// </summary>
private void ClearActiveTelemetryConfiguration()
{
TelemetryConfiguration.Active = null;
}
#pragma warning restore 612, 618 // obsolete TelemetryConfigration.Active

private double ComputeSomethingHeavy()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public TelemetryConfiguration(string instrumentationKey, ITelemetryChannel chann
/// If the configuration file does not exist, the active configuration instance is initialized with minimum defaults
/// needed to send telemetry to Application Insights.
/// </summary>
#if NETSTANDARD1_3 || NETSTANDARD2_0
[Obsolete("We do not recommend using TelemetryConfiguration.Active on .NET Core. See https://github.com/microsoft/ApplicationInsights-dotnet/issues/1152 for more details")]
#endif
public static TelemetryConfiguration Active
{
get
Expand Down Expand Up @@ -378,4 +381,4 @@ private void Dispose(bool disposing)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,16 @@ public int MaxDependencyTypesToDiscover
/// Specifically, this will also ensure that the <see cref="TelemetryClient" /> used internally for sending extracted metrics uses
/// the same configuration.
/// </summary>
/// <param name="configuration">The telemetric configuration to be used by this extractor.</param>
/// <param name="configuration">The telemetry configuration to be used by this extractor.</param>
public void Initialize(TelemetryConfiguration configuration)
{
#pragma warning disable 612, 618 // TelemetryConfigration.Active and TelemetryClient()
TelemetryClient metricsClient = (configuration == null)
? new TelemetryClient()
: new TelemetryClient(configuration);
#pragma warning restore 612, 618 // TelemetryConfigration.Active and TelemetryClient()

if (false == String.IsNullOrWhiteSpace(MetricTerms.Autocollection.Moniker.Key))
if (false == string.IsNullOrWhiteSpace(MetricTerms.Autocollection.Moniker.Key))
{
metricsClient.Context.GlobalProperties[MetricTerms.Autocollection.Moniker.Key] = MetricTerms.Autocollection.Moniker.Value;
}
Expand Down
7 changes: 6 additions & 1 deletion src/Microsoft.ApplicationInsights/TelemetryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ public sealed class TelemetryClient
#endif
private readonly TelemetryConfiguration configuration;
private string sdkVersion;


#pragma warning disable 612, 618 // TelemetryConfiguration.Active
/// <summary>
/// Initializes a new instance of the <see cref="TelemetryClient" /> class. Send telemetry with the active configuration, usually loaded from ApplicationInsights.config.
/// </summary>
#if NETSTANDARD1_3 || NETSTANDARD2_0
[Obsolete("We do not recommend using TelemetryConfiguration.Active on .NET Core. See https://github.com/microsoft/ApplicationInsights-dotnet/issues/1152 for more details")]
#endif
public TelemetryClient() : this(TelemetryConfiguration.Active)
{
}
Expand All @@ -57,6 +61,7 @@ public TelemetryClient(TelemetryConfiguration configuration)
throw new ArgumentException("The specified configuration does not have a telemetry channel.", nameof(configuration));
}
}
#pragma warning restore 612, 618 // TelemetryConfiguration.Active

/// <summary>
/// Gets the current context that will be used to augment telemetry you send.
Expand Down

0 comments on commit 6532148

Please sign in to comment.