Skip to content
This repository was archived by the owner on Jun 10, 2020. It is now read-only.

Commit 83d308a

Browse files
Sergey KanzhelevSergey Kanzhelev
authored andcommitted
allow monitoring of non-MVC apps
1 parent 0c9d84d commit 83d308a

File tree

10 files changed

+254
-15
lines changed

10 files changed

+254
-15
lines changed

ApplicationInsights.AspNet.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 14
4-
VisualStudioVersion = 14.0.23102.2
4+
VisualStudioVersion = 14.0.23107.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{2E6DDE9E-8C75-4F9C-8906-08EBDD6E73EF}"
77
EndProject
@@ -34,6 +34,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "StartupPerf", "StartupPerf"
3434
test\StartupPerf\startupPerf.ps1 = test\StartupPerf\startupPerf.ps1
3535
EndProjectSection
3636
EndProject
37+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "EmptyApp.FunctionalTests", "test\EmptyApp.FunctionalTests\EmptyApp.FunctionalTests.xproj", "{71CE2DB2-C7AA-4454-B5F2-774BC575E321}"
38+
EndProject
3739
Global
3840
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3941
Debug|Any CPU = Debug|Any CPU
@@ -60,6 +62,10 @@ Global
6062
{11FB2EE6-7199-4AFF-BC73-25F35675F233}.Debug|Any CPU.Build.0 = Debug|Any CPU
6163
{11FB2EE6-7199-4AFF-BC73-25F35675F233}.Release|Any CPU.ActiveCfg = Release|Any CPU
6264
{11FB2EE6-7199-4AFF-BC73-25F35675F233}.Release|Any CPU.Build.0 = Release|Any CPU
65+
{71CE2DB2-C7AA-4454-B5F2-774BC575E321}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
66+
{71CE2DB2-C7AA-4454-B5F2-774BC575E321}.Debug|Any CPU.Build.0 = Debug|Any CPU
67+
{71CE2DB2-C7AA-4454-B5F2-774BC575E321}.Release|Any CPU.ActiveCfg = Release|Any CPU
68+
{71CE2DB2-C7AA-4454-B5F2-774BC575E321}.Release|Any CPU.Build.0 = Release|Any CPU
6369
EndGlobalSection
6470
GlobalSection(SolutionProperties) = preSolution
6571
HideSolutionNode = FALSE
@@ -71,5 +77,6 @@ Global
7177
{B7217A00-66FA-49A8-8EF3-39C07E1F7E33} = {8B5230E5-8138-44D6-839F-DF9248F195EE}
7278
{11FB2EE6-7199-4AFF-BC73-25F35675F233} = {8B5230E5-8138-44D6-839F-DF9248F195EE}
7379
{16B44D67-6214-4DDE-B419-93EF073E2E69} = {8B5230E5-8138-44D6-839F-DF9248F195EE}
80+
{71CE2DB2-C7AA-4454-B5F2-774BC575E321} = {8B5230E5-8138-44D6-839F-DF9248F195EE}
7481
EndGlobalSection
7582
EndGlobal

src/Microsoft.ApplicationInsights.AspNet/TelemetryInitializers/OperationNameTelemetryInitializer.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ public class OperationNameTelemetryInitializer : TelemetryInitializerBase
2020
public OperationNameTelemetryInitializer(IHttpContextAccessor httpContextAccessor, INotifier notifier)
2121
: base(httpContextAccessor)
2222
{
23-
if (notifier == null)
23+
if (notifier != null)
2424
{
25-
throw new ArgumentNullException("notifier");
25+
notifier.EnlistTarget(this);
2626
}
27+
}
2728

28-
notifier.EnlistTarget(this);
29+
public OperationNameTelemetryInitializer(IHttpContextAccessor httpContextAccessor)
30+
: this(httpContextAccessor, null)
31+
{
2932
}
3033

