Skip to content

Commit

Permalink
Dev/saars/rename extension method (#149)
Browse files Browse the repository at this point in the history
* Change enablement method name

EnableKubernetes is not a good name for enable an enricher for application insights.

* Update documents for new method name

* Rename again

Reduce 2 chars

* Update task a bit
  • Loading branch information
xiaomi7732 committed Nov 8, 2018
1 parent 85e2e64 commit de1b629
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .vscode/tasks.json
Expand Up @@ -5,6 +5,7 @@
"label": "build",
"command": "dotnet",
"type": "process",
"group": "build",
"args": [
"build",
"${workspaceFolder}/ApplicationInsights.Kubernetes.sln"
Expand All @@ -15,6 +16,7 @@
"label": "Run Unit Tests",
"command": "dotnet",
"type": "process",
"group": "test",
"args": [
"test",
"${workspaceFolder}/tests/UnitTests"
Expand Down
2 changes: 1 addition & 1 deletion examples/BasicUsage/README.md
Expand Up @@ -31,7 +31,7 @@ public static IWebHost BuildWebHost(string[] args) =>
```
public void ConfigureServices(IServiceCollection services)
{
services.EnableKubernetes();
services.AddApplicationInsightsKubernetesEnricher();
services.AddMvc();
}
```
Expand Down
2 changes: 1 addition & 1 deletion examples/BasicUsage/Startup.cs
Expand Up @@ -21,7 +21,7 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.EnableKubernetes();
services.AddApplicationInsightsKubernetesEnricher();
services.AddMvc();
}

Expand Down
2 changes: 1 addition & 1 deletion examples/BasicUsage_clr21_RBAC/README.MD
Expand Up @@ -51,7 +51,7 @@ public static IWebHost BuildWebHost(string[] args) =>
```csharp
public void ConfigureServices(IServiceCollection services)
{
services.EnableKubernetes(); // Add this line of code
services.AddApplicationInsightsKubernetesEnricher(); // Add this line of code
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
```
Expand Down
2 changes: 1 addition & 1 deletion examples/BasicUsage_clr21_RBAC/app/Startup.cs
Expand Up @@ -32,7 +32,7 @@ public void ConfigureServices(IServiceCollection services)
});


services.EnableKubernetes();
services.AddApplicationInsightsKubernetesEnricher();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

