Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
commons
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
3
Merge Requests
3
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DevOps
commons
Commits
9b8246e4
Commit
9b8246e4
authored
May 14, 2018
by
xiaoguang.xu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 增加对exported 日志的输出. 修改traceId,spanId为 X-B3-TraceId,X-B3-SpanId
parent
45629ff0
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
127 additions
and
6 deletions
+127
-6
pom.xml
brave-spring-boot-starter/pom.xml
+0
-5
BraveAutoConfiguration.java
...roup/tech/brave/configuration/BraveAutoConfiguration.java
+1
-1
MDCCurrentTraceContext.java
...n/quantgroup/tech/brave/slf4j/MDCCurrentTraceContext.java
+126
-0
No files found.
brave-spring-boot-starter/pom.xml
View file @
9b8246e4
...
...
@@ -87,11 +87,6 @@
<artifactId>
brave-instrumentation-spring-rabbit
</artifactId>
<version>
${brave.version}
</version>
</dependency>
<dependency>
<groupId>
io.zipkin.brave
</groupId>
<artifactId>
brave-context-slf4j
</artifactId>
<version>
${brave.version}
</version>
</dependency>
</dependencies>
...
...
brave-spring-boot-starter/src/main/java/cn/quantgroup/tech/brave/configuration/BraveAutoConfiguration.java
View file @
9b8246e4
package
cn
.
quantgroup
.
tech
.
brave
.
configuration
;
import
brave.Tracing
;
import
brave.context.slf4j.MDCCurrentTraceContext
;
import
brave.http.HttpTracing
;
import
brave.httpclient.TracingHttpClientBuilder
;
import
brave.okhttp3.TracingInterceptor
;
...
...
@@ -15,6 +14,7 @@ import cn.quantgroup.tech.brave.properties.ServiceProperties;
import
cn.quantgroup.tech.brave.service.ITechRabbitBuilder
;
import
cn.quantgroup.tech.brave.service.TechRabbitBuilderNoTrace
;
import
cn.quantgroup.tech.brave.service.TechRabbitBuilderTrace
;
import
cn.quantgroup.tech.brave.slf4j.MDCCurrentTraceContext
;
import
lombok.extern.slf4j.Slf4j
;
import
okhttp3.Dispatcher
;
import
okhttp3.OkHttpClient
;
...
...
brave-spring-boot-starter/src/main/java/cn/quantgroup/tech/brave/slf4j/MDCCurrentTraceContext.java
0 → 100644
View file @
9b8246e4
package
cn
.
quantgroup
.
tech
.
brave
.
slf4j
;
import
brave.internal.HexCodec
;
import
brave.internal.Nullable
;
import
brave.propagation.CurrentTraceContext
;
import
brave.propagation.TraceContext
;
import
org.slf4j.MDC
;
import
static
brave
.
internal
.
HexCodec
.
lowerHexEqualsTraceId
;
import
static
brave
.
internal
.
HexCodec
.
lowerHexEqualsUnsignedLong
;
/**
* Adds {@linkplain MDC} properties TRACE_ID, PARENT_ID and SPAN_ID when a {@link
* brave.Tracer#currentSpan() span is current}. These can be used in log correlation.
*/
public
final
class
MDCCurrentTraceContext
extends
CurrentTraceContext
{
private
static
final
String
TRACE_ID
=
"X-B3-TraceId"
;
private
static
final
String
SPAN_ID
=
"X-B3-SpanId"
;
private
static
final
String
PARENT_ID
=
"X-B3-ParentId"
;
private
static
final
String
EXPORTABLE
=
"X-Span-Export"
;
public
static
MDCCurrentTraceContext
create
()
{
return
create
(
CurrentTraceContext
.
Default
.
inheritable
());
}
public
static
MDCCurrentTraceContext
create
(
CurrentTraceContext
delegate
)
{
return
new
MDCCurrentTraceContext
(
delegate
);
}
final
CurrentTraceContext
delegate
;
MDCCurrentTraceContext
(
CurrentTraceContext
delegate
)
{
if
(
delegate
==
null
)
throw
new
NullPointerException
(
"delegate == null"
);
this
.
delegate
=
delegate
;
}
@Override
public
TraceContext
get
()
{
return
delegate
.
get
();
}
@Override
public
Scope
newScope
(
@Nullable
TraceContext
currentSpan
)
{
return
newScope
(
currentSpan
,
MDC
.
get
(
TRACE_ID
),
MDC
.
get
(
SPAN_ID
),
MDC
.
get
(
EXPORTABLE
));
}
@Override
public
Scope
maybeScope
(
@Nullable
TraceContext
currentSpan
)
{
String
previousTraceId
=
MDC
.
get
(
TRACE_ID
);
String
previousSpanId
=
MDC
.
get
(
SPAN_ID
);
String
sampled
=
MDC
.
get
(
EXPORTABLE
);
if
(
currentSpan
==
null
)
{
if
(
previousTraceId
==
null
)
{
return
Scope
.
NOOP
;
}
return
newScope
(
null
,
previousTraceId
,
previousSpanId
,
sampled
);
}
if
(
lowerHexEqualsTraceId
(
previousTraceId
,
currentSpan
)
&&
lowerHexEqualsUnsignedLong
(
previousSpanId
,
currentSpan
.
spanId
()))
{
return
Scope
.
NOOP
;
}
return
newScope
(
currentSpan
,
previousTraceId
,
previousSpanId
,
sampled
);
}
// all input parameters are nullable
Scope
newScope
(
TraceContext
currentSpan
,
String
previousTraceId
,
String
previousSpanId
,
String
sampled
)
{
String
previousParentId
=
MDC
.
get
(
PARENT_ID
);
if
(
currentSpan
!=
null
)
{
maybeReplaceTraceContext
(
currentSpan
,
previousTraceId
,
previousParentId
,
previousSpanId
,
sampled
);
}
else
{
MDC
.
remove
(
TRACE_ID
);
MDC
.
remove
(
PARENT_ID
);
MDC
.
remove
(
SPAN_ID
);
MDC
.
remove
(
EXPORTABLE
);
}
Scope
scope
=
delegate
.
newScope
(
currentSpan
);
class
MDCCurrentTraceContextScope
implements
Scope
{
@Override
public
void
close
()
{
scope
.
close
();
replace
(
TRACE_ID
,
previousTraceId
);
replace
(
PARENT_ID
,
previousParentId
);
replace
(
SPAN_ID
,
previousSpanId
);
//true = 采样了. false = 未采样. null = 决定不了...等会再说
replace
(
EXPORTABLE
,
sampled
);
}
}
return
new
MDCCurrentTraceContextScope
();
}
void
maybeReplaceTraceContext
(
TraceContext
currentSpan
,
String
previousTraceId
,
@Nullable
String
previousParentId
,
String
previousSpanId
,
@Nullable
String
sampled
)
{
MDC
.
put
(
EXPORTABLE
,
String
.
valueOf
(
currentSpan
.
sampled
()));
boolean
sameTraceId
=
lowerHexEqualsTraceId
(
previousTraceId
,
currentSpan
);
if
(!
sameTraceId
)
{
MDC
.
put
(
TRACE_ID
,
currentSpan
.
traceIdString
());
}
long
parentId
=
currentSpan
.
parentIdAsLong
();
if
(
parentId
==
0L
)
{
MDC
.
remove
(
PARENT_ID
);
}
else
{
boolean
sameParentId
=
lowerHexEqualsUnsignedLong
(
previousParentId
,
parentId
);
if
(!
sameParentId
)
MDC
.
put
(
PARENT_ID
,
HexCodec
.
toLowerHex
(
parentId
));
}
boolean
sameSpanId
=
lowerHexEqualsUnsignedLong
(
previousSpanId
,
currentSpan
.
spanId
());
if
(!
sameSpanId
)
MDC
.
put
(
SPAN_ID
,
HexCodec
.
toLowerHex
(
currentSpan
.
spanId
()));
}
static
void
replace
(
String
key
,
@Nullable
String
value
)
{
if
(
value
!=
null
)
{
MDC
.
put
(
key
,
value
);
}
else
{
MDC
.
remove
(
key
);
}
}
}
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