forked from microsoft/ApplicationInsights-aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Commit for microsoft#29 - Read version information from project.json
- Loading branch information
Showing
5 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
....ApplicationInsights.AspNet/TelemetryInitializers/ComponentVersionTelemetryInitializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
namespace Microsoft.ApplicationInsights.AspNet.TelemetryInitializers | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Channel; | ||
using DataContracts; | ||
using Microsoft.AspNet.Http; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
/// <summary> | ||
/// A telemetry initializer that populates telemetry.Context.Component.Version to the value read from project.json | ||
/// </summary> | ||
public class ComponentVersionTelemetryInitializer : TelemetryInitializerBase | ||
{ | ||
private const string versionConfigurationOption = "dependencies:Microsoft.ApplicationInsights.AspNet"; | ||
|
||
public ComponentVersionTelemetryInitializer(IHttpContextAccessor httpContextAccessor):base(httpContextAccessor) | ||
{ | ||
//No need to initialize anything | ||
} | ||
|
||
protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry) | ||
{ | ||
if (string.IsNullOrEmpty(telemetry.Context.Component.Version)) | ||
{ | ||
var config = new ConfigurationBuilder().AddJsonFile("project.json").Build(); | ||
|
||
if (config == null) { | ||
telemetry.Context.Component.Version = null; | ||
} else { | ||
telemetry.Context.Component.Version = config[versionConfigurationOption].ToString(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...nInsights.AspNet.Tests/TelemetryInitializers/ComponentVersionTelemetryInitializerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
| ||
namespace Microsoft.ApplicationInsights.AspNet.Tests.TelemetryInitializers | ||
{ | ||
using System; | ||
using Microsoft.ApplicationInsights.AspNet.TelemetryInitializers; | ||
using Microsoft.ApplicationInsights.AspNet.Tests.Helpers; | ||
using Microsoft.ApplicationInsights.DataContracts; | ||
using Microsoft.AspNet.Hosting; | ||
using Xunit; | ||
using Microsoft.AspNet.Http.Internal; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
public class ComponentVersionTelemetryInitializerTests | ||
{ | ||
[Fact] | ||
public void InitializeThrowIfHttpContextAccessorIsNull() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => { var initializer = new ComponentVersionTelemetryInitializer(null); }); | ||
} | ||
|
||
[Fact] | ||
public void InitializeDoesNotThrowIfHttpContextIsUnavailable() | ||
{ | ||
var ac = new HttpContextAccessor() { HttpContext = null }; | ||
|
||
var initializer = new ComponentVersionTelemetryInitializer(ac); | ||
|
||
initializer.Initialize(new RequestTelemetry()); | ||
} | ||
|
||
[Fact] | ||
public void InitializeDoesNotThrowIfRequestTelemetryIsUnavailable() | ||
{ | ||
var ac = new HttpContextAccessor() { HttpContext = new DefaultHttpContext() }; | ||
|
||
var initializer = new ComponentVersionTelemetryInitializer(ac); | ||
|
||
initializer.Initialize(new RequestTelemetry()); | ||
} | ||
|
||
[Fact] | ||
public void InitializeSetsGetVersionInformationIsNotNull() | ||
{ | ||
var requestTelemetry = new RequestTelemetry(); | ||
var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry); | ||
|
||
var initializer = new ComponentVersionTelemetryInitializer(contextAccessor); | ||
|
||
initializer.Initialize(requestTelemetry); | ||
|
||
Assert.NotNull(requestTelemetry.Context.Component.Version); | ||
} | ||
} | ||
} |