1
0
Форкнуть 0
fix: exclude request filter processor can block telemetry
This commit is contained in:
Pratik Bhattacharya 2022-01-06 03:25:19 +05:30
Родитель 069e0b2b3d
Коммит 6e42c5900a
15 изменённых файлов: 300 добавлений и 12 удалений

Просмотреть файл

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<ApplicationInsightsResourceId>/subscriptions/05a315f7-744f-4692-b9dd-1aed7c6cee64/resourceGroups/fxpauthzprodrg/providers/microsoft.insights/components/PSAuthZServiceAIProd</ApplicationInsightsResourceId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\AppInsights.EnterpriseTelemetry.AspNetCore.Extension.csproj" />
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,71 @@
using System;
using Microsoft.AspNetCore.Mvc;
using AppInsights.EnterpriseTelemetry;
using AppInsights.EnterpriseTelemetry.Context;
using AppInsights.EnterpriseTelemetry.Web.Extension.Filters;
namespace AppInsights.EnterpriseTelemetry.AspNetCore.Extension.SampleNet6
{
[ServiceFilter(typeof(RequestResponseLoggerFilterAttribute))]
[Route("api/telemetry")]
public class TelemetryController : ControllerBase
{
private readonly ILogger _logger;
public TelemetryController(ILogger logger)
{
_logger = logger;
}
[HttpGet]
[Route("/api/probe/ping")]
public IActionResult Ping()
{
return new OkObjectResult("Pong");
}
[HttpPost]
[Route("message")]
public IActionResult LogMessage([FromQuery] string message)
{
var messageContext = new MessageContext(message);
_logger.LogRequestBody(message);
_logger.Log(messageContext);
return Ok("Message Logged");
}
[HttpPost]
[Route("exception")]
public IActionResult LogException([FromQuery] string message)
{
var context = new ExceptionContext(new Exception(message));
_logger.LogRequestBody(message);
_logger.Log(context);
return Ok("Exception Logged");
}
[HttpPost]
[Route("event")]
public IActionResult LogMessage([FromBody] EventContext context)
{
_logger.Log(context);
return Ok("Message Logged");
}
[HttpPost]
[Route("metric")]
public IActionResult LogMessage([FromBody] MetricContext context)
{
_logger.Log(context);
return Ok("Message Logged");
}
[HttpPost]
[Route("error")]
public IActionResult ThrowException([FromQuery] string errorMessage)
{
new UnhandledExceptionGenerator().Generate(errorMessage);
return new OkObjectResult("Should throw error");
}
}
}

28
SampleNet6/Program.cs Normal file
Просмотреть файл

@ -0,0 +1,28 @@
using AppInsights.EnterpriseTelemetry.Web.Extension;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddEnterpriseTelemetry(builder.Configuration);
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.UseEnterpriseTelemetry(builder.Configuration);
app.MapControllers();
app.Run();

Просмотреть файл

@ -0,0 +1,67 @@
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"resourceGroupName": {
"type": "string",
"defaultValue": "fxpauthzprodrg",
"metadata": {
"_parameterType": "resourceGroup",
"description": "Name of the resource group for the resource. It is recommended to put resources under same resource group for better tracking."
}
},
"resourceGroupLocation": {
"type": "string",
"defaultValue": "",
"metadata": {
"_parameterType": "location",
"description": "Location of the resource group. Resource groups could have different location than resources."
}
},
"resourceLocation": {
"type": "string",
"defaultValue": "[parameters('resourceGroupLocation')]",
"metadata": {
"_parameterType": "location",
"description": "Location of the resource. By default use resource group's location, unless the resource provider is not supported there."
}
}
},
"resources": [
{
"type": "Microsoft.Resources/resourceGroups",
"name": "[parameters('resourceGroupName')]",
"location": "[parameters('resourceGroupLocation')]",
"apiVersion": "2019-10-01"
},
{
"type": "Microsoft.Resources/deployments",
"name": "[concat(parameters('resourceGroupName'), 'Deployment', uniqueString(concat('PSAuthZServiceAIProd', subscription().subscriptionId)))]",
"resourceGroup": "[parameters('resourceGroupName')]",
"apiVersion": "2019-10-01",
"dependsOn": [
"[parameters('resourceGroupName')]"
],
"properties": {
"mode": "Incremental",
"template": {
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"name": "PSAuthZServiceAIProd",
"type": "microsoft.insights/components",
"location": "[parameters('resourceLocation')]",
"kind": "web",
"properties": {},
"apiVersion": "2015-05-01"
}
]
}
}
}
],
"metadata": {
"_dependencyType": "appInsights.azure"
}
}

