Skip to content

Commit

Permalink
OpenTelemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Jan 8, 2025
1 parent 381772e commit 6d8a039
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ClassifiedAds.Infrastructure.IdentityProviders.Azure;
using ClassifiedAds.Infrastructure.Logging;
using ClassifiedAds.Infrastructure.MessageBrokers;
using ClassifiedAds.Infrastructure.Monitoring;
using ClassifiedAds.Infrastructure.Notification;
using ClassifiedAds.Infrastructure.Storages;
using Microsoft.Extensions.Options;
Expand All @@ -14,9 +15,7 @@ public class AppSettings

public LoggingOptions Logging { get; set; }

public string AllowedHosts { get; set; }

public string CurrentUrl { get; set; }
public MonitoringOptions Monitoring { get; set; }

public StorageOptions Storage { get; set; }

Expand Down
3 changes: 3 additions & 0 deletions src/Monolith/ClassifiedAds.Background/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using ClassifiedAds.Infrastructure.IdentityProviders.Auth0;
using ClassifiedAds.Infrastructure.IdentityProviders.Azure;
using ClassifiedAds.Infrastructure.Logging;
using ClassifiedAds.Infrastructure.Monitoring;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
Expand Down Expand Up @@ -42,6 +43,8 @@

services.Configure<AppSettings>(configuration);

services.AddMonitoringServices(appSettings.Monitoring);

services.AddScoped<ICurrentUser, CurrentUser>();

services.AddDateTimeProvider();
Expand Down
10 changes: 10 additions & 0 deletions src/Monolith/ClassifiedAds.Background/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,15 @@
"Endpoint": "https://localhost:21052"
}
}
},
"Monitoring": {
"OpenTelemetry": {
"IsEnabled": true,
"ServiceName": "ClassifiedAds.Background",
"Otlp": {
"IsEnabled": true,
"Endpoint": "https://localhost:21052"
}
}
}
}
13 changes: 12 additions & 1 deletion src/Monolith/ClassifiedAds.Background/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@
"MinimumLogEventLevel": "Information"
}
},
"AllowedHosts": "*",
"Monitoring": {
"OpenTelemetry": {
"IsEnabled": false,
"ServiceName": "ClassifiedAds.Background",
"TraceEnabled": true,
"MetricEnabled": true,
"Otlp": {
"IsEnabled": false,
"Endpoint": "http://localhost:4317"
}
}
},
"MessageBroker": {
"Provider": "RabbitMQ",
"RabbitMQ": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@ public static IServiceCollection AddClassifiedAdsOpenTelemetry(this IServiceColl
return services;
}

services.AddOpenTelemetry()
.ConfigureResource(configureResource =>
{
configureResource.AddService(
serviceName: options.ServiceName,
serviceVersion: Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown",
serviceInstanceId: options.ServiceName + "-" + Environment.MachineName);
})
.WithTracing(builder =>
if (!options.TraceEnabled && !options.MetricEnabled)
{
return services;
}

var openTelemetry = services.AddOpenTelemetry()
.ConfigureResource(configureResource =>
{
configureResource.AddService(
serviceName: options.ServiceName,
serviceVersion: Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown",
serviceInstanceId: options.ServiceName + "-" + Environment.MachineName);
});

if (options.TraceEnabled)
{
openTelemetry.WithTracing(builder =>
{
builder
.SetSampler(new AlwaysOnSampler())
Expand Down Expand Up @@ -56,8 +64,12 @@ public static IServiceCollection AddClassifiedAdsOpenTelemetry(this IServiceColl
opts.ConnectionString = options.AzureMonitor.ConnectionString;
});
}
})
.WithMetrics(builder =>
});
}

if (options.MetricEnabled)
{
openTelemetry.WithMetrics(builder =>
{
builder
.AddRuntimeInstrumentation()
Expand All @@ -81,6 +93,7 @@ public static IServiceCollection AddClassifiedAdsOpenTelemetry(this IServiceColl
});
}
});
}

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ public class OpenTelemetryOptions

public string ServiceName { get; set; }

public bool TraceEnabled { get; set; }

public bool MetricEnabled { get; set; }

public ZipkinOptions Zipkin { get; set; }

public OtlpOptions Otlp { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions src/Monolith/ClassifiedAds.WebAPI/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
"OpenTelemetry": {
"IsEnabled": false,
"ServiceName": "ClassifiedAds.WebAPI",
"TraceEnabled": true,
"MetricEnabled": true,
"Otlp": {
"IsEnabled": false,
"Endpoint": "http://localhost:4317"
Expand Down
2 changes: 2 additions & 0 deletions src/Monolith/ClassifiedAds.WebMVC/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
"OpenTelemetry": {
"IsEnabled": false,
"ServiceName": "ClassifiedAds.WebMVC",
"TraceEnabled": true,
"MetricEnabled": true,
"Otlp": {
"IsEnabled": false,
"Endpoint": "http://localhost:4317"
Expand Down

0 comments on commit 6d8a039

Please sign in to comment.