34
34
import org .elasticsearch .cluster .service .ClusterService ;
35
35
import org .elasticsearch .common .Strings ;
36
36
import org .elasticsearch .common .component .AbstractLifecycleComponent ;
37
+ import org .elasticsearch .common .regex .Regex ;
37
38
import org .elasticsearch .common .settings .SecureSetting ;
38
39
import org .elasticsearch .common .settings .SecureString ;
39
40
import org .elasticsearch .common .settings .Setting ;
46
47
import java .security .AccessController ;
47
48
import java .security .PrivilegedAction ;
48
49
import java .util .Collection ;
50
+ import java .util .Collections ;
49
51
import java .util .HashMap ;
50
52
import java .util .List ;
51
53
import java .util .Map ;
52
54
import java .util .Objects ;
53
55
import java .util .Queue ;
54
56
import java .util .concurrent .Semaphore ;
55
57
import java .util .concurrent .TimeUnit ;
58
+ import java .util .function .Function ;
56
59
import java .util .function .Predicate ;
57
60
import java .util .stream .Collectors ;
58
61
import java .util .stream .Stream ;
@@ -67,6 +70,13 @@ public class APMTracer extends AbstractLifecycleComponent implements org.elastic
67
70
static final Setting <Boolean > APM_ENABLED_SETTING = Setting .boolSetting ("xpack.apm.tracing.enabled" , false , Dynamic , NodeScope );
68
71
static final Setting <SecureString > APM_ENDPOINT_SETTING = SecureSetting .secureString ("xpack.apm.endpoint" , null );
69
72
static final Setting <SecureString > APM_TOKEN_SETTING = SecureSetting .secureString ("xpack.apm.token" , null );
73
+ static final Setting <List <String >> APM_TRACING_NAMES_INCLUDE_SETTING = Setting .listSetting (
74
+ "xpack.apm.tracing.names.include" ,
75
+ Collections .emptyList (),
76
+ Function .identity (),
77
+ Dynamic ,
78
+ NodeScope
79
+ );
70
80
71
81
private final Semaphore shutdownPermits = new Semaphore (Integer .MAX_VALUE );
72
82
private final Map <String , Span > spans = ConcurrentCollections .newConcurrentMap ();
@@ -78,6 +88,8 @@ public class APMTracer extends AbstractLifecycleComponent implements org.elastic
78
88
private volatile boolean enabled ;
79
89
private volatile APMServices services ;
80
90
91
+ private List <String > includeNames ;
92
+
81
93
/** This class is required to make all open telemetry services visible at once */
82
94
private static class APMServices {
83
95
private final SdkTracerProvider provider ;
@@ -97,7 +109,9 @@ public APMTracer(Settings settings, ThreadPool threadPool, ClusterService cluste
97
109
this .endpoint = APM_ENDPOINT_SETTING .get (settings );
98
110
this .token = APM_TOKEN_SETTING .get (settings );
99
111
this .enabled = APM_ENABLED_SETTING .get (settings );
112
+ this .includeNames = APM_TRACING_NAMES_INCLUDE_SETTING .get (settings );
100
113
clusterService .getClusterSettings ().addSettingsUpdateConsumer (APM_ENABLED_SETTING , this ::setEnabled );
114
+ clusterService .getClusterSettings ().addSettingsUpdateConsumer (APM_TRACING_NAMES_INCLUDE_SETTING , this ::setIncludeNames );
101
115
}
102
116
103
117
public boolean isEnabled () {
@@ -113,6 +127,10 @@ private void setEnabled(boolean enabled) {
113
127
}
114
128
}
115
129
130
+ private void setIncludeNames (List <String > includeNames ) {
131
+ this .includeNames = includeNames ;
132
+ }
133
+
116
134
@ Override
117
135
protected void doStart () {
118
136
if (enabled ) {
@@ -195,6 +213,11 @@ public void onTraceStarted(Traceable traceable) {
195
213
if (services == null ) {
196
214
return ;
197
215
}
216
+
217
+ if (isSpanNameIncluded (traceable .getSpanName ()) == false ) {
218
+ return ;
219
+ }
220
+
198
221
spans .computeIfAbsent (traceable .getSpanId (), spanId -> {
199
222
// services might be in shutdown state by this point, but this is handled by the open telemetry internally
200
223
final SpanBuilder spanBuilder = services .tracer .spanBuilder (traceable .getSpanName ());
@@ -245,6 +268,12 @@ public void onTraceStarted(Traceable traceable) {
245
268
});
246
269
}
247
270
271
+ private boolean isSpanNameIncluded (String name ) {
272
+ // Alternatively we could use automata here but it is much more complex
273
+ // and it needs wrapping like done for use in the security plugin.
274
+ return includeNames .isEmpty () || Regex .simpleMatch (includeNames , name );
275
+ }
276
+
248
277
private Context getParentSpanContext (OpenTelemetry openTelemetry ) {
249
278
// If we already have a non-root span context that should be the parent
250
279
if (Context .current () != Context .root ()) {
0 commit comments