/
SourceAgentConfig.java
203 lines (178 loc) · 9.77 KB
/
SourceAgentConfig.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
package com.sourceplusplus.api.model.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonObject;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
/**
* Holds the current configuration used by the agent.
*
* @author <a href="mailto:brandon@srcpl.us">Brandon Fergerson</a>
* @version 0.2.6
* @since 0.1.0
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonSerialize(using = SourceAgentConfig.SourceAgentConfigSerializer.class)
@JsonDeserialize(using = SourceAgentConfig.SourceAgentConfigDeserializer.class)
public final class SourceAgentConfig {
public static final SourceAgentConfig current = new SourceAgentConfig();
public volatile String apiHost;
public volatile Integer apiPort;
public volatile String apiKey;
public volatile String apiVersion;
public volatile Boolean apiSslEnabled;
public volatile Boolean agentEnabled;
public volatile Boolean testMode;
public volatile Boolean manualSetupMode;
public volatile Boolean manualTraceSyncMode;
public volatile Boolean skywalkingEnabled;
public volatile String appUuid;
public volatile String appName;
public volatile String logLevel;
public volatile String logLocation;
public volatile Boolean logMetrics;
public volatile Boolean outputEnhancedClasses;
public volatile Integer sampleNPer3Secs;
public volatile Integer spanLimitPerSegment;
public volatile List<String> packages;
public volatile String pluginHost;
public volatile Integer pluginPort;
public volatile Boolean pluginBridgeEnabled;
@SuppressWarnings("unchecked")
public void applyConfig(JsonObject config) {
JsonObject agentConfig = Objects.requireNonNull(config);
if (agentConfig.containsKey("enabled")) agentEnabled = agentConfig.getBoolean("enabled");
if (agentConfig.containsKey("test_mode")) testMode = agentConfig.getBoolean("test_mode");
if (agentConfig.containsKey("manual_setup_mode")) manualSetupMode = agentConfig.getBoolean("manual_setup_mode");
if (agentConfig.containsKey("manual_trace_sync_mode")) manualTraceSyncMode = agentConfig.getBoolean("manual_trace_sync_mode");
if (agentConfig.containsKey("log_level")) logLevel = agentConfig.getString("log_level").toUpperCase();
if (agentConfig.containsKey("log_location")) logLocation = agentConfig.getString("log_location");
if (agentConfig.containsKey("log_metrics")) logMetrics = agentConfig.getBoolean("log_metrics");
JsonObject applicationConfig = agentConfig.getJsonObject("application");
if (applicationConfig != null) {
if (applicationConfig.containsKey("app_uuid")) appUuid = applicationConfig.getString("app_uuid");
if (applicationConfig.containsKey("app_name")) appName = applicationConfig.getString("app_name");
JsonObject sourceCodeConfig = applicationConfig.getJsonObject("application_source_code");
if (sourceCodeConfig != null) {
packages = (List<String>) sourceCodeConfig.getJsonArray("packages").getList();
}
}
JsonObject apiConfig = agentConfig.getJsonObject("api");
if (apiConfig != null) {
if (apiConfig.containsKey("version")) apiVersion = apiConfig.getString("version");
if (apiConfig.containsKey("host")) {
apiHost = System.getenv().getOrDefault("SPP_API_HOST", System.getProperty("SPP_API_HOST",
apiConfig.getString("host")));
}
if (apiConfig.containsKey("port")) apiPort = apiConfig.getInteger("port");
if (apiConfig.containsKey("ssl")) apiSslEnabled = apiConfig.getBoolean("ssl");
if (apiConfig.containsKey("key")) apiKey = apiConfig.getString("key");
}
JsonObject skywalkingConfig = agentConfig.getJsonObject("skywalking");
if (skywalkingConfig != null) {
if (skywalkingConfig.containsKey("enabled"))
skywalkingEnabled = skywalkingConfig.getBoolean("enabled");
if (skywalkingConfig.containsKey("output_enhanced_classes"))
outputEnhancedClasses = skywalkingConfig.getBoolean("output_enhanced_classes");
if (skywalkingConfig.containsKey("sample_n_per_3_secs"))
sampleNPer3Secs = skywalkingConfig.getInteger("sample_n_per_3_secs");
if (skywalkingConfig.containsKey("span_limit_per_segment"))
spanLimitPerSegment = skywalkingConfig.getInteger("span_limit_per_segment");
//todo: plugins
}
JsonObject pluginBridgeConfig = agentConfig.getJsonObject("plugin-bridge");
if (pluginBridgeConfig != null) {
if (pluginBridgeConfig.containsKey("enabled"))
pluginBridgeEnabled = pluginBridgeConfig.getBoolean("enabled");
if (pluginBridgeConfig.containsKey("host")) pluginHost = pluginBridgeConfig.getString("host");
if (pluginBridgeConfig.containsKey("port")) pluginPort = pluginBridgeConfig.getInteger("port");
}
}
public JsonObject toJsonObject() {
return new JsonObject(toString());
}
@Override
public String toString() {
return Json.encode(this);
}
public static class SourceAgentConfigSerializer extends StdSerializer<SourceAgentConfig> {
protected SourceAgentConfigSerializer() {
super(SourceAgentConfig.class);
}
@Override
public void serialize(SourceAgentConfig value, JsonGenerator gen, SerializerProvider provider)
throws IOException {
gen.writeStartObject();
if (value.agentEnabled != null) gen.writeBooleanField("enabled", value.agentEnabled);
if (value.testMode != null) gen.writeBooleanField("test_mode", value.testMode);
if (value.manualSetupMode != null) gen.writeBooleanField("manual_setup_mode", value.manualSetupMode);
if (value.manualTraceSyncMode != null) gen.writeBooleanField("manual_trace_sync_mode", value.manualTraceSyncMode);
if (value.logLevel != null) gen.writeStringField("log_level", value.logLevel);
if (value.logLocation != null) gen.writeStringField("log_location", value.logLocation);
if (value.logMetrics != null) gen.writeBooleanField("log_metrics", value.logMetrics);
gen.writeFieldName("application");
gen.writeStartObject();
if (value.appUuid != null) gen.writeStringField("app_uuid", value.appUuid);
if (value.appName != null) gen.writeStringField("app_name", value.appName);
gen.writeFieldName("application_source_code");
gen.writeStartObject();
gen.writeArrayFieldStart("packages");
if (value.packages != null) {
for (String packagePattern : value.packages) {
gen.writeString(packagePattern);
}
}
gen.writeEndArray();
gen.writeEndObject(); //application_source_code
gen.writeEndObject(); //application
gen.writeFieldName("api");
gen.writeStartObject();
if (value.apiVersion != null) gen.writeStringField("version", value.apiVersion);
if (value.apiHost != null) gen.writeStringField("host", value.apiHost);
if (value.apiPort != null) gen.writeNumberField("port", value.apiPort);
if (value.apiSslEnabled != null) gen.writeBooleanField("ssl", value.apiSslEnabled);
if (value.apiKey != null) gen.writeStringField("key", value.apiKey);
gen.writeEndObject(); //api
gen.writeFieldName("skywalking");
gen.writeStartObject();
if (value.skywalkingEnabled != null) gen.writeBooleanField("enabled", value.skywalkingEnabled);
if (value.sampleNPer3Secs != null) gen.writeNumberField("sample_n_per_3_secs", value.sampleNPer3Secs);
if (value.spanLimitPerSegment != null)
gen.writeNumberField("span_limit_per_segment", value.spanLimitPerSegment);
if (value.outputEnhancedClasses != null)
gen.writeBooleanField("output_enhanced_classes", value.outputEnhancedClasses);
//todo: plugins
gen.writeEndObject(); //skywalking
gen.writeFieldName("plugin-bridge");
gen.writeStartObject();
if (value.pluginBridgeEnabled != null) gen.writeBooleanField("enabled", value.pluginBridgeEnabled);
if (value.pluginHost != null) gen.writeStringField("host", value.pluginHost);
if (value.pluginPort != null) gen.writeNumberField("port", value.pluginPort);
gen.writeEndObject(); //plugin-bridge
gen.writeEndObject(); //root
}
}
public static class SourceAgentConfigDeserializer extends StdDeserializer<SourceAgentConfig> {
protected SourceAgentConfigDeserializer() {
super(SourceAgentConfig.class);
}
@Override
public SourceAgentConfig deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException {
JsonNode node = jp.getCodec().readTree(jp);
SourceAgentConfig agentConfig = new SourceAgentConfig();
agentConfig.applyConfig(new JsonObject(node.toString()));
return agentConfig;
}
}
}