3134
protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
8+
<PropertyGroup Label="Globals">
9+
<ProjectGuid>71ce2db2-c7aa-4454-b5f2-774bc575e321</ProjectGuid>
10+
<RootNamespace>EmptyApp.FunctionalTests</RootNamespace>
11+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
13+
</PropertyGroup>
14+
<PropertyGroup>
15+
<SchemaVersion>2.0</SchemaVersion>
16+
<DevelopmentServerPort>6556</DevelopmentServerPort>
17+
</PropertyGroup>
18+
<ItemGroup>
19+
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
20+
</ItemGroup>
21+
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
22+
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace EmptyApp.FunctionalTests.FunctionalTest
2+
{
3+
using System;
4+
using FunctionalTestUtils;
5+
using Microsoft.ApplicationInsights.DataContracts;
6+
using Xunit;
7+
8+
public class ExceptionTelemetryTests : TelemetryTestsBase
9+
{
10+
private const string assemblyName = "EmptyApp.FunctionalTests";
11+
12+
[Fact]
13+
public void TestBasicRequestPropertiesAfterRequestingRequestThatThrows()
14+
{
15+
using (var server = new InProcessServer(assemblyName))
16+
{
17+
const string RequestPath = "/Exception";
18+
19+
var expectedRequestTelemetry = new RequestTelemetry();
20+
expectedRequestTelemetry.HttpMethod = "GET";
21+
22+
// Request name is tracked incorretly in case of errors right now, tracked by https://github.com/Microsoft/ApplicationInsights-aspnet5/issues/91
23+
expectedRequestTelemetry.Name = "GET /Exception";
24+
expectedRequestTelemetry.ResponseCode = "500";
25+
expectedRequestTelemetry.Success = false;
26+
expectedRequestTelemetry.Url = new System.Uri(server.BaseHost + RequestPath);
27+
this.ValidateBasicRequest(server, "/Exception", expectedRequestTelemetry);
28+
}
29+
}
30+
31+
[Fact]
32+
public void TestBasicExceptionPropertiesAfterRequestingRequestThatThrows()
33+
{
34+
using (var server = new InProcessServer(assemblyName))
35+
{
36+
var expectedExceptionTelemetry = new ExceptionTelemetry();
37+
expectedExceptionTelemetry.HandledAt = ExceptionHandledAt.Platform;
38+
expectedExceptionTelemetry.Exception = new InvalidOperationException();
39+
40+
this.ValidateBasicException(server, "/Exception", expectedExceptionTelemetry);
41+
}
42+
}
43+
}
44+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
namespace EmptyApp.FunctionalTests.FunctionalTest
2+
{
3+
using System.Linq;
4+
using System.Net.Http;
5+
using FunctionalTestUtils;
6+
using Microsoft.ApplicationInsights.DataContracts;
7+
using Xunit;
8+
9+
public class RequestTelemetryTests : TelemetryTestsBase
10+
{
11+
private const string assemblyName = "EmptyApp.FunctionalTests";
12+
13+
[Fact]
14+
public void TestBasicRequestPropertiesAfterRequestingBasicPage()
15+
{
16+
using (var server = new InProcessServer(assemblyName))
17+
{
18+
const string RequestPath = "/";
19+
20+
var expectedRequestTelemetry = new RequestTelemetry();
21+
expectedRequestTelemetry.HttpMethod = "GET";
22+
expectedRequestTelemetry.Name = "GET /";
23+
expectedRequestTelemetry.ResponseCode = "200";
24+
expectedRequestTelemetry.Success = true;
25+
expectedRequestTelemetry.Url = new System.Uri(server.BaseHost + RequestPath);
26+
27+
this.ValidateBasicRequest(server, RequestPath, expectedRequestTelemetry);
28+
}
29+
}
30+
31+
[Fact]
32+
public void TestBasicRequestPropertiesAfterRequestingNotExistingPage()
33+
{
34+
using (var server = new InProcessServer(assemblyName))
35+
{
36+
const string RequestPath = "/not/existing/controller";
37+
38+
var expectedRequestTelemetry = new RequestTelemetry();
39+
expectedRequestTelemetry.HttpMethod = "GET";
40+
expectedRequestTelemetry.Name = "GET /not/existing/controller";
41+
expectedRequestTelemetry.ResponseCode = "404";
42+
expectedRequestTelemetry.Success = false;
43+
expectedRequestTelemetry.Url = new System.Uri(server.BaseHost + RequestPath);
44+
45+
this.ValidateBasicRequest(server, RequestPath, expectedRequestTelemetry);
46+
}
47+
}
48+
49+
[Fact]
50+
public void TestMixedTelemetryItemsReceived()
51+
{
52+
using (var server = new InProcessServer(assemblyName))
53+
{
54+
var httpClient = new HttpClient();
55+
var task = httpClient.GetAsync(server.BaseHost + "/Mixed");
56+
task.Wait(TestTimeoutMs);
57+
58+
var request = server.BackChannel.Buffer.OfType<RequestTelemetry>().Single();
59+
var eventTelemetry = server.BackChannel.Buffer.OfType<EventTelemetry>().Single();
60+
var metricTelemetry = server.BackChannel.Buffer.OfType<MetricTelemetry>().Single();
61+
var traceTelemetry = server.BackChannel.Buffer.OfType<TraceTelemetry>().Single();
62+
63+
Assert.Equal(4, server.BackChannel.Buffer.Count);
64+
Assert.NotNull(request);
65+
Assert.NotNull(eventTelemetry);
66+
Assert.NotNull(metricTelemetry);
67+
Assert.NotNull(traceTelemetry);
68+
}
69+
}
70+
}
71+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNet.Builder;
6+
using Microsoft.AspNet.Http;
7+
using Microsoft.Framework.DependencyInjection;
8+
using Microsoft.ApplicationInsights.AspNet.Extensions;
9+
using Microsoft.Framework.Configuration;
10+
using Microsoft.Framework.Configuration.Memory;
11+
using Microsoft.ApplicationInsights.Channel;
12+
using FunctionalTestUtils;
13+
using Microsoft.ApplicationInsights;
14+
using Microsoft.ApplicationInsights.DataContracts;
15+
using Microsoft.AspNet.Diagnostics;
16+
17+
namespace EmptyApp.FunctionalTests
18+
{
19+
public class Startup
20+
{
21+
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
22+
public void ConfigureServices(IServiceCollection services)
23+
{
24+
services.AddInstance<ITelemetryChannel>(new BackTelemetryChannel());
25+
26+
var builder = new ConfigurationBuilder();
27+
builder.AddApplicationInsightsSettings(instrumentationKey: "Foo");
28+
services.AddApplicationInsightsTelemetry(builder.Build());
29+
}
30+
31+
public void Configure(IApplicationBuilder app)
32+
{
33+
app.UseApplicationInsightsRequestTelemetry();
34+
app.UseErrorPage(new ErrorPageOptions());
35+
app.UseApplicationInsightsExceptionTelemetry();
36+
37+
app.Use(next =>
38+
{
39+
return async context =>
40+
{
41+
if (context.Request.GetUri().ToString().Contains("Exception"))
42+
{
43+
throw new InvalidOperationException();
44+
}
45+
else if (context.Request.GetUri().PathAndQuery == "/")
46+
{
47+
await context.Response.WriteAsync("Hello!");
48+
}
49+
else if (context.Request.GetUri().ToString().Contains("Mixed"))
50+
{
51+
TelemetryClient telemetryClient = (TelemetryClient)context.RequestServices.GetService(typeof(TelemetryClient));
52+
telemetryClient.TrackEvent("GetContact");
53+
telemetryClient.TrackMetric("ContactFile", 1);
54+
telemetryClient.TrackTrace("Fetched contact details.", SeverityLevel.Information);
55+
await context.Response.WriteAsync("Hello!");
56+
}
57+
else
58+
{
59+
await next(context);
60+
}
61+
};
62+
});
63+
}
64+
}
65+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
server=Microsoft.AspNet.Server.WebListener
2+
server.urls=http://localhost:5000
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"webroot": "wwwroot",
3+
"version": "1.0.0-*",
4+
5+
"dependencies": {
6+
"Microsoft.AspNet.Server.IIS": "1.0.0-beta7",
7+
"Microsoft.AspNet.Server.WebListener": "1.0.0-beta7",
8+
"Microsoft.ApplicationInsights.AspNet": "1.0.0-beta7-*",
9+
"FunctionalTestUtils": "1.0.0-beta7-*",
10+
"xunit.runner.aspnet": "2.0.0-aspnet-beta7-*",
11+
"Microsoft.AspNet.Diagnostics": "1.0.0-beta7-*"
12+
},
13+
14+
"commands": {
15+
"web": "Microsoft.AspNet.Hosting --config hosting.ini",
16+
"test": "xunit.runner.aspnet"
17+
},
18+
19+
"frameworks": {
20+
"dnx451": { },
21+
"dnxcore50": { }
22+
},
23+
24+
"publishExclude": [
25+
"node_modules",
26+
"bower_components",
27+
"**.xproj",
28+
"**.user",
29+
"**.vspscc"
30+
],
31+
"exclude": [
32+
"wwwroot",
33+
"node_modules",
34+
"bower_components"
35+
]
36+
}

test/FunctionalTestUtils/project.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
"dependencies": {
44
"Microsoft.ApplicationInsights": "1.1.1-beta",
55
"Microsoft.ApplicationInsights.AspNet": "1.0.0-beta7",
6-
"Microsoft.AspNet.Diagnostics": "1.0.0-beta7-*",
7-
"Microsoft.AspNet.Hosting": "1.0.0-beta7-*",
86
"Microsoft.AspNet.Http.Abstractions": "1.0.0-beta7-*",
9-
"Microsoft.AspNet.Mvc": "6.0.0-beta7-*",
107
"Microsoft.AspNet.Server.WebListener": "1.0.0-beta7-*",
118
"Microsoft.Dnx.Runtime": "1.0.0-beta7-*",
129
"Microsoft.Net.Http.Client": "1.0.0-*",

test/Microsoft.ApplicationInsights.AspNet.Tests/TelemetryInitializers/OperationNameTelemetryInitializerTests.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ public void InitializeThrowIfHttpContextAccessorIsNull()
2323
});
2424
}
2525

26-
[Fact]
27-
public void InitializeThrowIfNotifierIsNull()
28-
{
29-
var ac = new HttpContextAccessor() { HttpContext = null };
30-
31-
Assert.Throws<ArgumentNullException>(() => { var initializer = new OperationNameTelemetryInitializer(ac, null); });
32-
}
33-
3426
[Fact]
3527
public void InitializeDoesNotThrowIfHttpContextIsUnavailable()
3628
{

0 commit comments

Comments
 (0)