Skip to content

Commit

Permalink
Addressing warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Psichorex committed Jan 20, 2024
1 parent ba9db63 commit f1c4080
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Lombiq.Hosting.Azure.ApplicationInsights.Extensions;
using Lombiq.Hosting.Azure.ApplicationInsights.Extensions;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
Expand All @@ -9,18 +9,12 @@ namespace Lombiq.Hosting.Azure.ApplicationInsights.Services;
/// <summary>
/// Azure Blob Storage 404s are logged in a parent-child relationship, so we need to ignore the parents also.
/// </summary>
public class AzureBlobTelemetryFilter : ITelemetryProcessor
public class AzureBlobTelemetryFilter(ITelemetryProcessor next, IServiceProvider serviceProvider) : ITelemetryProcessor
{
private readonly ITelemetryProcessor _next;
private readonly IServiceProvider _serviceProvider;
private readonly ITelemetryProcessor _next = next;
private readonly IServiceProvider _serviceProvider = serviceProvider;
private string _parentId;

public AzureBlobTelemetryFilter(ITelemetryProcessor next, IServiceProvider serviceProvider)
{
_next = next;
_serviceProvider = serviceProvider;
}

public void Process(ITelemetry item)
{
SetAsIgnoredFailureWhenNeeded(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@

namespace Lombiq.Hosting.Azure.ApplicationInsights.Services;

internal sealed class BackgroundTaskTelemetryEventHandler : IBackgroundTaskEventHandler
internal sealed class BackgroundTaskTelemetryEventHandler(TelemetryClient telemetryClient) : IBackgroundTaskEventHandler
{
private readonly TelemetryClient _telemetryClient;

private IOperationHolder<DependencyTelemetry> _operation;
private Activity _activity;

public BackgroundTaskTelemetryEventHandler(TelemetryClient telemetryClient) => _telemetryClient = telemetryClient;

public Task ExecutingAsync(BackgroundTaskEventContext context, CancellationToken cancellationToken)
{
// Tried with a custom operation too, see c13dfdc47543d8635572be1f2ce1191dba06469b, but it remained
Expand All @@ -25,7 +21,7 @@ public Task ExecutingAsync(BackgroundTaskEventContext context, CancellationToken
// EnableDependencyTrackingTelemetryModule is false. Also, this is the recommended way of tracking background
// tasks, see:
// https://docs.microsoft.com/en-us/azure/azure-monitor/app/custom-operations-tracking#long-running-background-tasks.
var operation = _telemetryClient.StartOperation<DependencyTelemetry>(context.Name);
var operation = telemetryClient.StartOperation<DependencyTelemetry>(context.Name);
operation.Telemetry.Type = "BackgroundTask";

_operation = operation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@

namespace Lombiq.Hosting.Azure.ApplicationInsights.Services;

public class TrackingScriptFactory : ITrackingScriptFactory
public class TrackingScriptFactory(JavaScriptSnippet javaScriptSnippet) : ITrackingScriptFactory
{
private readonly JavaScriptSnippet _javaScriptSnippet;

public TrackingScriptFactory(JavaScriptSnippet javaScriptSnippet) =>
_javaScriptSnippet = javaScriptSnippet;

// The operation ID is NOT available in the injectable TelemetryClient, which will be basically empty. This is
// somehow by design. See e.g.:
// https://stackoverflow.com/questions/39149815/when-can-i-get-an-application-insights-operation-id
public HtmlString CreateJavaScriptTrackingScript() => new(
$@"<script>
{_javaScriptSnippet.ScriptBody}
{javaScriptSnippet.ScriptBody}
appInsights.queue.push(function () {{
appInsights.context.telemetryTrace.traceID = '{System.Diagnostics.Activity.Current.RootId}';
}});
Expand Down
16 changes: 5 additions & 11 deletions Lombiq.Hosting.Azure.ApplicationInsights/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@

namespace Lombiq.Hosting.Azure.ApplicationInsights;

public class Startup : StartupBase
public class Startup(
IOptions<ApplicationInsightsOptions> applicationInsightsOptions,
IOptions<ApplicationInsightsServiceOptions> applicationInsightsServiceOptions) : StartupBase
{
private readonly ApplicationInsightsOptions _applicationInsightsOptions;
private readonly ApplicationInsightsServiceOptions _applicationInsightsServiceOptions;

public Startup(
IOptions<ApplicationInsightsOptions> applicationInsightsOptions,
IOptions<ApplicationInsightsServiceOptions> applicationInsightsServiceOptions)
{
_applicationInsightsOptions = applicationInsightsOptions.Value;
_applicationInsightsServiceOptions = applicationInsightsServiceOptions.Value;
}
private readonly ApplicationInsightsOptions _applicationInsightsOptions = applicationInsightsOptions.Value;
private readonly ApplicationInsightsServiceOptions _applicationInsightsServiceOptions = applicationInsightsServiceOptions.Value;

public override void ConfigureServices(IServiceCollection services)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@

namespace Lombiq.Hosting.Azure.ApplicationInsights.TelemetryInitializers;

internal sealed class ShellNamePopulatingTelemetryInitializer : ITelemetryInitializer
internal sealed class ShellNamePopulatingTelemetryInitializer(IServiceProvider serviceProvider) : ITelemetryInitializer
{
private readonly IServiceProvider _serviceProvider;

public ShellNamePopulatingTelemetryInitializer(IServiceProvider serviceProvider) =>
_serviceProvider = serviceProvider;

public void Initialize(ITelemetry telemetry)
{
if (telemetry is not ISupportProperties supportProperties) return;

var httpContext = _serviceProvider.GetHttpContextSafely();
var httpContext = serviceProvider.GetHttpContextSafely();
if (httpContext != null)
{
var httpRequest = httpContext.Request;

var shellName = _serviceProvider
var shellName = serviceProvider
.GetService<IRunningShellTable>()
?.Match(httpRequest.Host, httpRequest.PathBase + httpRequest.Path)
?.Name;
Expand Down

0 comments on commit f1c4080

Please sign in to comment.