Skip to content

Commit 63d1025

Browse files
azure.monitor.otel.exporter add demo console app (Azure#24137)
* add sample app * update app * minor change * doc + update * rmv nuget * revert accidental nuget.config del * removing readme * fix * nit * resolve pr comments * indent * add comment for otlp * rename var name
1 parent 823e5e7 commit 63d1025

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/Azure.Monitor.OpenTelemetry.Exporter.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Monitor.OpenTelemetry
1717
EndProject
1818
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}"
1919
EndProject
20+
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}"
21+
EndProject
2022
Global
2123
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2224
Debug|Any CPU = Debug|Any CPU
@@ -51,6 +53,10 @@ Global
5153
{D57D79BA-6D39-4E9E-970C-A5F73A4425BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
5254
{D57D79BA-6D39-4E9E-970C-A5F73A4425BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
5355
{D57D79BA-6D39-4E9E-970C-A5F73A4425BB}.Release|Any CPU.Build.0 = Release|Any CPU
56+
{57A53135-3C1B-4106-B873-434B2F606B17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
57+
{57A53135-3C1B-4106-B873-434B2F606B17}.Debug|Any CPU.Build.0 = Debug|Any CPU
58+
{57A53135-3C1B-4106-B873-434B2F606B17}.Release|Any CPU.ActiveCfg = Release|Any CPU
59+
{57A53135-3C1B-4106-B873-434B2F606B17}.Release|Any CPU.Build.0 = Release|Any CPU
5460
EndGlobalSection
5561
GlobalSection(SolutionProperties) = preSolution
5662
HideSolutionNode = FALSE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Diagnostics;
5+
using OpenTelemetry;
6+
7+
namespace Azure.Monitor.OpenTelemetry.Exporter.Tracing.Customization
8+
{
9+
public class ActivityEnrichingProcessor : BaseProcessor<Activity>
10+
{
11+
public override void OnEnd(Activity activity)
12+
{
13+
// The updated activity will be available to all processors which are called after this processor.
14+
activity.DisplayName = "Updated-" + activity.DisplayName;
15+
activity.SetTag("CustomDimension1", "Value1");
16+
activity.SetTag("CustomDimension2", "Value2");
17+
}
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Diagnostics;
5+
using OpenTelemetry;
6+
7+
namespace Azure.Monitor.OpenTelemetry.Exporter.Tracing.Customization
8+
{
9+
internal class ActivityFilteringProcessor : BaseProcessor<Activity>
10+
{
11+
public override void OnStart(Activity activity)
12+
{
13+
// prevents all exporters from exporting internal activities
14+
if (activity.Kind == ActivityKind.Internal)
15+
{
16+
activity.IsAllDataRequested = false;
17+
}
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\src\Azure.Monitor.OpenTelemetry.Exporter.csproj" />
10+
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" VersionOverride="1.1.0" />
11+
</ItemGroup>
12+
13+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using OpenTelemetry;
7+
using OpenTelemetry.Resources;
8+
9+
using OpenTelemetry.Trace;
10+
11+
namespace Azure.Monitor.OpenTelemetry.Exporter.Tracing.Customization
12+
{
13+
public static class Program
14+
{
15+
private static readonly ActivitySource DemoSource = new ActivitySource("OTel.AzureMonitor.Demo");
16+
public static void Main()
17+
{
18+
var resourceAttributes = new Dictionary<string, object> {
19+
{ "service.name", "my-service" },
20+
{ "service.namespace", "my-namespace" },
21+
{ "service.instance.id", "my-instance" }};
22+
23+
var resourceBuilder = ResourceBuilder.CreateDefault().AddAttributes(resourceAttributes);
24+
25+
// For otlp: OpenTelemetry Collector with an OTLP receiver should be running.
26+
// For details refer to https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/examples/Console/TestOtlpExporter.cs
27+
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
28+
.AddSource("OTel.AzureMonitor.Demo")
29+
.SetResourceBuilder(resourceBuilder) // Sets cloud_RoleName as "my-namespace.my-service" and cloud_RoleInstance as "my-instance"
30+
.AddProcessor(new ActivityFilteringProcessor())
31+
.AddProcessor(new ActivityEnrichingProcessor())
32+
.AddAzureMonitorTraceExporter(o =>
33+
{
34+
o.ConnectionString = "<Your Connection String>";
35+
})
36+
.AddOtlpExporter()
37+
.Build();
38+
39+
using (var testActivity1 = DemoSource.StartActivity("TestInternalActivity", ActivityKind.Internal))
40+
{
41+
testActivity1?.SetTag("CustomTag1", "Value1");
42+
testActivity1?.SetTag("CustomTag2", "Value2");
43+
}
44+
45+
using (var testActivity2 = DemoSource.StartActivity("TestServerActivity", ActivityKind.Server))
46+
{
47+
testActivity2?.SetTag("CustomTag1", "Value1");
48+
testActivity2?.SetTag("CustomTag2", "Value2");
49+
}
50+
51+
System.Console.WriteLine("Press Enter key to exit.");
52+
System.Console.ReadLine();
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)