forked from elastic/elastic-otel-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpampManager.java
More file actions
246 lines (212 loc) · 8.52 KB
/
OpampManager.java
File metadata and controls
246 lines (212 loc) · 8.52 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
/*
* 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.internal;
import com.dslplatform.json.DslJson;
import com.dslplatform.json.JsonReader;
import com.dslplatform.json.MapConverter;
import io.opentelemetry.opamp.client.OpampClient;
import io.opentelemetry.opamp.client.OpampClientBuilder;
import io.opentelemetry.opamp.client.internal.connectivity.http.OkHttpSender;
import io.opentelemetry.opamp.client.internal.request.delay.PeriodicDelay;
import io.opentelemetry.opamp.client.internal.request.delay.RetryPeriodicDelay;
import io.opentelemetry.opamp.client.internal.request.service.HttpRequestService;
import io.opentelemetry.opamp.client.internal.response.MessageData;
import java.io.Closeable;
import java.io.IOException;
import java.time.Duration;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import okio.ByteString;
import opamp.proto.AgentConfigFile;
import opamp.proto.AgentRemoteConfig;
import opamp.proto.RemoteConfigStatus;
import opamp.proto.RemoteConfigStatuses;
import opamp.proto.ServerErrorResponse;
public final class OpampManager implements Closeable, OpampClient.Callbacks {
private final Configuration configuration;
private final DslJson<Object> dslJson = new DslJson<>(new DslJson.Settings<>());
private final Logger logger = Logger.getLogger(OpampManager.class.getName());
private volatile OpampClient client;
private volatile MutablePeriodicDelay pollingDelay;
private volatile CentralConfigurationProcessor processor;
private OpampManager(Configuration configuration) {
this.configuration = configuration;
}
public void start(CentralConfigurationProcessor processor) {
this.processor = processor;
pollingDelay = new MutablePeriodicDelay(configuration.pollingInterval);
OpampClientBuilder builder = OpampClient.builder();
builder.enableRemoteConfig();
OkHttpSender httpSender = OkHttpSender.create(configuration.configurationEndpoint);
if (configuration.serviceName != null) {
builder.putIdentifyingAttribute("service.name", configuration.serviceName);
}
if (configuration.environment != null) {
builder.putIdentifyingAttribute("deployment.environment.name", configuration.environment);
}
PeriodicDelay retryDelay = RetryPeriodicDelay.create(configuration.pollingInterval);
builder.setRequestService(HttpRequestService.create(httpSender, pollingDelay, retryDelay));
client = builder.build(this);
}
@Override
public void close() throws IOException {
client.close();
}
@Override
public void onMessage(MessageData messageData) {
logger.log(Level.FINE, "onMessage({0}, {1})", new Object[] {client, messageData});
AgentRemoteConfig remoteConfig = messageData.getRemoteConfig();
if (remoteConfig != null) {
processRemoteConfig(client, remoteConfig);
}
}
private void processRemoteConfig(OpampClient client, AgentRemoteConfig remoteConfig) {
Map<String, AgentConfigFile> configMapMap = remoteConfig.config.config_map;
AgentConfigFile centralConfig = configMapMap.get("elastic");
if (centralConfig != null) {
Map<String, String> configuration = parseCentralConfiguration(centralConfig.body);
RemoteConfigStatuses status;
if (configuration != null) {
CentralConfigurationProcessor.Result result = processor.process(configuration);
status =
(result == CentralConfigurationProcessor.Result.SUCCESS)
? RemoteConfigStatuses.RemoteConfigStatuses_APPLIED
: RemoteConfigStatuses.RemoteConfigStatuses_FAILED;
} else {
status = RemoteConfigStatuses.RemoteConfigStatuses_FAILED;
}
// Note if FAILED is sent, the config change is effectively dropped as the server will not
// re-send it
client.setRemoteConfigStatus(getRemoteConfigStatus(status, remoteConfig.config_hash));
}
}
private static RemoteConfigStatus getRemoteConfigStatus(
RemoteConfigStatuses status, ByteString hash) {
if (hash != null && status == RemoteConfigStatuses.RemoteConfigStatuses_APPLIED) {
return new RemoteConfigStatus.Builder().status(status).last_remote_config_hash(hash).build();
} else {
return new RemoteConfigStatus.Builder().status(status).build();
}
}
private Map<String, String> parseCentralConfiguration(ByteString centralConfig) {
try {
byte[] centralConfigBytes = centralConfig.toByteArray();
if (centralConfigBytes.length == 0) {
logger.log(
Level.WARNING,
"No central configuration returned - is this connected to an EDOT collector above 18.8?");
return null;
}
JsonReader<Object> reader = dslJson.newReader(centralConfig.toByteArray());
reader.startObject();
return Collections.unmodifiableMap(MapConverter.deserialize(reader));
} catch (IOException e) {
logger.log(Level.WARNING, "Could not parse central configuration.", e);
return null;
}
}
@Override
public void onConnect() {
logger.log(Level.INFO, "onConnect({0})", client);
}
@Override
public void onConnectFailed(@Nullable Throwable throwable) {
logger.log(Level.FINE, "onConnect({0}, {1})", new Object[] {client, throwable});
}
@Override
public void onErrorResponse(@Nonnull ServerErrorResponse errorResponse) {
logger.log(Level.INFO, "onErrorResponse({0}, {1})", new Object[] {client, errorResponse});
}
public void setPollingDelay(@Nonnull Duration duration) {
pollingDelay.setDelay(duration);
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String serviceName;
private String environment;
private String configurationEndpoint = "http://localhost:4320/v1/opamp";
private Duration pollingInterval = Duration.ofSeconds(30);
private Builder() {}
public Builder setServiceName(String serviceName) {
this.serviceName = serviceName;
return this;
}
public Builder setConfigurationEndpoint(String configurationEndpoint) {
this.configurationEndpoint = configurationEndpoint;
return this;
}
public Builder setPollingInterval(Duration pollingInterval) {
this.pollingInterval = pollingInterval;
return this;
}
public Builder setServiceEnvironment(String environment) {
this.environment = environment;
return this;
}
public OpampManager build() {
return new OpampManager(
new Configuration(serviceName, environment, configurationEndpoint, pollingInterval));
}
}
public interface CentralConfigurationProcessor {
Result process(Map<String, String> configuration);
enum Result {
SUCCESS,
FAILURE
}
}
private static class Configuration {
private final String serviceName;
private final String environment;
private final String configurationEndpoint;
private final Duration pollingInterval;
private Configuration(
String serviceName,
String environment,
String configurationEndpoint,
Duration pollingInterval) {
this.serviceName = serviceName;
this.environment = environment;
this.configurationEndpoint = configurationEndpoint;
this.pollingInterval = pollingInterval;
}
}
private static class MutablePeriodicDelay implements PeriodicDelay {
private final AtomicReference<Duration> duration = new AtomicReference<>();
public MutablePeriodicDelay(@Nonnull Duration initialValue) {
duration.set(initialValue);
}
@Override
public Duration getNextDelay() {
return duration.get();
}
@Override
public void reset() {}
public void setDelay(@Nonnull Duration value) {
duration.set(value);
}
}
}