Skip to content

Commit

Permalink
Add TraceTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jerviscui committed Apr 8, 2024
1 parent 43d928d commit a80a605
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Example.CSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EfCorePoolTest", "plugins\E
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LINQKitTest", "plugins\EfCore\LINQKitTest\LINQKitTest.csproj", "{61B33E92-5A3D-45DD-856F-E87E0C13B93D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AuthorizationTest", "Tests\AuthorizationTest\AuthorizationTest.csproj", "{D7BF1DC5-7C8C-4694-9790-A71F7909C2C5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AuthorizationTest", "Tests\AuthorizationTest\AuthorizationTest.csproj", "{D7BF1DC5-7C8C-4694-9790-A71F7909C2C5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TraceTest", "Tests\TraceTest\TraceTest.csproj", "{338A8713-A8FC-412F-A804-8AB6C2CC3867}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -599,6 +601,10 @@ Global
{D7BF1DC5-7C8C-4694-9790-A71F7909C2C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D7BF1DC5-7C8C-4694-9790-A71F7909C2C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D7BF1DC5-7C8C-4694-9790-A71F7909C2C5}.Release|Any CPU.Build.0 = Release|Any CPU
{338A8713-A8FC-412F-A804-8AB6C2CC3867}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{338A8713-A8FC-412F-A804-8AB6C2CC3867}.Debug|Any CPU.Build.0 = Debug|Any CPU
{338A8713-A8FC-412F-A804-8AB6C2CC3867}.Release|Any CPU.ActiveCfg = Release|Any CPU
{338A8713-A8FC-412F-A804-8AB6C2CC3867}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -702,6 +708,7 @@ Global
{A9B743BF-3D90-4E90-84C9-97846DAD1F7D} = {F5B1D9A3-285F-4FF7-8457-095152163258}
{61B33E92-5A3D-45DD-856F-E87E0C13B93D} = {F5B1D9A3-285F-4FF7-8457-095152163258}
{D7BF1DC5-7C8C-4694-9790-A71F7909C2C5} = {719AF6DF-48D1-4C7B-A8E9-493482D109DE}
{338A8713-A8FC-412F-A804-8AB6C2CC3867} = {719AF6DF-48D1-4C7B-A8E9-493482D109DE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CBE0CD6C-2E47-4D9F-B072-C138AA4A6D5A}
Expand Down
63 changes: 63 additions & 0 deletions src/Tests/TraceTest/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Diagnostics;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

Console.WriteLine("Hello, World!");

var builder = Sdk.CreateTracerProviderBuilder();

using var tracerProvider = builder.SetResourceBuilder(ResourceBuilder.CreateDefault()
.AddService("TraceTest"))
.AddSource("Sample.DistributedTracing")
.AddConsoleExporter()
.AddZipkinExporter(options => { })
.Build();

var activitySource = new ActivitySource("Sample.DistributedTracing", "1.0.0");

using (var activity = activitySource.StartActivity("Do", ActivityKind.Server))
{
activity?.SetTag("tag1", "foo");
activity?.SetTag("tag2", "bar");

await StepOne();
activity?.AddEvent(new ActivityEvent("StepOne end."));

await StepTwo();
activity?.AddEvent(new ActivityEvent("StepTwo end."));

activity?.SetTag("otel.status_code", DateTime.Now.Microsecond % 2 == 0 ? "OK" : "ERROR");
activity?.SetTag("otel.status_description", "Use this text give more information about the error");
}

Console.ReadLine();
activitySource.Dispose();

async Task StepOne()
{
using (var activity = activitySource.StartActivity(ActivityKind.Client))
{
Console.WriteLine("One");
await Task.Delay(50);
await StepOneOne();
}
}

async Task StepOneOne()
{
using (var activity = activitySource.StartActivity(ActivityKind.Server))
{
Console.WriteLine("OneOne");
await Task.Delay(50);
}
}

async Task StepTwo()
{
using (var activity = activitySource.StartActivity(ActivityKind.Producer))
{
Console.WriteLine("Two");
await Task.Delay(500);
}
}
15 changes: 15 additions & 0 deletions src/Tests/TraceTest/TraceTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.8.0" />
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="1.8.0" />
</ItemGroup>

</Project>

0 comments on commit a80a605

Please sign in to comment.