-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* move and rename "Samples" project to "samples\Exporters" * add "LoggingTracer" as an example of an API implementation * add readme to solution file * remove Samples project from solution * fix scoping * add "LoggingTracer" as an example of an API implementation * add readme to solution file * fix scoping * changed back to 2.1. was an accidental change. * added "Demo" to project names * adapted LoggingTracer to API changes. stripped down asp.net core example to bare minimum. * cleaned up LoggingTracer sample. not more NotImplementedExceptions, adhere to code style better (usings), don't log getters * consistency fixes * made console sample async, code style * adapt to API changes
- Loading branch information
1 parent
799da6d
commit d053e75
Showing
18 changed files
with
601 additions
and
0 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
20 changes: 20 additions & 0 deletions
20
samples/LoggingTracer/LoggingTracer.Demo.AspNetCore/LoggingTracer.Demo.AspNetCore.csproj
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 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.2</TargetFramework> | ||
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.App" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" /> | ||
|
||
<ProjectReference Include="..\..\..\src\OpenTelemetry.Abstractions\OpenTelemetry.Abstractions.csproj" /> | ||
|
||
<ProjectReference Include="..\..\..\src\OpenTelemetry.Collector.AspNetCore\OpenTelemetry.Collector.AspNetCore.csproj" /> | ||
|
||
<ProjectReference Include="..\..\..\src\OpenTelemetry.Collector.Dependencies\OpenTelemetry.Collector.Dependencies.csproj" /> | ||
<ProjectReference Include="..\LoggingTracer\LoggingTracer.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
29 changes: 29 additions & 0 deletions
29
samples/LoggingTracer/LoggingTracer.Demo.AspNetCore/LoggingTracerExtensions.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,29 @@ | ||
namespace LoggingTracer.Demo.AspNetCore | ||
{ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OpenTelemetry.Collector.AspNetCore; | ||
using OpenTelemetry.Collector.Dependencies; | ||
using OpenTelemetry.Trace; | ||
using OpenTelemetry.Trace.Sampler; | ||
|
||
static class LoggingTracerExtensions { | ||
internal static void AddLoggingTracer(this IServiceCollection services) | ||
{ | ||
services.AddSingleton<ITracer>(new global::LoggingTracer.LoggingTracer()); | ||
|
||
services.AddSingleton<ISampler>(Samplers.AlwaysSample); | ||
services.AddSingleton<RequestsCollectorOptions>(new RequestsCollectorOptions()); | ||
services.AddSingleton<RequestsCollector>(); | ||
|
||
services.AddSingleton<DependenciesCollectorOptions>(new DependenciesCollectorOptions()); | ||
services.AddSingleton<DependenciesCollector>(); | ||
} | ||
|
||
internal static void UseLoggingTracer(this IApplicationBuilder app) | ||
{ | ||
app.ApplicationServices.GetService<RequestsCollector>(); // get it instantiated | ||
app.ApplicationServices.GetService<DependenciesCollector>(); // get it instantiated | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
samples/LoggingTracer/LoggingTracer.Demo.AspNetCore/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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace LoggingTracer.Demo.AspNetCore | ||
{ | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Hosting; | ||
|
||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateWebHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => | ||
WebHost.CreateDefaultBuilder(args) | ||
.UseStartup<Startup>(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
samples/LoggingTracer/LoggingTracer.Demo.AspNetCore/Startup.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,30 @@ | ||
namespace LoggingTracer.Demo.AspNetCore | ||
{ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
public class Startup | ||
{ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddLoggingTracer(); | ||
} | ||
|
||
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | ||
{ | ||
if (env.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
|
||
app.UseLoggingTracer(); | ||
|
||
app.Run(async (context) => | ||
{ | ||
await context.Response.WriteAsync("Hello World!"); | ||
}); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
samples/LoggingTracer/LoggingTracer.Demo.AspNetCore/appsettings.Development.json
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,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Debug", | ||
"System": "Information", | ||
"Microsoft": "Information" | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
samples/LoggingTracer/LoggingTracer.Demo.AspNetCore/appsettings.json
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,10 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
12 changes: 12 additions & 0 deletions
12
samples/LoggingTracer/LoggingTracer.Demo.ConsoleApp/LoggingTracer.Demo.ConsoleApp.csproj
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,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.2</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\LoggingTracer\LoggingTracer.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
31 changes: 31 additions & 0 deletions
31
samples/LoggingTracer/LoggingTracer.Demo.ConsoleApp/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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace LoggingTracer.ConsoleApp | ||
{ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using OpenTelemetry.Trace; | ||
|
||
public class Program | ||
{ | ||
private static ITracer tracer = new LoggingTracer(); | ||
|
||
public static async Task Main(string[] args) | ||
{ | ||
var builder = tracer.SpanBuilder("Main (span1)"); | ||
using (tracer.WithSpan(builder.StartSpan())) | ||
{ | ||
await Task.Delay(100); | ||
await Foo(); | ||
} | ||
} | ||
|
||
private static async Task Foo() | ||
{ | ||
var builder = tracer.SpanBuilder("Foo (span2)"); | ||
using (tracer.WithSpan(builder.StartSpan())) | ||
{ | ||
tracer.CurrentSpan.SetAttribute("myattribute", "mvalue"); | ||
await Task.Delay(100); | ||
} | ||
} | ||
} | ||
} |
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,30 @@ | ||
namespace LoggingTracer | ||
{ | ||
using System; | ||
using System.Threading; | ||
|
||
public static class Logger | ||
{ | ||
private static DateTime startTime = DateTime.UtcNow; | ||
|
||
static Logger() | ||
{ | ||
PrintHeader(); | ||
} | ||
|
||
public static void PrintHeader() | ||
{ | ||
Console.WriteLine("MsSinceStart | ThreadId | API"); | ||
} | ||
|
||
public static void Log(string s) | ||
{ | ||
Console.WriteLine($"{MillisSinceStart(),12} | {Thread.CurrentThread.ManagedThreadId,8} | {s}"); | ||
} | ||
|
||
private static int MillisSinceStart() | ||
{ | ||
return (int)DateTime.UtcNow.Subtract(startTime).TotalMilliseconds; | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
samples/LoggingTracer/LoggingTracer/LoggingBinaryFormat.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,35 @@ | ||
namespace LoggingTracer | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using OpenTelemetry.Context.Propagation; | ||
using OpenTelemetry.Trace; | ||
|
||
public sealed class LoggingBinaryFormat : IBinaryFormat | ||
{ | ||
public ISet<string> Fields => null; | ||
|
||
public SpanContext Extract<T>(T carrier, Func<T, string, IEnumerable<string>> getter) | ||
{ | ||
Logger.Log($"LoggingBinaryFormat.Extract(...)"); | ||
return SpanContext.Blank; | ||
} | ||
|
||
public SpanContext FromByteArray(byte[] bytes) | ||
{ | ||
Logger.Log($"LoggingBinaryFormat.FromByteArray(...)"); | ||
return SpanContext.Blank; | ||
} | ||
|
||
public void Inject<T>(SpanContext spanContext, T carrier, Action<T, string, string> setter) | ||
{ | ||
Logger.Log($"LoggingBinaryFormat.Inject({spanContext}, ...)"); | ||
} | ||
|
||
public byte[] ToByteArray(SpanContext spanContext) | ||
{ | ||
Logger.Log($"LoggingBinaryFormat.ToByteArray({spanContext})"); | ||
return new byte[0]; | ||
} | ||
} | ||
} |
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,45 @@ | ||
namespace LoggingTracer | ||
{ | ||
using System.Threading; | ||
using OpenTelemetry.Context; | ||
using OpenTelemetry.Trace; | ||
|
||
public static class CurrentSpanUtils | ||
{ | ||
private static AsyncLocal<ISpan> asyncLocalContext = new AsyncLocal<ISpan>(); | ||
|
||
public static ISpan CurrentSpan => asyncLocalContext.Value; | ||
|
||
public class LoggingScope : IScope | ||
{ | ||
private readonly ISpan origContext; | ||
private readonly ISpan span; | ||
private readonly bool endSpan; | ||
|
||
public LoggingScope(ISpan span, bool endSpan = true) | ||
{ | ||
this.span = span; | ||
this.endSpan = endSpan; | ||
this.origContext = CurrentSpanUtils.asyncLocalContext.Value; | ||
CurrentSpanUtils.asyncLocalContext.Value = span; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Logger.Log($"Scope.Dispose"); | ||
var current = asyncLocalContext.Value; | ||
asyncLocalContext.Value = this.origContext; | ||
|
||
if (current != this.origContext) | ||
{ | ||
Logger.Log($"Scope.Dispose: current != this.origContext"); | ||
} | ||
|
||
if (this.endSpan) | ||
{ | ||
this.span.End(); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.