public async Task InvokeAsync(HttpContext context) { var spanIdentifier = Guid.NewGuid().ToString(); var dottyContext = new DottyLogsScopedContext { SpanId = spanIdentifier }; // First, check if we have something in headers already if (context.Request.Headers.TryGetValue("X-TRACE-IDENTIFIER", out StringValues traceIdentifer)) { if (traceIdentifer.Count == 1) { context.TraceIdentifier = traceIdentifer[0]; } } if (context.Request.Headers.TryGetValue("X-PARENT-SPANID", out StringValues parentSpanId)) { if (parentSpanId.Count == 1) { dottyContext.ParentSpanId = parentSpanId[0]; } } DottyLogsScopedContextAccessor.DottyLogsScopedContext = dottyContext; // Tell hub we started var requestUpdate = new StartSpanRequest { TraceIdentifier = context.TraceIdentifier, ThreadId = Thread.CurrentThread.ManagedThreadId, RequestUrl = context.Request.Path.ToString(), SpanIdentifier = spanIdentifier, ParentSpanIdentifier = DottyLogsScopedContextAccessor.DottyLogsScopedContext.ParentSpanId, Timestamp = DateTime.UtcNow.ToTimestamp(), Hostname = Dns.GetHostName(), //TODO move to lazy property ApplicationName = Assembly.GetEntryAssembly().GetName().Name }; await _updatePusherService.StartSpan(requestUpdate); try { await _next(context); } finally { await _updatePusherService.StopSpan(new StopSpanRequest { SpanIdentifier = spanIdentifier, WasSuccess = true, TraceIdentifier = context.TraceIdentifier }); } }
public override async Task <Empty> StartSpan(StartSpanRequest request, ServerCallContext context) { await _uiUpdateHub.Clients.All.SendAsync("StartSpan", request); var trace = await _dbContext .Traces .SingleOrDefaultAsync(t => t.TraceIdentifier == request.TraceIdentifier); var span = new DottySpan { ApplicationName = request.ApplicationName, HostName = request.Hostname, ParentSpanIdentifier = request.ParentSpanIdentifier, RequestUrl = request.RequestUrl, SpanIdentifier = request.SpanIdentifier, StartedAtUtc = DateTime.UtcNow, TraceIdentifier = request.TraceIdentifier }; if (trace == null) { _logger.LogInformation($"Creating new trace for { request.TraceIdentifier}"); trace = new DottyTrace(); trace.RequestUrl = request.RequestUrl; trace.StartedAtUtc = DateTime.UtcNow; trace.TraceIdentifier = request.TraceIdentifier; trace.SpanData = span; _dbContext.Traces.Add(trace); } else { var parentSpan = await _dbContext.Spans.SingleOrDefaultAsync(s => s.SpanIdentifier == request.ParentSpanIdentifier); parentSpan.ChildSpans.Add(span); } await _dbContext.SaveChangesAsync(); return(new Empty()); }
public async Task StartSpan(StartSpanRequest requestUpdate) { await _client.StartSpanAsync(requestUpdate); }