Skip to content

Commit

Permalink
Commit for microsoft#29 - Read version information from project.json
Browse files Browse the repository at this point in the history
  • Loading branch information
gzepeda committed Mar 21, 2016
1 parent f791471 commit ebac263
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static IApplicationBuilder UseApplicationInsightsExceptionTelemetry(this
public static void AddApplicationInsightsTelemetry(this IServiceCollection services, IConfiguration config)
{
services.AddSingleton<ITelemetryInitializer, DomainNameRoleInstanceTelemetryInitializer>();
services.AddSingleton<ITelemetryInitializer, ComponentVersionTelemetryInitializer>();
services.AddSingleton<ITelemetryInitializer, ClientIpHeaderTelemetryInitializer>();
services.AddSingleton<ITelemetryInitializer, OperationIdTelemetryInitializer>();
services.AddSingleton<ITelemetryInitializer, OperationNameTelemetryInitializer>();
Expand Down
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();
}
}
}
}
}
5 changes: 3 additions & 2 deletions src/Microsoft.ApplicationInsights.AspNet/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
"delaySign": true
},
"dependencies": {
"Microsoft.ApplicationInsights": "2.0.0-*",
"Microsoft.ApplicationInsights": "2.0.0-beta4",
"Microsoft.AspNet.Hosting.Abstractions": "1.0.0-rc1-final",
"Microsoft.AspNet.Http.Abstractions": "1.0.0-rc1-final",
"Microsoft.AspNet.Http.Features": "1.0.0-rc1-final",
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
"Microsoft.Extensions.Logging.Abstractions": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration": "1.0.0-rc1-final",
"Microsoft.Extensions.DiagnosticAdapter": "1.0.0-rc1-final"
"Microsoft.Extensions.DiagnosticAdapter": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final"
},

"frameworks": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static class AddApplicationInsightsTelemetry
[Theory]
[InlineData(typeof(ITelemetryInitializer), typeof(DomainNameRoleInstanceTelemetryInitializer), ServiceLifetime.Singleton)]
[InlineData(typeof(ITelemetryInitializer), typeof(ClientIpHeaderTelemetryInitializer), ServiceLifetime.Singleton)]
[InlineData(typeof(ITelemetryInitializer), typeof(ComponentVersionTelemetryInitializer), ServiceLifetime.Singleton)]
[InlineData(typeof(ITelemetryInitializer), typeof(OperationNameTelemetryInitializer), ServiceLifetime.Singleton)]
[InlineData(typeof(ITelemetryInitializer), typeof(OperationIdTelemetryInitializer), ServiceLifetime.Singleton)]
[InlineData(typeof(ITelemetryInitializer), typeof(UserAgentTelemetryInitializer), ServiceLifetime.Singleton)]
Expand Down
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);
}
}
}

0 comments on commit ebac263

Please sign in to comment.