Expand Down
8 changes: 4 additions & 4 deletions examples/MultipleIKeys/README.md
Expand Up @@ -6,22 +6,22 @@ If you are reading this example, we assume you are familiar with deploy Applicat
## Scenarios
Sometimes, you might have one application sending to different application insight backends. There are several ways to reach the goal. It could be done by using multiple channels ([Reference 1](https://github.com/Microsoft/ApplicationInsights-dotnet/blob/e544ffae4f3188bde01a367364ea3e36f2bf03a9/Test/Microsoft.ApplicationInsights.Test/Shared/Extensibility/TelemetryConfigurationFactoryTest.cs), [Reference 2](https://github.com/Microsoft/ApplicationInsights-dotnet/blob/e544ffae4f3188bde01a367364ea3e36f2bf03a9/Test/Microsoft.ApplicationInsights.Test/Shared/Extensibility/TelemetrySinkTests.cs)) or by building multiple `TelemetryConfiguration` instances to hold multiple iKeys. This example is going to focus on the multiple-iKey scenario, which will be supported from Application Insights Kubernetes 1.0.0-beta8.

Different than single Application Insights Instrumentation Key (iKey), in which case, you are suggested to use the TelemetryClient by ASP.NET Dependency Injection, for multiple-iKey scenario, you will need to build your own Telemetry Clients. The telemetry clients accept a telemetry configuration object, which contains the iKey property. Supporting of calling `EnableKubernetes()` on various `TelemetryConfiguration` is added.
Different than single Application Insights Instrumentation Key (iKey), in which case, you are suggested to use the TelemetryClient by ASP.NET Dependency Injection, for multiple-iKey scenario, you will need to build your own Telemetry Clients. The telemetry clients accept a telemetry configuration object, which contains the iKey property. Supporting of calling `AddApplicationInsightsKubernetesEnricher()` on various `TelemetryConfiguration` is added.

## Key code

Let's talk in code:
```csharp
// Build a TelemetryClient with iKey1
TelemetryConfiguration aiConfig = new TelemetryConfiguration("ikey 1", app.ApplicationServices.GetService<ITelemetryChannel>());
aiConfig.EnableKubernetes();
aiConfig.AddApplicationInsightsKubernetesEnricher();
TelemetryClient client = new TelemetryClient(aiConfig);
// Invoking the constructor for the TelemetryInitializer
client.TrackEvent("Hello");

// Build a TelemetryClient with iKey1
TelemetryConfiguration aiConfig2 = new TelemetryConfiguration("iKey 2", app.ApplicationServices.GetService<ITelemetryChannel>());
aiConfig2.EnableKubernetes();
aiConfig2.AddApplicationInsightsKubernetesEnricher();
TelemetryClient client2 = new TelemetryClient(aiConfig2);
```
Now you can have telemetry clients sending to different application insight backends. Refer [Startup.cs](./Startup.cs) for the full code.
Expand All @@ -36,7 +36,7 @@ There are some points worth to mention:

* We are still calling `UseApplicationInsights()` in [Program.cs](Program.cs). When an iKey is provided, you will have an additional telemetry client as well in the service provider.

* This is sort of obvious, but since this is the Application Insights Kubernetes example, I have to mention: please do not forget to call `.EnableKubernetes()` on the configuration object to inject Kubernetes information to the telemetry data.
* This is sort of obvious, but since this is the Application Insights Kubernetes example, I have to mention: please do not forget to call `.AddApplicationInsightsKubernetesEnricher()` on the configuration object to inject Kubernetes information to the telemetry data.

```
Side note: You might notice the very first TrackEvent doesn't come with the Kubernetes info. That is by design because the TelemetryInitializer is non-blocking but it will take an async call to fetch the Kubernetes info.
Expand Down
4 changes: 2 additions & 2 deletions examples/MultipleIKeys/Startup.cs
Expand Up @@ -41,13 +41,13 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)

// uService_prototype
TelemetryConfiguration aiConfig = new TelemetryConfiguration("8e9838a3-ad63-4d30-96f7-2f0a505bc0f6", app.ApplicationServices.GetService<ITelemetryChannel>());
aiConfig.EnableKubernetes();
aiConfig.AddApplicationInsightsKubernetesEnricher();
TelemetryClient client = new TelemetryClient(aiConfig);
// Invoking the constructor for the TelemetryInitializer
client.TrackEvent("Hello");
// saarsfun01
TelemetryConfiguration aiConfig2 = new TelemetryConfiguration("5789ad10-8b39-4f8a-88dc-632d1342d5e0", app.ApplicationServices.GetService<ITelemetryChannel>());
aiConfig2.EnableKubernetes();
aiConfig2.AddApplicationInsightsKubernetesEnricher();
TelemetryClient client2 = new TelemetryClient(aiConfig2);

var _forget = ThrowAnotherHelloAsync(client, client2);
Expand Down
2 changes: 1 addition & 1 deletion examples/WindowsContainer/README.md
Expand Up @@ -36,7 +36,7 @@ public static IWebHost BuildWebHost(string[] args) =>
```
public void ConfigureServices(IServiceCollection services)
{
services.EnableKubernetes();
services.AddApplicationInsightsKubernetesEnricher();
services.AddMvc();
}
```
Expand Down
2 changes: 1 addition & 1 deletion examples/WindowsContainer/Startup.cs
Expand Up @@ -21,7 +21,7 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.EnableKubernetes();
services.AddApplicationInsightsKubernetesEnricher();
services.AddMvc();
}

Expand Down
Expand Up @@ -17,7 +17,7 @@ public void Configure(IWebHostBuilder builder)
builder.UseApplicationInsights()
.ConfigureServices(services =>
{
services.EnableKubernetes();
services.AddApplicationInsightsKubernetesEnricher();
});
}
}
Expand Down
Expand Up @@ -16,14 +16,14 @@ namespace Microsoft.Extensions.DependencyInjection
public static class ApplicationInsightsExtensions
{
/// <summary>
/// Enables Application Insights Kubernetes for the Default TelemtryConfiguration in the dependency injection system.
/// Enables Application Insights for Kubernetes on the Default TelemtryConfiguration in the dependency injection system.
/// </summary>
/// <param name="services">Collection of service descriptors.</param>
/// <param name="timeout">Maximum time to wait for spinning up the container.</param>
/// <param name="kubernetesServiceCollectionBuilder">Collection builder.</param>
/// <param name="detectKubernetes">Delegate to detect if the current application is running in Kubernetes hosted container.</param>
/// <returns>The collection of services descriptors we injected into.</returns>
public static IServiceCollection EnableKubernetes(
public static IServiceCollection AddApplicationInsightsKubernetesEnricher(
this IServiceCollection services,
TimeSpan? timeout = null,
IKubernetesServiceCollectionBuilder kubernetesServiceCollectionBuilder = null,
Expand All @@ -35,16 +35,13 @@ public static class ApplicationInsightsExtensions
}

/// <summary>
/// Enables Application Insights Kubernetes for a given
/// TelemetryConfiguration.
/// Enables Application Insights Kubernetes for a given TelemetryConfiguration.
/// </summary>
/// <remarks>
/// The use of EnableKubernetes() on the ServiceCollection is always
/// preferred unless you have more than one TelemetryConfiguration
/// instance, or if you are using Application Insights from a non ASP.NET
/// environment, like a console app.
/// The use of AddApplicationInsightsKubernetesEnricher() on the ServiceCollection is always preferred unless you have more than one TelemetryConfiguration
/// instance, or if you are using Application Insights from a non ASP.NET environment, like a console app.
/// </remarks>
public static void EnableKubernetes(
public static void AddApplicationInsightsKubernetesEnricher(
this TelemetryConfiguration telemetryConfiguration,
TimeSpan? timeout = null,
IKubernetesServiceCollectionBuilder kubernetesServiceCollectionBuilder = null,
Expand All @@ -69,6 +66,47 @@ public static class ApplicationInsightsExtensions
}
}

/// <summary>
/// Please use AddApplicationInsightsKubernetesEnricher() insead.
/// Enables Application Insights Kubernetes for the Default TelemtryConfiguration in the dependency injection system.
/// </summary>
/// <param name="services">Collection of service descriptors.</param>
/// <param name="timeout">Maximum time to wait for spinning up the container.</param>
/// <param name="kubernetesServiceCollectionBuilder">Collection builder.</param>
/// <param name="detectKubernetes">Delegate to detect if the current application is running in Kubernetes hosted container.</param>
/// <returns>The collection of services descriptors we injected into.</returns>
[Obsolete("Use AddApplicationInsightsKubernetesEnricher() instead", false)]
public static IServiceCollection EnableKubernetes(
this IServiceCollection services,
TimeSpan? timeout = null,
IKubernetesServiceCollectionBuilder kubernetesServiceCollectionBuilder = null,
Func<bool> detectKubernetes = null)
{
return services.AddApplicationInsightsKubernetesEnricher(timeout, kubernetesServiceCollectionBuilder, detectKubernetes);
}

/// <summary>
/// Please use "AddApplicationInsightsKubernetesEnricher()" insead.
/// Enables Application Insights Kubernetes for a given
/// TelemetryConfiguration.
/// </summary>
/// <remarks>
/// The use of AddApplicationInsightsKubernetesEnricher() on the ServiceCollection is always
/// preferred unless you have more than one TelemetryConfiguration
/// instance, or if you are using Application Insights from a non ASP.NET
/// environment, like a console app.
/// </remarks>
[Obsolete("Use AddApplicationInsightsKubernetesEnricher() instead", false)]
public static void EnableKubernetes(
this TelemetryConfiguration telemetryConfiguration,
TimeSpan? timeout = null,
IKubernetesServiceCollectionBuilder kubernetesServiceCollectionBuilder = null,
Func<bool> detectKubernetes = null)
{
telemetryConfiguration.AddApplicationInsightsKubernetesEnricher(
timeout, kubernetesServiceCollectionBuilder, detectKubernetes);
}

/// <summary>
/// Enables applicaiton insights for kubernetes.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions tests/UnitTests/KubernetesEnablementTests.cs
Expand Up @@ -17,7 +17,7 @@ public class KubernetesEnablementTest
public void ServiceInjected()
{
IServiceCollection services = new ServiceCollection();
services = services.EnableKubernetes(detectKubernetes: () => true);
services = services.AddApplicationInsightsKubernetesEnricher(detectKubernetes: () => true);

// Replace the IKubeHttpClientSetingsProvider in case the test is not running inside a container.
Assert.NotNull(services.FirstOrDefault(s => s.ServiceType == typeof(IKubeHttpClientSettingsProvider)));
Expand Down Expand Up @@ -58,7 +58,7 @@ public void AddTheInitializerToGivenConfiguration()
.Returns(sc);

TelemetryConfiguration telemetryConfiguration = new TelemetryConfiguration("123", channelMock.Object);
telemetryConfiguration.EnableKubernetes(null, serviceCollectionBuilderMock.Object, detectKubernetes: () => true);
telemetryConfiguration.AddApplicationInsightsKubernetesEnricher(null, serviceCollectionBuilderMock.Object, detectKubernetes: () => true);

Assert.NotNull(telemetryConfiguration.TelemetryInitializers);
Assert.True(telemetryConfiguration.TelemetryInitializers.Count == 1);
Expand Down
2 changes: 1 addition & 1 deletion troubleshooting/AIK8sTroubleShooting/Startup.cs
Expand Up @@ -25,7 +25,7 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.EnableKubernetes(TimeSpan.FromSeconds(5));
services.AddApplicationInsightsKubernetesEnricher(TimeSpan.FromSeconds(5));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

Expand Down

0 comments on commit de1b629

Please sign in to comment.