From c3e15969b9e001a0bd57d2ea5318bf3e5bbf4875 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20Tom=C3=A1s?= Date: Wed, 11 Oct 2017 18:53:26 +0200 Subject: [PATCH] Added Appinsights K8s telemetry initializer --- .env | 1 + docker-compose.override.yml | 9 ++ k8s/conf_local.yml | 2 + k8s/deployments.yaml | 84 +++++++++++++++++-- .../Basket/Basket.API/Basket.API.csproj | 2 + src/Services/Basket/Basket.API/Program.cs | 1 + src/Services/Basket/Basket.API/Startup.cs | 10 ++- .../Catalog/Catalog.API/Catalog.API.csproj | 2 + src/Services/Catalog/Catalog.API/Program.cs | 4 +- src/Services/Catalog/Catalog.API/Startup.cs | 7 +- .../Identity/Identity.API/Identity.API.csproj | 2 + src/Services/Identity/Identity.API/Startup.cs | 6 ++ .../Locations.API/Locations.API.csproj | 2 + .../Location/Locations.API/Startup.cs | 6 ++ .../Marketing.API/Marketing.API.csproj | 2 + .../Marketing/Marketing.API/Startup.cs | 8 +- .../Ordering/Ordering.API/Ordering.API.csproj | 2 + src/Services/Ordering/Ordering.API/Startup.cs | 8 +- .../Payment/Payment.API/Payment.API.csproj | 2 + src/Services/Payment/Payment.API/Startup.cs | 14 +++- src/Web/WebMVC/Startup.cs | 6 ++ src/Web/WebMVC/WebMVC.csproj | 2 + src/Web/WebSPA/Startup.cs | 6 ++ src/Web/WebSPA/WebSPA.csproj | 2 + src/Web/WebStatus/Startup.cs | 6 ++ src/Web/WebStatus/WebStatus.csproj | 2 + 26 files changed, 182 insertions(+), 16 deletions(-) diff --git a/.env b/.env index a1c927e77..9deb66be7 100644 --- a/.env +++ b/.env @@ -24,3 +24,4 @@ ESHOP_PROD_EXTERNAL_DNS_NAME_OR_IP=10.121.122.92 #ESHOP_SERVICE_BUS_USERNAME= #ESHOP_SERVICE_BUS_PASSWORD= #INSTRUMENTATION_KEY= +#ORCHESTRATOR_TYPE= \ No newline at end of file diff --git a/docker-compose.override.yml b/docker-compose.override.yml index ece9c804b..81a6ba959 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -20,6 +20,7 @@ services: - EventBusPassword=${ESHOP_SERVICE_BUS_PASSWORD} - AzureServiceBusEnabled=False - ApplicationInsights__InstrumentationKey=${INSTRUMENTATION_KEY} + - OrchestratorType=${ORCHESTRATOR_TYPE} ports: - "5103:80" @@ -38,6 +39,7 @@ services: - AzureServiceBusEnabled=False - AzureStorageEnabled=False - ApplicationInsights__InstrumentationKey=${INSTRUMENTATION_KEY} + - OrchestratorType=${ORCHESTRATOR_TYPE} ports: - "5101:80" @@ -55,6 +57,7 @@ services: - OrderingApiClient=http://${ESHOP_EXTERNAL_DNS_NAME_OR_IP}:5102 - UseCustomizationData=True - ApplicationInsights__InstrumentationKey=${INSTRUMENTATION_KEY} + - OrchestratorType=${ORCHESTRATOR_TYPE} ports: - "5105:80" @@ -73,6 +76,7 @@ services: - CheckUpdateTime=30000 - GracePeriodTime=1 - ApplicationInsights__InstrumentationKey=${INSTRUMENTATION_KEY} + - OrchestratorType=${ORCHESTRATOR_TYPE} ports: - "5102:80" @@ -95,6 +99,7 @@ services: - AzureServiceBusEnabled=False - AzureStorageEnabled=False - ApplicationInsights__InstrumentationKey=${INSTRUMENTATION_KEY} + - OrchestratorType=${ORCHESTRATOR_TYPE} ports: - "5110:80" @@ -116,6 +121,7 @@ services: - PaymentUrlHC=http://payment.api/hc - UseCustomizationData=True - ApplicationInsights__InstrumentationKey=${INSTRUMENTATION_KEY} + - OrchestratorType=${ORCHESTRATOR_TYPE} ports: - "5104:80" @@ -137,6 +143,7 @@ services: - PaymentUrlHC=http://payment.api/hc - UseCustomizationData=True - ApplicationInsights__InstrumentationKey=${INSTRUMENTATION_KEY} + - OrchestratorType=${ORCHESTRATOR_TYPE} ports: - "5100:80" @@ -154,6 +161,7 @@ services: - mvc=http://webmvc/hc - spa=http://webspa/hc - ApplicationInsights__InstrumentationKey=${INSTRUMENTATION_KEY} + - OrchestratorType=${ORCHESTRATOR_TYPE} ports: - "5107:80" @@ -182,6 +190,7 @@ services: - EventBusPassword=${ESHOP_SERVICE_BUS_PASSWORD} - AzureServiceBusEnabled=False - ApplicationInsights__InstrumentationKey=${INSTRUMENTATION_KEY} + - OrchestratorType=${ORCHESTRATOR_TYPE} ports: - "5109:80" diff --git a/k8s/conf_local.yml b/k8s/conf_local.yml index 00d1c2398..a605ff7aa 100644 --- a/k8s/conf_local.yml +++ b/k8s/conf_local.yml @@ -25,5 +25,7 @@ data: keystore: keystore-data GracePeriodManager_GracePeriodTime: "1" GracePeriodManager_CheckUpdateTime: "15000" + Instrumentation_Key: "" + diff --git a/k8s/deployments.yaml b/k8s/deployments.yaml index 2a9d6862e..14b5ac31b 100644 --- a/k8s/deployments.yaml +++ b/k8s/deployments.yaml @@ -32,11 +32,18 @@ spec: configMapKeyRef: name: externalcfg key: UseAzureServiceBus + - name: ApplicationInsights__InstrumentationKey + valueFrom: + configMapKeyRef: + name: externalcfg + key: Instrumentation_Key - name: IdentityUrl valueFrom: configMapKeyRef: name: urls key: IdentityUrl + - name: OrchestratorType + value: 'K8S' ports: - containerPort: 80 readinessProbe: @@ -95,6 +102,13 @@ spec: configMapKeyRef: name: externalcfg key: CatalogBus + - name: ApplicationInsights__InstrumentationKey + valueFrom: + configMapKeyRef: + name: externalcfg + key: Instrumentation_Key + - name: OrchestratorType + value: 'K8S' ports: - containerPort: 80 readinessProbe: @@ -133,7 +147,7 @@ spec: env: - name: PATH_BASE value: /identity - - name: ConnectionStrings__DefaultConnection + - name: ConnectionString valueFrom: configMapKeyRef: name: externalcfg @@ -174,7 +188,14 @@ spec: valueFrom: configMapKeyRef: name: urls - key: OrderingApiClient + key: OrderingApiClient + - name: ApplicationInsights__InstrumentationKey + valueFrom: + configMapKeyRef: + name: externalcfg + key: Instrumentation_Key + - name: OrchestratorType + value: 'K8S' ports: - containerPort: 80 readinessProbe: @@ -242,7 +263,14 @@ spec: valueFrom: configMapKeyRef: name: externalcfg - key: GracePeriodManager_GracePeriodTime + key: GracePeriodManager_GracePeriodTime + - name: ApplicationInsights__InstrumentationKey + valueFrom: + configMapKeyRef: + name: externalcfg + key: Instrumentation_Key + - name: OrchestratorType + value: 'K8S' ports: - containerPort: 80 readinessProbe: @@ -310,7 +338,14 @@ spec: valueFrom: configMapKeyRef: name: urls - key: IdentityUrl + key: IdentityUrl + - name: ApplicationInsights__InstrumentationKey + valueFrom: + configMapKeyRef: + name: externalcfg + key: Instrumentation_Key + - name: OrchestratorType + value: 'K8S' ports: - containerPort: 80 readinessProbe: @@ -388,7 +423,14 @@ spec: valueFrom: configMapKeyRef: name: urls - key: Marketing_PicBaseUrl + key: Marketing_PicBaseUrl + - name: ApplicationInsights__InstrumentationKey + valueFrom: + configMapKeyRef: + name: externalcfg + key: Instrumentation_Key + - name: OrchestratorType + value: 'K8S' ports: - containerPort: 80 readinessProbe: @@ -437,6 +479,13 @@ spec: configMapKeyRef: name: externalcfg key: PaymentBus + - name: ApplicationInsights__InstrumentationKey + valueFrom: + configMapKeyRef: + name: externalcfg + key: Instrumentation_Key + - name: OrchestratorType + value: 'K8S' ports: - containerPort: 80 readinessProbe: @@ -546,7 +595,14 @@ spec: valueFrom: configMapKeyRef: name: urls - key: PaymentHealthCheckUrl + key: PaymentHealthCheckUrl + - name: ApplicationInsights__InstrumentationKey + valueFrom: + configMapKeyRef: + name: externalcfg + key: Instrumentation_Key + - name: OrchestratorType + value: 'K8S' ports: - containerPort: 80 readinessProbe: @@ -629,7 +685,14 @@ spec: valueFrom: configMapKeyRef: name: urls - key: PaymentHealthCheckUrl + key: PaymentHealthCheckUrl + - name: ApplicationInsights__InstrumentationKey + valueFrom: + configMapKeyRef: + name: externalcfg + key: Instrumentation_Key + - name: OrchestratorType + value: 'K8S' ports: - containerPort: 80 imagePullSecrets: @@ -726,6 +789,13 @@ spec: configMapKeyRef: name: urls key: PaymentHealthCheckUrl + - name: ApplicationInsights__InstrumentationKey + valueFrom: + configMapKeyRef: + name: externalcfg + key: Instrumentation_Key + - name: OrchestratorType + value: 'K8S' ports: - containerPort: 80 readinessProbe: diff --git a/src/Services/Basket/Basket.API/Basket.API.csproj b/src/Services/Basket/Basket.API/Basket.API.csproj index 4943faf19..5e07fbddb 100644 --- a/src/Services/Basket/Basket.API/Basket.API.csproj +++ b/src/Services/Basket/Basket.API/Basket.API.csproj @@ -17,6 +17,8 @@ + + diff --git a/src/Services/Basket/Basket.API/Program.cs b/src/Services/Basket/Basket.API/Program.cs index 2ae9b47b2..19bbf2c24 100644 --- a/src/Services/Basket/Basket.API/Program.cs +++ b/src/Services/Basket/Basket.API/Program.cs @@ -34,6 +34,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API builder.AddConsole(); builder.AddDebug(); }) + .UseApplicationInsights() .Build(); } } diff --git a/src/Services/Basket/Basket.API/Startup.cs b/src/Services/Basket/Basket.API/Startup.cs index e7f3c1276..ae65a8486 100644 --- a/src/Services/Basket/Basket.API/Startup.cs +++ b/src/Services/Basket/Basket.API/Startup.cs @@ -65,6 +65,12 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API services.Configure(Configuration); + if(Configuration.GetValue("OrchestratorType").Equals("K8S")) + { + // Enable K8s telemetry initializer + services.EnableKubernetes(); + } + //By connecting here we are making sure that our service //cannot start until redis is ready. This might slow down startup, //but given that there is a delay on resolving the ip address @@ -203,8 +209,8 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API // prevent from mapping "sub" claim to nameidentifier. JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); - var identityUrl = Configuration.GetValue("IdentityUrl"); - + var identityUrl = Configuration.GetValue("IdentityUrl"); + services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; diff --git a/src/Services/Catalog/Catalog.API/Catalog.API.csproj b/src/Services/Catalog/Catalog.API/Catalog.API.csproj index a2a4baad9..5d5812953 100644 --- a/src/Services/Catalog/Catalog.API/Catalog.API.csproj +++ b/src/Services/Catalog/Catalog.API/Catalog.API.csproj @@ -36,6 +36,8 @@ + + diff --git a/src/Services/Catalog/Catalog.API/Program.cs b/src/Services/Catalog/Catalog.API/Program.cs index 7d18e2e66..bef3fa360 100644 --- a/src/Services/Catalog/Catalog.API/Program.cs +++ b/src/Services/Catalog/Catalog.API/Program.cs @@ -32,6 +32,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup() + .UseApplicationInsights() .UseHealthChecks("/hc") .UseContentRoot(Directory.GetCurrentDirectory()) .UseWebRoot("Pics") @@ -44,8 +45,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API builder.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); builder.AddConsole(); builder.AddDebug(); - }) - .UseApplicationInsights() + }) .Build(); } } \ No newline at end of file diff --git a/src/Services/Catalog/Catalog.API/Startup.cs b/src/Services/Catalog/Catalog.API/Startup.cs index 34b80526b..ef61f1aeb 100644 --- a/src/Services/Catalog/Catalog.API/Startup.cs +++ b/src/Services/Catalog/Catalog.API/Startup.cs @@ -13,7 +13,6 @@ using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus; - using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services; using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.EventHandling; @@ -43,6 +42,12 @@ services.AddApplicationInsightsTelemetry(Configuration); + if (Configuration.GetValue("OrchestratorType").Equals("K8S")) + { + // Enable K8s telemetry initializer + services.EnableKubernetes(); + } + services.AddHealthChecks(checks => { var minutes = 1; diff --git a/src/Services/Identity/Identity.API/Identity.API.csproj b/src/Services/Identity/Identity.API/Identity.API.csproj index a84d21d55..84b1c280c 100644 --- a/src/Services/Identity/Identity.API/Identity.API.csproj +++ b/src/Services/Identity/Identity.API/Identity.API.csproj @@ -17,6 +17,8 @@ + + diff --git a/src/Services/Identity/Identity.API/Startup.cs b/src/Services/Identity/Identity.API/Startup.cs index 11e946da6..da93e1a51 100644 --- a/src/Services/Identity/Identity.API/Startup.cs +++ b/src/Services/Identity/Identity.API/Startup.cs @@ -33,6 +33,12 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API { services.AddApplicationInsightsTelemetry(Configuration); + if (Configuration.GetValue("OrchestratorType").Equals("K8S")) + { + // Enable K8s telemetry initializer + services.EnableKubernetes(); + } + // Add framework services. services.AddDbContext(options => options.UseSqlServer(Configuration["ConnectionString"], diff --git a/src/Services/Location/Locations.API/Locations.API.csproj b/src/Services/Location/Locations.API/Locations.API.csproj index 3d94cdd33..f6a2b0e98 100644 --- a/src/Services/Location/Locations.API/Locations.API.csproj +++ b/src/Services/Location/Locations.API/Locations.API.csproj @@ -12,6 +12,8 @@ + + diff --git a/src/Services/Location/Locations.API/Startup.cs b/src/Services/Location/Locations.API/Startup.cs index 47cf592f7..115fa5f32 100644 --- a/src/Services/Location/Locations.API/Startup.cs +++ b/src/Services/Location/Locations.API/Startup.cs @@ -39,6 +39,12 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API { services.AddApplicationInsightsTelemetry(Configuration); + if (Configuration.GetValue("OrchestratorType").Equals("K8S")) + { + // Enable K8s telemetry initializer + services.EnableKubernetes(); + } + services.AddMvc(options => { options.Filters.Add(typeof(HttpGlobalExceptionFilter)); diff --git a/src/Services/Marketing/Marketing.API/Marketing.API.csproj b/src/Services/Marketing/Marketing.API/Marketing.API.csproj index 9b29871f8..a462eb04e 100644 --- a/src/Services/Marketing/Marketing.API/Marketing.API.csproj +++ b/src/Services/Marketing/Marketing.API/Marketing.API.csproj @@ -22,6 +22,8 @@ + + diff --git a/src/Services/Marketing/Marketing.API/Startup.cs b/src/Services/Marketing/Marketing.API/Startup.cs index 321bf93c7..540cb8a12 100644 --- a/src/Services/Marketing/Marketing.API/Startup.cs +++ b/src/Services/Marketing/Marketing.API/Startup.cs @@ -46,6 +46,12 @@ { services.AddApplicationInsightsTelemetry(Configuration); + if (Configuration.GetValue("OrchestratorType").Equals("K8S")) + { + // Enable K8s telemetry initializer + services.EnableKubernetes(); + } + // Add framework services. services.AddMvc(options => { @@ -200,7 +206,7 @@ ConfigureEventBus(app); } - private void ConfigureAuthService(IServiceCollection services) + private void ConfigureAuthService(IServiceCollection services, ILoggerFactory loggerFactory) { // prevent from mapping "sub" claim to nameidentifier. JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); diff --git a/src/Services/Ordering/Ordering.API/Ordering.API.csproj b/src/Services/Ordering/Ordering.API/Ordering.API.csproj index d4ebd294a..89a3b81a6 100644 --- a/src/Services/Ordering/Ordering.API/Ordering.API.csproj +++ b/src/Services/Ordering/Ordering.API/Ordering.API.csproj @@ -38,6 +38,8 @@ + + diff --git a/src/Services/Ordering/Ordering.API/Startup.cs b/src/Services/Ordering/Ordering.API/Startup.cs index a4b5d1e1f..79de89394 100644 --- a/src/Services/Ordering/Ordering.API/Startup.cs +++ b/src/Services/Ordering/Ordering.API/Startup.cs @@ -47,7 +47,13 @@ public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddApplicationInsightsTelemetry(Configuration); - + + if (Configuration.GetValue("OrchestratorType").Equals("K8S")) + { + // Enable K8s telemetry initializer + services.EnableKubernetes(); + } + // Add framework services. services.AddMvc(options => { diff --git a/src/Services/Payment/Payment.API/Payment.API.csproj b/src/Services/Payment/Payment.API/Payment.API.csproj index 736d13d7a..465acae4c 100644 --- a/src/Services/Payment/Payment.API/Payment.API.csproj +++ b/src/Services/Payment/Payment.API/Payment.API.csproj @@ -9,6 +9,8 @@ + + diff --git a/src/Services/Payment/Payment.API/Startup.cs b/src/Services/Payment/Payment.API/Startup.cs index 0fc94fc1d..e20b2fd76 100644 --- a/src/Services/Payment/Payment.API/Startup.cs +++ b/src/Services/Payment/Payment.API/Startup.cs @@ -32,9 +32,16 @@ namespace Payment.API // This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { + services.AddApplicationInsightsTelemetry(Configuration); services.Configure(Configuration); + if (Configuration.GetValue("OrchestratorType").Equals("K8S")) + { + // Enable K8s telemetry initializer + services.EnableKubernetes(); + } + if (Configuration.GetValue("AzureServiceBusEnabled")) { services.AddSingleton(sp => @@ -84,8 +91,11 @@ namespace Payment.API } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env) - { + public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) + { + loggerFactory.AddAzureWebAppDiagnostics(); + loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace); + var pathBase = Configuration["PATH_BASE"]; if (!string.IsNullOrEmpty(pathBase)) { diff --git a/src/Web/WebMVC/Startup.cs b/src/Web/WebMVC/Startup.cs index 6208b4c06..509a55947 100644 --- a/src/Web/WebMVC/Startup.cs +++ b/src/Web/WebMVC/Startup.cs @@ -33,6 +33,12 @@ namespace Microsoft.eShopOnContainers.WebMVC { services.AddApplicationInsightsTelemetry(Configuration); + if (Configuration.GetValue("OrchestratorType").Equals("K8S")) + { + // Enable K8s telemetry initializer + services.EnableKubernetes(); + } + services.AddMvc(); services.AddSession(); diff --git a/src/Web/WebMVC/WebMVC.csproj b/src/Web/WebMVC/WebMVC.csproj index 5b086a904..d1a957b27 100644 --- a/src/Web/WebMVC/WebMVC.csproj +++ b/src/Web/WebMVC/WebMVC.csproj @@ -37,6 +37,8 @@ + + diff --git a/src/Web/WebSPA/Startup.cs b/src/Web/WebSPA/Startup.cs index 0960d68e1..9315088cf 100644 --- a/src/Web/WebSPA/Startup.cs +++ b/src/Web/WebSPA/Startup.cs @@ -38,6 +38,12 @@ namespace eShopConContainers.WebSPA { services.AddApplicationInsightsTelemetry(Configuration); + if (Configuration.GetValue("OrchestratorType").Equals("K8S")) + { + // Enable K8s telemetry initializer + services.EnableKubernetes(); + } + services.AddHealthChecks(checks => { var minutes = 1; diff --git a/src/Web/WebSPA/WebSPA.csproj b/src/Web/WebSPA/WebSPA.csproj index 40ab683fe..f3fa64cdc 100644 --- a/src/Web/WebSPA/WebSPA.csproj +++ b/src/Web/WebSPA/WebSPA.csproj @@ -28,6 +28,8 @@ + + diff --git a/src/Web/WebStatus/Startup.cs b/src/Web/WebStatus/Startup.cs index 0b7271238..f869fbb9b 100644 --- a/src/Web/WebStatus/Startup.cs +++ b/src/Web/WebStatus/Startup.cs @@ -25,6 +25,12 @@ namespace WebStatus { services.AddApplicationInsightsTelemetry(Configuration); + if (Configuration.GetValue("OrchestratorType").Equals("K8S")) + { + // Enable K8s telemetry initializer + services.EnableKubernetes(); + } + services.AddOptions(); // Add framework services. diff --git a/src/Web/WebStatus/WebStatus.csproj b/src/Web/WebStatus/WebStatus.csproj index 603db6850..9a547e3fc 100644 --- a/src/Web/WebStatus/WebStatus.csproj +++ b/src/Web/WebStatus/WebStatus.csproj @@ -6,6 +6,8 @@ + +