Last active
July 1, 2020 00:20
-
-
Save yallie/a59ad0a7a59ca4a395260369b0142c22 to your computer and use it in GitHub Desktop.
OpenTelemetry + Jaeger. Overlapping spans demo
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
// Compile the code using: | |
// csc.exe test.cs | |
// /r:OpenTelemetry.Exporter.Jaeger.dll | |
// /r:OpenTelemetry.Api.dll | |
// /r:OpenTelemetry.dll | |
// /r:System.Diagnostics.DiagnosticSource.dll | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Threading; | |
using OpenTelemetry.Trace; | |
using OpenTelemetry.Trace.Configuration; | |
using static System.Console; | |
class Program | |
{ | |
static void Main() | |
{ | |
using (TracerFactory) | |
{ | |
var tracer = TracerFactory.GetTracer(null); | |
using (tracer.StartActiveSpan("main", out _)) | |
{ | |
DatabaseDemo(); | |
} | |
} | |
} | |
static void DatabaseDemo() | |
{ | |
ExecuteNonQuery("init_session"); | |
ExecuteNonQuery("create_temp_table"); | |
using (var reader = ExecuteReader("select * from temp_table")) | |
{ | |
while (reader.Read()) | |
WriteLine(reader["Name"]); | |
} | |
ExecuteNonQuery("load_data"); | |
ExecuteNonQuery("calculate_report"); | |
using (var reader = ExecuteReader("select * from report")) | |
{ | |
while (reader.Read()) | |
WriteLine(reader["Name"]); | |
} | |
} | |
static void ExecuteNonQuery(string sql) | |
{ | |
var tracer = TracerFactory.GetTracer(null); | |
using (tracer.StartActiveSpan("execute-non-query", out var span)) | |
{ | |
span.SetAttribute("timestamp", DateTime.Now.ToString("mm:ss.fff")); | |
span.SetAttribute("db.command", sql); | |
Thread.Sleep(150); | |
} | |
} | |
static DataReader ExecuteReader(string sql) | |
{ | |
var tracer = TracerFactory.GetTracer(null); | |
using (tracer.StartActiveSpan("execute-reader", out var span)) | |
{ | |
span.SetAttribute("timestamp", DateTime.Now.ToString("mm:ss.fff")); | |
span.SetAttribute("db.command", sql); | |
var reader = new DataReader(sql); | |
// performing a query | |
Thread.Sleep(200); | |
return reader; | |
} | |
} | |
public class DataReader : IDisposable | |
{ | |
private Tracer Tracer { get; } = TracerFactory.GetTracer(null); | |
private IDisposable TraceScope { get; } | |
private int Counter { get; set; } = 0; | |
public DataReader(string sql) | |
{ | |
TraceScope = Tracer.StartActiveSpan("data-reader-read", out var span); | |
span.SetAttribute("timestamp", DateTime.Now.ToString("mm:ss.fff")); | |
span.SetAttribute("db.command", sql); | |
} | |
public void Dispose() | |
{ | |
TraceScope.Dispose(); | |
} | |
public bool Read() | |
{ | |
// fetching data | |
Thread.Sleep(50 * Counter); | |
return Counter++ < 3; | |
} | |
public string this[string name] => new[] { "foo", "bar", "quux" } [Counter % 3]; | |
} | |
static TracerFactory TracerFactory { get; } = SetupTelemetry(); | |
static TracerFactory SetupTelemetry() | |
{ | |
return TracerFactory.Create(b => b.UseJaeger(o => | |
{ | |
o.ServiceName = "database-service"; | |
o.AgentHost = "localhost"; | |
})); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Traces and spans in JSON format: