-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AzureMonitorExporter] refactor demo projects (#31204)
* refactor demo project * update comment * cleanup. added comments. removed Shutdown.
- Loading branch information
1 parent
e1318c5
commit d6f7e00
Showing
7 changed files
with
213 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...or.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Demo/Logs/LogDemo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...nTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Demo/Metrics/MetricDemo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
82 changes: 12 additions & 70 deletions
82
...Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Demo/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...rter/tests/Azure.Monitor.OpenTelemetry.Exporter.Demo/Traces/ActivityEnrichingProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...rter/tests/Azure.Monitor.OpenTelemetry.Exporter.Demo/Traces/ActivityFilteringProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...penTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Demo/Traces/TraceDemo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
.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(); | ||
} | ||
} | ||
} |