Skip to content

Commit

Permalink
fix: made telememtry extensions build extensive and added refacted he…
Browse files Browse the repository at this point in the history
…aders in custom filters

updated pacage version to 1.0.3
  • Loading branch information
Pratik Bhattacharya committed Apr 6, 2021
1 parent ef4828e commit 290f030
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 44 deletions.
2 changes: 1 addition & 1 deletion sample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddEnterpriseLogger(Configuration, new CustomStaticInitializer());
services.AddEnterpriseTelemetry(Configuration, new CustomStaticInitializer());
services.AddSingleton<IGlobalExceptionHandler, CustomExceptionHandler>();
services.AddMvc(options =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageId>AppInsights.EnterpriseTelemetry.AspNetCore.Extension</PackageId>
<Title>AppInsights.EnterpriseTelemetry.AspNetCore.Extension</Title>
<Version>1.0.2</Version>
<Version>1.0.3</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>
Expand Down
4 changes: 2 additions & 2 deletions src/EnterpriseTelemetryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private static ITelemetryExtensionsBuilder GetBuilder(IConfiguration config, par
{
return extensionBuilder;
}
extensionBuilder = TelemetryExtensionsBuilder.Create(config, customTelemetryInitializers);
extensionBuilder = new TelemetryExtensionsBuilder(config, customTelemetryInitializers);
return extensionBuilder;
}
}
Expand All @@ -52,7 +52,7 @@ public static ILogger CreateLogger(IConfiguration config, params ITelemetryIniti
/// <param name="services"></param>
/// <param name="config" cref="IConfiguration">Configuration</param>
/// <param name="customTelemetryInitializer" cref="ITelemetryInitializer[]">App specific additional telemetry initializers</param>
public static void AddEnterpriseLogger(this IServiceCollection services, IConfiguration config, params ITelemetryInitializer[] customTelemetryInitializers)
public static void AddEnterpriseTelemetry(this IServiceCollection services, IConfiguration config, params ITelemetryInitializer[] customTelemetryInitializers)
{
ITelemetryExtensionsBuilder builder = GetBuilder(config, customTelemetryInitializers);
builder.AddEnterpriseTelemetry(services);
Expand Down
33 changes: 20 additions & 13 deletions src/Filters/RequestResponseLoggerFilterAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,32 @@ public sealed class RequestResponseLoggerFilterAttribute : ActionFilterAttribute
private readonly string _correlationIdHeaderKey;
private readonly string _transactionIdHeaderKey;
private readonly string _endToEndHeaderKey;
private readonly List<string> _redactedHeaders;

public RequestResponseLoggerFilterAttribute(IConfiguration config, ILogger logger)
:this(logger,
: this(logger,
config.GetValue<string>("Logging:LogLevel:Default") == "Trace" || config.GetValue<string>("Logging:LogLevel:Default") == "Debug",
config.GetValue<bool>("Logging:EnableHttpContextBodyLogging"),
config.GetValue<string>("Application:CorrelationIdHeaderKey"),
config.GetValue<string>("Application:TransactionIdHeaderKey"),
config.GetValue<string>("Application:EndToEndTrackingHeaderKey"))
{
}
config.GetValue<string>("Application:CorrelationIdHeaderKey") ?? config.GetValue<string>("ItTelemetryExtensions:CorrelationKey") ?? TelemetryConstant.HEADER_DEFAULT_CORRELATION_KEY,
config.GetValue<string>("Application:TransactionIdHeaderKey") ?? config.GetValue<string>("ItTelemetryExtensions:TransactionKey") ?? TelemetryConstant.HEADER_DEFAULT_TRANSACTION_KEY,
config.GetValue<string>("Application:EndToEndTrackingHeaderKey") ?? config.GetValue<string>("ItTelemetryExtensions:EndToEndKey") ?? TelemetryConstant.HEADER_DEFAULT_E2E_KEY,
config.GetValue<string>("Logging:RedactedHeaders"))
{ }

private RequestResponseLoggerFilterAttribute(ILogger logger, bool isVerboseLoggingEnabled, bool isHttpContextBodyLoggingEnabled, string correlationIdHeaderKey, string transactionIdHeaderKey, string e2eTrackingIdHeaderKey)
private RequestResponseLoggerFilterAttribute(ILogger logger, bool isVerboseLoggingEnabled, bool isHttpContextBodyLoggingEnabled, string correlationIdHeaderKey, string transactionIdHeaderKey, string e2eTrackingIdHeaderKey, string redactedHeaders)
{
_logger = logger;
_isVerboseLoggingEnabled = isVerboseLoggingEnabled;
_isHttpContextBodyLoggingEnabled = isHttpContextBodyLoggingEnabled;
_correlationIdHeaderKey = correlationIdHeaderKey;
_transactionIdHeaderKey = transactionIdHeaderKey;
_endToEndHeaderKey = e2eTrackingIdHeaderKey;
_redactedHeaders = new List<string>() { "Authorization" };
if (!string.IsNullOrWhiteSpace(redactedHeaders))
{
string[] splitRedactedHeaders = redactedHeaders.Split(',');
_redactedHeaders.AddRange(splitRedactedHeaders);
}
}

public override void OnActionExecuting(ActionExecutingContext context)
Expand Down Expand Up @@ -125,16 +132,16 @@ private Dictionary<string, string> GetLogPropertiesFromRequest(HttpRequest reque

foreach (var header in request.Headers)
{
if (header.Key == "Authorization")
if (_redactedHeaders != null && _redactedHeaders.Contains(header.Key))
logProperties.AddOrUpdate($"Request:Header:{header.Key}", TelemetryConstant.REDACTED);
else
logProperties.AddOrUpdate($"Request:Header:{header.Key}", string.Join(",", header.Value));
}
logProperties.AddOrUpdate("Request:Method", request.Method);
logProperties.AddOrUpdate("Request:Protocol", request.Protocol);
logProperties.AddOrUpdate("Request:Scheme", request.Scheme);
logProperties.AddOrUpdate("Request:Host", request.Host.Value);
logProperties.AddOrUpdate("Request:Path", request.Path.Value);
logProperties.AddOrUpdate("Request:Method", request.Method);
logProperties.AddOrUpdate("Request:Protocol", request.Protocol);
logProperties.AddOrUpdate("Request:Scheme", request.Scheme);
logProperties.AddOrUpdate("Request:Host", request.Host.Value);
logProperties.AddOrUpdate("Request:Path", request.Path.Value);
logProperties.AddOrUpdate("Request:QueryString", request.QueryString.HasValue ? request.QueryString.Value : string.Empty);

if (request.Method != HttpMethod.Get.ToString() && _isHttpContextBodyLoggingEnabled)
Expand Down
23 changes: 16 additions & 7 deletions src/Filters/ResponseLoggerFilterAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,39 @@

#pragma warning disable CA1031 // Do not catch general exception types
namespace AppInsights.EnterpriseTelemetry.Web.Extension.Filters
{
{
public sealed class ResponseLoggerFilterAttribute : ActionFilterAttribute
{
private readonly ILogger _logger;
private readonly bool _isHttpContextBodyLoggingEnabled;
private readonly string _correlationIdHeaderKey;
private readonly string _transactionIdHeaderKey;
private readonly string _endToEndHeaderKey;
private readonly List<string> _redactedHeaders;

public ResponseLoggerFilterAttribute(ILogger logger, IConfiguration config)
: this(
logger,
config.GetValue<bool>("Logging:ResponseBodyTrackingEnabled"),
config.GetValue<string>("Application:CorrelationIdHeaderKey") ?? config.GetValue<string>("ItTelemetryExtensions:CorrelationKey") ?? TelemetryConstant.HEADER_DEFAULT_CORRELATION_KEY,
config.GetValue<string>("Application:TransactionIdHeaderKey") ?? config.GetValue<string>("ItTelemetryExtensions:TransactionKey") ?? TelemetryConstant.HEADER_DEFAULT_TRANSACTION_KEY,
config.GetValue<string>("Application:EndToEndTrackingHeaderKey") ?? config.GetValue<string>("ItTelemetryExtensions:EndToEndKey") ?? TelemetryConstant.HEADER_DEFAULT_E2E_KEY
)
config.GetValue<string>("Application:EndToEndTrackingHeaderKey") ?? config.GetValue<string>("ItTelemetryExtensions:EndToEndKey") ?? TelemetryConstant.HEADER_DEFAULT_E2E_KEY,
config.GetValue<string>("Logging:RedactedHeaders"))
{ }

private ResponseLoggerFilterAttribute(ILogger logger, bool isHttpContextBodyLoggingEnabled, string correlationIdHeaderKey, string transactionIdHeaderKey, string e2eTrackingIdHeaderKey)
private ResponseLoggerFilterAttribute(ILogger logger, bool isHttpContextBodyLoggingEnabled, string correlationIdHeaderKey, string transactionIdHeaderKey, string e2eTrackingIdHeaderKey, string redactedHeaders)
{
_logger = logger;
_isHttpContextBodyLoggingEnabled = isHttpContextBodyLoggingEnabled;
_correlationIdHeaderKey = correlationIdHeaderKey;
_transactionIdHeaderKey = transactionIdHeaderKey;
_endToEndHeaderKey = e2eTrackingIdHeaderKey;
_redactedHeaders = new List<string>() { "Authorization" };
if (!string.IsNullOrWhiteSpace(redactedHeaders))
{
string[] splitRedactedHeaders = redactedHeaders.Split(',');
_redactedHeaders.AddRange(splitRedactedHeaders);
}
}

public override void OnActionExecuted(ActionExecutedContext context)
Expand Down Expand Up @@ -90,7 +97,7 @@ private Dictionary<string, string> GetLogPropertiesFromRequest(HttpRequest reque

foreach (var header in request.Headers)
{
if (header.Key == "Authorization")
if (_redactedHeaders != null && _redactedHeaders.Contains(header.Key))
logProperties.AddOrUpdate($"Request:Header:{header.Key}", TelemetryConstant.REDACTED);
else
logProperties.AddOrUpdate($"Request:Header:{header.Key}", string.Join(",", header.Value));
Expand Down Expand Up @@ -124,8 +131,10 @@ private Dictionary<string, string> GetLogPropertiesFromResponse(HttpResponse res

foreach (var header in response.Headers)
{
logProperties.AddOrUpdate($"Response:Header:{header.Key}", string.Join(",", header.Value));
Console.WriteLine($"Response:Header:{header.Key} - " + string.Join(",", header.Value));
if (_redactedHeaders != null && _redactedHeaders.Contains(header.Key))
logProperties.AddOrUpdate($"Response:Header:{header.Key}", TelemetryConstant.REDACTED);
else
logProperties.AddOrUpdate($"Response:Header:{header.Key}", string.Join(",", header.Value));
}
logProperties.AddOrUpdate("Response:StatusCode", response.StatusCode.ToString());
return logProperties;
Expand Down
28 changes: 9 additions & 19 deletions src/TelemetryExtensionsBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -16,34 +17,23 @@ public class TelemetryExtensionsBuilder : ITelemetryExtensionsBuilder
protected readonly IConfiguration _configuration;
protected readonly IAppInsightsConfigurationResolver _appInsightsConfigurationResolver;
protected readonly IAppMetadataConfigurationResolver _appMetadataConfigurationResolver;
protected readonly ITelemetryInitializer[] _telemetryInitializers;
protected readonly IList<ITelemetryInitializer> _telemetryInitializers;

private ILogger _logger;
private static TelemetryExtensionsBuilder _instance = null;
private static readonly object _lock = new object();
protected ILogger _logger;
protected static readonly object _lock = new object();

private TelemetryExtensionsBuilder(IConfiguration configuration, params ITelemetryInitializer[] telemetryInitializers)
public TelemetryExtensionsBuilder() { }

public TelemetryExtensionsBuilder(IConfiguration configuration, params ITelemetryInitializer[] telemetryInitializers)
{
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
_appInsightsConfigurationResolver = new AppInsightsConfigurationResolver(configuration, telemetryInitializers);
_appMetadataConfigurationResolver = new AppMetadataConfigurationResolver(configuration);
_telemetryInitializers = telemetryInitializers;
_telemetryInitializers = telemetryInitializers ?? Array.Empty<ITelemetryInitializer>();
CreateLogger();
}

public static TelemetryExtensionsBuilder Create(IConfiguration configuration, params ITelemetryInitializer[] telemetryInitializers)
{
lock (_lock)
{
if (_instance == null)
{
_instance = new TelemetryExtensionsBuilder(configuration, telemetryInitializers);
}
return _instance;
}
}

public ILogger CreateLogger()
public virtual ILogger CreateLogger()
{
lock (_lock)
{
Expand Down

0 comments on commit 290f030

Please sign in to comment.