Skip to content

Commit 1d33ee8

Browse files
author
yuqian.wjm
committed
code optimize
1 parent 29aa299 commit 1d33ee8

File tree

6 files changed

+239
-43
lines changed

6 files changed

+239
-43
lines changed

tracer-core/src/main/java/com/alipay/common/tracer/core/SofaTracer.java

Lines changed: 155 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
* SofaTracer
5353
*
5454
* @author yangguanchao
55-
* @since 2017/06/17
55+
* @since 2017 /06/17
5656
*/
5757
public class SofaTracer implements Tracer {
5858

@@ -90,6 +90,15 @@ public class SofaTracer implements Tracer {
9090
*/
9191
private final Sampler sampler;
9292

93+
/**
94+
* Instantiates a new Sofa tracer.
95+
*
96+
* @param tracerType the tracer type
97+
* @param clientReporter the client reporter
98+
* @param serverReporter the server reporter
99+
* @param sampler the sampler
100+
* @param tracerTags the tracer tags
101+
*/
93102
protected SofaTracer(String tracerType, Reporter clientReporter, Reporter serverReporter,
94103
Sampler sampler, Map<String, Object> tracerTags) {
95104
this.tracerType = tracerType;
@@ -103,6 +112,12 @@ protected SofaTracer(String tracerType, Reporter clientReporter, Reporter server
103112
}
104113
}
105114

115+
/**
116+
* Instantiates a new Sofa tracer.
117+
*
118+
* @param tracerType the tracer type
119+
* @param sampler the sampler
120+
*/
106121
protected SofaTracer(String tracerType, Sampler sampler) {
107122
this.tracerType = tracerType;
108123
this.clientReporter = null;
@@ -112,6 +127,17 @@ protected SofaTracer(String tracerType, Sampler sampler) {
112127
this.sampler = sampler;
113128
}
114129

130+
/**
131+
* Instantiates a new Sofa tracer.
132+
*
133+
* @param tracerType the tracer type
134+
* @param clientReporter the client reporter
135+
* @param serverReporter the server reporter
136+
* @param clientEventReporter the client event reporter
137+
* @param serverEventReporter the server event reporter
138+
* @param sampler the sampler
139+
* @param tracerTags the tracer tags
140+
*/
115141
protected SofaTracer(String tracerType, Reporter clientReporter, Reporter serverReporter,
116142
Reporter clientEventReporter, Reporter serverEventReporter,
117143
Sampler sampler, Map<String, Object> tracerTags) {
@@ -150,6 +176,11 @@ public <C> SpanContext extract(Format<C> format, C carrier) {
150176
return registryExtractor.extract(carrier);
151177
}
152178

179+
/**
180+
* Report span.
181+
*
182+
* @param span the span
183+
*/
153184
public void reportSpan(SofaTracerSpan span) {
154185
if (span == null) {
155186
return;
@@ -175,15 +206,20 @@ public void reportSpan(SofaTracerSpan span) {
175206
}
176207
}
177208

209+
/**
210+
* Report event.
211+
*
212+
* @param span the span
213+
*/
178214
public void reportEvent(SofaTracerSpan span) {
179215
if (span == null) {
180216
return;
181217
}
182-
// //sampler is support & current span is root span
218+
// sampler is support & current span is root span
183219
if (sampler != null && (span.isClient() && span.getParentSofaTracerSpan() == null)) {
184220
span.getSofaTracerSpanContext().setSampled(sampler.sample(span).isSampled());
185221
}
186-
//invoke listener
222+
// invoke listener
187223
this.invokeReportListeners(span);
188224
if (span.isClient()
189225
|| this.getTracerType().equalsIgnoreCase(ComponentNameConstants.FLEXIBLE)) {
@@ -195,7 +231,7 @@ public void reportEvent(SofaTracerSpan span) {
195231
this.serverEventReporter.report(span);
196232
}
197233
} else {
198-
//ignore ,do not statical
234+
// ignore ,do not statical
199235
SelfLog.warn("Span reported neither client nor server.Ignore!");
200236
}
201237
}
@@ -224,30 +260,65 @@ public void close() {
224260
}
225261
}
226262

263+
/**
264+
* Gets tracer type.
265+
*
266+
* @return the tracer type
267+
*/
227268
public String getTracerType() {
228269
return tracerType;
229270
}
230271

272+
/**
273+
* Gets client reporter.
274+
*
275+
* @return the client reporter
276+
*/
231277
public Reporter getClientReporter() {
232278
return clientReporter;
233279
}
234280

281+
/**
282+
* Gets server reporter.
283+
*
284+
* @return the server reporter
285+
*/
235286
public Reporter getServerReporter() {
236287
return serverReporter;
237288
}
238289

290+
/**
291+
* Gets client event reporter.
292+
*
293+
* @return the client event reporter
294+
*/
239295
public Reporter getClientEventReporter() {
240296
return clientEventReporter;
241297
}
242298

299+
/**
300+
* Gets server event reporter.
301+
*
302+
* @return the server event reporter
303+
*/
243304
public Reporter getServerEventReporter() {
244305
return serverEventReporter;
245306
}
246307

308+
/**
309+
* Gets sampler.
310+
*
311+
* @return the sampler
312+
*/
247313
public Sampler getSampler() {
248314
return sampler;
249315
}
250316

317+
/**
318+
* Gets tracer tags.
319+
*
320+
* @return the tracer tags
321+
*/
251322
public Map<String, Object> getTracerTags() {
252323
return tracerTags;
253324
}
@@ -257,6 +328,11 @@ public String toString() {
257328
return "SofaTracer{" + "tracerType='" + tracerType + '}';
258329
}
259330

331+
/**
332+
* Invoke report listeners.
333+
*
334+
* @param sofaTracerSpan the sofa tracer span
335+
*/
260336
protected void invokeReportListeners(SofaTracerSpan sofaTracerSpan) {
261337
List<SpanReportListener> listeners = SpanReportListenerHolder
262338
.getSpanReportListenersHolder();
@@ -287,6 +363,11 @@ public class SofaTracerSpanBuilder implements io.opentracing.Tracer.SpanBuilder
287363

288364
private final Map<String, Object> tags = new HashMap<>();
289365

366+
/**
367+
* Instantiates a new Sofa tracer span builder.
368+
*
369+
* @param operationName the operation name
370+
*/
290371
public SofaTracerSpanBuilder(String operationName) {
291372
this.operationName = operationName;
292373
}
@@ -455,6 +536,9 @@ private SofaTracerSpanContext preferredReference() {
455536
}
456537
}
457538

539+
/**
540+
* The type Builder.
541+
*/
458542
public static final class Builder {
459543

460544
private final String tracerType;
@@ -471,51 +555,113 @@ public static final class Builder {
471555

472556
private Sampler sampler;
473557

558+
/**
559+
* Instantiates a new Builder.
560+
*
561+
* @param tracerType the tracer type
562+
*/
474563
public Builder(String tracerType) {
475564
AssertUtils.isTrue(StringUtils.isNotBlank(tracerType), "tracerType must be not empty");
476565
this.tracerType = tracerType;
477566
}
478567

568+
/**
569+
* With client reporter builder.
570+
*
571+
* @param clientReporter the client reporter
572+
* @return the builder
573+
*/
479574
public Builder withClientReporter(Reporter clientReporter) {
480575
this.clientReporter = clientReporter;
481576
return this;
482577
}
483578

579+
/**
580+
* With server reporter builder.
581+
*
582+
* @param serverReporter the server reporter
583+
* @return the builder
584+
*/
484585
public Builder withServerReporter(Reporter serverReporter) {
485586
this.serverReporter = serverReporter;
486587
return this;
487588
}
488589

590+
/**
591+
* With client event reporter builder.
592+
*
593+
* @param clientEventReporter the client event reporter
594+
* @return the builder
595+
*/
489596
public Builder withClientEventReporter(Reporter clientEventReporter) {
490597
this.clientEventReporter = clientEventReporter;
491598
return this;
492599
}
493600

601+
/**
602+
* With server event reporter builder.
603+
*
604+
* @param serverEventReporter the server event reporter
605+
* @return the builder
606+
*/
494607
public Builder withServerEventReporter(Reporter serverEventReporter) {
495608
this.serverEventReporter = serverEventReporter;
496609
return this;
497610
}
498611

612+
/**
613+
* With sampler builder.
614+
*
615+
* @param sampler the sampler
616+
* @return the builder
617+
*/
499618
public Builder withSampler(Sampler sampler) {
500619
this.sampler = sampler;
501620
return this;
502621
}
503622

623+
/**
624+
* With tag builder.
625+
*
626+
* @param key the key
627+
* @param value the value
628+
* @return the builder
629+
*/
504630
public Builder withTag(String key, String value) {
505631
tracerTags.put(key, value);
506632
return this;
507633
}
508634

635+
/**
636+
* With tag builder.
637+
*
638+
* @param key the key
639+
* @param value the value
640+
* @return the builder
641+
*/
509642
public Builder withTag(String key, Boolean value) {
510643
tracerTags.put(key, value);
511644
return this;
512645
}
513646

647+
/**
648+
* With tag builder.
649+
*
650+
* @param key the key
651+
* @param value the value
652+
* @return the builder
653+
*/
514654
public Builder withTag(String key, Number value) {
515655
tracerTags.put(key, value);
516656
return this;
517657
}
518658

659+
/**
660+
* With tags builder.
661+
*
662+
* @param tags the tags
663+
* @return the builder
664+
*/
519665
public Builder withTags(Map<String, ?> tags) {
520666
if (tags == null || tags.size() <= 0) {
521667
return this;
@@ -543,6 +689,11 @@ public Builder withTags(Map<String, ?> tags) {
543689
return this;
544690
}
545691

692+
/**
693+
* Build sofa tracer.
694+
*
695+
* @return the sofa tracer
696+
*/
546697
public SofaTracer build() {
547698
try {
548699
sampler = SamplerFactory.getSampler();

tracer-core/src/main/java/com/alipay/common/tracer/core/constants/SofaTracerConstant.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,4 @@ public class SofaTracerConstant {
122122
public static final String STAT_FLAG_FAILS = DIGEST_FLAG_FAILS;
123123

124124
public static final String SPACE_ID = "sofa-tracer";
125-
126-
/**
127-
* The constant MAX_SPAN_EVENT_NUM.
128-
*/
129-
public static final int MAX_SPAN_EVENT_NUM = 100;
130125
}

0 commit comments

Comments
 (0)