Skip to content

Commit

Permalink
(#244) OpenTelemetry with Azure
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Jul 17, 2024
1 parent be0247c commit 65e23dc
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.10.0" />
<PackageReference Include="Azure.Messaging.EventHubs.Processor" Version="5.10.0" />
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.17.1" />
<PackageReference Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.2.0" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.19.1" />
<PackageReference Include="Azure.Storage.Queues" Version="12.17.1" />
<PackageReference Include="Castle.Core" Version="5.1.1" />
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Hosting;
using Azure.Monitor.OpenTelemetry.Exporter;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
Expand All @@ -10,6 +11,7 @@
using Serilog.Exceptions;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -54,6 +56,7 @@ private static void UseClassifiedAdsLogger(this IWebHostEnvironment env, Logging
return false;
})
.WriteTo.File(Path.Combine(logsPath, "log.txt"),
formatProvider: CultureInfo.InvariantCulture,
fileSizeLimitBytes: 10 * 1024 * 1024,
rollOnFileSizeLimit: true,
shared: true,
Expand All @@ -77,12 +80,12 @@ private static LoggingOptions SetDefault(LoggingOptions options)
options.LogLevel["Default"] = "Warning";
}

options.File ??= new FileOptions
options.File ??= new LoggingOptions.FileOptions
{
MinimumLogEventLevel = Serilog.Events.LogEventLevel.Warning,
};

options.EventLog ??= new EventLogOptions
options.EventLog ??= new LoggingOptions.EventLogOptions
{
IsEnabled = false,
};
Expand Down Expand Up @@ -143,11 +146,23 @@ public static IWebHostBuilder UseClassifiedAdsLogger(this IWebHostBuilder builde

logging.AddOpenTelemetry(configure =>
{
configure.SetResourceBuilder(resourceBuilder)
.AddOtlpExporter(otlpOptions =>
configure.SetResourceBuilder(resourceBuilder);

if (options?.OpenTelemetry?.Otlp?.IsEnabled ?? false)
{
configure.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri(options.OpenTelemetry.Otlp.Endpoint);
});
}

if (options?.OpenTelemetry?.AzureMonitor?.IsEnabled ?? false)
{
otlpOptions.Endpoint = new Uri(options.OpenTelemetry.Otlp.Endpoint);
});
configure.AddAzureMonitorLogExporter(opts =>
{
opts.ConnectionString = options.OpenTelemetry.AzureMonitor.ConnectionString;
});
}
});
}

Expand Down Expand Up @@ -197,11 +212,23 @@ public static IHostBuilder UseClassifiedAdsLogger(this IHostBuilder builder, Fun

logging.AddOpenTelemetry(configure =>
{
configure.SetResourceBuilder(resourceBuilder)
.AddOtlpExporter(otlpOptions =>
configure.SetResourceBuilder(resourceBuilder);

if (options?.OpenTelemetry?.Otlp?.IsEnabled ?? false)
{
configure.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri(options.OpenTelemetry.Otlp.Endpoint);
});
}

if (options?.OpenTelemetry?.AzureMonitor?.IsEnabled ?? false)
{
otlpOptions.Endpoint = new Uri(options.OpenTelemetry.Otlp.Endpoint);
});
configure.AddAzureMonitorLogExporter(opts =>
{
opts.ConnectionString = options.OpenTelemetry.AzureMonitor.ConnectionString;
});
}
});
}