Просмотреть файл

@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:3224",
"sslPort": 44303
}
},
"profiles": {
"SampleNet6": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7010;http://localhost:5010",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

Просмотреть файл

@ -0,0 +1,8 @@
{
"dependencies": {
"appInsights1": {
"type": "appInsights",
"connectionId": "APPLICATIONINSIGHTS_CONNECTION_STRING"
}
}
}

Просмотреть файл

@ -0,0 +1,10 @@
{
"dependencies": {
"appInsights1": {
"secretStore": null,
"resourceId": "/subscriptions/[parameters('subscriptionId')]/resourceGroups/[parameters('resourceGroupName')]/providers/microsoft.insights/components/PSAuthZServiceAIProd",
"type": "appInsights.azure",
"connectionId": "APPLICATIONINSIGHTS_CONNECTION_STRING"
}
}
}

Просмотреть файл

@ -0,0 +1,20 @@
namespace AppInsights.EnterpriseTelemetry.AspNetCore.Extension.SampleNet6
{
public class UnhandledExceptionGenerator
{
public void Generate(string error)
{
throw new System.Exception(error);
}
public void GenerateInner(string error)
{
throw new System.Exception("Generated Exception", new System.Exception(error));
}
public void GenerateArgument(string argName)
{
throw new ArgumentException(argName);
}
}
}

Просмотреть файл

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

Просмотреть файл

@ -0,0 +1,14 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ApplicationInsights": {
"TraceLevel": "Verbose",
"ConnectionString": "InstrumentationKey=40aa80ba-2e03-4b17-9f8b-65694205db32;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/",
"InstrumentationKey": "40aa80ba-2e03-4b17-9f8b-65694205db32"
}
}

Просмотреть файл

@ -5,14 +5,12 @@
<LangVersion>Preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AppInsights.EnterpriseTelemetry.AspNetCore.Extension" Version="1.1.0" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="4.1.2" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.13" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\AppInsights.EnterpriseTelemetry.AspNetCore.Extension.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>

Просмотреть файл

@ -6,15 +6,12 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AppInsights.EnterpriseTelemetry.AspNetCore.Extension" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\AppInsights.EnterpriseTelemetry.AspNetCore.Extension.csproj" />
</ItemGroup>
<ItemGroup>
<Content Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>

Просмотреть файл

@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<PackageId>AppInsights.EnterpriseTelemetry.AspNetCore.Extension</PackageId>
<Title>AppInsights.EnterpriseTelemetry.AspNetCore.Extension</Title>
<Version>1.1.0</Version>
<Version>6.0.0-rc-1.0</Version>
<Authors>Pratik Bhattacharya</Authors>
<Company>Microsoft</Company>
<PackageDescription>Library for capturing enterprise level telemetry in an Web application built on ASP.NET Core 2 and above</PackageDescription>
@ -12,11 +13,10 @@
<RepositoryUrl>https://github.com/microsoft/AppInsights.EnterpriseTelemetry</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<RepositoryBranch>main</RepositoryBranch>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AppInsights.EnterpriseTelemetry" Version="1.1.0" />
<PackageReference Include="AppInsights.EnterpriseTelemetry" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
</ItemGroup>

Просмотреть файл

