Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AzureMonitorExporter] refactor demo projects #31204

Merged
merged 3 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Monitor.OpenTelemetry
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Monitor.OpenTelemetry.Exporter.Benchmarks", "tests\Azure.Monitor.OpenTelemetry.Exporter.Benchmarks\Azure.Monitor.OpenTelemetry.Exporter.Benchmarks.csproj", "{D57D79BA-6D39-4E9E-970C-A5F73A4425BB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Monitor.OpenTelemetry.Exporter.Tracing.Customization", "tests\Azure.Monitor.OpenTelemetry.Exporter.Tracing.Customization\Azure.Monitor.OpenTelemetry.Exporter.Tracing.Customization.csproj", "{57A53135-3C1B-4106-B873-434B2F606B17}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Monitor.OpenTelemetry.Exporter.E2E.Tests", "tests\E2E.Tests\Azure.Monitor.OpenTelemetry.Exporter.E2E.Tests.csproj", "{5083A0B8-24CE-4AC4-AC23-C9BD87EE2FC8}"
EndProject
Global
Expand Down Expand Up @@ -55,10 +53,6 @@ Global
{D57D79BA-6D39-4E9E-970C-A5F73A4425BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D57D79BA-6D39-4E9E-970C-A5F73A4425BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D57D79BA-6D39-4E9E-970C-A5F73A4425BB}.Release|Any CPU.Build.0 = Release|Any CPU
{57A53135-3C1B-4106-B873-434B2F606B17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{57A53135-3C1B-4106-B873-434B2F606B17}.Debug|Any CPU.Build.0 = Debug|Any CPU
{57A53135-3C1B-4106-B873-434B2F606B17}.Release|Any CPU.ActiveCfg = Release|Any CPU
{57A53135-3C1B-4106-B873-434B2F606B17}.Release|Any CPU.Build.0 = Release|Any CPU
{5083A0B8-24CE-4AC4-AC23-C9BD87EE2FC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5083A0B8-24CE-4AC4-AC23-C9BD87EE2FC8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5083A0B8-24CE-4AC4-AC23-C9BD87EE2FC8}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Microsoft.Extensions.Logging;

namespace Azure.Monitor.OpenTelemetry.Exporter.Demo.Logs
{
internal class LogDemo : IDisposable
{
private readonly ILoggerFactory loggerFactory;

public LogDemo(string connectionString)
{
this.loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.AddAzureMonitorLogExporter(o => o.ConnectionString = connectionString);
});
});
}

/// <remarks>
/// Logs will be ingested as an Application Insights trace.
/// These can be differentiated by their severityLevel.
/// </remarks>
public void GenerateLogs()
{
var logger = this.loggerFactory.CreateLogger<LogDemo>();
logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
logger.LogError("An error occurred.");
}

