Skip to content

Commit

Permalink
Merge pull request #61 from wemogy/feature/improve-manual-setup
Browse files Browse the repository at this point in the history
Improved Manual setup
  • Loading branch information
SebastianKuesters authored Jan 12, 2024
2 parents 96bccbc + 7790721 commit a8845d5
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/Wemogy.AspNet/Monitoring/MonitoringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@ public static IServiceCollection AddDefaultMonitoring(
throw new ArgumentException("MonitoringEnvironment is not configured.");
}

return services.AddDefaultMonitoring(options.MonitoringEnvironment);
}

public static IServiceCollection AddDefaultMonitoring(
this IServiceCollection services,
MonitoringEnvironment monitoringEnvironment)
{
// Metrics
services.AddOpenTelemetry().WithMetrics(builder =>
{
foreach (var meterName in options.MonitoringEnvironment.MeterNames)
foreach (var meterName in monitoringEnvironment.MeterNames)
{
builder.AddMeter(meterName);
}
Expand All @@ -33,7 +40,7 @@ public static IServiceCollection AddDefaultMonitoring(
builder.AddHttpClientInstrumentation();
builder.AddAspNetCoreInstrumentation();

if (options.MonitoringEnvironment.UsePrometheus)
if (monitoringEnvironment.UsePrometheus)
{
builder.AddPrometheusExporter();
}
Expand All @@ -45,40 +52,40 @@ public static IServiceCollection AddDefaultMonitoring(
builder.ConfigureResource((resource) =>
{
resource.AddService(
serviceName: options.MonitoringEnvironment.ServiceName,
serviceNamespace: options.MonitoringEnvironment.ServiceNamespace,
serviceInstanceId: options.MonitoringEnvironment.ServiceInstanceId,
serviceVersion: options.MonitoringEnvironment.ServiceVersion);
serviceName: monitoringEnvironment.ServiceName,
serviceNamespace: monitoringEnvironment.ServiceNamespace,
serviceInstanceId: monitoringEnvironment.ServiceInstanceId,
serviceVersion: monitoringEnvironment.ServiceVersion);
});

builder.AddAspNetCoreInstrumentation();
builder.AddEntityFrameworkCoreInstrumentation();

foreach (var activitySourceName in options.MonitoringEnvironment.ActivitySourceNames)
foreach (var activitySourceName in monitoringEnvironment.ActivitySourceNames)
{
builder.AddSource(activitySourceName);
}

if (options.MonitoringEnvironment.UseOtlpExporter)
if (monitoringEnvironment.UseOtlpExporter)
{
builder.AddOtlpExporter(oltpOptions =>
{
oltpOptions.Endpoint = options.MonitoringEnvironment.OtlpExportEndpoint;
oltpOptions.Endpoint = monitoringEnvironment.OtlpExportEndpoint;
});
}
});

// Azure
if (options.MonitoringEnvironment.UseApplicationInsights)
if (monitoringEnvironment.UseApplicationInsights)
{
services.AddOpenTelemetry().UseAzureMonitor(azureMonitorOptions =>
{
azureMonitorOptions.ConnectionString = options.MonitoringEnvironment.ApplicationInsightsConnectionString;
azureMonitorOptions.SamplingRatio = options.MonitoringEnvironment.ApplicationInsightsSamplingRatio;
azureMonitorOptions.ConnectionString = monitoringEnvironment.ApplicationInsightsConnectionString;
azureMonitorOptions.SamplingRatio = monitoringEnvironment.ApplicationInsightsSamplingRatio;
});
}

services.AddSingleton(options.MonitoringEnvironment);
services.AddSingleton(monitoringEnvironment);
return services;
}

Expand All @@ -91,7 +98,14 @@ public static void UseDefaultMonitoring(
throw new ArgumentException("OpenApiEnvironment is not configured.");
}

if (options.MonitoringEnvironment.UsePrometheus)
applicationBuilder.UseDefaultMonitoring(options.MonitoringEnvironment);
}

public static void UseDefaultMonitoring(
this IApplicationBuilder applicationBuilder,
MonitoringEnvironment monitoringEnvironment)
{
if (monitoringEnvironment.UsePrometheus)
{
applicationBuilder.UseOpenTelemetryPrometheusScrapingEndpoint();
}
Expand Down

0 comments on commit a8845d5

Please sign in to comment.