Skip to content

Commit

Permalink
Add ApplicationInsights (#187)
Browse files Browse the repository at this point in the history
* Add ApplicationInsights

* Make test less flaky since CosmosDb emulator on GHA is slow
  • Loading branch information
AButler authored Oct 12, 2023
1 parent ace7572 commit 910c381
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 15 deletions.
4 changes: 3 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<PackageVersion Include="Azure.Identity" Version="1.10.1" />
<PackageVersion Include="Bogus" Version="34.0.2" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageVersion Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.11" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.11" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.11" />
Expand All @@ -32,4 +34,4 @@
<PackageVersion Include="Xunit.SkippableFact" Version="1.4.13" />
<PackageVersion Include="Yarp.ReverseProxy" Version="2.0.0" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ public class GetAllCustomersTests
public async Task Valid_ReturnsOk()
{
using var customerApi = new TestCustomerApi();

await customerApi.Given.AnExistingCustomer(CustomerUri.Parse("/customers/ExistingUser"), "bob@bobertson.co.uk", "Bob", "Bobertson");
await customerApi.Given.AnExistingCustomer(CustomerUri.Parse("/customers/AnotherExistingUser"), "bobetta@bobson.co.uk", "Bobetta", "Bobson");
var createdTimestamp = DateTime.UtcNow;

var authHeader = await customerApi.Given.AnExistingConsumer("Customers.Read");

Expand Down Expand Up @@ -43,15 +45,15 @@ public async Task Valid_ReturnsOk()
bob.FirstName.Should().Be("Bob");
bob.LastName.Should().Be("Bobertson");
bob.CustomerUri.Should().Be("/customers/ExistingUser");
bob.CreatedOn.Should().BeWithin(TimeSpan.FromSeconds(2)).Before(DateTimeOffset.UtcNow);
bob.CreatedOn.Should().BeWithin(TimeSpan.FromSeconds(2)).Before(createdTimestamp);

var bobetta = customersResponse.Customers.FirstOrDefault(c => c.CustomerUri == "/customers/AnotherExistingUser");
bobetta.Should().NotBeNull();
bobetta!.EmailAddress.Should().Be("bobetta@bobson.co.uk");
bobetta.FirstName.Should().Be("Bobetta");
bobetta.LastName.Should().Be("Bobson");
bobetta.CustomerUri.Should().Be("/customers/AnotherExistingUser");
bobetta.CreatedOn.Should().BeWithin(TimeSpan.FromSeconds(1)).Before(DateTimeOffset.UtcNow);
bobetta.CreatedOn.Should().BeWithin(TimeSpan.FromSeconds(2)).Before(createdTimestamp);
}

[Fact]
Expand Down
1 change: 1 addition & 0 deletions sample/CustomerApi/CustomerApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
<PackageReference Include="Microsoft.SourceLink.GitHub">
<PrivateAssets>all</PrivateAssets>
Expand Down
1 change: 1 addition & 0 deletions sample/CustomerApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
services.AddControllers();
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
services.AddApplicationInsightsTelemetry();

services.AddAuthentication().AddJwtBearer();

Expand Down
3 changes: 3 additions & 0 deletions sample/CustomerApi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@
"CosmosDb": {
"ConnectionString": "<Secret>",
"DatabaseId": "CustomerApi"
},
"ApplicationInsights": {
"ConnectionString": "<Secret>"
}
}
1 change: 1 addition & 0 deletions sample/CustomerWorker/CustomerWorker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" />
<PackageReference Include="Microsoft.Extensions.Hosting"/>
</ItemGroup>

Expand Down
2 changes: 2 additions & 0 deletions sample/CustomerWorker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
.ConfigureServices(
(_, services) =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services
.AddHealthChecks()
.AddCheck<AlwaysHealthyCheck>("StartupCheck", tags: new[] { "ready" })
Expand Down
3 changes: 3 additions & 0 deletions sample/CustomerWorker/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
{
"ApplicationInsights": {
"ConnectionString": "<Secret>"
}
}
42 changes: 30 additions & 12 deletions sample/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ resource "azurerm_resource_group" "rg" {
location = "uksouth"
}

resource "azurerm_log_analytics_workspace" "log-analytics-workspace" {
name = "log-otter-log-analytics-workspace"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
sku = "PerGB2018"
retention_in_days = 30
}

resource "azurerm_application_insights" "app-insights" {
name = "log-otter-app-insights"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
workspace_id = azurerm_log_analytics_workspace.log-analytics-workspace.id
application_type = "web"
}

resource "azurerm_cosmosdb_account" "db" {
name = "log-otter-samples"
location = azurerm_resource_group.rg.location
Expand Down Expand Up @@ -61,13 +77,14 @@ resource "azurerm_linux_web_app" "customer-api" {
https_only = true

app_settings = {
ASPNETCORE_ENVIRONMENT = "Development"
COSMOSDB__CONNECTIONSTRING = azurerm_cosmosdb_account.db.primary_sql_connection_string
DOCKER_REGISTRY_SERVER_URL = var.docker_registry_url
DOCKER_REGISTRY_SERVER_USERNAME = var.docker_registry_username
DOCKER_REGISTRY_SERVER_PASSWORD = var.docker_registry_password
WEBSITES_ENABLE_APP_SERVICE_STORAGE = "false"
WEBSITE_WARMUP_PATH = "/health"
APPLICATIONINSIGHTS__CONNECTIONSTRING = azurerm_application_insights.app-insights.connection_string
ASPNETCORE_ENVIRONMENT = "Development"
COSMOSDB__CONNECTIONSTRING = azurerm_cosmosdb_account.db.primary_sql_connection_string
DOCKER_REGISTRY_SERVER_URL = var.docker_registry_url
DOCKER_REGISTRY_SERVER_USERNAME = var.docker_registry_username
DOCKER_REGISTRY_SERVER_PASSWORD = var.docker_registry_password
WEBSITES_ENABLE_APP_SERVICE_STORAGE = "false"
WEBSITE_WARMUP_PATH = "/health"
}

site_config {
Expand Down Expand Up @@ -100,11 +117,12 @@ resource "azurerm_linux_web_app" "customer-worker" {
https_only = true

app_settings = {
DOCKER_REGISTRY_SERVER_URL = var.docker_registry_url
DOCKER_REGISTRY_SERVER_USERNAME = var.docker_registry_username
DOCKER_REGISTRY_SERVER_PASSWORD = var.docker_registry_password
WEBSITES_ENABLE_APP_SERVICE_STORAGE = "false"
WEBSITE_WARMUP_PATH = "/health"
APPLICATIONINSIGHTS__CONNECTIONSTRING = azurerm_application_insights.app-insights.connection_string
DOCKER_REGISTRY_SERVER_URL = var.docker_registry_url
DOCKER_REGISTRY_SERVER_USERNAME = var.docker_registry_username
DOCKER_REGISTRY_SERVER_PASSWORD = var.docker_registry_password
WEBSITES_ENABLE_APP_SERVICE_STORAGE = "false"
WEBSITE_WARMUP_PATH = "/health"
}

site_config {
Expand Down

0 comments on commit 910c381

Please sign in to comment.