Commit c4f2bba5 by huangzhihong

fix trace ignore

parent 95284a80
......@@ -51,5 +51,90 @@ namespace Bailun.ServiceFabric.Trace
/// Http响应体
/// </summary>
public const string ResponseBodyPropertyName = "ResponseBody";
}
//
// 摘要:
// Specifies the media type information for an email message attachment.
public static class MediaTypeNames
{
//
// 摘要:
// Specifies the kind of application data in an email message attachment.
public static class Application
{
public const string Json = "application/json";
//
// 摘要:
// Specifies that the System.Net.Mime.MediaTypeNames.Application data is not interpreted.
public const string Octet = "application/octet-stream";
//
// 摘要:
// Specifies that the System.Net.Mime.MediaTypeNames.Application data is in Portable
// Document Format (PDF).
public const string Pdf = "application/pdf";
//
// 摘要:
// Specifies that the System.Net.Mime.MediaTypeNames.Application data is in Rich
// Text Format (RTF).
public const string Rtf = "application/rtf";
//
// 摘要:
// Specifies that the System.Net.Mime.MediaTypeNames.Application data is a SOAP
// document.
public const string Soap = "application/soap+xml";
public const string Xml = "application/xml";
//
// 摘要:
// Specifies that the System.Net.Mime.MediaTypeNames.Application data is compressed.
public const string Zip = "application/zip";
}
//
// 摘要:
// Specifies the type of image data in an email message attachment.
public static class Image
{
//
// 摘要:
// Specifies that the System.Net.Mime.MediaTypeNames.Image data is in Graphics Interchange
// Format (GIF).
public const string Gif = "image/gif";
//
// 摘要:
// Specifies that the System.Net.Mime.MediaTypeNames.Image data is in Joint Photographic
// Experts Group (JPEG) format.
public const string Jpeg = "image/jpeg";
//
// 摘要:
// Specifies that the System.Net.Mime.MediaTypeNames.Image data is in Tagged Image
// File Format (TIFF).
public const string Tiff = "image/tiff";
}
//
// 摘要:
// Specifies the type of text data in an email message attachment.
public static class Text
{
//
// 摘要:
// Specifies that the System.Net.Mime.MediaTypeNames.Text data is in HTML format.
public const string Html = "text/html";
//
// 摘要:
// Specifies that the System.Net.Mime.MediaTypeNames.Text data is in plain text
// format.
public const string Plain = "text/plain";
//
// 摘要:
// Specifies that the System.Net.Mime.MediaTypeNames.Text data is in Rich Text Format
// (RTF).
public const string RichText = "text/richtext";
//
// 摘要:
// Specifies that the System.Net.Mime.MediaTypeNames.Text data is in XML format.
public const string Xml = "text/xml";
}
}
}
......@@ -21,7 +21,6 @@ namespace Bailun.Diagnostics.AspNetCore
private readonly ITracingContext _tracingContext;
private readonly IEntrySegmentContextAccessor _segmentContextAccessor;
private SegmentContext _segmentContext;
public HostingTracingDiagnosticProcessor(IEntrySegmentContextAccessor segmentContextAccessor,
ITracingContext tracingContext)
......@@ -40,9 +39,8 @@ namespace Bailun.Diagnostics.AspNetCore
var requestBody = httpContext.Request.GetBodyString();
_segmentContext = _tracingContext.CreateEntrySegmentContext(httpContext.Request.Path,
var context = _tracingContext.CreateEntrySegmentContext(httpContext.Request.Path,
new HttpRequestCarrierHeaderCollection(httpContext.Request));
var context = _segmentContext;
if (!string.IsNullOrEmpty(requestBody))
context.Span.AddTag(TagsExtension.REQUEST, requestBody);
......@@ -63,7 +61,7 @@ namespace Bailun.Diagnostics.AspNetCore
[DiagnosticName("Microsoft.AspNetCore.Hosting.EndRequest")]
public void EndRequest([Property] HttpContext httpContext)
{
var context = _segmentContext;
var context = _segmentContextAccessor.Context;
if (context == null)
{
return;
......@@ -83,16 +81,17 @@ namespace Bailun.Diagnostics.AspNetCore
_tracingContext.Release(context);
}
[DiagnosticName("Microsoft.AspNetCore.Diagnostics.UnhandledException")]
public void DiagnosticUnhandledException([Property] HttpContext httpContext, [Property] Exception exception)
{
_segmentContext?.Span?.ErrorOccurred(exception);
_segmentContextAccessor.Context?.Span?.ErrorOccurred(exception);
}
[DiagnosticName("Microsoft.AspNetCore.Hosting.UnhandledException")]
public void HostingUnhandledException([Property] HttpContext httpContext, [Property] Exception exception)
{
_segmentContext?.Span?.ErrorOccurred(exception);
_segmentContextAccessor.Context?.Span?.ErrorOccurred(exception);
}
//[DiagnosticName("Microsoft.AspNetCore.Mvc.BeforeAction")]
......
......@@ -3,9 +3,11 @@ using Newtonsoft.Json;
using SkyApm;
using SkyApm.Common;
using SkyApm.Diagnostics;
using SkyApm.Logging;
using SkyApm.Tracing;
using SkyApm.Tracing.Segments;
using System;
using System.Linq;
using System.Net.Http;
namespace Bailun.Diagnostics.HttpClient
......@@ -14,16 +16,20 @@ namespace Bailun.Diagnostics.HttpClient
{
public string ListenerName { get; } = "HttpHandlerDiagnosticListener";
//private readonly IContextCarrierFactory _contextCarrierFactory;
private readonly ILogger _logger;
private readonly ILoggerFactory _loggerFactory;
private readonly ITracingContext _tracingContext;
private readonly IExitSegmentContextAccessor _contextAccessor;
private SegmentContext _segmentContext;
public HttpClientTracingDiagnosticProcessor(ITracingContext tracingContext,
IExitSegmentContextAccessor contextAccessor)
IExitSegmentContextAccessor contextAccessor,
ILoggerFactory loggerFactory)
{
_tracingContext = tracingContext;
_contextAccessor = contextAccessor;
_logger = loggerFactory.CreateLogger(typeof(HttpClientTracingDiagnosticProcessor));
_loggerFactory = loggerFactory;
}
[DiagnosticName("System.Net.Http.Request")]
......@@ -33,27 +39,30 @@ namespace Bailun.Diagnostics.HttpClient
{
return;
}
_segmentContext = _tracingContext.CreateExitSegmentContext(request.RequestUri.ToString(),
var context = _tracingContext.CreateExitSegmentContext(request.RequestUri.ToString(),
$"{request.RequestUri.Host}:{request.RequestUri.Port}",
new HttpClientICarrierHeaderCollection(request));
var context = _segmentContext;
if (request.Content != null)
{
var requestStr = request.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
context.Span.AddTag(TagsExtension.REQUEST, requestStr);
}
context.Span.SpanLayer = SpanLayer.HTTP;
context.Span.Component = Components.HTTPCLIENT;
context.Span.AddTag(Tags.URL, request.RequestUri.ToString());
context.Span.AddTag(Tags.HTTP_METHOD, request.Method.ToString());
context.Span.AddTag(TagsExtension.HEADERS, JsonConvert.SerializeObject(request.Headers));
string requestStr = ReadContentAsString(request.Content, true);
if (!string.IsNullOrEmpty(requestStr))
{
context.Span.AddTag(TagsExtension.REQUEST, requestStr);
}
}
[DiagnosticName("System.Net.Http.Response")]
public void HttpResponse([Property(Name = "Response")] HttpResponseMessage response)
{
var context = _segmentContext;
var context = _contextAccessor.Context;
if (context == null)
{
return;
......@@ -67,23 +76,51 @@ namespace Bailun.Diagnostics.HttpClient
context.Span.ErrorOccurred();
}
if (response.Content != null)
context.Span.AddTag(Tags.STATUS_CODE, statusCode);
string responseStr = ReadContentAsString(response.Content);
if (!string.IsNullOrEmpty(responseStr))
{
var responseStr = response.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
context.Span.AddTag(TagsExtension.RESPONSE, responseStr);
}
context.Span.AddTag(Tags.STATUS_CODE, statusCode);
}
_tracingContext.Release(context);
}
[DiagnosticName("System.Net.Http.Exception")]
public void HttpException([Property(Name = "Request")] HttpRequestMessage request,
[Property(Name = "Exception")] Exception exception)
{
_segmentContext?.Span?.ErrorOccurred(exception);
_contextAccessor.Context?.Span?.ErrorOccurred(exception);
}
private string ReadContentAsString(HttpContent content, bool isRequest = false)
{
try
{
if (content == null)
{
return string.Empty;
}
var includeMediaTypes = new string[] { MediaTypeNames.Application.Json, MediaTypeNames.Application.Xml };
var mediaType = content.Headers?.ContentType?.MediaType;
if (includeMediaTypes.Contains(mediaType))
{
return content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
}
else
{
return string.Empty;
}
}
catch (Exception ex)
{
_logger.Error($"获取{(isRequest ? "请求" : "响应")}报文异常", ex);
return string.Empty;
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment