Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
BailunTrace
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
CI / CD
CI / CD
Pipelines
Schedules
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
wms
BailunTrace
Commits
c4f2bba5
Commit
c4f2bba5
authored
Apr 13, 2020
by
huangzhihong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix trace ignore
parent
95284a80
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
145 additions
and
23 deletions
+145
-23
Constants.cs
src/Bailun.ServiceFabric.Trace/Constants.cs
+85
-0
HostingDiagnosticProcessor.cs
...race/Diagnostics/AspNetCore/HostingDiagnosticProcessor.cs
+5
-6
HttpClientDiagnosticProcessor.cs
...e/Diagnostics/HttpClient/HttpClientDiagnosticProcessor.cs
+55
-17
No files found.
src/Bailun.ServiceFabric.Trace/Constants.cs
View file @
c4f2bba5
...
...
@@ -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"
;
}
}
}
src/Bailun.ServiceFabric.Trace/Diagnostics/AspNetCore/HostingDiagnosticProcessor.cs
View file @
c4f2bba5
...
...
@@ -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
();
_segmentC
ontext
=
_tracingContext
.
CreateEntrySegmentContext
(
httpContext
.
Request
.
Path
,
var
c
ontext
=
_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
=
_segmentContext
Accessor
.
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
);
_segmentContext
Accessor
.
Context
?.
Span
?.
ErrorOccurred
(
exception
);
}
[
DiagnosticName
(
"Microsoft.AspNetCore.Hosting.UnhandledException"
)]
public
void
HostingUnhandledException
([
Property
]
HttpContext
httpContext
,
[
Property
]
Exception
exception
)
{
_segmentContext
?.
Span
?.
ErrorOccurred
(
exception
);
_segmentContext
Accessor
.
Context
?.
Span
?.
ErrorOccurred
(
exception
);
}
//[DiagnosticName("Microsoft.AspNetCore.Mvc.BeforeAction")]
...
...
src/Bailun.ServiceFabric.Trace/Diagnostics/HttpClient/HttpClientDiagnosticProcessor.cs
View file @
c4f2bba5
...
...
@@ -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
;
}
_segmentC
ontext
=
_tracingContext
.
CreateExitSegmentContext
(
request
.
RequestUri
.
ToString
(),
var
c
ontext
=
_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
=
_
segment
Context
;
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment