public void CreateTraceHeaderContext() { var header = $"{_traceId}/{_spanId};o=1"; var provider = CreateProviderForTraceHeaderContext(header); var headerContext = CloudTraceExtension.CreateTraceHeaderContext(provider); Assert.Equal(TraceHeaderContext.FromHeader(header).ToString(), headerContext.ToString()); }
public void CreateTraceHeaderContext_UseShouldTraceFallback() { var header = $"{_traceId}/{_spanId};"; var provider = CreateProviderForTraceHeaderContext(header); var headerContext = CloudTraceExtension.ProvideGoogleTraceHeaderContext(provider); Assert.Equal(TraceHeaderContext.FromHeader(header).ToString(), headerContext.ToString()); }
public void FromRequest_ForceTrace(bool?shouldTrace) { var context = TraceHeaderContext.FromHeader( CreateTraceHeaderValue(), () => shouldTrace); Assert.Equal(TraceId, context.TraceId); Assert.Equal(SpanId, context.SpanId); Assert.Equal(shouldTrace, context.ShouldTrace); }
/// <summary> /// Creates an <see cref="TraceHeaderContext"/> based on the current <see cref="HttpContext"/> /// and a <see cref="ShouldTraceRequest"/>. /// </summary> internal static TraceHeaderContext CreateTraceHeaderContext(IServiceProvider provider) { var accessor = provider.GetServiceCheckNotNull <IHttpContextAccessor>(); var shouldTraceRequest = provider.GetServiceCheckNotNull <ShouldTraceRequest>(); string header = accessor.HttpContext?.Request?.Headers[TraceHeaderContext.TraceHeader]; Func <bool?> shouldTraceFunc = () => shouldTraceRequest?.ShouldTrace(accessor.HttpContext?.Request); return(TraceHeaderContext.FromHeader(header, shouldTraceFunc)); }
private static void AssertGoogleTraceHeader(HttpResponseMessage response, string traceId, ulong?spanId, bool?shouldTrace) { string headerValue = Assert.Single(response.Headers.GetValues(TraceHeaderContext.TraceHeader)); var header = TraceHeaderContext.FromHeader(headerValue); Assert.Equal(traceId, header.TraceId); Assert.Equal(spanId, header.SpanId); Assert.Equal(shouldTrace, header.ShouldTrace); AssertNoCustomTraceHeader(response); }
/// <summary> /// Gets the full trace name if the log target is a project, we have an /// HTTP accessor and a valid trace header exists on the current context. /// If the trace name cannot be determined null is returned. /// See: See: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry /// </summary> internal string GetTraceName() { if (_traceTarget == null || _accessor == null) { return(null); } string header = _accessor.HttpContext?.Request?.Headers[TraceHeaderContext.TraceHeader]; var traceContext = TraceHeaderContext.FromHeader(header); return(traceContext.TraceId == null ? null : _traceTarget.GetFullTraceName(traceContext.TraceId)); }
protected override Task <HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { if (_context != null) { var traceHeader = request.Headers.GetValues(TraceHeaderContext.TraceHeader).First(); var currentContext = TraceHeaderContext.FromHeader(traceHeader); Assert.Equal(_context.TraceId, currentContext.TraceId); Assert.Equal(_context.SpanId, currentContext.SpanId); Assert.Equal(_context.ShouldTrace, currentContext.ShouldTrace); } return(Task.FromResult(new HttpResponseMessage())); }
/// <summary> /// Gets the full trace name if the log target is a project, we have an /// HTTP accessor and a valid trace header exists on the current context. /// If the trace name cannot be determined null is returned. /// See: See: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry /// </summary> internal string GetTraceName() { var httpContext = _serviceProvider?.GetService <IHttpContextAccessor>()?.HttpContext; if (_traceTarget == null || httpContext == null) { return(null); } string header = httpContext.Request?.Headers[TraceHeaderContext.TraceHeader]; var traceContext = TraceHeaderContext.FromHeader(header); return(traceContext.TraceId == null ? null : _traceTarget.GetFullTraceName(traceContext.TraceId)); }
public void CreateTraceHeaderContext() { string header = $"{TraceId}/{SpanId};o=1"; var context = new DefaultHttpContext(); var request = new DefaultHttpRequest(context); request.Headers[TraceHeaderContext.TraceHeader] = header; var accessor = new HttpContextAccessor(); accessor.HttpContext = context; Mock <IServiceProvider> mockProvider = new Mock <IServiceProvider>(); mockProvider.Setup(p => p.GetService(typeof(IHttpContextAccessor))).Returns(accessor); var headerContext = CloudTraceExtension.CreateTraceHeaderContext(mockProvider.Object); Assert.Equal(TraceHeaderContext.FromHeader(header).ToString(), headerContext.ToString()); }
public void FromRequest_InvalidHeader() { CheckInvalid(TraceHeaderContext.FromHeader("1234=0")); }
public void FromRequest_NoHeader() { CheckInvalid(TraceHeaderContext.FromHeader(null)); }
public void FromRequest_ForceTrace_NullFunc_Invalid() { CheckInvalid(TraceHeaderContext.FromHeader("", null)); }