diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 8283bc5..4869d7a 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -5,6 +5,7 @@ "label": "build", "command": "dotnet", "type": "process", + "group": "build", "args": [ "build", "${workspaceFolder}/ApplicationInsights.Kubernetes.sln" @@ -15,6 +16,7 @@ "label": "Run Unit Tests", "command": "dotnet", "type": "process", + "group": "test", "args": [ "test", "${workspaceFolder}/tests/UnitTests" diff --git a/examples/BasicUsage/README.md b/examples/BasicUsage/README.md index 3939dd1..e7f3043 100644 --- a/examples/BasicUsage/README.md +++ b/examples/BasicUsage/README.md @@ -31,7 +31,7 @@ public static IWebHost BuildWebHost(string[] args) => ``` public void ConfigureServices(IServiceCollection services) { - services.EnableKubernetes(); + services.AddApplicationInsightsKubernetesEnricher(); services.AddMvc(); } ``` diff --git a/examples/BasicUsage/Startup.cs b/examples/BasicUsage/Startup.cs index 524ce92..22702bb 100644 --- a/examples/BasicUsage/Startup.cs +++ b/examples/BasicUsage/Startup.cs @@ -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(); } diff --git a/examples/BasicUsage_clr21_RBAC/README.MD b/examples/BasicUsage_clr21_RBAC/README.MD index 9e88c70..729599b 100644 --- a/examples/BasicUsage_clr21_RBAC/README.MD +++ b/examples/BasicUsage_clr21_RBAC/README.MD @@ -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); } ``` diff --git a/examples/BasicUsage_clr21_RBAC/app/Startup.cs b/examples/BasicUsage_clr21_RBAC/app/Startup.cs index 0a95f25..e1e2196 100644 --- a/examples/BasicUsage_clr21_RBAC/app/Startup.cs +++ b/examples/BasicUsage_clr21_RBAC/app/Startup.cs @@ -32,7 +32,7 @@ public void ConfigureServices(IServiceCollection services) }); - services.EnableKubernetes(); + services.AddApplicationInsightsKubernetesEnricher(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); } diff --git a/examples/MultipleIKeys/README.md b/examples/MultipleIKeys/README.md index 5e1444b..7add9ee 100644 --- a/examples/MultipleIKeys/README.md +++ b/examples/MultipleIKeys/README.md @@ -6,7 +6,7 @@ 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 @@ -14,14 +14,14 @@ Let's talk in code: ```csharp // Build a TelemetryClient with iKey1 TelemetryConfiguration aiConfig = new TelemetryConfiguration("ikey 1", app.ApplicationServices.GetService()); - 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()); - 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. @@ -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. diff --git a/examples/MultipleIKeys/Startup.cs b/examples/MultipleIKeys/Startup.cs index f2f2672..7b4a5c2 100644 --- a/examples/MultipleIKeys/Startup.cs +++ b/examples/MultipleIKeys/Startup.cs @@ -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()); - 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()); - aiConfig2.EnableKubernetes(); + aiConfig2.AddApplicationInsightsKubernetesEnricher(); TelemetryClient client2 = new TelemetryClient(aiConfig2); var _forget = ThrowAnotherHelloAsync(client, client2); diff --git a/examples/WindowsContainer/README.md b/examples/WindowsContainer/README.md index 5291bc8..541d287 100644 --- a/examples/WindowsContainer/README.md +++ b/examples/WindowsContainer/README.md @@ -36,7 +36,7 @@ public static IWebHost BuildWebHost(string[] args) => ``` public void ConfigureServices(IServiceCollection services) { - services.EnableKubernetes(); + services.AddApplicationInsightsKubernetesEnricher(); services.AddMvc(); } ``` diff --git a/examples/WindowsContainer/Startup.cs b/examples/WindowsContainer/Startup.cs index d4daf0e..7a9aff0 100644 --- a/examples/WindowsContainer/Startup.cs +++ b/examples/WindowsContainer/Startup.cs @@ -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(); } diff --git a/src/ApplicationInsights.Kubernetes.HostingStartup/K8sInjection.cs b/src/ApplicationInsights.Kubernetes.HostingStartup/K8sInjection.cs index 0261935..db1800c 100644 --- a/src/ApplicationInsights.Kubernetes.HostingStartup/K8sInjection.cs +++ b/src/ApplicationInsights.Kubernetes.HostingStartup/K8sInjection.cs @@ -17,7 +17,7 @@ public void Configure(IWebHostBuilder builder) builder.UseApplicationInsights() .ConfigureServices(services => { - services.EnableKubernetes(); + services.AddApplicationInsightsKubernetesEnricher(); }); } } diff --git a/src/ApplicationInsights.Kubernetes/Extensions/ApplicationInsightsExtensions.cs b/src/ApplicationInsights.Kubernetes/Extensions/ApplicationInsightsExtensions.cs index 062905d..f5b492f 100644 --- a/src/ApplicationInsights.Kubernetes/Extensions/ApplicationInsightsExtensions.cs +++ b/src/ApplicationInsights.Kubernetes/Extensions/ApplicationInsightsExtensions.cs @@ -16,14 +16,14 @@ namespace Microsoft.Extensions.DependencyInjection public static class ApplicationInsightsExtensions { /// - /// 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. /// /// Collection of service descriptors. /// Maximum time to wait for spinning up the container. /// Collection builder. /// Delegate to detect if the current application is running in Kubernetes hosted container. /// The collection of services descriptors we injected into. - public static IServiceCollection EnableKubernetes( + public static IServiceCollection AddApplicationInsightsKubernetesEnricher( this IServiceCollection services, TimeSpan? timeout = null, IKubernetesServiceCollectionBuilder kubernetesServiceCollectionBuilder = null, @@ -35,16 +35,13 @@ public static class ApplicationInsightsExtensions } /// - /// Enables Application Insights Kubernetes for a given - /// TelemetryConfiguration. + /// Enables Application Insights Kubernetes for a given TelemetryConfiguration. /// /// - /// 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. /// - public static void EnableKubernetes( + public static void AddApplicationInsightsKubernetesEnricher( this TelemetryConfiguration telemetryConfiguration, TimeSpan? timeout = null, IKubernetesServiceCollectionBuilder kubernetesServiceCollectionBuilder = null, @@ -69,6 +66,47 @@ public static class ApplicationInsightsExtensions } } + /// + /// Please use AddApplicationInsightsKubernetesEnricher() insead. + /// Enables Application Insights Kubernetes for the Default TelemtryConfiguration in the dependency injection system. + /// + /// Collection of service descriptors. + /// Maximum time to wait for spinning up the container. + /// Collection builder. + /// Delegate to detect if the current application is running in Kubernetes hosted container. + /// The collection of services descriptors we injected into. + [Obsolete("Use AddApplicationInsightsKubernetesEnricher() instead", false)] + public static IServiceCollection EnableKubernetes( + this IServiceCollection services, + TimeSpan? timeout = null, + IKubernetesServiceCollectionBuilder kubernetesServiceCollectionBuilder = null, + Func detectKubernetes = null) + { + return services.AddApplicationInsightsKubernetesEnricher(timeout, kubernetesServiceCollectionBuilder, detectKubernetes); + } + + /// + /// Please use "AddApplicationInsightsKubernetesEnricher()" insead. + /// Enables Application Insights Kubernetes for a given + /// TelemetryConfiguration. + /// + /// + /// 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. + /// + [Obsolete("Use AddApplicationInsightsKubernetesEnricher() instead", false)] + public static void EnableKubernetes( + this TelemetryConfiguration telemetryConfiguration, + TimeSpan? timeout = null, + IKubernetesServiceCollectionBuilder kubernetesServiceCollectionBuilder = null, + Func detectKubernetes = null) + { + telemetryConfiguration.AddApplicationInsightsKubernetesEnricher( + timeout, kubernetesServiceCollectionBuilder, detectKubernetes); + } + /// /// Enables applicaiton insights for kubernetes. /// diff --git a/tests/UnitTests/KubernetesEnablementTests.cs b/tests/UnitTests/KubernetesEnablementTests.cs index 36ce568..41226bd 100644 --- a/tests/UnitTests/KubernetesEnablementTests.cs +++ b/tests/UnitTests/KubernetesEnablementTests.cs @@ -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))); @@ -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); diff --git a/troubleshooting/AIK8sTroubleShooting/Startup.cs b/troubleshooting/AIK8sTroubleShooting/Startup.cs index c4d1c88..24b6342 100644 --- a/troubleshooting/AIK8sTroubleShooting/Startup.cs +++ b/troubleshooting/AIK8sTroubleShooting/Startup.cs @@ -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); }