Skip to content

Commit

Permalink
[AzureMonitorDistro] fix livemetrics demo and wire up logs (#43516)
Browse files Browse the repository at this point in the history
* fix livemetrics demo and wire up logs

* change to use internal api
  • Loading branch information
TimothyMothra committed Apr 19, 2024
1 parent 8460d64 commit d1b7212
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageTags>Azure Monitor OpenTelemetry Exporter Distro ApplicationInsights</PackageTags>
<TargetFrameworks>net6.0;$(RequiredTargetFrameworks)</TargetFrameworks>
<IncludeOperationsSharedSource>true</IncludeOperationsSharedSource>
<NoWarn>SA1636</NoWarn>
<NoWarn>SA1636;AZC0011</NoWarn>
<DefineConstants>$(DefineConstants);ASP_NET_CORE_DISTRO;</DefineConstants>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Azure.Monitor.OpenTelemetry.AspNetCore.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100d15ddcb29688295338af4b7686603fe614abd555e09efba8fb88ee09e1f7b1ccaeed2e8f823fa9eef3fdd60217fc012ea67d2479751a0b8c087a4185541b851bd8b16f8d91b840e51b1cb0ba6fe647997e57429265e85ef62d565db50a69ae1647d54d7bd855e4db3d8a91510e5bcbd0edfbbecaa20a7bd9ae74593daa7b11b4")]
[assembly: InternalsVisibleTo("Azure.Monitor.OpenTelemetry.LiveMetrics.Demo, PublicKey=0024000004800000940000000602000000240000525341310004000001000100d15ddcb29688295338af4b7686603fe614abd555e09efba8fb88ee09e1f7b1ccaeed2e8f823fa9eef3fdd60217fc012ea67d2479751a0b8c087a4185541b851bd8b16f8d91b840e51b1cb0ba6fe647997e57429265e85ef62d565db50a69ae1647d54d7bd855e4db3d8a91510e5bcbd0edfbbecaa20a7bd9ae74593daa7b11b4")]

// Moq
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using OpenTelemetry;
using OpenTelemetry.Trace;
using Azure.Monitor.OpenTelemetry.AspNetCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Trace;
using Azure.Monitor.OpenTelemetry.AspNetCore.Internals.LiveMetrics;
using Azure.Monitor.OpenTelemetry.Exporter.Internals.Platform;
using OpenTelemetry;
using OpenTelemetry.Logs;

namespace Azure.Monitor.OpenTelemetry.LiveMetrics.Demo
{
internal class Program
{
private const string ActivitySourceName = "MyCompany.MyProduct.MyLibrary";
private static readonly ActivitySource s_activitySource = new(ActivitySourceName);
private static ILogger? _logger;

private const string ConnectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000";

Expand All @@ -24,10 +30,23 @@ internal class Program

public static async Task Main(string[] args)
{
var azureMonitorOptions = new AzureMonitorOptions
{
ConnectionString = ConnectionString
};

var manager = new Manager(azureMonitorOptions, new DefaultPlatform());

using TracerProvider tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource(ActivitySourceName)
//.AddLiveMetrics(configure => configure.ConnectionString = ConnectionString) // TODO: FIX THIS IN FOLLOW UP PR
.Build();
.AddSource(ActivitySourceName)
.AddProcessor(new LiveMetricsActivityProcessor(manager))
.Build();

var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options => options.AddProcessor(new LiveMetricsLogProcessor(manager)));
});
_logger = loggerFactory.CreateLogger<Program>();

Console.WriteLine("Press any key to stop the loop.");

Expand Down Expand Up @@ -69,17 +88,22 @@ private static async Task GenerateTelemetry()
// Exception
if (GetRandomBool(percent: 40))
{
activity?.SetTag("url.path", "/request/fail");
Console.WriteLine("Request Exception");
try
{
throw new Exception("Test exception");
throw new Exception("Test Request Exception");
}
catch (Exception ex)
{
activity?.SetStatus(ActivityStatusCode.Error);
activity?.RecordException(ex);
}
}
else
{
activity?.SetTag("url.path", "/request/success");
}
}
}

Expand All @@ -95,7 +119,7 @@ private static async Task GenerateTelemetry()
Console.WriteLine("Dependency Exception");
try
{
throw new Exception("Test exception");
throw new Exception("Test Dependency Exception");
}
catch (Exception ex)
{
Expand All @@ -106,6 +130,38 @@ private static async Task GenerateTelemetry()
}
}

// Logs
if (GetRandomBool(percent: 70))
{
Console.WriteLine("Log");

_logger?.Log(
logLevel: LogLevel.Information,
eventId: 0,
exception: null,
message: "Hello {name}.",
args: new object[] { "World" });

// Exception
if (GetRandomBool(percent: 40))
{
Console.WriteLine("Log Exception");
try
{
throw new Exception("Test Log Exception");
}
catch (Exception ex)
{
_logger?.Log(
logLevel: LogLevel.Error,
eventId: 0,
exception: ex,
message: "Hello {name}.",
args: new object[] { "World" });
}
}
}

// Release the allocated memory
memoryChunk = null;
}
Expand Down

0 comments on commit d1b7212

Please sign in to comment.