forked from signalfx/splunk-otel-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpampActivator.java
More file actions
181 lines (166 loc) · 7.47 KB
/
OpampActivator.java
File metadata and controls
181 lines (166 loc) · 7.47 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
/*
* Copyright Splunk Inc.
*
* Licensed 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 com.splunk.opentelemetry.opamp;
import static io.opentelemetry.opamp.client.internal.request.service.HttpRequestService.DEFAULT_DELAY_BETWEEN_REQUESTS;
import static io.opentelemetry.opamp.client.internal.request.service.HttpRequestService.DEFAULT_DELAY_BETWEEN_RETRIES;
import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getConfig;
import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getResource;
import static java.util.logging.Level.WARNING;
import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.AgentListener;
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.service.HttpRequestService;
import io.opentelemetry.opamp.client.internal.response.MessageData;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.resources.Resource;
import java.io.IOException;
import java.time.Duration;
import java.util.List;
import java.util.logging.Logger;
import opamp.proto.ServerErrorResponse;
import org.jetbrains.annotations.Nullable;
@AutoService(AgentListener.class)
public class OpampActivator implements AgentListener {
private static final Logger logger = Logger.getLogger(OpampActivator.class.getName());
private static final String OP_AMP_ENABLED_PROPERTY = "splunk.opamp.enabled";
private static final String OP_AMP_ENDPOINT = "splunk.opamp.endpoint";
private static final String OP_AMP_POLLING_INTERVAL = "splunk.opamp.polling.interval";
@Override
public void afterAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) {
ConfigProperties config = getConfig(autoConfiguredOpenTelemetrySdk);
if (!config.getBoolean(OP_AMP_ENABLED_PROPERTY, false)) {
return;
}
Resource resource = getResource(autoConfiguredOpenTelemetrySdk);
long pollingDuration =
config.getLong(
OP_AMP_POLLING_INTERVAL, DEFAULT_DELAY_BETWEEN_REQUESTS.getNextDelay().toMillis());
String endpoint = config.getString(OP_AMP_ENDPOINT);
OpampClient client =
startOpampClient(
endpoint,
resource,
pollingDuration,
new OpampClient.Callbacks() {
@Override
public void onConnect(OpampClient opampClient) {}
@Override
public void onConnectFailed(OpampClient opampClient, @Nullable Throwable throwable) {
logger.log(WARNING, "Connection to OpAMP server failed", throwable);
}
@Override
public void onErrorResponse(
OpampClient opampClient, ServerErrorResponse serverErrorResponse) {
logger.log(WARNING, "OpAMP server returned error " + serverErrorResponse);
}
@Override
public void onMessage(OpampClient opampClient, MessageData messageData) {}
});
Runtime.getRuntime()
.addShutdownHook(
new Thread(
() -> {
try {
client.close();
} catch (IOException e) {
logger.log(WARNING, "Error shutting down OpAMP client", e);
}
}));
}
static OpampClient startOpampClient(
String endpoint,
Resource resource,
long pollingDurationMillis,
OpampClient.Callbacks callbacks) {
OpampClientBuilder builder = OpampClient.builder();
// TODO: Uncomment once we are able to report our effective config
// builder.enableEffectiveConfigReporting();
if (endpoint != null) {
PeriodicDelay pollingDelay =
PeriodicDelay.ofFixedDuration(Duration.ofMillis(pollingDurationMillis));
OkHttpSender okhttp = OkHttpSender.create(endpoint);
HttpRequestService httpSender =
HttpRequestService.create(okhttp, pollingDelay, DEFAULT_DELAY_BETWEEN_RETRIES);
builder.setRequestService(httpSender);
}
addIdentifyingAttributes(builder, resource);
return builder.build(callbacks);
}
static void addIdentifyingAttributes(OpampClientBuilder builder, Resource res) {
res.getAttributes()
.forEach(
(key, value) -> {
switch (key.getType()) {
case STRING:
builder.putIdentifyingAttribute(key.getKey(), (String) value);
break;
case LONG:
builder.putIdentifyingAttribute(key.getKey(), (long) value);
break;
case DOUBLE:
builder.putIdentifyingAttribute(key.getKey(), (double) value);
break;
case BOOLEAN:
builder.putIdentifyingAttribute(key.getKey(), (boolean) value);
break;
case VALUE:
builder.putIdentifyingAttribute(key.getKey(), value.toString());
break;
case STRING_ARRAY:
{
List<String> typedValueList = (List<String>) value;
String[] array = typedValueList.toArray(new String[] {});
builder.putIdentifyingAttribute(key.getKey(), array);
break;
}
case LONG_ARRAY:
{
List<Long> typedValueList = (List<Long>) value;
long[] primitiveArray = new long[typedValueList.size()];
for (int i = 0; i < typedValueList.size(); i++) {
primitiveArray[i] = typedValueList.get(i);
}
builder.putIdentifyingAttribute(key.getKey(), primitiveArray);
break;
}
case DOUBLE_ARRAY:
{
List<Double> typedValueList = (List<Double>) value;
double[] primitiveArray = new double[typedValueList.size()];
for (int i = 0; i < typedValueList.size(); i++) {
primitiveArray[i] = typedValueList.get(i);
}
builder.putIdentifyingAttribute(key.getKey(), primitiveArray);
break;
}
case BOOLEAN_ARRAY:
{
List<Boolean> typedValueList = (List<Boolean>) value;
boolean[] primitiveArray = new boolean[typedValueList.size()];
for (int i = 0; i < typedValueList.size(); i++) {
primitiveArray[i] = typedValueList.get(i);
}
builder.putIdentifyingAttribute(key.getKey(), primitiveArray);
break;
}
}
});
}
}