Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use OpenTelemetry tooling for diagnostics setup #638

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions src/Storage/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;

Check warning on line 1 in src/Storage/Program.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Refactor this top-level file to reduce its Cognitive Complexity from 27 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
using System.Collections.Generic;
using System.IO;
using System.Reflection;
Expand Down Expand Up @@ -70,20 +70,8 @@
Configure(builder.Configuration);

logger.LogInformation("// Running...");
app.Run();

Check warning on line 73 in src/Storage/Program.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Await RunAsync instead. (https://rules.sonarsource.com/csharp/RSPEC-6966)

void ConfigureSetupLogging()
{
var logFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter("Altinn.Platform.Register.Program", LogLevel.Debug)
.AddConsole();
});

logger = logFactory.CreateLogger<Program>();
}

void ConfigureApplicationLogging(ILoggingBuilder logging)
{
logging.AddOpenTelemetry(builder =>
Expand Down Expand Up @@ -173,10 +161,12 @@
{
o.ConnectionString = applicationInsightsConnectionString;
}));
services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddAzureMonitorTraceExporter(o =>
services.ConfigureOpenTelemetryTracerProvider(tracing => tracing
.AddAzureMonitorTraceExporter(o =>
{
o.ConnectionString = applicationInsightsConnectionString;
}));
})
.AddProcessor(new DependencyFilterProcessor()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the placement. AddAzureMonitorTelemetryExporters isn't called unless we have an applicationInsightsConnectionString.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forget it. I finally understood the purpose. Looks fine except the order. Documentation emphasis adding the processor before the call to AddAzureMonitorTraceExporter.

}

void ConfigureServices(IServiceCollection services, IConfiguration config)
Expand Down
28 changes: 28 additions & 0 deletions src/Storage/Telemetry/DependencyFilterProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Diagnostics;
using OpenTelemetry;

namespace Altinn.Platform.Storage.Telemetry
{
/// <summary>
/// Filter to avoid dependency telemetry for migrations
/// </summary>
public class DependencyFilterProcessor : BaseProcessor<Activity>
{
/// <summary>
/// Filter to avoid dependency telemetry for migrations
/// </summary>
public override void OnEnd(Activity activity)
{
if (!OKtoSend(activity))
{
activity.ActivityTraceFlags &= ~ActivityTraceFlags.Recorded;
}
}

private bool OKtoSend(Activity activity)

Check warning on line 23 in src/Storage/Telemetry/DependencyFilterProcessor.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Make 'OKtoSend' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)
{
return activity is not null && !activity.OperationName.StartsWith("POST Migration", StringComparison.OrdinalIgnoreCase);
}
}
}
1 change: 1 addition & 0 deletions src/Storage/Telemetry/TelemetryEnrichingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public async Task InvokeAsync(HttpContext context)
if (_disableTelemetryForMigration && context.Request is not null && context.Request.Path.ToUriComponent().StartsWith("/storage/api/v1/migration", StringComparison.OrdinalIgnoreCase))
{
await _next(context);
return;
}

try
Expand Down
Loading