Expand Down Expand Up @@ -246,6 +273,7 @@ private static void UseClassifiedAdsLogger(this IHostEnvironment env, LoggingOpt
return false;
})
.WriteTo.File(Path.Combine(logsPath, "log.txt"),
formatProvider: CultureInfo.InvariantCulture,
fileSizeLimitBytes: 10 * 1024 * 1024,
rollOnFileSizeLimit: true,
shared: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Serilog.Events;
using System.Collections.Generic;

namespace ClassifiedAds.Infrastructure.Logging;

Expand All @@ -13,4 +14,50 @@ public class LoggingOptions
public ApplicationInsightsOptions ApplicationInsights { get; set; }

public OpenTelemetryOptions OpenTelemetry { get; set; }

public class FileOptions
{
public LogEventLevel MinimumLogEventLevel { get; set; }
}

public class EventLogOptions
{
public bool IsEnabled { get; set; }

public string LogName { get; set; }

public string SourceName { get; set; }
}

public class ApplicationInsightsOptions
{
public bool IsEnabled { get; set; }

public string InstrumentationKey { get; set; }
}

public class OpenTelemetryOptions
{
public bool IsEnabled { get; set; }

public string ServiceName { get; set; }

public OtlpOptions Otlp { get; set; }

public AzureMonitorOptions AzureMonitor { get; set; }

public class OtlpOptions
{
public bool IsEnabled { get; set; }

public string Endpoint { get; set; }
}

public class AzureMonitorOptions
{
public bool IsEnabled { get; set; }

public string ConnectionString { get; set; }
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Azure.Monitor.OpenTelemetry.Exporter;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
Expand Down Expand Up @@ -47,6 +48,14 @@ public static IServiceCollection AddClassifiedAdsOpenTelemetry(this IServiceColl
otlpOptions.Endpoint = new Uri(options.Otlp.Endpoint);
});
}

if (options?.AzureMonitor?.IsEnabled ?? false)
{
builder.AddAzureMonitorTraceExporter(opts =>
{
opts.ConnectionString = options.AzureMonitor.ConnectionString;
});
}
})
.WithMetrics(builder =>
{
Expand All @@ -63,6 +72,14 @@ public static IServiceCollection AddClassifiedAdsOpenTelemetry(this IServiceColl
otlpOptions.Endpoint = new Uri(options.Otlp.Endpoint);
});
}

if (options?.AzureMonitor?.IsEnabled ?? false)
{
builder.AddAzureMonitorMetricExporter(opts =>
{
opts.ConnectionString = options.AzureMonitor.ConnectionString;
});
}
});

return services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class OpenTelemetryOptions
public ZipkinOptions Zipkin { get; set; }

public OtlpOptions Otlp { get; set; }

public AzureMonitorOptions AzureMonitor { get; set; }
}

public class ZipkinOptions
Expand All @@ -24,3 +26,10 @@ public class OtlpOptions

public string Endpoint { get; set; }
}

public class AzureMonitorOptions
{
public bool IsEnabled { get; set; }

public string ConnectionString { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"IsEnabled": true,
"ServiceName": "ClassifiedAds.WebAPI",
"Otlp": {
"IsEnabled": true,
"Endpoint": "https://localhost:21052"
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/Monolith/ClassifiedAds.WebAPI/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@
"IsEnabled": false,
"ServiceName": "ClassifiedAds.WebAPI",
"Otlp": {
"IsEnabled": false,
"Endpoint": "http://localhost:4317"
},
"AzureMonitor": {
"IsEnabled": false,
"ConnectionString": "xxx"
}
}
},
Expand Down Expand Up @@ -83,6 +88,10 @@
"Otlp": {
"IsEnabled": false,
"Endpoint": "http://localhost:4317"
},
"AzureMonitor": {
"IsEnabled": false,
"ConnectionString": "xxx"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"IsEnabled": true,
"ServiceName": "ClassifiedAds.WebMVC",
"Otlp": {
"IsEnabled": true,
"Endpoint": "https://localhost:21052"
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/Monolith/ClassifiedAds.WebMVC/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@
"IsEnabled": false,
"ServiceName": "ClassifiedAds.WebMVC",
"Otlp": {
"IsEnabled": false,
"Endpoint": "http://localhost:4317"
},
"AzureMonitor": {
"IsEnabled": false,
"ConnectionString": "xxx"
}
}
},
Expand Down Expand Up @@ -81,6 +86,10 @@
"Otlp": {
"IsEnabled": false,
"Endpoint": "http://localhost:4317"
},
"AzureMonitor": {
"IsEnabled": false,
"ConnectionString": "xxx"
}
}
},
Expand Down

0 comments on commit 65e23dc

Please sign in to comment.