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

Add opentelemetry #67

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Add opentelemetry #67

wants to merge 2 commits into from

Conversation

lizard-boy
Copy link

@lizard-boy lizard-boy commented Oct 19, 2024

Type of change

- [ ] Bug fix
- [x] New feature development
- [ ] Tech debt (refactoring, code cleanup, dependency upgrades, etc)
- [ ] Build/deploy pipeline (DevOps)
- [ ] Other

Objective

Adds opentelemetry-based tracing to server projects. Note: tracing is used exclusively as a system profiler to aid in performance optimizations, not intended to trace usage at all. Please let me know if you find some leaked PII in here!

Code changes

  • launch.json/tasks.json/launchSettings.json Include Events project in builds, correct the port for events processor self host to be consistent with visual studio build settings and pattern for self-host version of other project (it was previously using the Events port by mistake).
  • docker-compose: include jaeger as a tool to analyze tracing
  • api/startup: Add tracing attribute to controller action execution. Turn on middleware telemetry to monitor execution of middlewares
  • tracingAttribute: Logs tracing even at begin and end of action execution as well as wrapping result processing
  • entityFramework.csproj: include EF-specific opentelemetry package
  • DiagnosticsConfig: helper class to configure activity and service information in shared service configuration helpers.
  • MiddlewareAnalysisDiagnosticsHelper: The idea behind this is to wrap all middleware with trace activities to track their execution time -- much like the controller action wrapping done by the TracingAttribute. It works OK, but the library it relies on, Microsoft.Extensions.MiddlewareAnalysis, is poorly documented and seems to be long-forgotten. Sometimes it seems to randomly miss middlewares, but It certainly catches more than if we didn't have it...
  • diagnosticsHelpers: static class to add configuration helpers for diagnostics purposes
  • serviceCollectionExtension: Add openetelemetry and diagnostic event emissions listened to by MiddlewareAnalysisDiagnosticsHelper

Before you submit

  • Please check for formatting errors (dotnet format --verify-no-changes) (required)
  • If making database changes - make sure you also update Entity Framework queries and/or migrations
  • Please add unit tests where it makes sense to do so (encouraged but not required)
  • If this change requires a documentation update - notify the documentation team
  • If this change has particular deployment requirements - notify the DevOps team

Greptile Summary

This pull request introduces OpenTelemetry-based tracing to the server projects, focusing on system profiling for performance optimization without tracking user-specific data.

  • Added Jaeger service in docker-compose.yml for distributed tracing analysis
  • Implemented TracingAttribute in src/Core/Utilities/TracingAttribute.cs to wrap controller actions and results with tracing activities
  • Created DiagnosticsConfig in src/SharedWeb/Health/DiagnosticsConfig.cs to configure ActivitySource based on project name
  • Introduced MiddlewareAnalysisDiagnosticAdapter in src/SharedWeb/Health/MiddlewareAnalysisDiagnosticAdapter.cs for middleware execution tracing
  • Modified src/Api/Startup.cs to integrate tracing and middleware telemetry in the application pipeline

MGibson1 added 2 commits June 26, 2024 14:53
Use Jaeger for tracing
Add diagnostics for aspdotnet core, middleware, sqlserver, ef core,
http clients, and action/result filters for controller actions
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

11 file(s) reviewed, 14 comment(s)
Edit PR Review Bot Settings | Greptile

@@ -199,6 +199,7 @@ public void ConfigureServices(IServiceCollection services)
{
config.Conventions.Add(new ApiExplorerGroupConvention());
config.Conventions.Add(new PublicApiControllersModelConvention());
config.Filters.Add(new TracingAttribute()); // Add tracing to controller actions and results
Copy link

Choose a reason for hiding this comment

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

style: Adding tracing to all controller actions may introduce performance overhead. Consider selective tracing for critical paths only.

{
IdentityModelEventSource.ShowPII = true;
app.UseSerilog(env, appLifetime, globalSettings);

// Middleware telemetry
DiagnosticsHelpers.AddMiddlewareDiagnostics(serviceProvider);
Copy link

Choose a reason for hiding this comment

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

logic: Middleware diagnostics may miss some middleware. Ensure comprehensive coverage or implement fallback monitoring.

@@ -217,11 +218,15 @@ public void Configure(
IWebHostEnvironment env,
IHostApplicationLifetime appLifetime,
GlobalSettings globalSettings,
ILogger<Startup> logger)
ILogger<Startup> logger,
IServiceProvider serviceProvider)
{
IdentityModelEventSource.ShowPII = true;
Copy link

Choose a reason for hiding this comment

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

logic: Showing PII in logs could lead to security vulnerabilities. Ensure this is disabled in production environments.

Comment on lines +48 to +50
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.5.1-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.5.1-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.5.1-beta.1" />
Copy link

Choose a reason for hiding this comment

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

style: These packages are in beta. Consider using stable versions for production, or document the use of beta packages.

Comment on lines +36 to +37
_actionActivity.AddTag("exception", context.Exception.ToString());
_actionActivity.AddTag("exceptionStackTrace", context.Exception.StackTrace);
Copy link

Choose a reason for hiding this comment

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

style: Be cautious about logging full exception details, as it may expose sensitive information. Consider logging only exception type and message

Comment on lines +28 to +31
if (_activitySource.StartActivity(name, ActivityKind.Server) is Activity activity)
{
_activities[name] = activity;
}
Copy link

Choose a reason for hiding this comment

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

logic: This could potentially lead to a memory leak if activities are not properly removed. Consider implementing a cleanup mechanism

Comment on lines +41 to +42
_activities[name].AddTag("exception", exception.ToString());
_activities[name].AddTag("exceptionStackTrace", exception.StackTrace);
Copy link

Choose a reason for hiding this comment

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

style: Logging full exception details might expose sensitive information. Consider logging only necessary information

Comment on lines +37 to +40
if (name == "Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware" || !_activities.ContainsKey(name))
{
return;
}
Copy link

Choose a reason for hiding this comment

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

logic: The activity might have already been stopped if an exception occurred. Add a check to ensure the activity is still running before stopping it

Comment on lines +50 to +55
if (name == "Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware" || !_activities.ContainsKey(name))
{
return;
}
_activities[name].Stop();
}
Copy link

Choose a reason for hiding this comment

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

style: Consider removing the activity from the dictionary after stopping it to prevent potential memory leaks

Comment on lines +248 to +257
services.AddOpenTelemetry().WithTracing(tracerProviderBuilder =>
tracerProviderBuilder
.AddSource(diagnosticsConfig.ActivitySource.Name)
.ConfigureResource(resource => resource
.AddService(diagnosticsConfig.ServiceName))
.AddAspNetCoreInstrumentation()
.AddSqlClientInstrumentation()
.AddEntityFrameworkCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddOtlpExporter());
Copy link

Choose a reason for hiding this comment

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

style: Consider adding sampling to reduce the volume of telemetry data in production

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants