-
Notifications
You must be signed in to change notification settings - Fork 576
/
OpentracingMockTracer.java
158 lines (137 loc) · 5.87 KB
/
OpentracingMockTracer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*******************************************************************************
* Copyright (c) 2017 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.ws.opentracing.mock;
import java.util.Map;
import io.opentracing.ActiveSpan;
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
import io.opentracing.mock.MockSpan;
import io.opentracing.mock.MockTracer;
import io.opentracing.mock.MockTracer.Propagator;
import io.opentracing.propagation.Format;
import io.opentracing.util.ThreadLocalActiveSpanSource;
/**
* <p>Open liberty mock tracer implementation.</p>
*
* <p>This tracer implementation provides two key functions:</p>
*
* <p>This tracer wraps a {@link MockTracer}, to which all tracer operations
* are forwarded.</p>
*
* <p>This tracer provides an implementation of {@link #toString} which places
* details of spans managed by the wrapped tracer. These details are provided
* through {@link #toString} so to provide access to span details without adding
* a new API.</p>
*/
public class OpentracingMockTracer implements Tracer {
/** <p>Mock tracer wrapped by this tracer.</p> */
private final MockTracer tracer;
/**
* <p>Answer the tracer wrapped by this mock tracer.</p>
*
* @return The traccer wrapped by this tracer.
*/
private MockTracer getTracer() {
return tracer;
}
/**
* <p>Create and return a new mock tracer.</p>
*
* <p>This implementation wraps an instance of {@link MockTracer}.</p>
*/
public OpentracingMockTracer() {
this.tracer = new MockTracer(new ThreadLocalActiveSpanSource(), Propagator.TEXT_MAP);
System.out.println("OpentracingMockTracer.<init>");
}
// Tracer pass-throughs.
//
// 'finishedSpans' is an operation on MockTracer, not on Tracer, and is not implemented as a
// pass through.
/** {@inheritDoc} */
@Override
public ActiveSpan activeSpan() {
ActiveSpan activeSpan = getTracer().activeSpan();
long threadId = Thread.currentThread().getId();
System.out.println("OpentracingMockTracer.activeSpan Thread = " + threadId + " Span = " + activeSpan);
return activeSpan;
}
/** {@inheritDoc} */
@Override
public ActiveSpan makeActive(Span span) {
long threadId = Thread.currentThread().getId();
System.out.println("OpentracingMockTracer.makeActive Thread = " + threadId + " Span = " + span);
ActiveSpan active = getTracer().makeActive(span);
System.out.println("OpentracingMockTracer.makeActive Thread = " + threadId + " ActiveSpan = " + active);
return active;
}
/** {@inheritDoc} */
@Override
public SpanBuilder buildSpan(String operationName) {
long threadId = Thread.currentThread().getId();
System.out.println("OpentracingMockTracer.buildSpan Thread = " + threadId + " OperationName = " + operationName);
return getTracer().buildSpan(operationName);
}
/** {@inheritDoc} */
@Override
public <C> SpanContext extract(Format<C> format, C carrier) {
long threadId = Thread.currentThread().getId();
System.out.println("OpentracingMockTracer.extract Thread = " + threadId);
return getTracer().extract(format, carrier);
}
/** {@inheritDoc} */
@Override
public <C> void inject(SpanContext spanContext, Format<C> format, C carrier) {
long threadId = Thread.currentThread().getId();
System.out.println("OpentracingMockTracer.inject Thread = " + threadId + " SpanContext = " + spanContext);
getTracer().inject(spanContext, format, carrier);
}
/**
* <p>Subclass re-implementation: Implement to answer a JSON formatted list of
* the finished spans of the wrapped tracer.</p>
*
* @return A print string for this mock tracer. This implementation
* emits a JSON formatted list of the finished spans.
*/
@Override
public String toString() {
StringBuilder result = new StringBuilder("{ ");
result.append("\"completedSpans\": [");
String elementPrefix = "\n ";
for ( MockSpan finishedSpan : getTracer().finishedSpans() ) {
long elapsedMicros = finishedSpan.finishMicros() - finishedSpan.startMicros();
result.append(elementPrefix);
result.append("{ ");
result.append("\"traceId\": \"" + finishedSpan.context().traceId() + "\", ");
result.append("\"parentId\": \"" + finishedSpan.parentId() + "\", ");
result.append("\"spanId\": \"" + finishedSpan.context().spanId() + "\", ");
result.append("\"operation\": \"" + finishedSpan.operationName() + "\", ");
result.append("\"start\": " + Long.toString(finishedSpan.startMicros()) + ", ");
result.append("\"finish\": " + Long.toString(finishedSpan.finishMicros()) + ", ");
result.append("\"elapsed\": " + Long.toString(elapsedMicros) + ", ");
result.append("\"tags\": {");
String tagPrefix = " ";
for ( Map.Entry<String, Object> tagEntry : finishedSpan.tags().entrySet() ) {
result.append(tagPrefix);
result.append("\"" + tagEntry.getKey() + "\": \"" + tagEntry.getValue() + "\"");
tagPrefix = ", ";
}
result.append(" }");
result.append(" }");
elementPrefix = ",\n ";
}
result.append(" ]");
result.append(" }");
return result.toString();
}
}