forked from elastic/apm-agent-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElasticApmTracer.java
976 lines (858 loc) · 39 KB
/
ElasticApmTracer.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.apm.agent.impl;
import co.elastic.apm.agent.bci.IndyBootstrap;
import co.elastic.apm.agent.collections.WeakReferenceCountedMap;
import co.elastic.apm.agent.common.JvmRuntimeInfo;
import co.elastic.apm.agent.common.util.WildcardMatcher;
import co.elastic.apm.agent.configuration.AutoDetectedServiceInfo;
import co.elastic.apm.agent.configuration.CoreConfiguration;
import co.elastic.apm.agent.configuration.MetricsConfiguration;
import co.elastic.apm.agent.configuration.ServerlessConfiguration;
import co.elastic.apm.agent.tracer.service.ServiceInfo;
import co.elastic.apm.agent.configuration.SpanConfiguration;
import co.elastic.apm.agent.context.ClosableLifecycleListenerAdapter;
import co.elastic.apm.agent.context.LifecycleListener;
import co.elastic.apm.agent.impl.baggage.Baggage;
import co.elastic.apm.agent.impl.baggage.W3CBaggagePropagation;
import co.elastic.apm.agent.impl.error.ErrorCapture;
import co.elastic.apm.agent.impl.metadata.MetaDataFuture;
import co.elastic.apm.agent.impl.sampling.ProbabilitySampler;
import co.elastic.apm.agent.impl.sampling.Sampler;
import co.elastic.apm.agent.impl.stacktrace.StacktraceConfiguration;
import co.elastic.apm.agent.impl.transaction.AbstractSpan;
import co.elastic.apm.agent.impl.transaction.ElasticContext;
import co.elastic.apm.agent.impl.transaction.Span;
import co.elastic.apm.agent.impl.transaction.TraceContext;
import co.elastic.apm.agent.impl.transaction.Transaction;
import co.elastic.apm.agent.logging.LoggingConfiguration;
import co.elastic.apm.agent.metrics.MetricRegistry;
import co.elastic.apm.agent.objectpool.ObjectPool;
import co.elastic.apm.agent.objectpool.ObjectPoolFactory;
import co.elastic.apm.agent.report.ApmServerClient;
import co.elastic.apm.agent.report.Reporter;
import co.elastic.apm.agent.report.ReporterConfiguration;
import co.elastic.apm.agent.sdk.internal.util.PrivilegedActionUtils;
import co.elastic.apm.agent.sdk.internal.util.VersionUtils;
import co.elastic.apm.agent.sdk.logging.Logger;
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent;
import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap;
import co.elastic.apm.agent.tracer.GlobalTracer;
import co.elastic.apm.agent.tracer.Scope;
import co.elastic.apm.agent.tracer.dispatch.HeaderGetter;
import co.elastic.apm.agent.tracer.reference.ReferenceCounted;
import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap;
import co.elastic.apm.agent.util.DependencyInjectingServiceLoader;
import co.elastic.apm.agent.util.ExecutorUtils;
import org.stagemonitor.configuration.ConfigurationOption;
import org.stagemonitor.configuration.ConfigurationOptionProvider;
import org.stagemonitor.configuration.ConfigurationRegistry;
import javax.annotation.Nullable;
import java.io.Closeable;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* This is the tracer implementation which provides access to lower level agent functionality.
* <p>
* Note that this is a internal API, so there are no guarantees in terms of backwards compatibility.
* </p>
*/
public class ElasticApmTracer implements Tracer {
private static final Logger logger = LoggerFactory.getLogger(ElasticApmTracer.class);
private static final WeakMap<ClassLoader, ServiceInfo> serviceInfoByClassLoader = WeakConcurrent.buildMap();
private static final Map<Class<?>, Class<? extends ConfigurationOptionProvider>> configs = new HashMap<>();
public static final Set<String> TRACE_HEADER_NAMES;
static {
Set<String> headerNames = new HashSet<>();
headerNames.addAll(TraceContext.TRACE_TEXTUAL_HEADERS);
headerNames.add(W3CBaggagePropagation.BAGGAGE_HEADER_NAME);
TRACE_HEADER_NAMES = Collections.unmodifiableSet(headerNames);
}
private static volatile boolean classloaderCheckOk = false;
private final ConfigurationRegistry configurationRegistry;
private final ApmServerClient apmServerClient;
private final List<LifecycleListener> lifecycleListeners = new CopyOnWriteArrayList<>();
private final ObjectPool<Transaction> transactionPool;
private final ObjectPool<Span> spanPool;
private final ObjectPool<ErrorCapture> errorPool;
private final ObjectPool<TraceContext> spanLinkPool;
private final Reporter reporter;
private final ObjectPoolFactory objectPoolFactory;
private final EmptyElasticContext emptyContext;
private final ThreadLocal<ActiveStack> activeStack = new ThreadLocal<ActiveStack>() {
@Override
protected ActiveStack initialValue() {
return new ActiveStack(transactionMaxSpans, emptyContext);
}
};
private final CoreConfiguration coreConfiguration;
private final int transactionMaxSpans;
private final SpanConfiguration spanConfiguration;
private final List<ActivationListener> activationListeners;
private final MetricRegistry metricRegistry;
private final ScheduledThreadPoolExecutor sharedPool;
private final int approximateContextSize;
private Sampler sampler;
boolean assertionsEnabled = false;
/**
* The tracer state is volatile to ensure thread safety when queried through {@link ElasticApmTracer#isRunning()} or
* {@link ElasticApmTracer#getState()}, or when updated through one of the lifecycle-effecting synchronized methods
* {@link ElasticApmTracer#start(boolean)}}, {@link ElasticApmTracer#pause()}, {@link ElasticApmTracer#resume()} or
* {@link ElasticApmTracer#stop()}.
*/
private volatile TracerState tracerState = TracerState.UNINITIALIZED;
private volatile boolean currentlyUnderStress = false;
private volatile boolean recordingConfigOptionSet;
private final String ephemeralId;
private final MetaDataFuture metaDataFuture;
static {
checkClassloader();
configs.put(co.elastic.apm.agent.tracer.configuration.CoreConfiguration.class, CoreConfiguration.class);
configs.put(co.elastic.apm.agent.tracer.configuration.LoggingConfiguration.class, LoggingConfiguration.class);
configs.put(co.elastic.apm.agent.tracer.configuration.MetricsConfiguration.class, MetricsConfiguration.class);
configs.put(co.elastic.apm.agent.tracer.configuration.ReporterConfiguration.class, ReporterConfiguration.class);
configs.put(co.elastic.apm.agent.tracer.configuration.ServerlessConfiguration.class, ServerlessConfiguration.class);
configs.put(co.elastic.apm.agent.tracer.configuration.StacktraceConfiguration.class, StacktraceConfiguration.class);
}
private static void checkClassloader() {
ClassLoader cl = PrivilegedActionUtils.getClassLoader(GlobalTracer.class);
// agent currently loaded in the bootstrap CL, which is the current correct location
if (cl == null) {
return;
}
if (classloaderCheckOk) {
return;
}
String agentLocation = PrivilegedActionUtils.getProtectionDomain(GlobalTracer.class).getCodeSource().getLocation().getFile();
if (!agentLocation.endsWith(".jar")) {
// agent is not packaged, thus we assume running tests
classloaderCheckOk = true;
return;
}
String premainClass = VersionUtils.getManifestEntry(new File(agentLocation), "Premain-Class");
if (null == premainClass) {
// packaged within a .jar, but not within an agent jar, thus we assume it's still for testing
classloaderCheckOk = true;
return;
}
if (premainClass.startsWith("co.elastic.apm.agent")) {
// premain class will only be present when packaged as an agent jar
classloaderCheckOk = true;
return;
}
// A packaged agent class has been loaded outside of bootstrap classloader, we are not in the context of
// unit/integration tests, that's likely a setup issue where the agent jar has been added to application
// classpath.
throw new IllegalStateException(String.format("Agent setup error: agent jar file \"%s\" likely referenced in JVM or application classpath", agentLocation));
}
ElasticApmTracer(ConfigurationRegistry configurationRegistry, MetricRegistry metricRegistry, Reporter reporter, ObjectPoolFactory poolFactory,
ApmServerClient apmServerClient, final String ephemeralId, MetaDataFuture metaDataFuture) {
this.emptyContext = new EmptyElasticContext(this);
this.metricRegistry = metricRegistry;
this.configurationRegistry = configurationRegistry;
this.reporter = reporter;
this.apmServerClient = apmServerClient;
this.ephemeralId = ephemeralId;
this.metaDataFuture = metaDataFuture;
int maxPooledElements = configurationRegistry.getConfig(ReporterConfiguration.class).getMaxQueueSize() * 2;
coreConfiguration = configurationRegistry.getConfig(CoreConfiguration.class);
transactionMaxSpans = coreConfiguration.getTransactionMaxSpans();
spanConfiguration = configurationRegistry.getConfig(SpanConfiguration.class);
TracerConfiguration tracerConfiguration = configurationRegistry.getConfig(TracerConfiguration.class);
recordingConfigOptionSet = tracerConfiguration.getRecordingConfig().get();
tracerConfiguration.getRecordingConfig().addChangeListener(new ConfigurationOption.ChangeListener<Boolean>() {
@Override
public void onChange(ConfigurationOption<?> configurationOption, Boolean oldValue, Boolean newValue) {
ElasticApmTracer.this.recordingConfigChanged(oldValue, newValue);
}
});
this.objectPoolFactory = poolFactory;
transactionPool = poolFactory.createTransactionPool(maxPooledElements, this);
spanPool = poolFactory.createSpanPool(maxPooledElements, this);
// we are assuming that we don't need as many errors as spans or transactions
errorPool = poolFactory.createErrorPool(maxPooledElements / 2, this);
// span links pool allows for 10X the maximum allowed span links per span
spanLinkPool = poolFactory.createSpanLinkPool(AbstractSpan.MAX_ALLOWED_SPAN_LINKS * 10, this);
sampler = ProbabilitySampler.of(coreConfiguration.getSampleRate().get());
coreConfiguration.getSampleRate().addChangeListener(new ConfigurationOption.ChangeListener<Double>() {
@Override
public void onChange(ConfigurationOption<?> configurationOption, Double oldValue, Double newValue) {
sampler = ProbabilitySampler.of(newValue);
}
});
this.activationListeners = DependencyInjectingServiceLoader.load(ActivationListener.class, this);
sharedPool = ExecutorUtils.createSingleThreadSchedulingDaemonPool("shared");
IndyBootstrap.setFallbackLogExecutor(sharedPool);
// The estimated number of wrappers is linear to the number of the number of external/OTel plugins
// - for an internal agent context, there will be at most one wrapper per external/OTel plugin.
// - for a context created by an external/OTel, we have one less wrapper required
approximateContextSize = coreConfiguration.getExternalPluginsCount() + 1; // +1 extra is for the OTel API plugin
// sets the assertionsEnabled flag to true if indeed enabled
//noinspection AssertWithSideEffects
assert assertionsEnabled = true;
}
@Override
@Nullable
public Transaction startRootTransaction(@Nullable ClassLoader initiatingClassLoader) {
return startRootTransaction(sampler, -1, currentContext().getBaggage(), initiatingClassLoader);
}
@Override
@Nullable
public Transaction startRootTransaction(@Nullable ClassLoader initiatingClassLoader, long epochMicro) {
return startRootTransaction(sampler, epochMicro, currentContext().getBaggage(), initiatingClassLoader);
}
@Nullable
@Override
public Transaction startRootTransaction(@Nullable ClassLoader initiatingClassLoader, Baggage baseBaggage, long epochMicro) {
return startRootTransaction(sampler, epochMicro, baseBaggage, initiatingClassLoader);
}
@Nullable
public Transaction startRootTransaction(Sampler sampler, long epochMicros, @Nullable ClassLoader initiatingClassLoader) {
return startRootTransaction(sampler, epochMicros, currentContext().getBaggage(), initiatingClassLoader);
}
@Override
@Nullable
public Transaction startRootTransaction(Sampler sampler, long epochMicros, Baggage baseBaggage, @Nullable ClassLoader initiatingClassLoader) {
Transaction transaction = null;
if (isRunning()) {
transaction = createTransaction().startRoot(epochMicros, sampler, baseBaggage);
afterTransactionStart(initiatingClassLoader, transaction);
}
return transaction;
}
@Override
@Nullable
public <T, C> Transaction startChildTransaction(@Nullable C headerCarrier, HeaderGetter<T, C> headersGetter, @Nullable ClassLoader initiatingClassLoader) {
return startChildTransaction(headerCarrier, headersGetter, sampler, -1, initiatingClassLoader);
}
@Override
@Nullable
public <T, C> Transaction startChildTransaction(@Nullable C headerCarrier, HeaderGetter<T, C> headersGetter, @Nullable ClassLoader initiatingClassLoader, Baggage baseBaggage, long epochMicros) {
return startChildTransaction(headerCarrier, headersGetter, sampler, epochMicros, initiatingClassLoader);
}
@Override
@Nullable
public <T, C> Transaction startChildTransaction(@Nullable C headerCarrier, HeaderGetter<T, C> headersGetter, Sampler sampler,
long epochMicros, @Nullable ClassLoader initiatingClassLoader) {
return startChildTransaction(headerCarrier, headersGetter, sampler, epochMicros, currentContext().getBaggage(), initiatingClassLoader);
}
@Nullable
private <T, C> Transaction startChildTransaction(@Nullable C headerCarrier, HeaderGetter<T, C> headersGetter, Sampler sampler,
long epochMicros, Baggage baseBaggage, @Nullable ClassLoader initiatingClassLoader) {
Transaction transaction = null;
if (isRunning()) {
transaction = createTransaction().start(headerCarrier,
headersGetter, epochMicros, sampler, baseBaggage);
afterTransactionStart(initiatingClassLoader, transaction);
}
return transaction;
}
private void afterTransactionStart(@Nullable ClassLoader initiatingClassLoader, Transaction transaction) {
if (logger.isDebugEnabled()) {
logger.debug("startTransaction {}", transaction);
if (logger.isTraceEnabled()) {
logger.trace("starting transaction at",
new RuntimeException("this exception is just used to record where the transaction has been started from"));
}
}
final ServiceInfo serviceInfo = getServiceInfoForClassLoader(initiatingClassLoader);
if (serviceInfo != null) {
transaction.getTraceContext().setServiceInfo(serviceInfo.getServiceName(), serviceInfo.getServiceVersion());
}
}
public Transaction noopTransaction() {
return createTransaction().startNoop();
}
private Transaction createTransaction() {
Transaction transaction = transactionPool.createInstance();
while (transaction.getReferenceCount() != 0) {
logger.warn("Tried to start a transaction with a non-zero reference count {} {}", transaction.getReferenceCount(), transaction);
transaction = transactionPool.createInstance();
}
return transaction;
}
@Override
@Nullable
public Transaction currentTransaction() {
return currentContext().getTransaction();
}
@Nullable
@Override
public ErrorCapture getActiveError() {
return ErrorCapture.getActive();
}
/**
* Starts a span with a given parent context.
* <p>
* This method makes it possible to start a span after the parent has already ended.
* </p>
*
* @param parentContext the trace context of the parent
* @return a new started span
*/
/**
* Starts a span with a given parent context.
* <p>
* This method makes it possible to start a span after the parent has already ended.
* </p>
*
* @param childContextCreator extracts the trace context to generate based on the provided parent
* @param parentContext the trace context of the parent
* @param baggage the baggage to use for the newly created span
* @param <T> the type of the parent context
* @return a new started span
*/
public <T> Span startSpan(TraceContext.ChildContextCreator<T> childContextCreator, T parentContext, Baggage baggage) {
return startSpan(childContextCreator, parentContext, baggage, -1);
}
public Span startSpan(AbstractSpan<?> parent, Baggage baggage, long epochMicros) {
return startSpan(TraceContext.fromParent(), parent, baggage, epochMicros);
}
/**
* @param parentContext the trace context of the parent
* @param epochMicros the start timestamp of the span in microseconds after epoch
* @return a new started span
*/
public <T> Span startSpan(TraceContext.ChildContextCreator<T> childContextCreator, T parentContext, Baggage baggage, long epochMicros) {
return createSpan().start(childContextCreator, parentContext, baggage, epochMicros);
}
private Span createSpan() {
Span span = spanPool.createInstance();
while (span.getReferenceCount() != 0) {
logger.warn("Tried to start a span with a non-zero reference count {} {}", span.getReferenceCount(), span);
span = spanPool.createInstance();
}
return span;
}
@Override
public void captureAndReportException(@Nullable Throwable e, ClassLoader initiatingClassLoader) {
ErrorCapture errorCapture = captureException(System.currentTimeMillis() * 1000, e, currentContext(), initiatingClassLoader);
if (errorCapture != null) {
errorCapture.end();
}
}
@Override
@Nullable
public String captureAndReportException(long epochMicros, @Nullable Throwable e, ElasticContext<?> parentContext) {
String id = null;
ErrorCapture errorCapture = captureException(epochMicros, e, parentContext, null);
if (errorCapture != null) {
id = errorCapture.getTraceContext().getId().toString();
errorCapture.end();
}
return id;
}
@Override
@Nullable
public ErrorCapture captureException(@Nullable Throwable e, ElasticContext<?> parentContext, @Nullable ClassLoader initiatingClassLoader) {
return captureException(System.currentTimeMillis() * 1000, e, parentContext, initiatingClassLoader);
}
@Nullable
@Override
public ErrorCapture captureException(@Nullable Throwable e, @Nullable ClassLoader initiatingClassLoader) {
return captureException(System.currentTimeMillis() * 1000, e, currentContext(), initiatingClassLoader);
}
@Nullable
private ErrorCapture captureException(long epochMicros, @Nullable Throwable e, ElasticContext<?> parentContext, @Nullable ClassLoader initiatingClassLoader) {
if (!isRunning() || e == null) {
return null;
}
if (!coreConfiguration.captureExceptionDetails()) {
return null;
}
while (e != null && WildcardMatcher.anyMatch(coreConfiguration.getUnnestExceptions(), e.getClass().getName()) != null) {
e = e.getCause();
}
// note: if we add inheritance support for exception filtering, caching would be required for performance
if (e != null && !WildcardMatcher.isAnyMatch(coreConfiguration.getIgnoreExceptions(), e.getClass().getName())) {
ErrorCapture error = errorPool.createInstance();
error.withTimestamp(epochMicros);
error.setException(e);
Transaction currentTransaction = currentTransaction();
if (currentTransaction != null) {
if (currentTransaction.getNameForSerialization().length() > 0) {
error.setTransactionName(currentTransaction.getNameForSerialization());
}
error.setTransactionType(currentTransaction.getType());
error.setTransactionSampled(currentTransaction.isSampled());
}
AbstractSpan<?> parent = parentContext.getSpan();
if (parent != null) {
error.asChildOf(parent);
// don't discard spans leading up to an error, otherwise they'd point to an invalid parent
parent.setNonDiscardable();
} else {
error.getTraceContext().getId().setToRandomValue();
ServiceInfo serviceInfo = getServiceInfoForClassLoader(initiatingClassLoader);
if (serviceInfo != null) {
error.getTraceContext().setServiceInfo(serviceInfo.getServiceName(), serviceInfo.getServiceVersion());
}
}
parentContext.getBaggage()
.storeBaggageInContext(error.getContext(), getConfig(CoreConfiguration.class).getBaggageToAttach());
return error;
}
return null;
}
public ConfigurationRegistry getConfigurationRegistry() {
return configurationRegistry;
}
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public <T> T getConfig(Class<T> configProvider) {
T configuration = null;
if (configs.containsKey(configProvider)) {
configuration = (T) configurationRegistry.getConfig(configs.get(configProvider));
} else if (ConfigurationOptionProvider.class.isAssignableFrom(configProvider)) {
configuration = (T) configurationRegistry.getConfig((Class) configProvider);
}
if (configuration == null) {
throw new IllegalStateException("no configuration available for " + configProvider.getName());
}
return configuration;
}
public void endTransaction(Transaction transaction) {
if (logger.isDebugEnabled()) {
logger.debug("endTransaction {}", transaction);
if (logger.isTraceEnabled()) {
logger.trace("ending transaction at",
new RuntimeException("this exception is just used to record where the transaction has been ended from"));
}
}
if (!transaction.isNoop() &&
(transaction.isSampled() || apmServerClient.supportsKeepingUnsampledTransaction())) {
// we do report non-sampled transactions (without the context)
reporter.report(transaction);
} else {
transaction.decrementReferences();
}
}
public void reportPartialTransaction(Transaction transaction) {
reporter.reportPartialTransaction(transaction);
}
public void endSpan(Span span) {
if (logger.isDebugEnabled()) {
logger.debug("endSpan {}", span);
if (logger.isTraceEnabled()) {
logger.trace("ending span at", new RuntimeException("this exception is just used to record where the span has been ended from"));
}
}
if (!span.isSampled()) {
Transaction transaction = span.getTransaction();
if (transaction != null) {
transaction.captureDroppedSpan(span);
}
span.decrementReferences();
return;
}
if (span.isExit()) {
if (span.getDuration() < spanConfiguration.getExitSpanMinDuration().getMicros()) {
logger.debug("Span faster than exit_span_min_duration. Request discarding {}", span);
span.requestDiscarding();
}
} else if (!span.isComposite()) {
if (span.getDuration() < coreConfiguration.getSpanMinDuration().getMicros()) {
logger.debug("Span faster than span_min_duration. Request discarding {}", span);
span.requestDiscarding();
}
}
if (span.isDiscarded()) {
logger.debug("Discarding span {}", span);
Transaction transaction = span.getTransaction();
if (transaction != null) {
transaction.captureDroppedSpan(span);
}
span.decrementReferences();
return;
}
reportSpan(span);
}
private void reportSpan(Span span) {
AbstractSpan<?> parent = span.getParent();
if (parent != null && parent.isDiscarded()) {
logger.warn("Reporting a child of an discarded span. The current span '{}' will not be shown in the UI. Consider deactivating span_min_duration.", span);
}
Transaction transaction = span.getTransaction();
if (transaction != null) {
transaction.getSpanCount().getReported().incrementAndGet();
}
// makes sure that parents are also non-discardable
span.setNonDiscardable();
reporter.report(span);
}
public void endError(ErrorCapture error) {
reporter.report(error);
}
public TraceContext createSpanLink() {
return spanLinkPool.createInstance();
}
public void recycle(Transaction transaction) {
transactionPool.recycle(transaction);
}
public void recycle(Span span) {
spanPool.recycle(span);
}
public void recycle(ErrorCapture error) {
errorPool.recycle(error);
}
public void recycle(TraceContext traceContext) {
spanLinkPool.recycle(traceContext);
}
public synchronized void stop() {
if (tracerState == TracerState.STOPPED) {
// may happen if explicitly stopped in a unit test and executed again within a shutdown hook
return;
}
for (LifecycleListener lifecycleListener : lifecycleListeners) {
try {
lifecycleListener.stop();
} catch (Exception e) {
logger.warn("Suppressed exception while calling stop()", e);
}
}
ExecutorUtils.shutdownAndWaitTermination(sharedPool);
tracerState = TracerState.STOPPED;
logger.info("Tracer switched to STOPPED state");
if (logger.isDebugEnabled()) {
logger.debug("Tracer stop stack trace: ", new Throwable("Expected - for debugging purposes"));
}
try {
configurationRegistry.close();
reporter.close();
} catch (Exception e) {
logger.warn("Suppressed exception while calling stop()", e);
}
//Shutting down logging resets the log level to OFF - subsequent tests in the class will get no log output, hence the guard
if (!assertionsEnabled) {
LoggingConfiguration.shutdown();
}
}
public Reporter getReporter() {
return reporter;
}
public Sampler getSampler() {
return sampler;
}
@Override
public ObjectPoolFactory getObjectPoolFactory() {
return objectPoolFactory;
}
@Override
public <K, V extends ReferenceCounted> ReferenceCountedMap<K, V> newReferenceCountedMap() {
return new WeakReferenceCountedMap<>();
}
public void registerSpanListener(ActivationListener activationListener) {
this.activationListeners.add(activationListener);
}
public List<ActivationListener> getActivationListeners() {
return activationListeners;
}
/**
* As opposed to {@link ElasticApmTracer#start(boolean)}, this method does not change the tracer's state and it's purpose
* is to be called at JVM bootstrap.
*
* @param lifecycleListeners Lifecycle listeners
*/
void init(List<LifecycleListener> lifecycleListeners) {
this.lifecycleListeners.addAll(lifecycleListeners);
for (LifecycleListener lifecycleListener : lifecycleListeners) {
try {
lifecycleListener.init(this);
} catch (Exception e) {
logger.error("Failed to init " + lifecycleListener.getClass().getName(), e);
}
}
}
/**
* Starts the tracer. Depending on several environment and setup parameters, the tracer may be started synchronously
* on this thread, or its start may be delayed and invoked on a dedicated thread.
*
* @param premain true if this tracer is attached through the {@code premain()} method (i.e. using the `javaagent`
* jvm parameter); false otherwise
*/
public synchronized void start(boolean premain) {
long delayInitMs = getConfig(CoreConfiguration.class).getDelayTracerStartMs();
if (premain && shouldDelayOnPremain()) {
delayInitMs = Math.max(delayInitMs, 5000L);
}
if (delayInitMs > 0) {
startWithDelay(delayInitMs);
} else {
startSync();
}
}
private boolean shouldDelayOnPremain() {
return JvmRuntimeInfo.ofCurrentVM().getMajorVersion() <= 8 &&
ClassLoader.getSystemClassLoader().getResource("org/apache/catalina/startup/Bootstrap.class") != null;
}
private synchronized void startWithDelay(final long delayInitMs) {
ThreadPoolExecutor pool = ExecutorUtils.createSingleThreadDaemonPool("tracer-initializer", 1);
pool.submit(new Runnable() {
@Override
public void run() {
try {
logger.info("Delaying initialization of tracer for " + delayInitMs + "ms");
Thread.sleep(delayInitMs);
logger.info("end wait");
} catch (InterruptedException e) {
logger.error(e.getMessage(), e);
} finally {
ElasticApmTracer.this.startSync();
}
}
});
pool.shutdown();
}
private synchronized void startSync() {
if (tracerState != TracerState.UNINITIALIZED) {
logger.warn("Trying to start an already initialized agent");
return;
}
apmServerClient.start();
reporter.start();
for (LifecycleListener lifecycleListener : lifecycleListeners) {
try {
lifecycleListener.start(this);
} catch (Exception e) {
logger.error("Failed to start " + lifecycleListener.getClass().getName(), e);
}
}
tracerState = TracerState.RUNNING;
if (recordingConfigOptionSet) {
logger.info("Tracer switched to RUNNING state");
} else {
pause();
}
}
public synchronized void onStressDetected() {
currentlyUnderStress = true;
if (tracerState == TracerState.RUNNING) {
pause();
}
}
public synchronized void onStressRelieved() {
currentlyUnderStress = false;
if (tracerState == TracerState.PAUSED && recordingConfigOptionSet) {
resume();
}
}
private synchronized void recordingConfigChanged(boolean oldValue, boolean newValue) {
// if changed from true to false then:
// if current state is RUNNING - pause the agent
// otherwise - ignore
// if changed from false to true then:
// if current state is RUNNING or STOPPED - no effect
// if current state is PAUSED and currentlyUnderStress==false - then resume
if (oldValue && !newValue && tracerState == TracerState.RUNNING) {
pause();
} else if (!oldValue && newValue && tracerState == TracerState.PAUSED && !currentlyUnderStress) {
resume();
}
recordingConfigOptionSet = newValue;
}
synchronized void pause() {
if (tracerState != TracerState.RUNNING) {
logger.warn("Attempting to pause the agent when it is already in a {} state", tracerState);
return;
}
tracerState = TracerState.PAUSED;
logger.info("Tracer switched to PAUSED state");
for (LifecycleListener lifecycleListener : lifecycleListeners) {
try {
lifecycleListener.pause();
} catch (Exception e) {
logger.warn("Suppressed exception while calling pause()", e);
}
}
}
synchronized void resume() {
if (tracerState != TracerState.PAUSED) {
logger.warn("Attempting to resume the agent when it is in a {} state", tracerState);
return;
}
for (LifecycleListener lifecycleListener : lifecycleListeners) {
try {
lifecycleListener.resume();
} catch (Exception e) {
logger.warn("Suppressed exception while calling resume()", e);
}
}
tracerState = TracerState.RUNNING;
logger.info("Tracer switched to RUNNING state");
}
@Override
public boolean isRunning() {
return tracerState == TracerState.RUNNING;
}
@Override
@Nullable
public Span createExitChildSpan() {
AbstractSpan<?> active = getActive();
if (active == null) {
return null;
}
return active.createExitSpan();
}
@Override
public TracerState getState() {
return tracerState;
}
@Nullable
public <T> T getLifecycleListener(Class<T> listenerClass) {
for (LifecycleListener lifecycleListener : lifecycleListeners) {
if (listenerClass.isInstance(lifecycleListener)) {
return (T) lifecycleListener;
}
}
return null;
}
/**
* @return the currently active context, {@literal null} if there is none.
*/
public ElasticContext<?> currentContext() {
return activeStack.get().currentContext();
}
@Nullable
@Override
public AbstractSpan<?> getActive() {
return currentContext().getSpan();
}
/**
* Lazily wraps the currently active context if required, wrapper instance is cached with wrapperClass as key.
* Wrapping is transparently handled by {@link #currentContext()}.
*
* @param wrapperClass wrapper type
* @param wrapFunction wrapper creation function
* @param <T> wrapper type
* @return newly (or previously) created wrapper
*/
public <T extends ElasticContext<T>> T wrapActiveContextIfRequired(Class<T> wrapperClass, Callable<T> wrapFunction) {
return activeStack.get().wrapActiveContextIfRequired(wrapperClass, wrapFunction, approximateContextSize);
}
public void activate(ElasticContext<?> context) {
activeStack.get().activate(context, activationListeners);
}
public Scope activateInScope(final ElasticContext<?> context) {
// already in scope
if (currentContext() == context) {
return NoopScope.INSTANCE;
}
context.activate();
if (context instanceof Scope) {
// we can take shortcut and avoid creating a separate object
return (Scope) context;
}
return new Scope() {
@Override
public void close() {
context.deactivate();
}
};
}
public void deactivate(ElasticContext<?> context) {
activeStack.get().deactivate(context, activationListeners, assertionsEnabled);
}
public MetricRegistry getMetricRegistry() {
return metricRegistry;
}
public List<ServiceInfo> getServiceInfoOverrides() {
List<ServiceInfo> serviceInfos = new ArrayList<>(serviceInfoByClassLoader.approximateSize());
for (Map.Entry<ClassLoader, ServiceInfo> entry : serviceInfoByClassLoader) {
serviceInfos.add(entry.getValue());
}
return serviceInfos;
}
@Override
public void setServiceInfoForClassLoader(@Nullable ClassLoader classLoader, ServiceInfo serviceInfo) {
// overriding the service name/version for the bootstrap class loader is not an actual use-case
// null may also mean we don't know about the initiating class loader
if (classLoader == null
|| !serviceInfo.hasServiceName()
// if the service name is set explicitly, don't override it
|| coreConfiguration.getServiceNameConfig().getUsedKey() != null) {
return;
}
logger.debug("Using `{}` as the service name and `{}` as the service version for class loader [{}]", serviceInfo.getServiceName(), serviceInfo.getServiceVersion(), classLoader);
if (!serviceInfoByClassLoader.containsKey(classLoader)) {
serviceInfoByClassLoader.putIfAbsent(classLoader, serviceInfo);
}
}
@Nullable
@Override
public ServiceInfo getServiceInfoForClassLoader(@Nullable ClassLoader initiatingClassLoader) {
if (initiatingClassLoader == null) {
return null;
}
return serviceInfoByClassLoader.get(initiatingClassLoader);
}
public void resetServiceInfoOverrides() {
serviceInfoByClassLoader.clear();
}
public ApmServerClient getApmServerClient() {
return apmServerClient;
}
public String getEphemeralId() {
return ephemeralId;
}
public MetaDataFuture getMetaDataFuture() {
return metaDataFuture;
}
public ScheduledThreadPoolExecutor getSharedSingleThreadedPool() {
return sharedPool;
}
public void addShutdownHook(Closeable closeable) {
lifecycleListeners.add(ClosableLifecycleListenerAdapter.of(closeable));
}
@Nullable
@Override
public <T extends co.elastic.apm.agent.tracer.Tracer> T probe(Class<T> type) {
if (type.isInstance(this)) {
return type.cast(this);
} else {
return null;
}
}
@Override
public <T extends co.elastic.apm.agent.tracer.Tracer> T require(Class<T> type) {
T cast = probe(type);
if (cast == null) {
throw new IllegalStateException(this + " does not implement " + type.getName());
}
return cast;
}
@Override
public Set<String> getTraceHeaderNames() {
return TRACE_HEADER_NAMES;
}
@Override
public ServiceInfo autoDetectedServiceInfo() {
return AutoDetectedServiceInfo.autoDetected();
}
}