Skip to content

Instantly share code, notes, and snippets.

@yallie
Last active July 1, 2020 00:20
Show Gist options
  • Save yallie/a59ad0a7a59ca4a395260369b0142c22 to your computer and use it in GitHub Desktop.
Save yallie/a59ad0a7a59ca4a395260369b0142c22 to your computer and use it in GitHub Desktop.
OpenTelemetry + Jaeger. Overlapping spans demo
// 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";
}));
}
}
@yallie
Copy link
Author

yallie commented Jul 1, 2020

Traces and spans in JSON format:

{
    "data": [
        {
            "traceID": "712a81a6bb0bdf43b20055002c7639b1",
            "spans": [
                {
                    "traceID": "712a81a6bb0bdf43b20055002c7639b1",
                    "spanID": "6792318383a3704a",
                    "flags": 1,
                    "operationName": "execute-non-query",
                    "references": [
                        {
                            "refType": "CHILD_OF",
                            "traceID": "712a81a6bb0bdf43b20055002c7639b1",
                            "spanID": "b9088b4e2faa8a48"
                        }
                    ],
                    "startTime": 1593560132101814,
                    "duration": 154652,
                    "tags": [
                        {
                            "key": "timestamp",
                            "type": "string",
                            "value": "35:32.101"
                        },
                        {
                            "key": "db.command",
                            "type": "string",
                            "value": "init_session"
                        },
                        {
                            "key": "ot.status_code",
                            "type": "string",
                            "value": "Ok"
                        },
                        {
                            "key": "internal.span.format",
                            "type": "string",
                            "value": "proto"
                        }
                    ],
                    "logs": [],
                    "processID": "p1",
                    "warnings": null
                },
                {
                    "traceID": "712a81a6bb0bdf43b20055002c7639b1",
                    "spanID": "8f327c246f5b3948",
                    "flags": 1,
                    "operationName": "execute-non-query",
                    "references": [
                        {
                            "refType": "CHILD_OF",
                            "traceID": "712a81a6bb0bdf43b20055002c7639b1",
                            "spanID": "b9088b4e2faa8a48"
                        }
                    ],
                    "startTime": 1593560132258197,
                    "duration": 150174,
                    "tags": [
                        {
                            "key": "timestamp",
                            "type": "string",
                            "value": "35:32.257"
                        },
                        {
                            "key": "db.command",
                            "type": "string",
                            "value": "create_temp_table"
                        },
                        {
                            "key": "ot.status_code",
                            "type": "string",
                            "value": "Ok"
                        },
                        {
                            "key": "internal.span.format",
                            "type": "string",
                            "value": "proto"
                        }
                    ],
                    "logs": [],
                    "processID": "p1",
                    "warnings": null
                },
                {
                    "traceID": "712a81a6bb0bdf43b20055002c7639b1",
                    "spanID": "7c69602bad43494a",
                    "flags": 1,
                    "operationName": "execute-non-query",
                    "references": [
                        {
                            "refType": "CHILD_OF",
                            "traceID": "712a81a6bb0bdf43b20055002c7639b1",
                            "spanID": "627974dac54c1f49"
                        }
                    ],
                    "startTime": 1593560132433561,
                    "duration": 150960,
                    "tags": [
                        {
                            "key": "timestamp",
                            "type": "string",
                            "value": "35:33.064"
                        },
                        {
                            "key": "db.command",
                            "type": "string",
                            "value": "calculate_report"
                        },
                        {
                            "key": "ot.status_code",
                            "type": "string",
                            "value": "Ok"
                        },
                        {
                            "key": "internal.span.format",
                            "type": "string",
                            "value": "proto"
                        }
                    ],
                    "logs": [],
                    "processID": "p1",
                    "warnings": [
                        "This span's timestamps were adjusted by -630.6975ms"
                    ]
                },
                {
                    "traceID": "712a81a6bb0bdf43b20055002c7639b1",
                    "spanID": "627974dac54c1f49",
                    "flags": 1,
                    "operationName": "execute-reader",
                    "references": [
                        {
                            "refType": "CHILD_OF",
                            "traceID": "712a81a6bb0bdf43b20055002c7639b1",
                            "spanID": "b9088b4e2faa8a48"
                        }
                    ],
                    "startTime": 1593560132408748,
                    "duration": 200587,
                    "tags": [
                        {
                            "key": "timestamp",
                            "type": "string",
                            "value": "35:32.408"
                        },
                        {
                            "key": "db.command",
                            "type": "string",
                            "value": "select * from temp_table"
                        },
                        {
                            "key": "ot.status_code",
                            "type": "string",
                            "value": "Ok"
                        },
                        {
                            "key": "internal.span.format",
                            "type": "string",
                            "value": "proto"
                        }
                    ],
                    "logs": [],
                    "processID": "p1",
                    "warnings": null
                },
                {
                    "traceID": "712a81a6bb0bdf43b20055002c7639b1",
                    "spanID": "5342bff832ea8949",
                    "flags": 1,
                    "operationName": "execute-non-query",
                    "references": [
                        {
                            "refType": "CHILD_OF",
                            "traceID": "712a81a6bb0bdf43b20055002c7639b1",
                            "spanID": "627974dac54c1f49"
                        }
                    ],
                    "startTime": 1593560132433702,
                    "duration": 150678,
                    "tags": [
                        {
                            "key": "timestamp",
                            "type": "string",
                            "value": "35:32.913"
                        },
                        {
                            "key": "db.command",
                            "type": "string",
                            "value": "load_data"
                        },
                        {
                            "key": "ot.status_code",
                            "type": "string",
                            "value": "Ok"
                        },
                        {
                            "key": "internal.span.format",
                            "type": "string",
                            "value": "proto"
                        }
                    ],
                    "logs": [],
                    "processID": "p1",
                    "warnings": [
                        "This span's timestamps were adjusted by -479.8585ms"
                    ]
                },
                {
                    "traceID": "712a81a6bb0bdf43b20055002c7639b1",
                    "spanID": "31f66fc22f9c0e4b",
                    "flags": 1,
                    "operationName": "data-reader-read",
                    "references": [
                        {
                            "refType": "CHILD_OF",
                            "traceID": "712a81a6bb0bdf43b20055002c7639b1",
                            "spanID": "627974dac54c1f49"
                        }
                    ],
                    "startTime": 1593560132409030,
                    "duration": 504513,
                    "tags": [
                        {
                            "key": "timestamp",
                            "type": "string",
                            "value": "35:32.408"
                        },
                        {
                            "key": "db.command",
                            "type": "string",
                            "value": "select * from temp_table"
                        },
                        {
                            "key": "ot.status_code",
                            "type": "string",
                            "value": "Ok"
                        },
                        {
                            "key": "internal.span.format",
                            "type": "string",
                            "value": "proto"
                        }
                    ],
                    "logs": [],
                    "processID": "p1",
                    "warnings": null
                },
                {
                    "traceID": "712a81a6bb0bdf43b20055002c7639b1",
                    "spanID": "d766e2c0851c7b41",
                    "flags": 1,
                    "operationName": "execute-reader",
                    "references": [
                        {
                            "refType": "CHILD_OF",
                            "traceID": "712a81a6bb0bdf43b20055002c7639b1",
                            "spanID": "627974dac54c1f49"
                        }
                    ],
                    "startTime": 1593560133215243,
                    "duration": 201032,
                    "tags": [
                        {
                            "key": "timestamp",
                            "type": "string",
                            "value": "35:33.215"
                        },
                        {
                            "key": "db.command",
                            "type": "string",
                            "value": "select * from report"
                        },
                        {
                            "key": "ot.status_code",
                            "type": "string",
                            "value": "Ok"
                        },
                        {
                            "key": "internal.span.format",
                            "type": "string",
                            "value": "proto"
                        }
                    ],
                    "logs": [],
                    "processID": "p1",
                    "warnings": null
                },
                {
                    "traceID": "712a81a6bb0bdf43b20055002c7639b1",
                    "spanID": "7b2a9835c8cee045",
                    "flags": 1,
                    "operationName": "data-reader-read",
                    "references": [
                        {
                            "refType": "CHILD_OF",
                            "traceID": "712a81a6bb0bdf43b20055002c7639b1",
                            "spanID": "d766e2c0851c7b41"
                        }
                    ],
                    "startTime": 1593560133215276,
                    "duration": 504183,
                    "tags": [
                        {
                            "key": "timestamp",
                            "type": "string",
                            "value": "35:33.215"
                        },
                        {
                            "key": "db.command",
                            "type": "string",
                            "value": "select * from report"
                        },
                        {
                            "key": "ot.status_code",
                            "type": "string",
                            "value": "Ok"
                        },
                        {
                            "key": "internal.span.format",
                            "type": "string",
                            "value": "proto"
                        }
                    ],
                    "logs": [],
                    "processID": "p1",
                    "warnings": null
                },
                {
                    "traceID": "712a81a6bb0bdf43b20055002c7639b1",
                    "spanID": "b9088b4e2faa8a48",
                    "flags": 1,
                    "operationName": "main",
                    "references": [],
                    "startTime": 1593560132095795,
                    "duration": 1623674,
                    "tags": [
                        {
                            "key": "ot.status_code",
                            "type": "string",
                            "value": "Ok"
                        },
                        {
                            "key": "internal.span.format",
                            "type": "string",
                            "value": "proto"
                        }
                    ],
                    "logs": [],
                    "processID": "p1",
                    "warnings": null
                }
            ],
            "processes": {
                "p1": {
                    "serviceName": "database-service",
                    "tags": []
                }
            },
            "warnings": null
        }
    ],
    "total": 0,
    "limit": 0,
    "offset": 0,
    "errors": null
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment