forked from pinpoint-apm/pinpoint
-
Notifications
You must be signed in to change notification settings - Fork 2
/
DefaultBaseTraceFactory.java
262 lines (212 loc) · 12.7 KB
/
DefaultBaseTraceFactory.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Copyright 2018 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.profiler.context;
import com.navercorp.pinpoint.bootstrap.context.AsyncState;
import com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder;
import com.navercorp.pinpoint.bootstrap.context.SpanRecorder;
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceId;
import com.navercorp.pinpoint.bootstrap.sampler.TraceSampler;
import com.navercorp.pinpoint.common.annotations.InterfaceAudience;
import com.navercorp.pinpoint.profiler.context.active.ActiveTraceHandle;
import com.navercorp.pinpoint.profiler.context.active.ActiveTraceRepository;
import com.navercorp.pinpoint.profiler.context.id.ListenableAsyncState;
import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;
import com.navercorp.pinpoint.profiler.context.id.TraceRootFactory;
import com.navercorp.pinpoint.profiler.context.recorder.RecorderFactory;
import com.navercorp.pinpoint.profiler.context.recorder.WrappedSpanEventRecorder;
import com.navercorp.pinpoint.profiler.context.storage.Storage;
import com.navercorp.pinpoint.profiler.context.storage.StorageFactory;
import com.navercorp.pinpoint.profiler.context.storage.UriStatStorage;
import java.util.Objects;
/**
* @author emeroad
* @author Taejin Koo
*/
public class DefaultBaseTraceFactory implements BaseTraceFactory {
private final CallStackFactory<SpanEvent> callStackFactory;
private final StorageFactory storageFactory;
private final TraceSampler traceSampler;
private final SpanFactory spanFactory;
private final RecorderFactory recorderFactory;
private final TraceRootFactory traceRootFactory;
private final ActiveTraceRepository activeTraceRepository;
private final UriStatStorage uriStatStorage;
public DefaultBaseTraceFactory(TraceRootFactory traceRootFactory,
CallStackFactory<SpanEvent> callStackFactory,
StorageFactory storageFactory,
TraceSampler traceSampler,
SpanFactory spanFactory, RecorderFactory recorderFactory,
ActiveTraceRepository activeTraceRepository,
UriStatStorage uriStatStorage) {
this.traceRootFactory = Objects.requireNonNull(traceRootFactory, "traceRootFactory");
this.callStackFactory = Objects.requireNonNull(callStackFactory, "callStackFactory");
this.storageFactory = Objects.requireNonNull(storageFactory, "storageFactory");
this.traceSampler = Objects.requireNonNull(traceSampler, "traceSampler");
this.spanFactory = Objects.requireNonNull(spanFactory, "spanFactory");
this.recorderFactory = Objects.requireNonNull(recorderFactory, "recorderFactory");
this.activeTraceRepository = Objects.requireNonNull(activeTraceRepository, "activeTraceRepository");
this.uriStatStorage = Objects.requireNonNull(uriStatStorage, "uriStatStorage");
}
// continue to trace the request that has been determined to be sampled on previous nodes
@Override
public Trace continueTraceObject(final TraceId traceId) {
// TODO need to modify how to bind a datasender
// always set true because the decision of sampling has been made on previous nodes
// TODO need to consider as a target to sample in case Trace object has a sampling flag (true) marked on previous node.
// Check max throughput(permits per seconds)
final TraceSampler.State state = traceSampler.isContinueSampled();
final boolean sampling = state.isSampled();
if (sampling) {
final TraceRoot traceRoot = traceRootFactory.continueTraceRoot(traceId, state.nextId());
final Span span = spanFactory.newSpan(traceRoot);
final SpanChunkFactory spanChunkFactory = new DefaultSpanChunkFactory(traceRoot);
final Storage storage = storageFactory.createStorage(spanChunkFactory);
final CallStack<SpanEvent> callStack = callStackFactory.newCallStack();
final SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span, traceId.isRoot(), sampling);
final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot);
final ActiveTraceHandle handle = registerActiveTrace(traceRoot);
return new DefaultTrace(span, callStack, storage, sampling, spanRecorder,
wrappedSpanEventRecorder, handle, uriStatStorage);
} else {
return newLocalTrace(state.nextId());
}
}
private ActiveTraceHandle registerActiveTrace(TraceRoot traceRoot) {
return activeTraceRepository.register(traceRoot);
}
private ActiveTraceHandle registerActiveTrace(long localTransactionId, long startTime, long threadId) {
return activeTraceRepository.register(localTransactionId, startTime, threadId);
}
@Override
public Trace newTraceObject() {
// TODO need to modify how to inject a datasender
final TraceSampler.State state = traceSampler.isNewSampled();
return newTraceObject(state);
}
@Override
public Trace newTraceObject(String urlPath) {
final TraceSampler.State state = traceSampler.isNewSampled(urlPath);
return newTraceObject(state);
}
Trace newTraceObject(TraceSampler.State state) {
final boolean sampling = state.isSampled();
if (sampling) {
final TraceRoot traceRoot = traceRootFactory.newTraceRoot(state.nextId());
final Span span = spanFactory.newSpan(traceRoot);
final SpanChunkFactory spanChunkFactory = new DefaultSpanChunkFactory(traceRoot);
final Storage storage = storageFactory.createStorage(spanChunkFactory);
final CallStack<SpanEvent> callStack = callStackFactory.newCallStack();
final TraceId traceId = traceRoot.getTraceId();
final SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span, traceId.isRoot(), sampling);
final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot);
final ActiveTraceHandle handle = registerActiveTrace(traceRoot);
return new DefaultTrace(span, callStack, storage, sampling, spanRecorder, wrappedSpanEventRecorder,
handle, uriStatStorage);
} else {
return newLocalTrace(state.nextId());
}
}
// internal async trace.
@Override
public Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId) {
final boolean sampling = true;
final SpanChunkFactory spanChunkFactory = new AsyncSpanChunkFactory(traceRoot, localAsyncId);
final Storage storage = storageFactory.createStorage(spanChunkFactory);
final CallStack<SpanEvent> callStack = callStackFactory.newCallStack();
final SpanRecorder spanRecorder = recorderFactory.newTraceRootSpanRecorder(traceRoot, sampling);
final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot);
return new AsyncChildTrace(traceRoot, callStack, storage, sampling, spanRecorder, wrappedSpanEventRecorder, localAsyncId);
}
@Override
public Trace continueDisableAsyncContextTraceObject(LocalTraceRoot traceRoot) {
final AsyncState asyncState = new DisableAsyncState();
SpanRecorder spanRecorder = recorderFactory.newDisableSpanRecorder(traceRoot);
SpanEventRecorder spanEventRecorder = recorderFactory.newDisableSpanEventRecorder(traceRoot, asyncState);
return new DisableAsyncChildTrace(traceRoot, spanRecorder, spanEventRecorder);
}
// entry point async trace.
@InterfaceAudience.LimitedPrivate("vert.x")
@Override
public Trace continueAsyncTraceObject(final TraceId traceId) {
final TraceSampler.State state = traceSampler.isContinueSampled();
final boolean sampling = state.isSampled();
if (sampling) {
final TraceRoot traceRoot = traceRootFactory.continueTraceRoot(traceId, state.nextId());
final Span span = spanFactory.newSpan(traceRoot);
final SpanChunkFactory spanChunkFactory = new DefaultSpanChunkFactory(traceRoot);
final Storage storage = storageFactory.createStorage(spanChunkFactory);
final CallStack<SpanEvent> callStack = callStackFactory.newCallStack();
final ActiveTraceHandle handle = registerActiveTrace(traceRoot);
final ListenableAsyncState.AsyncStateListener asyncStateListener = new SpanAsyncStateListener(span, storageFactory);
final AsyncState asyncState = new ListenableAsyncState(traceRoot, asyncStateListener, handle, uriStatStorage);
final SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span, traceId.isRoot(), sampling);
final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot, asyncState);
final DefaultTrace trace = new DefaultTrace(span, callStack, storage, sampling, spanRecorder, wrappedSpanEventRecorder);
return new AsyncTrace(traceRoot, trace, asyncState);
} else {
return newLocalTrace(state.nextId());
}
}
// entry point async trace.
@InterfaceAudience.LimitedPrivate("vert.x")
@Override
public Trace newAsyncTraceObject() {
final TraceSampler.State state = traceSampler.isNewSampled();
return newAsyncTraceObject(state);
}
@Override
public Trace newAsyncTraceObject(String urlPath) {
final TraceSampler.State state = traceSampler.isNewSampled(urlPath);
return newAsyncTraceObject(state);
}
Trace newAsyncTraceObject(TraceSampler.State state) {
final boolean sampling = state.isSampled();
if (sampling) {
final TraceRoot traceRoot = traceRootFactory.newTraceRoot(state.nextId());
final Span span = spanFactory.newSpan(traceRoot);
final SpanChunkFactory spanChunkFactory = new DefaultSpanChunkFactory(traceRoot);
final Storage storage = storageFactory.createStorage(spanChunkFactory);
final CallStack<SpanEvent> callStack = callStackFactory.newCallStack();
final ActiveTraceHandle handle = registerActiveTrace(traceRoot);
final ListenableAsyncState.AsyncStateListener asyncStateListener = new SpanAsyncStateListener(span, storageFactory);
final AsyncState asyncState = new ListenableAsyncState(traceRoot, asyncStateListener, handle, uriStatStorage);
final TraceId traceId = traceRoot.getTraceId();
final SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span, traceId.isRoot(), sampling);
final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot, asyncState);
final DefaultTrace trace = new DefaultTrace(span, callStack, storage, sampling, spanRecorder, wrappedSpanEventRecorder);
return new AsyncTrace(traceRoot, trace, asyncState);
} else {
return newLocalTrace(state.nextId());
}
}
@Override
public Trace disableSampling() {
final TraceSampler.State state = traceSampler.getContinueDisableState();
final long nextContinuedDisabledId = state.nextId();
return newLocalTrace(nextContinuedDisabledId);
}
private Trace newLocalTrace(long nextDisabledId) {
final LocalTraceRoot traceRoot = traceRootFactory.newDisableTraceRoot(nextDisabledId);
final SpanRecorder spanRecorder = recorderFactory.newDisableSpanRecorder(traceRoot);
final SpanEventRecorder spanEventRecorder = recorderFactory.newDisableSpanEventRecorder(traceRoot, new DisableAsyncState());
final long traceStartTime = traceRoot.getTraceStartTime();
final long threadId = Thread.currentThread().getId();
final ActiveTraceHandle activeTraceHandle = registerActiveTrace(nextDisabledId, traceStartTime, threadId);
return new DisableTrace(traceRoot, spanRecorder, spanEventRecorder, activeTraceHandle, uriStatStorage);
}
}