public void Dispose()
{
this.loggerFactory.Dispose();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Diagnostics.Metrics;
using OpenTelemetry;
using OpenTelemetry.Metrics;

namespace Azure.Monitor.OpenTelemetry.Exporter.Demo.Metrics
{
internal class MetricDemo : IDisposable
{
private const string meterName = "MyCompany.MyProduct.MyLibrary";
private static readonly Meter meter = new(meterName);

private readonly MeterProvider meterProvider;

public MetricDemo(string connectionString)
{
this.meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meterName)
.AddAzureMonitorMetricExporter(o => o.ConnectionString = connectionString)
.Build();
}

/// <remarks>
/// These counters will be aggregated and ingested as Application Insights customMetrics.
/// </remarks>
public void GenerateMetrics()
{
Counter<long> MyFruitCounter = meter.CreateCounter<long>("MyFruitCounter");

MyFruitCounter.Add(1, new("name", "apple"), new("color", "red"));
MyFruitCounter.Add(2, new("name", "lemon"), new("color", "yellow"));
MyFruitCounter.Add(1, new("name", "lemon"), new("color", "yellow"));
MyFruitCounter.Add(2, new("name", "apple"), new("color", "green"));
MyFruitCounter.Add(5, new("name", "apple"), new("color", "red"));
MyFruitCounter.Add(4, new("name", "lemon"), new("color", "yellow"));
}

public void Dispose()
{
this.meterProvider.Dispose();
TimothyMothra marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
@@ -1,88 +1,30 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
using OpenTelemetry.Extensions.AzureMonitor;
using System;
using Azure.Monitor.OpenTelemetry.Exporter.Demo.Logs;
using Azure.Monitor.OpenTelemetry.Exporter.Demo.Metrics;
using Azure.Monitor.OpenTelemetry.Exporter.Demo.Traces;

namespace Azure.Monitor.OpenTelemetry.Exporter.Demo
{
public class Program
{
private const string ConnectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000";
private static readonly ActivitySource source = new ActivitySource("MyCompany.MyProduct.MyLibrary");
private static readonly Meter MyMeter = new("MyCompany.MyProduct.MyLibrary", "1.0");

public static void Main()
{
GenerateTraces();
GenerateLogs();
GenerateMetrics();
}

private static void GenerateTraces()
{
var resourceAttributes = new Dictionary<string, object> { { "service.name", "my-service" }, { "service.namespace", "my-namespace" }, { "service.instance.id", "my-instance" } };
var resourceBuilder = ResourceBuilder.CreateDefault().AddAttributes(resourceAttributes);
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetResourceBuilder(resourceBuilder)
.AddSource("MyCompany.MyProduct.MyLibrary")
.SetSampler(new ApplicationInsightsSampler(1.0F))
.AddAzureMonitorTraceExporter(o =>
{
o.ConnectionString = ConnectionString;
})
.Build();

using (var activity = source.StartActivity("SayHello"))
{
activity?.SetTag("foo", 1);
activity?.SetTag("baz", new int[] { 1, 2, 3 });
activity?.SetStatus(ActivityStatusCode.Ok);

using (var nestedActivity = source.StartActivity("SayHelloAgain"))
{
nestedActivity?.SetTag("bar", "Hello, World!");
nestedActivity?.SetStatus(ActivityStatusCode.Ok);
}
}
}
using var traceDemo = new TraceDemo(ConnectionString);
traceDemo.GenerateTraces();

private static void GenerateLogs()
{
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.AddAzureMonitorLogExporter(o => o.ConnectionString = ConnectionString);
});
});

var logger = loggerFactory.CreateLogger<Program>();
logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
}

private static void GenerateMetrics()
{
Counter<long> MyFruitCounter = MyMeter.CreateCounter<long>("MyFruitCounter");
using var metricDemo = new MetricDemo(ConnectionString);
metricDemo.GenerateMetrics();

using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter("MyCompany.MyProduct.MyLibrary")
.AddAzureMonitorMetricExporter(o => o.ConnectionString = ConnectionString)
.Build();
using var logDemo = new LogDemo(ConnectionString);
logDemo.GenerateLogs();

MyFruitCounter.Add(1, new("name", "apple"), new("color", "red"));
MyFruitCounter.Add(2, new("name", "lemon"), new("color", "yellow"));
MyFruitCounter.Add(1, new("name", "lemon"), new("color", "yellow"));
MyFruitCounter.Add(2, new("name", "apple"), new("color", "green"));
MyFruitCounter.Add(5, new("name", "apple"), new("color", "red"));
MyFruitCounter.Add(4, new("name", "lemon"), new("color", "yellow"));
Console.WriteLine("Press Enter key to exit.");
Console.ReadLine();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Diagnostics;
using OpenTelemetry;

namespace Azure.Monitor.OpenTelemetry.Exporter.Tracing.Customization
{
public class ActivityEnrichingProcessor : BaseProcessor<Activity>
{
public override void OnEnd(Activity activity)
{
// The updated activity will be available to all processors which are called after this processor.
activity.DisplayName = "Enriched-" + activity.DisplayName;
activity.SetTag("CustomDimension1", "Value1");
activity.SetTag("CustomDimension2", "Value2");
activity.SetTag("ActivityKind", activity.Kind);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Diagnostics;
using OpenTelemetry;

namespace Azure.Monitor.OpenTelemetry.Exporter.Tracing.Customization
{
internal class ActivityFilteringProcessor : BaseProcessor<Activity>
{
public override void OnStart(Activity activity)
{
// prevents all exporters from exporting internal activities
if (activity.Kind == ActivityKind.Producer)
{
activity.IsAllDataRequested = false;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using Azure.Monitor.OpenTelemetry.Exporter.Tracing.Customization;
using OpenTelemetry;
using OpenTelemetry.Extensions.AzureMonitor;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

namespace Azure.Monitor.OpenTelemetry.Exporter.Demo.Traces
{
internal class TraceDemo : IDisposable
{
private const string activitySourceName = "MyCompany.MyProduct.MyLibrary";
private static readonly ActivitySource activitySource = new(activitySourceName);

private readonly TracerProvider tracerProvider;

public TraceDemo(string connectionString)
{
var resourceAttributes = new Dictionary<string, object>
{
{ "service.name", "my-service" },
{ "service.namespace", "my-namespace" },
{ "service.instance.id", "my-instance" },
};

var resourceBuilder = ResourceBuilder.CreateDefault().AddAttributes(resourceAttributes);

this.tracerProvider = Sdk.CreateTracerProviderBuilder()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its confusing that the ActivityEnrichingProcessor() and ActivityFilteringProcessor() are part of this project and not the SDK. Given that there are links to just this file as a sample, I got lost trying to find these - especially as github search is somewhat broken these days,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HI @samsp-msft, point taken. I'll bring this up with the team about making these more discoverable. Thanks!

.SetResourceBuilder(resourceBuilder)
.AddSource(activitySourceName)
.AddProcessor(new ActivityFilteringProcessor())
.AddProcessor(new ActivityEnrichingProcessor())
.SetSampler(new ApplicationInsightsSampler(1.0F))
.AddAzureMonitorTraceExporter(o => o.ConnectionString = connectionString)
.Build();
}

/// <remarks>
/// Activities will be ingested and stored in Application Insights
/// as either a request or dependency, according to their ActivityKind.
/// </remarks>
public void GenerateTraces()
{
// Note: This activity will be dropped due to the ActivityFilteringProcessor filtering ActivityKind.Producer.
using (var testActivity1 = activitySource.StartActivity("TestInternalActivity", ActivityKind.Producer))
{
testActivity1?.SetTag("CustomTag1", "Value1");
testActivity1?.SetTag("CustomTag2", "Value2");
}

using (var activity = activitySource.StartActivity("SayHello", ActivityKind.Client))
{
activity?.SetTag("foo", 1);
activity?.SetTag("baz", new int[] { 1, 2, 3 });
activity?.SetStatus(ActivityStatusCode.Ok);

using (var nestedActivity = activitySource.StartActivity("SayHelloAgain", ActivityKind.Server))
{
nestedActivity?.SetTag("bar", "Hello, World!");
nestedActivity?.SetStatus(ActivityStatusCode.Ok);
}
}
}

public void Dispose()
{
this.tracerProvider.Dispose();
}
}
}