public static void UseMyTracingMiddlewareOfAspNetCore( this MicropartOfCallChainAsZipkinBuilder micropartOfCallChainAsZipkinBuilder, IApplicationBuilder app) { var extractor = new ZipkinHttpTraceExtractor(); app.Use(async(context, next) => { var request = context.Request; var httpRequestFilter = app.ApplicationServices.GetService <IMicroserviceFilterManager <HttpRequestFilter, HttpRequest> >(); if (httpRequestFilter.ShouldBeFiltered(request)) { return; } if (!extractor.TryExtract(request.Headers, (c, key) => c[key], out var trace)) { trace = Trace.Create(); } Trace.Current = trace; using (var serverTrace = new ServerTrace(AppInfoProvider.Service.Name, $"{request.Path}/{request.Method}")) { trace.Record(Annotations.Tag("http.host", request.Host.ToString())); trace.Record(Annotations.Tag("http.uri", UriHelper.GetDisplayUrl(request))); trace.Record(Annotations.Tag("http.path", request.Path)); await serverTrace.TracedActionAsync(next()); } }); }
/// <summary> /// 注册服务 /// </summary> public static void RegisterService(Func <HttpContext, string> getRpc = null) { getRpc = getRpc ?? (c => c.Request.Method); var extractor = Propagations.B3String.Extractor <IHeaderDictionary>((carrier, key) => carrier[key]); var request = ZipkinServiceHttpContext.Current.Request; var traceContext = extractor.Extract(request.Headers); var trace = traceContext == null?Trace.Create() : Trace.CreateFromId(traceContext); Trace.Current = trace; using (var serverTrace = new ServerTrace(ServiceName, getRpc(ZipkinServiceHttpContext.Current))) { if (request.Host.HasValue) { trace.Record(Annotations.Tag("http.host", request.Host.ToString())); } trace.Record(Annotations.Tag("http.uri", UriHelper.GetDisplayUrl(request))); trace.Record(Annotations.Tag("http.path", request.Path)); trace.Record(Annotations.Tag("Execute.Time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"))); trace.Record(Annotations.Tag("Request.Body", JSONHelper.SerializeObject(request.Body))); serverTrace.AddAnnotation(Annotations.ServiceName(ServiceName)); } }
public static void UseTracing(this IApplicationBuilder app, string serviceName, Func <HttpContext, string> getRpc = null) { getRpc = getRpc ?? (context => context.Request.Method); var extractor = Propagations.B3String.Extractor <IHeaderDictionary>((carrier, key) => carrier[key]); app.Use(async(context, next) => { var request = context.Request; var traceContext = extractor.Extract(request.Headers); var trace = traceContext == null ? Trace.Create() : Trace.CreateFromId(traceContext); Trace.Current = trace; using (var serverTrace = new ServerTrace(serviceName, getRpc(context))) { if (request.Host.HasValue) { trace.Record(Annotations.Tag("http.host", request.Host.ToString())); } trace.Record(Annotations.Tag("http.uri", UriHelper.GetDisplayUrl(request))); trace.Record(Annotations.Tag("http.path", request.Path)); await serverTrace.TracedActionAsync(next()); } }); }
private void SetUpTracing() { trace = Trace.Create(); Trace.Current = trace; using (var serverTrace = new ServerTrace(serviceName, "TraceRestClient")) { trace.Record(Annotations.Tag("http.host", "http.url")); trace.Record(Annotations.Tag("http.url", "http.url")); trace.Record(Annotations.Tag("http.path", "http.url")); } }
public async Task Invoke(HttpContext context) { var trace = Trace.Create(); Trace.Current = trace; using (var serverTrace = new ServerTrace(_serviceName, context.Request.Method)) { trace.Record(Annotations.Tag("http.host", context.Request.Host.ToString())); trace.Record(Annotations.Tag("http.path", context.Request.Path)); await serverTrace.TracedActionAsync(_next.Invoke(context)); } }
public override async Task Invoke(IOwinContext context) { var traceContext = traceExtractor.Extract(context.Request.Headers); var trace = traceContext == null?Trace.Create() : Trace.CreateFromId(traceContext); Trace.Current = trace; using (var serverTrace = new ServerTrace(this.serviceName, context.Request.Method)) { trace.Record(Annotations.Tag("http.host", context.Request.Host.Value)); trace.Record(Annotations.Tag("http.url", context.Request.Uri.AbsoluteUri)); trace.Record(Annotations.Tag("http.path", context.Request.Uri.AbsolutePath)); await serverTrace.TracedActionAsync(Next.Invoke(context)); } }
private void TraceStartWithNewInternal( string operationName, string operationMemberName, string operationFilePath) { var trace = Trace.Create(); Trace.Current = trace; var fileName = Path.GetFileNameWithoutExtension(operationFilePath); using (var serverTrace = new ServerTrace(AppInfoProvider.Service.Name, $"{fileName}/{operationMemberName}")) { //empty } TraceStartInternal(operationName, operationMemberName, operationFilePath); }
private static ServiceContext CreateServiceContext(IOptions <ServerOptions> options, ILoggerFactory loggerFactory) { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (loggerFactory == null) { throw new ArgumentNullException(nameof(loggerFactory)); } var serverOptions = options.Value ?? new ServerOptions(); var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Server.Kestrel"); var trace = new ServerTrace(logger); var systemClock = new SystemClock(); // TODO: This logic will eventually move into the IConnectionHandler<T> and off // the service context once we get to https://github.com/aspnet/KestrelHttpServer/issues/1662 PipeScheduler scheduler = null; switch (serverOptions.ApplicationSchedulingMode) { case SchedulingMode.Default: case SchedulingMode.ThreadPool: scheduler = PipeScheduler.ThreadPool; break; case SchedulingMode.Inline: scheduler = PipeScheduler.Inline; break; default: throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Unknown transport mode: '{0}'.", serverOptions.ApplicationSchedulingMode)); } return(new ServiceContext { Log = trace, Scheduler = scheduler, SystemClock = systemClock, ServerOptions = serverOptions }); }
public override async Task Invoke(IOwinContext context) { Trace trace; if (!this.traceExtractor.TryExtract(context.Request.Headers, (dic, k) => string.Join(",", dic[k]), out trace)) { trace = Trace.Create(); } Trace.Current = trace; using (var serverTrace = new ServerTrace(this.serviceName, context.Request.Method)) { trace.Record(Annotations.Tag("http.host", context.Request.Host.Value)); trace.Record(Annotations.Tag("http.url", context.Request.Uri.AbsoluteUri)); trace.Record(Annotations.Tag("http.path", context.Request.Uri.AbsolutePath)); await serverTrace.TracedActionAsync(Next.Invoke(context)); } }
public static void UseTracing(this IApplicationBuilder app, string serviceName) { var extractor = new ZipkinHttpTraceExtractor(); app.Use(async(context, next) => { Trace trace; var request = context.Request; if (!extractor.TryExtract(request.Headers, (c, key) => c[key], out trace)) { trace = Trace.Create(); } Trace.Current = trace; using (var serverTrace = new ServerTrace(serviceName, request.Method)) { trace.Record(Annotations.Tag("http.host", request.Host.ToString())); trace.Record(Annotations.Tag("http.uri", UriHelper.GetDisplayUrl(request))); trace.Record(Annotations.Tag("http.path", request.Path)); await serverTrace.TracedActionAsync(next()); } }); }
public void ShouldLogServerAnnotations() { // Arrange dispatcher .Setup(h => h.Dispatch(It.IsAny <Record>())) .Returns(true); // Act var trace = Trace.Create(); trace.ForceSampled(); Trace.Current = trace; using (var server = new ServerTrace(serviceName, rpc)) { // Assert dispatcher .Verify(h => h.Dispatch(It.Is <Record>(m => m.Annotation is ServerRecv))); dispatcher .Verify(h => h.Dispatch(It.Is <Record>(m => m.Annotation is ServiceName && ((ServiceName)m.Annotation).Service == serviceName))); dispatcher .Verify(h => h.Dispatch(It.Is <Record>(m => m.Annotation is Rpc && ((Rpc)m.Annotation).Name == rpc))); } // Assert dispatcher .Verify(h => h.Dispatch(It.Is <Record>(m => m.Annotation is ServerSend))); }