40
40
import java .util .logging .Logger ;
41
41
import java .util .stream .Collectors ;
42
42
43
+ import org .mapstruct .factory .Mappers ;
44
+
43
45
import io .grpc .NameResolverProvider ;
44
46
import io .opentelemetry .api .common .Attributes ;
45
47
import io .opentelemetry .api .internal .OtelEncodingUtils ;
55
57
56
58
import com .navercorp .pinpoint .bootstrap .context .SpanId ;
57
59
import com .navercorp .pinpoint .bootstrap .context .TraceId ;
60
+ import com .navercorp .pinpoint .common .profiler .util .TransactionIdUtils ;
58
61
import com .navercorp .pinpoint .common .trace .AnnotationKey ;
59
62
import com .navercorp .pinpoint .common .trace .ServiceType ;
60
63
import com .navercorp .pinpoint .common .util .JvmUtils ;
67
70
import com .navercorp .pinpoint .profiler .AgentInfoSender ;
68
71
import com .navercorp .pinpoint .profiler .JvmInformation ;
69
72
import com .navercorp .pinpoint .profiler .context .DefaultServerMetaDataRegistryService ;
73
+ import com .navercorp .pinpoint .profiler .context .DefaultSpanFactory ;
70
74
import com .navercorp .pinpoint .profiler .context .ServerMetaDataRegistryService ;
71
75
import com .navercorp .pinpoint .profiler .context .Span ;
72
76
import com .navercorp .pinpoint .profiler .context .SpanEvent ;
75
79
import com .navercorp .pinpoint .profiler .context .grpc .GrpcAgentInfoMessageConverter ;
76
80
import com .navercorp .pinpoint .profiler .context .grpc .GrpcSpanMessageConverter ;
77
81
import com .navercorp .pinpoint .profiler .context .grpc .config .GrpcTransportConfig ;
78
- import com .navercorp .pinpoint .profiler .context .id .DefaultTraceId ;
79
- import com .navercorp .pinpoint .profiler .context .id .DefaultTraceRoot ;
82
+ import com .navercorp .pinpoint .profiler .context .grpc .config .SpanAutoUriGetter ;
83
+ import com .navercorp .pinpoint .profiler .context .grpc .config .SpanUriGetter ;
84
+ import com .navercorp .pinpoint .profiler .context .grpc .mapper .AgentInfoMapper ;
85
+ import com .navercorp .pinpoint .profiler .context .grpc .mapper .AgentInfoMapperImpl ;
86
+ import com .navercorp .pinpoint .profiler .context .grpc .mapper .AnnotationValueMapper ;
87
+ import com .navercorp .pinpoint .profiler .context .grpc .mapper .JvmGcTypeMapper ;
88
+ import com .navercorp .pinpoint .profiler .context .grpc .mapper .JvmGcTypeMapperImpl ;
89
+ import com .navercorp .pinpoint .profiler .context .grpc .mapper .SpanMessageMapper ;
90
+ import com .navercorp .pinpoint .profiler .context .grpc .mapper .SpanMessageMapperImpl ;
91
+ import com .navercorp .pinpoint .profiler .context .id .DefaultTraceIdFactory ;
92
+ import com .navercorp .pinpoint .profiler .context .id .DefaultTraceRootFactory ;
93
+ import com .navercorp .pinpoint .profiler .context .id .TraceIdFactory ;
80
94
import com .navercorp .pinpoint .profiler .context .id .TraceRoot ;
95
+ import com .navercorp .pinpoint .profiler .context .id .TraceRootFactory ;
81
96
import com .navercorp .pinpoint .profiler .context .provider .AgentInformationProvider ;
82
97
import com .navercorp .pinpoint .profiler .context .provider .grpc .DnsExecutorServiceProvider ;
83
98
import com .navercorp .pinpoint .profiler .context .provider .grpc .GrpcNameResolverProvider ;
99
+ import com .navercorp .pinpoint .profiler .context .provider .grpc .SSLContextProvider ;
84
100
import com .navercorp .pinpoint .profiler .metadata .MetaDataType ;
85
101
import com .navercorp .pinpoint .profiler .monitor .metric .gc .JvmGcType ;
86
102
import com .navercorp .pinpoint .profiler .receiver .ProfilerCommandLocatorBuilder ;
@@ -117,29 +133,43 @@ public final class PinpointSpanExporter implements SpanExporter {
117
133
private final String applicationName ;
118
134
119
135
private final GrpcTransportConfig grpcTransportConfig ;
136
+ private final SSLContextProvider sslContextProvider ;
120
137
121
138
private final HeaderFactory headerFactory ;
122
139
123
140
private final AgentInfoSender agentInfoSender ;
124
141
125
142
private final SpanGrpcDataSender spanGrpcDataSender ;
126
143
144
+ private final JvmGcTypeMapper jvmGcTypeMapper = new JvmGcTypeMapperImpl ();
145
+ private final AgentInfoMapper agentInfoMapper = new AgentInfoMapperImpl (jvmGcTypeMapper );
146
+
147
+ private final SpanUriGetter spanUriGetter = new SpanAutoUriGetter ();
148
+ private final AnnotationValueMapper annotationValueMapper = Mappers .getMapper (AnnotationValueMapper .class );
149
+ private final SpanMessageMapper spanMessageMapper = new SpanMessageMapperImpl (annotationValueMapper , spanUriGetter );
150
+
127
151
public PinpointSpanExporter (final String agentId ,
128
- final String agentName ,
129
- final String applicationName ,
130
- final GrpcTransportConfig grpcTransportConfig ) {
152
+ final String agentName ,
153
+ final String applicationName ,
154
+ final GrpcTransportConfig grpcTransportConfig ) {
131
155
132
156
this .agentId = Objects .requireNonNull (agentId , "agentId cannot be null" );
133
157
this .agentName = Objects .requireNonNull (agentName , "agentName cannot be null" );
134
158
this .applicationName = Objects .requireNonNull (applicationName , "applicationName cannot be null" );
135
159
this .grpcTransportConfig = Objects .requireNonNull (grpcTransportConfig , "grpcTransportConfig cannot be null" );
136
160
161
+ if (grpcTransportConfig .getSslOption () != null ) {
162
+ this .sslContextProvider = new SSLContextProvider (grpcTransportConfig );
163
+ } else {
164
+ this .sslContextProvider = null ;
165
+ }
166
+
137
167
this .headerFactory = new AgentHeaderFactory (
138
- agentId ,
139
- agentName ,
140
- applicationName ,
141
- ServiceType .UNDEFINED .getCode (),
142
- agentStartTime );
168
+ agentId ,
169
+ agentName ,
170
+ applicationName ,
171
+ ServiceType .UNDEFINED .getCode (),
172
+ agentStartTime );
143
173
144
174
this .agentInfoSender = createAgentInfoSender ();
145
175
this .agentInfoSender .start ();
@@ -155,7 +185,7 @@ private AgentInfoSender createAgentInfoSender() {
155
185
grpcTransportConfig .getAgentCollectorIp (),
156
186
grpcTransportConfig .getAgentCollectorPort (),
157
187
grpcTransportConfig .getAgentSenderExecutorQueueSize (),
158
- new GrpcAgentInfoMessageConverter (),
188
+ new GrpcAgentInfoMessageConverter (agentInfoMapper ),
159
189
reconnectExecutor ,
160
190
scheduledExecutorService ,
161
191
agentChannelFactory ,
@@ -171,17 +201,17 @@ private AgentInfoSender createAgentInfoSender() {
171
201
ServiceType .STAND_ALONE );
172
202
173
203
final JvmInformation jvmInformation = new JvmInformation (
174
- JvmUtils .getSystemProperty (SystemPropertyKey .JAVA_VERSION ),
175
- JvmGcType .UNKNOWN );
204
+ JvmUtils .getSystemProperty (SystemPropertyKey .JAVA_VERSION ),
205
+ JvmGcType .UNKNOWN );
176
206
177
207
final ServerMetaDataRegistryService serverMetaDataRegistryService = new DefaultServerMetaDataRegistryService (
178
- Collections .emptyList ());
208
+ Collections .emptyList ());
179
209
serverMetaDataRegistryService .setServerName (EventMeshTraceConstants .SERVICE_NAME );
180
210
181
211
final AgentInfoFactory agentInfoFactory = new AgentInfoFactory (
182
- agentInformationProvider .createAgentInformation (),
183
- serverMetaDataRegistryService ,
184
- jvmInformation );
212
+ agentInformationProvider .createAgentInformation (),
213
+ serverMetaDataRegistryService ,
214
+ jvmInformation );
185
215
186
216
return new AgentInfoSender .Builder (agentGrpcDataSender , agentInfoFactory ).build ();
187
217
}
@@ -193,41 +223,45 @@ private SpanGrpcDataSender createSpanGrpcDataSender() {
193
223
new GrpcSpanMessageConverter (
194
224
agentId ,
195
225
ServiceType .STAND_ALONE .getCode (),
196
- new GrpcSpanProcessorV2 ());
226
+ new GrpcSpanProcessorV2 (),
227
+ this .spanMessageMapper );
197
228
198
229
final StreamState streamState =
199
230
new SimpleStreamState (
200
231
grpcTransportConfig .getSpanClientOption ().getLimitCount (),
201
232
grpcTransportConfig .getSpanClientOption ().getLimitTime ());
202
233
203
234
return new SpanGrpcDataSender (
204
- grpcTransportConfig .getSpanCollectorIp (),
205
- grpcTransportConfig .getSpanCollectorPort (),
206
- grpcTransportConfig .getSpanSenderExecutorQueueSize (),
207
- messageConverter ,
208
- reconnectExecutor ,
209
- spanChannelFactory ,
210
- streamState );
235
+ grpcTransportConfig .getSpanCollectorIp (),
236
+ grpcTransportConfig .getSpanCollectorPort (),
237
+ grpcTransportConfig .getSpanSenderExecutorQueueSize (),
238
+ messageConverter ,
239
+ reconnectExecutor ,
240
+ spanChannelFactory ,
241
+ streamState ,
242
+ grpcTransportConfig .getSpanRpcMaxAgeMillis ());
211
243
}
212
244
213
245
private ChannelFactory createAgentChannelFactory () {
214
- final ChannelFactoryBuilder channelFactoryBuilder =
215
- new DefaultChannelFactoryBuilder (AGENT_CHANNEL_FACTORY );
246
+ final ChannelFactoryBuilder channelFactoryBuilder = new DefaultChannelFactoryBuilder (AGENT_CHANNEL_FACTORY );
216
247
channelFactoryBuilder .setHeaderFactory (headerFactory );
217
248
channelFactoryBuilder .setNameResolverProvider (nameResolverProvider );
218
- channelFactoryBuilder .setSslOption (grpcTransportConfig .getSslOption ());
249
+ if (this .sslContextProvider != null ) {
250
+ channelFactoryBuilder .setSslContext (this .sslContextProvider .get ());
251
+ }
219
252
channelFactoryBuilder .setClientOption (grpcTransportConfig .getAgentClientOption ());
220
253
channelFactoryBuilder .setExecutorQueueSize (grpcTransportConfig .getAgentChannelExecutorQueueSize ());
221
254
222
255
return channelFactoryBuilder .build ();
223
256
}
224
257
225
258
private ChannelFactory createSpanChannelFactory () {
226
- final ChannelFactoryBuilder channelFactoryBuilder =
227
- new DefaultChannelFactoryBuilder (SPAN_CHANNEL_FACTORY );
259
+ final ChannelFactoryBuilder channelFactoryBuilder = new DefaultChannelFactoryBuilder (SPAN_CHANNEL_FACTORY );
228
260
channelFactoryBuilder .setHeaderFactory (headerFactory );
229
261
channelFactoryBuilder .setNameResolverProvider (nameResolverProvider );
230
- channelFactoryBuilder .setSslOption (grpcTransportConfig .getSslOption ());
262
+ if (this .sslContextProvider != null ) {
263
+ channelFactoryBuilder .setSslContext (this .sslContextProvider .get ());
264
+ }
231
265
channelFactoryBuilder .setClientOption (grpcTransportConfig .getSpanClientOption ());
232
266
channelFactoryBuilder .setExecutorQueueSize (grpcTransportConfig .getSpanChannelExecutorQueueSize ());
233
267
@@ -289,12 +323,21 @@ private Span toSpan(final SpanData spanData) {
289
323
}
290
324
});
291
325
292
- final TraceId traceId = new DefaultTraceId (agentId , startTimestamp , transactionId , parentSpanId [0 ], spanId ,
293
- (short ) spanData .getKind ().ordinal ());
294
-
295
- final TraceRoot traceRoot = new DefaultTraceRoot (traceId , this .agentId , startTimestamp , transactionId );
326
+ final TraceIdFactory traceIdFactory = new DefaultTraceIdFactory (this .agentId , startTimestamp );
327
+ final TraceRootFactory traceRootFactory = new DefaultTraceRootFactory (this .agentId , traceIdFactory );
328
+
329
+ final TraceRoot traceRoot ;
330
+ if (parentSpanId [0 ] == SpanId .NULL ) {
331
+ traceRoot = traceRootFactory .newTraceRoot (transactionId );
332
+ } else {
333
+ final TraceId traceId = traceIdFactory .continueTraceId (
334
+ TransactionIdUtils .formatString (this .agentId , startTimestamp , transactionId ), parentSpanId [0 ],
335
+ spanId , (short ) spanData .getKind ().ordinal ());
336
+ traceRoot = traceRootFactory .continueTraceRoot (traceId , transactionId );
337
+ }
296
338
297
- final Span span = new Span (traceRoot );
339
+ final DefaultSpanFactory spanFactory = new DefaultSpanFactory ();
340
+ final Span span = spanFactory .newSpan (traceRoot );
298
341
299
342
final StatusData statusData = spanData .getStatus ();
300
343
if (statusData != null ) {
@@ -315,15 +358,15 @@ private Span toSpan(final SpanData spanData) {
315
358
span .setRemoteAddr (UNKNOWN_REQ_IP );
316
359
317
360
Optional .ofNullable (spanData .getAttributes ())
318
- .ifPresent (attributes -> {
319
- span .addAnnotation (Annotations .of (AnnotationKey .HTTP_PARAM_ENTITY .getCode (),
320
- JsonUtils .toJSONString (attributes )));
321
- attributes .forEach ((key , value ) -> {
322
- if (REQ_IP .equals (key .getKey ())) {
323
- span .setRemoteAddr (String .valueOf (value ));
324
- }
361
+ .ifPresent (attributes -> {
362
+ span .addAnnotation (Annotations .of (AnnotationKey .HTTP_PARAM_ENTITY .getCode (),
363
+ JsonUtils .toJSONString (attributes )));
364
+ attributes .forEach ((key , value ) -> {
365
+ if (REQ_IP .equals (key .getKey ())) {
366
+ span .setRemoteAddr (String .valueOf (value ));
367
+ }
368
+ });
325
369
});
326
- });
327
370
328
371
if (CollectionUtils .isNotEmpty (spanData .getEvents ())) {
329
372
final AtomicInteger sequence = new AtomicInteger ();
@@ -342,7 +385,7 @@ private SpanEvent toSpanEvent(final EventData eventData) {
342
385
spanEvent .setServiceType (ServiceType .INTERNAL_METHOD .getCode ());
343
386
spanEvent .setEndPoint (eventData .getName ());
344
387
spanEvent .addAnnotation (Annotations .of (AnnotationKey .HTTP_PARAM_ENTITY .getCode (),
345
- JsonUtils .toJSONString (eventData .getAttributes ())));
388
+ JsonUtils .toJSONString (eventData .getAttributes ())));
346
389
spanEvent .setElapsedTime ((int ) toMillis (eventData .getEpochNanos ()));
347
390
return spanEvent ;
348
391
}
@@ -354,16 +397,16 @@ private static long toMillis(final long epochNanos) {
354
397
private static long hex32StringToLong (final String hex32String ) {
355
398
final CharSequence charSequence = new StringBuilder ().append (hex32String );
356
399
return OtelEncodingUtils .isValidBase16String (charSequence )
357
- ? OtelEncodingUtils .longFromBase16String (charSequence , 0 )
358
- & OtelEncodingUtils .longFromBase16String (charSequence , 16 )
359
- : hex32String .hashCode ();
400
+ ? OtelEncodingUtils .longFromBase16String (charSequence , 0 )
401
+ & OtelEncodingUtils .longFromBase16String (charSequence , 16 )
402
+ : hex32String .hashCode ();
360
403
}
361
404
362
405
private static long hex16StringToLong (final String hex16String ) {
363
406
final CharSequence charSequence = new StringBuilder ().append (hex16String );
364
407
return OtelEncodingUtils .isValidBase16String (charSequence )
365
- ? OtelEncodingUtils .longFromBase16String (charSequence , 0 )
366
- : hex16String .hashCode ();
408
+ ? OtelEncodingUtils .longFromBase16String (charSequence , 0 )
409
+ : hex16String .hashCode ();
367
410
}
368
411
369
412
private static String getEndpoint (final Resource resource ) {
0 commit comments