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 6d8a039 commit 0be6d44
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ClassifiedAds.Infrastructure.Logging;
using ClassifiedAds.Infrastructure.MessageBrokers;
using ClassifiedAds.Infrastructure.Monitoring;
using Microsoft.Extensions.Options;

namespace ClassifiedAds.Background.ConfigurationOptions;
Expand All @@ -8,9 +9,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 MessageBrokerOptions MessageBroker { get; set; }

Expand Down
3 changes: 3 additions & 0 deletions src/ModularMonolith/ClassifiedAds.Background/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ClassifiedAds.Contracts.Identity.Services;
using ClassifiedAds.Domain.Infrastructure.MessageBrokers;
using ClassifiedAds.Infrastructure.Logging;
using ClassifiedAds.Infrastructure.Monitoring;
using ClassifiedAds.Modules.Identity.Repositories;
using ClassifiedAds.Modules.Storage.DTOs;
using ClassifiedAds.Modules.Storage.MessageBusConsumers;
Expand Down Expand Up @@ -36,6 +37,8 @@

services.Configure<AppSettings>(configuration);

services.AddMonitoringServices(appSettings.Monitoring);

services.AddScoped<ICurrentUser, CurrentUser>();

services.AddDateTimeProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
},
"OpenTelemetry": {
"IsEnabled": true,
"ServiceName": "ClassifiedAds.Background",
"Otlp": {
"IsEnabled": true,
"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/ModularMonolith/ClassifiedAds.Background/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@
}
}
},
"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/ModularMonolith/ClassifiedAds.WebAPI/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
"OpenTelemetry": {
"IsEnabled": false,
"ServiceName": "ClassifiedAds.WebAPI",
"TraceEnabled": true,
"MetricEnabled": true,
"Otlp": {
"IsEnabled": false,
"Endpoint": "http://localhost:4317"
Expand Down

0 comments on commit 0be6d44

Please sign in to comment.