Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LoggingTracer sample #92

Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
996ec40
merge (#1)
discostu105 Jun 4, 2019
8a8b534
move and rename "Samples" project to "samples\Exporters"
discostu105 Jun 4, 2019
38e7674
add "LoggingTracer" as an example of an API implementation
discostu105 Jun 4, 2019
9b49e89
add readme to solution file
discostu105 Jun 4, 2019
ae76b21
remove Samples project from solution
discostu105 Jun 4, 2019
d01c916
Merge branch 'sample-restruct' into add-loggingtracer-samples
discostu105 Jun 4, 2019
3fb21bf
fix scoping
discostu105 Jun 4, 2019
7e6e91f
Merge remote-tracking branch 'upstream/master'
discostu105 Jun 14, 2019
62b6bd1
add "LoggingTracer" as an example of an API implementation
discostu105 Jun 4, 2019
a12cd96
add readme to solution file
discostu105 Jun 4, 2019
2f00bda
fix scoping
discostu105 Jun 4, 2019
5e310c7
Merge branch 'add-loggingtracer-samples' of https://github.com/dynatr…
discostu105 Jun 14, 2019
29cb983
changed back to 2.1. was an accidental change.
discostu105 Jun 14, 2019
cc6403c
added "Demo" to project names
discostu105 Jun 14, 2019
70fc94c
adapted LoggingTracer to API changes. stripped down asp.net core exam…
discostu105 Jun 14, 2019
310dd09
Merge remote-tracking branch 'upstream/master' into add-loggingtracer…
discostu105 Jul 2, 2019
eb97f97
cleaned up LoggingTracer sample. not more NotImplementedExceptions, a…
discostu105 Jul 2, 2019
4825a64
consistency fixes
discostu105 Jul 2, 2019
84524c2
made console sample async, code style
discostu105 Jul 3, 2019
d3a3581
Merge branch 'master' into add-loggingtracer-samples
SergeyKanzhelev Jul 9, 2019
94143dc
Merge branch 'master' into add-loggingtracer-samples
SergeyKanzhelev Jul 9, 2019
c36d231
Merge branch 'master' into add-loggingtracer-samples
SergeyKanzhelev Jul 11, 2019
b1b3e52
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
discostu105 Jul 16, 2019
775ba8d
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
discostu105 Jul 23, 2019
e527afe
adapt to API changes
discostu105 Jul 23, 2019
3071ac9
Merge branch 'add-loggingtracer-samples' of https://github.com/dynatr…
discostu105 Jul 23, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (var scope = builder.StartScopedSpan())
{
await Task.Delay(100);
Foo();
}
}

private static async Task Foo()
{
var builder = tracer.SpanBuilder("Foo (span2)");
using (var scope = builder.StartScopedSpan())
{
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];
}
}
}
46 changes: 46 additions & 0 deletions samples/LoggingTracer/LoggingTracer/LoggingScope.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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;
private readonly int depth;

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