Skip to content

Commit

Permalink
Add LoggingTracer sample (#92)
Browse files Browse the repository at this point in the history
* 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
discostu105 authored and austinlparker committed Jul 23, 2019
1 parent 799da6d commit d053e75
Show file tree
Hide file tree
Showing 18 changed files with 601 additions and 0 deletions.
27 changes: 27 additions & 0 deletions OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{E359
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Exporters", "samples\Exporters\Exporters.csproj", "{6EC9DEC9-086F-4F29-BF3F-5FC7253829D5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "LoggingTracer", "LoggingTracer", "{D9C14CDA-5182-4DEA-8606-98FF1A388BD3}"
ProjectSection(SolutionItems) = preProject
samples\LoggingTracer\readme.md = samples\LoggingTracer\readme.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LoggingTracer", "samples\LoggingTracer\LoggingTracer\LoggingTracer.csproj", "{1EEF77DF-7552-4265-B64C-FA13BC40C256}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LoggingTracer.Demo.ConsoleApp", "samples\LoggingTracer\LoggingTracer.Demo.ConsoleApp\LoggingTracer.Demo.ConsoleApp.csproj", "{607B3861-4D69-43EF-84CE-19F18CD5339A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LoggingTracer.Demo.AspNetCore", "samples\LoggingTracer\LoggingTracer.Demo.AspNetCore\LoggingTracer.Demo.AspNetCore.csproj", "{1EB74FCE-55C5-476A-8BB0-C46B77FE1070}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -147,6 +158,18 @@ Global
{6EC9DEC9-086F-4F29-BF3F-5FC7253829D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6EC9DEC9-086F-4F29-BF3F-5FC7253829D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6EC9DEC9-086F-4F29-BF3F-5FC7253829D5}.Release|Any CPU.Build.0 = Release|Any CPU
{1EEF77DF-7552-4265-B64C-FA13BC40C256}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1EEF77DF-7552-4265-B64C-FA13BC40C256}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1EEF77DF-7552-4265-B64C-FA13BC40C256}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1EEF77DF-7552-4265-B64C-FA13BC40C256}.Release|Any CPU.Build.0 = Release|Any CPU
{607B3861-4D69-43EF-84CE-19F18CD5339A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{607B3861-4D69-43EF-84CE-19F18CD5339A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{607B3861-4D69-43EF-84CE-19F18CD5339A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{607B3861-4D69-43EF-84CE-19F18CD5339A}.Release|Any CPU.Build.0 = Release|Any CPU
{1EB74FCE-55C5-476A-8BB0-C46B77FE1070}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1EB74FCE-55C5-476A-8BB0-C46B77FE1070}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1EB74FCE-55C5-476A-8BB0-C46B77FE1070}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1EB74FCE-55C5-476A-8BB0-C46B77FE1070}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -155,6 +178,10 @@ Global
{7CB2F02E-03FA-4FFF-89A5-C51F107623FD} = {61188153-47FB-4567-AC9B-79B2435853EB}
{F2F81E76-6A0E-466B-B673-EBBF1A9ED075} = {77C7929A-2EED-4AA6-8705-B5C443C8AA0F}
{6EC9DEC9-086F-4F29-BF3F-5FC7253829D5} = {E359BB2B-9AEC-497D-B321-7DF2450C3B8E}
{D9C14CDA-5182-4DEA-8606-98FF1A388BD3} = {E359BB2B-9AEC-497D-B321-7DF2450C3B8E}
{1EEF77DF-7552-4265-B64C-FA13BC40C256} = {D9C14CDA-5182-4DEA-8606-98FF1A388BD3}
{607B3861-4D69-43EF-84CE-19F18CD5339A} = {D9C14CDA-5182-4DEA-8606-98FF1A388BD3}
{1EB74FCE-55C5-476A-8BB0-C46B77FE1070} = {D9C14CDA-5182-4DEA-8606-98FF1A388BD3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {55639B5C-0770-4A22-AB56-859604650521}
Expand Down
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>
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 samples/LoggingTracer/LoggingTracer.Demo.AspNetCore/Program.cs
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 samples/LoggingTracer/LoggingTracer.Demo.AspNetCore/Startup.cs
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!");
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
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": "*"
}
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 samples/LoggingTracer/LoggingTracer.Demo.ConsoleApp/Program.cs
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);
}
}
}
}
30 changes: 30 additions & 0 deletions samples/LoggingTracer/LoggingTracer/Logger.cs
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 samples/LoggingTracer/LoggingTracer/LoggingBinaryFormat.cs
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];
}
}
}
45 changes: 45 additions & 0 deletions samples/LoggingTracer/LoggingTracer/LoggingScope.cs
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();
}
}
}
}
}
Loading

0 comments on commit d053e75

Please sign in to comment.