@ -11,6 +11,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sample", "Sample", "{993104
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AppInsights.EnterpriseTelemetry.AspNetCore.Extension.FunctionAppSample", "..\functionAppSample\AppInsights.EnterpriseTelemetry.AspNetCore.Extension.FunctionAppSample.csproj", "{81F3893F-4EDE-4CE7-AD05-F6066BE8AFFE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppInsights.EnterpriseTelemetry.AspNetCore.Extension.SampleNet6", "..\SampleNet6\AppInsights.EnterpriseTelemetry.AspNetCore.Extension.SampleNet6.csproj", "{853F88DD-BD9E-4548-B33D-D1BC8C32FD5B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ext", "ext", "{5DF60990-6F22-4D31-828D-36056518B841}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AppInsights.EnterpriseTelemetry", "..\..\AppInsights.EnterpriseTelemetry\src\AppInsights.EnterpriseTelemetry.csproj", "{32E7ECE9-6955-4787-8EC2-30045A53397E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -29,6 +35,14 @@ Global
{81F3893F-4EDE-4CE7-AD05-F6066BE8AFFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{81F3893F-4EDE-4CE7-AD05-F6066BE8AFFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{81F3893F-4EDE-4CE7-AD05-F6066BE8AFFE}.Release|Any CPU.Build.0 = Release|Any CPU
{853F88DD-BD9E-4548-B33D-D1BC8C32FD5B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{853F88DD-BD9E-4548-B33D-D1BC8C32FD5B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{853F88DD-BD9E-4548-B33D-D1BC8C32FD5B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{853F88DD-BD9E-4548-B33D-D1BC8C32FD5B}.Release|Any CPU.Build.0 = Release|Any CPU
{32E7ECE9-6955-4787-8EC2-30045A53397E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{32E7ECE9-6955-4787-8EC2-30045A53397E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{32E7ECE9-6955-4787-8EC2-30045A53397E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{32E7ECE9-6955-4787-8EC2-30045A53397E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -36,6 +50,8 @@ Global
GlobalSection(NestedProjects) = preSolution
{0F0071BA-01EE-412A-A568-41F231E842FE} = {99310426-83A4-42A2-80FC-FAEC65498D33}
{81F3893F-4EDE-4CE7-AD05-F6066BE8AFFE} = {99310426-83A4-42A2-80FC-FAEC65498D33}
{853F88DD-BD9E-4548-B33D-D1BC8C32FD5B} = {99310426-83A4-42A2-80FC-FAEC65498D33}
{32E7ECE9-6955-4787-8EC2-30045A53397E} = {5DF60990-6F22-4D31-828D-36056518B841}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B6D83FD0-A179-4524-BCF7-A67567E6FE89}

Просмотреть файл

@ -7,9 +7,9 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.ApplicationInsights.Extensibility;
using AppInsights.EnterpriseTelemetry.Configurations;
using AppInsights.EnterpriseTelemetry.Web.Extension.Filters;
using AppInsights.EnterpriseTelemetry.AppInsightsProcessors;
using AppInsights.EnterpriseTelemetry.AppInsightsInitializers;
using AppInsights.EnterpriseTelemetry.Web.Extension.Middlewares;
using AppInsights.EnterpriseTelemetry.AppInsightsProcessors;
namespace AppInsights.EnterpriseTelemetry.AspNetCore.Extension
{
@ -21,7 +21,7 @@ namespace AppInsights.EnterpriseTelemetry.AspNetCore.Extension
protected readonly IList<ITelemetryInitializer> _telemetryInitializers;
protected ILogger _logger;
protected static readonly object _lock = new object();
protected static readonly object _lock = new();
public TelemetryExtensionsBuilder() { }
@ -62,6 +62,8 @@ namespace AppInsights.EnterpriseTelemetry.AspNetCore.Extension
{
ILogger logger = CreateLogger();
services.AddSingleton(logger);
services.AddSingleton(_appInsightsConfigurationResolver.Resolve());
services.AddSingleton(_appMetadataConfigurationResolver.Resolve());
AddTrackingFilter(services);
AddRequestResponseFilter(services);
services.AddApplicationInsightsTelemetry(_configuration);