forked from elastic/elastic-otel-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicConfiguration.java
More file actions
258 lines (232 loc) · 10.3 KB
/
DynamicConfiguration.java
File metadata and controls
258 lines (232 loc) · 10.3 KB
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
/*
* 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.otel.dynamicconfig;
import static co.elastic.otel.dynamicconfig.DynamicInstrumentation.setProviderTracerConfigurator;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.internal.ScopeConfigurator;
import io.opentelemetry.sdk.trace.internal.TracerConfig;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DynamicConfiguration {
static final String INSTRUMENTATION_NAME_PREPEND = "io.opentelemetry.";
private static final String ALL_INSTRUMENTATION = "_ALL_";
private static final String ALL_INSTRUMENTATION_FULL_NAME =
INSTRUMENTATION_NAME_PREPEND + ALL_INSTRUMENTATION;
private static final DynamicConfiguration INSTANCE = new DynamicConfiguration();
private static final Logger logger = Logger.getLogger(DynamicConfiguration.class.getName());
public static DynamicConfiguration getInstance() {
return INSTANCE;
}
public static final String DISABLE_SEND_OPTION = "elastic.otel.java.experimental.disable_send";
public static final String INSTRUMENTATION_DISABLE_OPTION =
"elastic.otel.java.experimental.disable_instrumentations";
private Boolean recoverySendSpansState;
private Boolean recoverySendLogsState;
private Boolean recoverySendMetricsState;
private final ConcurrentMap<String, Boolean> alreadyDeactivated = new ConcurrentHashMap<>();
private String cachedDeactivationList;
private void initSendingStates() {
if (recoverySendSpansState == null) {
if (BlockableSpanExporter.getInstance() == null) {
logger.log(
Level.WARNING,
"BlockableSpanExporter.getInstance() == null which is unexpected unless this is a test");
} else {
recoverySendSpansState = BlockableSpanExporter.getInstance().sendingSpans();
}
}
if (recoverySendMetricsState == null) {
if (BlockableMetricExporter.getInstance() == null) {
logger.log(
Level.WARNING,
"BlockableMetricExporter.getInstance() == null which is unexpected unless this is a test");
} else {
recoverySendMetricsState = BlockableMetricExporter.getInstance().sendingMetrics();
}
}
if (recoverySendLogsState == null) {
if (BlockableLogRecordExporter.getInstance() == null) {
logger.log(
Level.WARNING,
"BlockableLogRecordExporter.getInstance() == null which is unexpected unless this is a test");
} else {
recoverySendLogsState = BlockableLogRecordExporter.getInstance().sendingLogs();
}
}
}
/** Can be executed repeatedly regardless of the current state */
public void setSendingSpans(boolean send) {
initSendingStates();
if (recoverySendSpansState != null) {
BlockableSpanExporter.getInstance().setSendingSpans(send);
}
}
/** Can be executed repeatedly regardless of the current state */
public void setSendingMetrics(boolean send) {
initSendingStates();
if (recoverySendMetricsState != null) {
BlockableMetricExporter.getInstance().setSendingMetrics(send);
}
}
/** Can be executed repeatedly regardless of the current state */
public void setSendingLogs(boolean send) {
initSendingStates();
if (recoverySendLogsState != null) {
BlockableLogRecordExporter.getInstance().setSendingLogs(send);
}
}
/** Can be executed repeatedly even if sending is currently stopped */
public void stopAllSending() {
initSendingStates();
if (recoverySendSpansState != null) {
BlockableSpanExporter.getInstance().setSendingSpans(false);
}
if (recoverySendMetricsState != null) {
BlockableMetricExporter.getInstance().setSendingMetrics(false);
}
if (recoverySendLogsState != null) {
BlockableLogRecordExporter.getInstance().setSendingLogs(false);
}
}
/** Can be executed repeatedly even if sending is currently proceeding */
public void restartAllSending() {
initSendingStates();
if (recoverySendSpansState != null) {
BlockableSpanExporter.getInstance().setSendingSpans(recoverySendSpansState);
}
if (recoverySendMetricsState != null) {
BlockableMetricExporter.getInstance().setSendingMetrics(recoverySendMetricsState);
}
if (recoverySendLogsState != null) {
BlockableLogRecordExporter.getInstance().setSendingLogs(recoverySendLogsState);
}
}
private void reactivateInstrumentation(String instrumentationName) {
UpdatableConfigurator.INSTANCE.put(
InstrumentationScopeInfo.create(INSTRUMENTATION_NAME_PREPEND + instrumentationName),
TracerConfig.enabled());
setProviderTracerConfigurator(
GlobalOpenTelemetry.getTracerProvider(), UpdatableConfigurator.INSTANCE);
}
private void deactivateInstrumentation(String instrumentationName) {
UpdatableConfigurator.INSTANCE.put(
InstrumentationScopeInfo.create(INSTRUMENTATION_NAME_PREPEND + instrumentationName),
TracerConfig.disabled());
setProviderTracerConfigurator(
GlobalOpenTelemetry.getTracerProvider(), UpdatableConfigurator.INSTANCE);
}
public void deactivateAllInstrumentations() {
deactivateInstrumentation(ALL_INSTRUMENTATION);
}
public void reactivateAllInstrumentations() {
reactivateInstrumentation(ALL_INSTRUMENTATION);
}
// okay to synchronize as this should only be called after multi-second intervals and
// additionally only called from threads which are not doing anything application-blocking
public synchronized void deactivateInstrumentations(String deactivateList) {
// Avoid doing anything if the deactivateList hasn't changed -
// this is just a GC optimization to avoid creating new objects in the most common case
if (Objects.equals(deactivateList, cachedDeactivationList)) {
return;
}
cachedDeactivationList = deactivateList;
// Algorithm:
// 1. If the list is empty, then everything alreadyDeactivated needs to be re-activated
// 2. Otherwise
// 2a. everything in both deactivateList and alreadyDeactivated is ignored (already deactivated)
// 2b. everything in deactivateList not in alreadyDeactivated is deactivated
// 2c. everything in alreadyDeactivated not in deactivateList is re-activated
if (deactivateList == null || deactivateList.trim().isEmpty()) {
// Applying (1) - keySet.remove() is a valid concurrent mutation here within the loop
Set<String> keySet = alreadyDeactivated.keySet();
for (String instrumentation : keySet) {
DynamicConfiguration.getInstance().reactivateInstrumentation(instrumentation);
keySet.remove(instrumentation);
}
} else {
// Applying (2)
Deactivations deactivations =
new Deactivations(
convertToSet(deactivateList), new HashSet<>(alreadyDeactivated.keySet()));
deactivations.applyDeactivations(alreadyDeactivated);
}
}
private static Set<String> convertToSet(String deactivateList) {
Set<String> theSet = new HashSet<>();
if (deactivateList != null && !deactivateList.trim().isEmpty()) {
for (String toBeDisabled : deactivateList.split(",")) {
theSet.add(toBeDisabled.trim());
}
}
return theSet;
}
public static class Deactivations {
final Set<String> instrumentationsToReactivate;
final Set<String> instrumentationsToDeactivate;
public Deactivations(Set<String> deactivateList, Set<String> alreadyDeactivated) {
instrumentationsToReactivate = new HashSet<>();
instrumentationsToDeactivate = new HashSet<>();
for (String instrumentation : deactivateList) {
if (!alreadyDeactivated.contains(instrumentation)) {
// Requested to deactivate this and it's not already deactivated, so
instrumentationsToDeactivate.add(instrumentation);
}
}
for (String instrumentation : alreadyDeactivated) {
if (!deactivateList.contains(instrumentation)) {
// Currently deactivated but now NOT requested to be deactivated, so
instrumentationsToReactivate.add(instrumentation);
}
}
}
public void applyDeactivations(ConcurrentMap<String, Boolean> alreadyDeactivated) {
for (String instrumentation : instrumentationsToReactivate) {
DynamicConfiguration.getInstance().reactivateInstrumentation(instrumentation);
alreadyDeactivated.remove(instrumentation);
}
for (String instrumentation : instrumentationsToDeactivate) {
DynamicConfiguration.getInstance().deactivateInstrumentation(instrumentation);
alreadyDeactivated.put(instrumentation, Boolean.TRUE);
}
}
}
public static class UpdatableConfigurator implements ScopeConfigurator<TracerConfig> {
public static final UpdatableConfigurator INSTANCE = new UpdatableConfigurator();
private final ConcurrentMap<String, TracerConfig> map = new ConcurrentHashMap<>();
private UpdatableConfigurator() {}
@Override
public TracerConfig apply(InstrumentationScopeInfo scopeInfo) {
// If key "_ALL_" is set to disabled, then always return disabled
// otherwise fallback to the individual instrumentation
if (!map.getOrDefault(ALL_INSTRUMENTATION_FULL_NAME, TracerConfig.enabled()).isEnabled()) {
return TracerConfig.disabled();
}
return map.getOrDefault(scopeInfo.getName(), TracerConfig.defaultConfig());
}
public void put(InstrumentationScopeInfo scope, TracerConfig tracerConfig) {
map.put(scope.getName(), tracerConfig);
}
}
}